@@ -10,10 +10,21 @@ import {
10
10
import { hasDepInstalled } from './modules/hasDepInstalled' ;
11
11
import { concat } from './modules/concat' ;
12
12
import { getTagInfo } from './modules/tagInfo' ;
13
- import { addLanguageAlias , getLanguageFromAlias } from './modules/language' ;
14
- import { throwError } from './modules/errors' ;
13
+ import {
14
+ addLanguageAlias ,
15
+ getLanguageFromAlias ,
16
+ SOURCE_MAP_PROP_MAP ,
17
+ } from './modules/language' ;
15
18
import { prepareContent } from './modules/prepareContent' ;
16
19
20
+ type AutoPreprocessGroup = PreprocessorGroup & {
21
+ defaultLanguages : Readonly < {
22
+ markup : string ;
23
+ style : string ;
24
+ script : string ;
25
+ } > ;
26
+ } ;
27
+
17
28
type AutoPreprocessOptions = {
18
29
markupTagName ?: string ;
19
30
aliases ?: Array < [ string , string ] > ;
@@ -23,6 +34,7 @@ type AutoPreprocessOptions = {
23
34
style ?: string ;
24
35
script ?: string ;
25
36
} ;
37
+ sourceMap ?: boolean ;
26
38
27
39
// transformers
28
40
typescript ?: TransformerOptions < Options . Typescript > ;
@@ -64,21 +76,15 @@ export const runTransformer = async (
64
76
return options ( { content, map, filename, attributes } ) ;
65
77
}
66
78
67
- try {
68
- const { transformer } = await import ( `./transformers/${ name } ` ) ;
79
+ const { transformer } = await import ( `./transformers/${ name } ` ) ;
69
80
70
- return transformer ( {
71
- content,
72
- filename,
73
- map,
74
- attributes,
75
- options : typeof options === 'boolean' ? null : options ,
76
- } ) ;
77
- } catch ( e ) {
78
- throwError (
79
- `Error transforming '${ name } '.\n\nMessage:\n${ e . message } \n\nStack:\n${ e . stack } ` ,
80
- ) ;
81
- }
81
+ return transformer ( {
82
+ content,
83
+ filename,
84
+ map,
85
+ attributes,
86
+ options : typeof options === 'boolean' ? null : options ,
87
+ } ) ;
82
88
} ;
83
89
84
90
export function autoPreprocess (
@@ -87,17 +93,18 @@ export function autoPreprocess(
87
93
markupTagName = 'template' ,
88
94
preserve = [ ] ,
89
95
defaults,
96
+ sourceMap = false ,
90
97
...rest
91
- } : AutoPreprocessOptions = { } as AutoPreprocessOptions ,
92
- ) : PreprocessorGroup {
98
+ } = { } as AutoPreprocessOptions ,
99
+ ) : AutoPreprocessGroup {
93
100
markupTagName = markupTagName . toLocaleLowerCase ( ) ;
94
101
95
- const defaultLanguages = {
102
+ const defaultLanguages = Object . freeze ( {
96
103
markup : 'html' ,
97
104
style : 'css' ,
98
105
script : 'javascript' ,
99
106
...defaults ,
100
- } ;
107
+ } ) ;
101
108
102
109
const transformers = rest as Transformers ;
103
110
const markupPattern = new RegExp (
@@ -108,30 +115,37 @@ export function autoPreprocess(
108
115
addLanguageAlias ( aliases ) ;
109
116
}
110
117
111
- const optionsCache : Record < string , any > = { } ;
112
118
const getTransformerOptions = (
113
- lang : string ,
114
- alias : string ,
119
+ name : string ,
120
+ alias ? : string ,
115
121
) : TransformerOptions < unknown > => {
116
- if ( typeof transformers [ alias ] === 'function' ) return transformers [ alias ] ;
117
- if ( typeof transformers [ lang ] === 'function' ) return transformers [ lang ] ;
118
- if ( optionsCache [ alias ] != null ) return optionsCache [ alias ] ;
122
+ const { [ name ] : nameOpts , [ alias ] : aliasOpts } = transformers ;
123
+
124
+ if ( typeof aliasOpts === 'function' ) return aliasOpts ;
125
+ if ( typeof nameOpts === 'function' ) return nameOpts ;
126
+ if ( aliasOpts === false || nameOpts === false ) return false ;
119
127
120
128
const opts : TransformerOptions < unknown > = { } ;
121
129
122
- if ( typeof transformers [ lang ] === 'object' ) {
123
- Object . assign ( opts , transformers [ lang ] ) ;
130
+ if ( typeof nameOpts === 'object' ) {
131
+ Object . assign ( opts , nameOpts ) ;
124
132
}
125
133
126
- if ( lang !== alias ) {
134
+ if ( name !== alias ) {
127
135
Object . assign ( opts , ALIAS_OPTION_OVERRIDES [ alias ] || null ) ;
128
136
129
- if ( typeof transformers [ alias ] === 'object' ) {
130
- Object . assign ( opts , transformers [ alias ] ) ;
137
+ if ( typeof aliasOpts === 'object' ) {
138
+ Object . assign ( opts , aliasOpts ) ;
131
139
}
132
140
}
133
141
134
- return ( optionsCache [ alias ] = opts ) ;
142
+ if ( sourceMap && name in SOURCE_MAP_PROP_MAP ) {
143
+ const [ propName , value ] = SOURCE_MAP_PROP_MAP [ name ] ;
144
+
145
+ opts [ propName ] = value ;
146
+ }
147
+
148
+ return opts ;
135
149
} ;
136
150
137
151
const getTransformerTo = (
@@ -183,112 +197,131 @@ export function autoPreprocess(
183
197
const cssTransformer = getTransformerTo ( 'style' , 'css' ) ;
184
198
const markupTransformer = getTransformerTo ( 'markup' , 'html' ) ;
185
199
186
- return {
187
- async markup ( { content, filename } ) {
188
- if ( transformers . replace ) {
189
- const transformed = await runTransformer (
190
- 'replace' ,
191
- transformers . replace ,
192
- { content, filename } ,
193
- ) ;
200
+ const markup : PreprocessorGroup [ 'markup' ] = async ( { content, filename } ) => {
201
+ if ( transformers . replace ) {
202
+ const transformed = await runTransformer (
203
+ 'replace' ,
204
+ transformers . replace ,
205
+ { content, filename } ,
206
+ ) ;
194
207
195
- content = transformed . code ;
196
- }
208
+ content = transformed . code ;
209
+ }
197
210
198
- const templateMatch = content . match ( markupPattern ) ;
211
+ const templateMatch = content . match ( markupPattern ) ;
199
212
200
- /** If no <template> was found, just return the original markup */
201
- if ( ! templateMatch ) {
202
- return { code : content } ;
203
- }
213
+ /** If no <template> was found, just return the original markup */
214
+ if ( ! templateMatch ) {
215
+ return { code : content } ;
216
+ }
217
+
218
+ const [ fullMatch , attributesStr , templateCode ] = templateMatch ;
219
+
220
+ /** Transform an attribute string into a key-value object */
221
+ const attributes = attributesStr
222
+ . split ( / \s + / )
223
+ . filter ( Boolean )
224
+ . reduce ( ( acc : Record < string , string | boolean > , attr ) => {
225
+ const [ name , value ] = attr . split ( '=' ) ;
226
+
227
+ // istanbul ignore next
228
+ acc [ name ] = value ? value . replace ( / [ ' " ] / g, '' ) : true ;
229
+
230
+ return acc ;
231
+ } , { } ) ;
232
+
233
+ /** Transform the found template code */
234
+ let { code, map, dependencies } = await markupTransformer ( {
235
+ content : templateCode ,
236
+ attributes,
237
+ filename,
238
+ } ) ;
204
239
205
- const [ fullMatch , attributesStr , templateCode ] = templateMatch ;
206
-
207
- /** Transform an attribute string into a key-value object */
208
- const attributes = attributesStr
209
- . split ( / \s + / )
210
- . filter ( Boolean )
211
- . reduce ( ( acc : Record < string , string | boolean > , attr ) => {
212
- const [ name , value ] = attr . split ( '=' ) ;
213
-
214
- // istanbul ignore next
215
- acc [ name ] = value ? value . replace ( / [ ' " ] / g, '' ) : true ;
216
-
217
- return acc ;
218
- } , { } ) ;
219
-
220
- /** Transform the found template code */
221
- let { code, map, dependencies } = await markupTransformer ( {
222
- content : templateCode ,
223
- attributes,
224
- filename,
225
- } ) ;
226
-
227
- code =
228
- content . slice ( 0 , templateMatch . index ) +
229
- code +
230
- content . slice ( templateMatch . index + fullMatch . length ) ;
231
-
232
- return { code, map, dependencies } ;
233
- } ,
234
- async script ( { content, attributes, filename } ) {
235
- const transformResult : Processed = await scriptTransformer ( {
236
- content,
237
- attributes,
238
- filename,
239
- } ) ;
240
-
241
- let { code, map, dependencies, diagnostics } = transformResult ;
242
-
243
- if ( transformers . babel ) {
244
- const transformed = await runTransformer ( 'babel' , transformers . babel , {
240
+ code =
241
+ content . slice ( 0 , templateMatch . index ) +
242
+ code +
243
+ content . slice ( templateMatch . index + fullMatch . length ) ;
244
+
245
+ return { code, map, dependencies } ;
246
+ } ;
247
+
248
+ const script : PreprocessorGroup [ 'script' ] = async ( {
249
+ content,
250
+ attributes,
251
+ filename,
252
+ } ) => {
253
+ const transformResult : Processed = await scriptTransformer ( {
254
+ content,
255
+ attributes,
256
+ filename,
257
+ } ) ;
258
+
259
+ let { code, map, dependencies, diagnostics } = transformResult ;
260
+
261
+ if ( transformers . babel ) {
262
+ const transformed = await runTransformer (
263
+ 'babel' ,
264
+ getTransformerOptions ( 'babel' ) ,
265
+ {
245
266
content : code ,
246
267
map,
247
268
filename,
248
269
attributes,
249
- } ) ;
270
+ } ,
271
+ ) ;
250
272
251
- code = transformed . code ;
252
- map = transformed . map ;
253
- dependencies = concat ( dependencies , transformed . dependencies ) ;
254
- diagnostics = concat ( diagnostics , transformed . diagnostics ) ;
255
- }
273
+ code = transformed . code ;
274
+ map = transformed . map ;
275
+ dependencies = concat ( dependencies , transformed . dependencies ) ;
276
+ diagnostics = concat ( diagnostics , transformed . diagnostics ) ;
277
+ }
256
278
257
- return { code, map, dependencies, diagnostics } ;
258
- } ,
259
- async style ( { content, attributes, filename } ) {
260
- const transformResult = await cssTransformer ( {
261
- content,
262
- attributes,
263
- filename,
264
- } ) ;
265
-
266
- let { code, map, dependencies } = transformResult ;
267
-
268
- if ( await hasDepInstalled ( 'postcss' ) ) {
269
- if ( transformers . postcss ) {
270
- const transformed = await runTransformer (
271
- 'postcss' ,
272
- transformers . postcss ,
273
- { content : code , map, filename, attributes } ,
274
- ) ;
275
-
276
- code = transformed . code ;
277
- map = transformed . map ;
278
- dependencies = concat ( dependencies , transformed . dependencies ) ;
279
- }
279
+ return { code, map, dependencies, diagnostics } ;
280
+ } ;
281
+
282
+ const style : PreprocessorGroup [ 'style' ] = async ( {
283
+ content,
284
+ attributes,
285
+ filename,
286
+ } ) => {
287
+ const transformResult = await cssTransformer ( {
288
+ content,
289
+ attributes,
290
+ filename,
291
+ } ) ;
292
+
293
+ let { code, map, dependencies } = transformResult ;
280
294
295
+ if ( await hasDepInstalled ( 'postcss' ) ) {
296
+ if ( transformers . postcss ) {
281
297
const transformed = await runTransformer (
282
- 'globalStyle ' ,
283
- transformers ?. globalStyle ,
298
+ 'postcss ' ,
299
+ getTransformerOptions ( 'postcss' ) ,
284
300
{ content : code , map, filename, attributes } ,
285
301
) ;
286
302
287
303
code = transformed . code ;
288
304
map = transformed . map ;
305
+ dependencies = concat ( dependencies , transformed . dependencies ) ;
289
306
}
290
307
291
- return { code, map, dependencies } ;
292
- } ,
308
+ const transformed = await runTransformer (
309
+ 'globalStyle' ,
310
+ getTransformerOptions ( 'globalStyle' ) ,
311
+ { content : code , map, filename, attributes } ,
312
+ ) ;
313
+
314
+ code = transformed . code ;
315
+ map = transformed . map ;
316
+ }
317
+
318
+ return { code, map, dependencies } ;
319
+ } ;
320
+
321
+ return {
322
+ defaultLanguages,
323
+ markup,
324
+ script,
325
+ style,
293
326
} ;
294
327
}
0 commit comments