@@ -13,14 +13,9 @@ const DEFAULT_MODULE_FORMAT = 'nestedEvaluate';
13
13
const DEFAULT_FILE_PREFIX = '/bundled-source' ;
14
14
const SUPPORTED_FORMATS = [ 'getExport' , 'nestedEvaluate' ] ;
15
15
16
- const removeComments = comments => {
17
- if ( ! comments ) {
18
- return ;
19
- }
20
- // We remove all the JS comments to prevent misidentifying HTML
21
- // comments, or import expressions.
22
- comments . splice ( 0 , comments . length ) ;
23
- } ;
16
+ const IMPORT_RE = new RegExp ( '\\b(import)(\\s*(?:\\(|/[/*]))' , 'sg' ) ;
17
+ const HTML_COMMENT_START_RE = new RegExp ( `${ '<' } !--` , 'g' ) ;
18
+ const HTML_COMMENT_END_RE = new RegExp ( `--${ '>' } ` , 'g' ) ;
24
19
25
20
export function tildotPlugin ( ) {
26
21
const transformer = makeTransform ( babelParser , babelGenerate ) ;
@@ -92,11 +87,14 @@ export default async function bundleSource(
92
87
// its source lines back to the right place.
93
88
// eslint-disable-next-line no-await-in-loop
94
89
const consumer = await new SourceMapConsumer ( chunk . map ) ;
90
+ const unmapped = new WeakSet ( ) ;
95
91
let lastPos = ast . loc . start ;
96
92
unmapLoc = loc => {
97
- if ( ! loc ) {
93
+ if ( ! loc || unmapped . has ( loc ) ) {
98
94
return ;
99
95
}
96
+ // Ensure we start in the right position... doesn't matter where we end.
97
+ loc . end = loc . start ;
100
98
for ( const pos of [ 'start' , 'end' ] ) {
101
99
if ( loc [ pos ] ) {
102
100
const newPos = consumer . originalPositionFor ( loc [ pos ] ) ;
@@ -108,29 +106,46 @@ export default async function bundleSource(
108
106
loc [ pos ] = lastPos ;
109
107
}
110
108
}
109
+ unmapped . add ( loc ) ;
111
110
} ;
112
111
}
113
112
113
+ const rewriteComment = node => {
114
+ node . type = 'CommentBlock' ;
115
+ node . value = node . value
116
+ . replace ( / ^ \s * / gm, '' ) // Strip extraneous comment whitespace.
117
+ . replace ( HTML_COMMENT_START_RE , '<!X-' )
118
+ . replace ( HTML_COMMENT_END_RE , '-X>' )
119
+ . replace ( IMPORT_RE , 'X$1$2' ) ;
120
+ if ( unmapLoc ) {
121
+ unmapLoc ( node . loc ) ;
122
+ }
123
+ // console.log(JSON.stringify(node, undefined, 2));
124
+ } ;
125
+
114
126
babelTraverse ( ast , {
115
127
enter ( p ) {
116
128
const { loc, leadingComments, trailingComments } = p . node ;
117
- // Remove all comments.
118
- removeComments ( leadingComments ) ;
119
- removeComments ( trailingComments ) ;
129
+ if ( p . node . comments ) {
130
+ p . node . comments = [ ] ;
131
+ }
132
+ // Rewrite all comments.
133
+ ( leadingComments || [ ] ) . forEach ( rewriteComment ) ;
120
134
if ( p . node . type . startsWith ( 'Comment' ) ) {
121
- p . replaceWithMultiple ( [ ] ) ;
122
- return ;
135
+ rewriteComment ( p . node ) ;
123
136
}
124
137
// If not a comment, and we are unmapping the source maps,
125
138
// then do it for this location.
126
139
if ( unmapLoc ) {
127
140
unmapLoc ( loc ) ;
128
141
}
142
+ ( trailingComments || [ ] ) . forEach ( rewriteComment ) ;
129
143
} ,
130
144
} ) ;
131
145
132
146
// Now generate the sources with the new positions.
133
147
sourceBundle [ fileName ] = babelGenerate ( ast , { retainLines : true } ) . code ;
148
+ // console.log(`==== sourceBundle[${fileName}]\n${sourceBundle[fileName]}\n====`);
134
149
}
135
150
136
151
if ( ! entrypoint ) {
0 commit comments