forked from glena/webpack-custom-var-library-name-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (94 loc) · 2.21 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
function CustomVarLibraryNameAppendPlugin(options)
{
this.options = options;
}
CustomVarLibraryNameAppendPlugin.prototype.apply = function (compiler)
{
var _this = this;
var replaceMap = null;
if (typeof _this.options.name === 'object')
{
replaceMap = Object.keys(_this.options.name)
.reduce(function (prev, key)
{
if (_this.options.name[key].var)
{
prev.push(
{
replace: 'root["' + key + '"]',
with: 'root["' + _this.options.name[key].var + '"] = ' +
// [Yagasoft | 2018-07-31 | Add] append logic
'root.' + _this.options.name[key].var +
' ? Object.assign(root.' + _this.options.name[key].var + ', factory())' +
' : factory(); //'
});
}
if (_this.options.name[key].file)
{
prev.push(
{
replace: key + '.min.js.map',
with: _this.options.name[key].file + '.min.js.map'
});
prev.push(
{
replace: key + '.js',
with: _this.options.name[key].file + '.js'
});
prev.push(
{
replace: key + '.min.js',
with: _this.options.name[key].file + '.min.js'
});
}
return prev;
}, []);
}
function changeVarNames(path, data)
{
if (replaceMap)
{
if (data.chunk && data.chunk.name)
{
path = path.replace('[name]', data.chunk.name);
}
return replaceMap
.reduce(function (prev, curr)
{
return prev.replace(curr.replace, curr.with);
}, path);
}
else
{
return path.indexOf('root[') === 0
// [Yagasoft | 2018-07-31 | Add] append logic
? 'root["' + _this.options.name + '"] = ' +
'root.' + _this.options.name +
' ? Object.assign(root.' + _this.options.name + ', factory())' +
' : factory(); //'
: path;
}
}
if (compiler.hooks)
{
// Webpack 4
compiler.hooks.compilation.tap(
'CustomVarLibraryNameAppendPlugin',
function (compilation)
{
compilation.mainTemplate.hooks.assetPath
.tap('CustomVarLibraryNameAppendPluginAssetPath', changeVarNames);
});
}
else
{
// Webpack 2-3
compiler.plugin(
'compilation',
function (compilation)
{
compilation.mainTemplate.plugin('asset-path', changeVarNames);
});
}
};
module.exports = CustomVarLibraryNameAppendPlugin;