-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
136 lines (124 loc) · 3.35 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
const replaceOnce = require('replace-once');
const fs = require('fs-utils');
const { join, extname } = require('path');
function ReplaceStringPatternPlugin(options = {}, config = {}) {
this.directReplace = false;
if (options instanceof Array) {
this.directReplace = true;
}
this.options = options;
this.config = config;
}
function replaceContent(result, [patterns = [], replacement = []]) {
if (
Array.isArray(patterns) &&
Array.isArray(replacement) &&
patterns.length === replacement.length
) {
return replaceOnce(result, patterns, replacement, 'g');
} else {
throw new Error(
`[webpack-plugin-create-multiple-bundle-from-string-replace]:
options patterns/replacement must be an array and them length are equal, got
patterns = ${patterns}
replacement = ${replacement}
`,
);
}
}
function writeFile({ distPath, env, file, result }) {
fs.writeFileSync(join(distPath, env, file), result);
}
function replaceFile({ file, sourcePath, replacePattern }) {
const fullFilePath = join(sourcePath, file);
const fileContent = fs.readFileSync(fullFilePath);
return replaceContent(fileContent, replacePattern);
}
function copyFile({
file,
env = '',
sourcePath,
distPath,
replaceExtname = [],
}) {
if (
[
'html',
'htm',
'css',
'js',
'jsx',
'json',
'md',
...replaceExtname,
].includes(extname(file).replace('.', ''))
) {
return true;
}
fs.writeFileSync(
join(distPath, env, file),
fs.readFileSync(join(sourcePath, file), { encoding: null }),
);
return false;
}
ReplaceStringPatternPlugin.prototype.apply = function(compiler) {
const { directReplace, options: replacePattern, config } = this;
const sourcePath = config.sourcePath || compiler.options.output.path;
const distPath =
config.distPath ||
`${compiler.options.output.path}/multiple-bundle-from-string-replace`;
const eventDone = (compilation, done) => {
console.log(
`[webpack-plugin-create-multiple-bundle-from-string-replace]: In ${distPath}`,
);
const files = fs.glob.sync('**/*', {
cwd: sourcePath,
nodir: true,
});
if (directReplace) {
files.forEach(file => {
if (
copyFile({
file,
sourcePath,
distPath,
replaceExtname: (config.replaceExtname || []),
})
) {
const result = replaceFile({ file, sourcePath, replacePattern });
writeFile({ distPath, file, result });
}
});
} else {
files.forEach(file => {
for (const env in this.options) {
if (
copyFile({
file,
sourcePath,
distPath,
env,
replaceExtname: (config.replaceExtname || []),
})
) {
const result = replaceFile({
file,
sourcePath,
replacePattern: this.options[env],
});
writeFile({ distPath, env, file, result });
}
}
});
}
console.log('files are replaced!');
done && done();
};
if (compiler.hooks) {
const plugin = { name: 'MultipleBundlePlugin' };
compiler.hooks.done.tapAsync(plugin, eventDone);
} else {
compiler.plugin('done', eventDone);
}
};
module.exports = ReplaceStringPatternPlugin;