-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
60 lines (51 loc) · 1.85 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
'use strict'
var fs = require('fs')
var path = require('path')
var staticMapKey = require('static-file-loader').key
function StaticFilesWebpackPlugin (options) {
options = options || {}
var outputPath = options.outputPath || 'static.json'
if (!path.isAbsolute(outputPath)) {
outputPath = path.join(process.cwd(), outputPath)
}
this.options = {
outputPath: outputPath,
useRelativePaths: options.useRelativePaths,
prefix: options.prefix,
replace: options.replace || function (processedPath) { return processedPath }
}
}
StaticFilesWebpackPlugin.prototype.apply = function (compiler) {
compiler.plugin('after-emit', function (compilation, callback) {
var map = compilation[staticMapKey] || {}
if (this.options.useRelativePaths) {
var relativePathBase
if (typeof this.options.useRelativePaths === 'string') {
relativePathBase = this.options.useRelativePaths
if (!path.isAbsolute(relativePathBase)) {
relativePathBase = path.join(process.cwd(), relativePathBase)
}
} else {
relativePathBase = process.cwd()
}
var replace = this.options.replace
var prefix = this.options.prefix ? this.options.prefix + path.sep : ''
map = Object.keys(map).reduce(function (mapAcc, filePath) {
var relativePath = filePath.replace(relativePathBase + path.sep, prefix)
relativePath = replace(relativePath)
mapAcc[relativePath] = map[filePath]
return mapAcc
}, {})
}
// Ensure that the keys in the map are sorted
map = Object.keys(map).sort().reduce(function (mapAcc, filePath) {
mapAcc[filePath] = map[filePath]
return mapAcc
}, {})
fs.writeFile(this.options.outputPath, JSON.stringify(map), function (err) {
if (err) throw err
callback()
})
}.bind(this))
}
module.exports = StaticFilesWebpackPlugin