-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
551 lines (470 loc) · 15.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import loaderUtils from 'loader-utils'
import path from 'path'
const PATH_REGEXP = /"\[\[(.*?)\]\]"/g,
PATH_MATCH_INDEX = 1,
PATH_REPLACER = '"[path]"'
const INLINE_REGEXP = /\[\[\s*INLINE\(([^)]*)\)\s*\]\]/g
const ABS_PATH_REGEXP = /^[/]|^\w+:[/][/]/,
HASH_REGEXP_SRC = '[\\w\\d_-]+[=]*'
class PathRewriter
{
/**
* Marks a resource for rewriting paths and emitting to the file system.
* Use it in conjunction with the `new PathRewriter()` plugin.
*
* The `opts` argument is either string or object. If string, it specifies
* the resource's loader string along with the options, e.g.
*
* PathRewriter.rewriteAndEmit('?name=[path][name]-[hash].[ext]')
* PathRewriter.rewriteAndEmit('jade-html?pretty')
* PathRewriter.rewriteAndEmit('?name=[path][name].[ext]!jade-html?pretty')
*
* Object form allows to specify the following options:
*
* - loader: the resource's loader string.
*
* - loaders: the array of loaders; mutually exclusive with the `loader` option.
*
* - name: the path to the output file, may contain the following tokens:
*
* - [ext] the extension of the resource;
* - [name] the basename of the resource;
* - [path] the path of the resource relative to the `context` option;
* - [hash] the hash of the resource's content;
* - [<hashType>:hash:<digestType>:<length>]
* see https://github.com/webpack/loader-utils#interpolatename;
* - [N] content of N-th capturing group obtained from matching
* the resource's path against the `nameRegExp` option.
*
* Defaults to '[path][name].[ext]'.
*
* - nameRegExp, context: see `name`.
*
* - publicPath: allows to override the global output.publicPath setting.
*
* - pathRegExp, pathMatchIndex, pathReplacer: allow to override options for
* rewriting paths in this resource. See documentation for the constructor.
*
* Example:
*
* PathRewriter.rewriteAndEmit({
* name: '[path][name]-[hash].html',
* loader: 'jade-html?pretty'
* })
*/
static rewriteAndEmit(rwOpts)
{
var thisLoader = module.filename,
loader = ''
if ('string' == typeof rwOpts) {
return thisLoader + (/^[?!]/.test(rwOpts)
? rwOpts
: rwOpts && '!' + rwOpts
)
}
if (rwOpts.loader != undefined) {
loader = rwOpts.loader
}
if (rwOpts.loaders != undefined) {
if (rwOpts.loader != undefined) {
throw new Error('cannot use both loader and loaders')
}
loader = rwOpts.loaders.join('!')
}
var query = extend({}, rwOpts, (key) => key != 'loader' && key != 'loaders')
if (query.pathRegExp) {
let re = query.pathRegExp; query.pathRegExp = re instanceof RegExp ? {
source: re.source,
flags: (re.ignoreCase ? 'i' : '') + (re.multiline ? 'm' : '')
} : {
source: '' + re,
flags: ''
}
}
if (query.inlineRegExp) {
let re = query.inlineRegExp; query.inlineRegExp = re instanceof RegExp ? {
source: re.source,
flags: (re.ignoreCase ? 'i' : '') + (re.multiline ? 'm' : '')
} : {
source: '' + re,
flags: ''
}
}
// we need to temporarily replace all exclamation marks because they have
// special meaning in loader strings v
//
return thisLoader + '?' + JSON.stringify(query).replace(/!/g, ':BANG:') + (/^!/.test(loader)
? loader
: loader && '!' + loader
)
}
/**
* A plugin that emits to the filesystem all resources that are marked
* with `PathRewriter.rewriteAndEmit()` loader.
*
* Options:
*
* - silent: don't print rewritten paths. Defaults to false.
*
* - emitStats: write stats.json file. May be string specifying
* the file's name. Defaults to true.
*
* - pathRegExp: regular expression for matching paths. Defaults
* to /"\[\[(.*?)\]\]"/, which tests for \"[[...]]\" constructions,
* capturing the string between the braces.
*
* - pathMatchIndex: the index of capturing group in the pathRegExp
* that corresponds to a path.
*
* - pathReplacer: template for replacing matched path with the
* rewritten one. May contain following tokens:
* - [path] the rewritten path;
* - [N] the content of N-th capturing group of pathRegExp.
* Defaults to \"[path]\".
*
* - includeHash: make compilation's hash dependent on contents
* of this resource. Useful with live reload, as it causes the app
* to reload each time this resources changes. Defaults to false.
*/
constructor(opts)
{
this.opts = extend({
silent: false,
emitStats: true,
includeHash: false,
pathRegExp: undefined,
pathReplacer: undefined,
pathMatchIndex: undefined
}, opts)
this.pathRegExp = PathRewriter.makeRegExp(this.opts.pathRegExp) || PATH_REGEXP
this.pathMatchIndex = this.opts.pathMatchIndex == undefined
? PATH_MATCH_INDEX
: this.opts.pathMatchIndex
this.pathReplacer = this.opts.pathReplacer || PATH_REPLACER
this.inlineRegExp = PathRewriter.makeRegExp(this.opts.inlineRegExp) || INLINE_REGEXP
this.rwPathsCache = {}
this.modules = []
this.modulesByRequest = {}
this.compilationRwPathsCache = undefined
this.compilationAssetsPaths = undefined
}
static loader(content) // loader entry point, called by Webpack; see PathRewriterEntry
{
this.cacheable && this.cacheable()
var rewriter = this[ __dirname ]
if (rewriter == undefined) {
throw new Error(
'webpack-path-rewriter loader is used without the corresponding plugin;\n ' +
'add `new PathRewriter()` to the list of plugins in the Webpack config'
)
}
var query = loaderUtils.parseQuery(this.query && this.query.replace(/:BANG:/g, '!')),
topLevelContext = this.options.context,
publicPath = query.publicPath || this.options.output.publicPath || ''
if (publicPath.length && publicPath[ publicPath.length - 1 ] != '/') {
publicPath = publicPath + '/'
}
var url = loaderUtils.interpolateName(this, query.name || '[path][name].[ext]', {
content: content,
context: query.context || topLevelContext,
regExp: query.nameRegExp
})
var moduleData = {url, content, publicPath, topLevelContext,
request: this.request,
context: this.context,
relPath: path.relative(topLevelContext, this.resourcePath),
pathRegExp: PathRewriter.makeRegExp(query.pathRegExp) || rewriter.pathRegExp,
pathReplacer: query.pathReplacer || rewriter.pathReplacer,
pathMatchIndex: +(query.pathMatchIndex == undefined
? rewriter.pathMatchIndex
: query.pathMatchIndex
),
inlineRegExp: PathRewriter.makeRegExp(query.inlineRegExp) || rewriter.inlineRegExp
}
var exportStatement = 'module.exports = "' + publicPath + url + (rewriter.opts.includeHash
? '" // content hash: ' + loaderUtils.interpolateName(this, '[hash]', { content })
: '"'
)
var callback = this.async(); PathRewriter.extractAssets(this, moduleData, assetRequests =>
{
rewriter.addModule(moduleData)
callback(null, assetRequests
.map(req => `require(${ JSON.stringify(req) })`)
.join('\n') + '\n' + exportStatement
)
})
}
static makeRegExp(desc)
{
if (desc == undefined) {
return undefined
}
if (desc instanceof RegExp) {
return new RegExp(desc.source, 'g'
+ (desc.ignoreCase ? 'i' : '')
+ (desc.multiline ? 'm' : '')
)
}
var src = desc.source || '' + desc,
flags = desc.flags || 'g'
return new RegExp(src, flags.indexOf('g') == -1
? flags + 'g'
: flags
)
}
static extractAssets(loaderCtx, moduleData, cb)
{
var paths = PathRewriter.findNonWildcardPaths(moduleData),
numPaths = paths.length,
numPathsDone = 0,
assetsData = []
if (paths.length == 0) {
return done(assetsData)
}
paths.forEach(path => {
var request = loaderUtils.urlToRequest(path)
// we need to discard all possibly generated assets, i.e. those
// that are not present in the source tree, because we cannot
// require them
loaderCtx.resolve(moduleData.context, request, (err, _) => {
if (err == null) {
assetsData.push({ path, request, rwPath: undefined })
}
if (++numPathsDone == numPaths) {
done(assetsData)
}
})
})
function done(assetsData) {
var byPath = {}, byRequest = {}
assetsData.forEach(data => {
byRequest[ data.request ] = data
byPath[ data.path ] = data
})
moduleData.assetDataByPath = byPath
moduleData.assetDataByRequest = byRequest
cb(assetsData.map(data => data.request))
}
}
static findNonWildcardPaths({ content, pathRegExp, pathMatchIndex })
{
var results = [],
matches
while (matches = pathRegExp.exec(content)) {
var path = trim(matches[ pathMatchIndex ])
if (path
&& path.indexOf('*') == -1
&& results.indexOf(path) == -1
&& loaderUtils.isUrlRequest(path)
){
results.push(path)
}
}
pathRegExp.lastIndex = 0
return results
}
addModule(moduleData)
{
this.modules.push(moduleData)
this.modulesByRequest[ moduleData.request ] = moduleData
}
apply(compiler) // plugin entry point, called by Webpack
{
compiler.plugin('compilation', (compilation) => {
compilation.plugin('normal-module-loader', (loaderContext, module) => {
if (loaderContext[ __dirname ])
throw new Error('cannot use more than one instance of PathRewriter in the plugins list')
loaderContext[ __dirname ] = this
})
})
compiler.plugin('after-compile', (compilation, callback) => {
this.onAfterCompile(compilation, callback)
})
compiler.plugin('emit', (compiler, callback) => {
this.onEmit(compiler, callback)
})
}
onAfterCompile(compilation, callback)
{
compilation.modules.forEach(module => {
var moduleData = this.modulesByRequest[ module.request ]
moduleData && this.extractModuleAssetsPublicPaths(moduleData, module)
})
callback()
}
extractModuleAssetsPublicPaths(moduleData, module)
{
var assetDataByRequest = moduleData.assetDataByRequest,
deps = module.dependencies
for (var i = 0; i < deps.length; ++i) {
var dep = deps[i]
if (!dep.request || !dep.module) continue
var assetData = assetDataByRequest[ dep.request ]
if (assetData == undefined) continue
var assets = dep.module.assets && Object.keys(dep.module.assets) || []
if (assets.length != 1) {
let paths = assets.map(a => `"${a}"`).join(', ')
assetData.error = new PathRewriterError(
`invalid number of assets for path "${ assetData.path }", assets: [${ paths }]`,
moduleData
)
continue
}
assetData.rwPath = assets[0]
}
}
onEmit(compiler, callback)
{
var stats = compiler.getStats().toJson()
this.compilationAssetsPaths = stats.assets.map(asset => asset.name)
this.compilationRwPathsCache = {}
this.modules.forEach(moduleData => {
this.rewriteModulePaths(moduleData, compiler)
})
this.modules = []
this.modulesByRequest = {}
// WARN WTF
//
// We need to cache assets from previous compilations in watch mode
// because, for some reason, some assets don't get included in the
// assets list after recompilations.
//
extend(this.rwPathsCache, this.compilationRwPathsCache)
if (this.opts.emitStats) {
var statsJson = JSON.stringify(stats, null, ' '),
statsPath = typeof this.opts.emitStats == 'string'
? this.opts.emitStats
: 'stats.json'
compiler.assets[ statsPath ] = {
source: () => statsJson,
size: () => statsJson.length
}
}
callback()
}
rewriteModulePaths(moduleData, compiler)
{
var content = moduleData.content.replace(moduleData.pathRegExp, (...matches) => {
var srcPath = trim(matches[ moduleData.pathMatchIndex ])
try {
var rwPath = this.rewritePath(srcPath, moduleData)
rwPath = this.prependPublicPath(moduleData.publicPath, rwPath)
this.opts.silent || (srcPath != rwPath) && console.error(
`PathRewriter[ ${ moduleData.relPath } ]: "${ srcPath }" -> "${ rwPath }"`
)
return moduleData.pathReplacer.replace(/\[(path|\d+)\]/g, (_, t) => {
return t == 'path' ? rwPath : matches[ +t ]
})
}
catch(e) {
if (!(e instanceof PathRewriterError) && !this.opts.silent) {
console.error(e.stack)
}
compiler.errors.push(e)
return srcPath
}
}).replace(moduleData.inlineRegExp, (match, assetUrl) => {
let asset = compiler.assets[ assetUrl ]
if (!asset) {
compiler.errors.push(new Error(
`Cannot inline asset "${ assetUrl }" in ${ moduleData.relPath }: not found`
))
return match
}
else if (!this.opts.silent) {
console.error(`PathRewriter[ ${ moduleData.relPath } ]: inlined "${ assetUrl }"`)
}
return asset.source()
})
compiler.assets[ moduleData.url ] = {
source: () => content,
size: () => content.length
}
}
rewritePath(srcPath, moduleData)
{
var key = moduleData.context + '|' + srcPath,
rwPath = this.compilationRwPathsCache[ key ]
if (rwPath)
return rwPath
var assetData = moduleData.assetDataByPath[ srcPath ]
if (assetData) {
if (assetData.error)
throw assetData.error
rwPath = assetData.rwPath
}
else {
rwPath = this.rewriteGeneratedAssetPath(srcPath, moduleData)
}
if (rwPath == undefined) {
// in watch mode, sometimes some assets are not listed during
// recompilations, so we need to use a long-term cache
rwPath = this.rwPathsCache[ key ]
}
if (rwPath == undefined || rwPath.length == 0) {
throw new PathRewriterError(`could not resolve path "${ srcPath }"`, moduleData)
}
this.compilationRwPathsCache[ key ] = rwPath
return rwPath
}
rewriteGeneratedAssetPath(srcPath, moduleData)
{
if (ABS_PATH_REGEXP.test(srcPath))
return srcPath
var parts = srcPath.split(/[*]+/)
if (parts.join('').length == 0) {
throw new PathRewriterError(
`invalid wildcard path "${ srcPath }", must contain at least one non-wildcard symbol`,
moduleData
)
}
var searchRE = new RegExp('^' + parts.map(escapeRegExp).join(HASH_REGEXP_SRC) + '$')
for (var i = 0; i < this.compilationAssetsPaths.length; ++i) {
var rwPath = this.compilationAssetsPaths[i]
if (searchRE.test(rwPath)) {
return rwPath
}
}
return undefined
}
prependPublicPath(publicPath, path)
{
return ABS_PATH_REGEXP.test(path)
? path
: publicPath + path
}
}
function trim(s) {
return s && s.replace(/^\s+|\s+$/g, '')
}
function escapeRegExp(s) {
return s && s.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&')
}
function extend(dst, src, filter) {
if (src) Object.keys(src).forEach(filter == undefined
? key => { dst[ key ] = src[ key ] }
: key => {
if (filter(key)) {
dst[ key ] = src[ key ]
}
}
)
return dst
}
function PathRewriterError(msg, moduleData)
{
Error.call(this)
Error.captureStackTrace(this, PathRewriterError)
this.name = 'PathRewriterError'
this.message = moduleData.relPath + ': ' + msg
}
PathRewriterError.prototype = Object.create(Error.prototype)
function PathRewriterEntry(arg)
{
return this instanceof PathRewriterEntry
? new PathRewriter(arg) // called with new => plugin
: PathRewriter.loader.call(this, arg) // called as a funtion => loader
}
PathRewriterEntry.rewriteAndEmit = PathRewriter.rewriteAndEmit
module.exports = PathRewriterEntry
export default PathRewriterEntry