This repository has been archived by the owner on Feb 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (44 loc) · 1.6 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
const fs = require("fs");
const path = require("path");
const createHtmlWebpackPluginInstance = require("rocketact-scripts/dist/utils/createHtmlWebpackPluginInstance")
.default;
const { resolveToAppRoot } = require("rocketact-dev-utils");
module.exports = api => {
const jsEntries = {};
fs.readdirSync(resolveToAppRoot("./src/js/pages"))
.filter(p => p.match(/\.[tj]sx?$/))
.map(p => path.join(resolveToAppRoot("./src/js/pages"), p))
.forEach(p => {
const entryName = path.basename(p).replace(/\.[tj]sx?$/, "");
jsEntries[entryName] = p;
});
const htmlEntries = {};
fs.readdirSync(resolveToAppRoot("./src/html"))
.filter(p => p.match(/\.html$/))
.map(p => path.join(resolveToAppRoot("./src/html"), p))
.forEach(p => {
const entryName = path.basename(p).replace(/\.html$/, "");
htmlEntries[entryName] = p;
});
api.chainWebpack(webpackChain => {
// clear default entries and HtmlWebpackPlugin instances
webpackChain.entryPoints.clear();
Object.keys(webpackChain.plugins.entries()).forEach(pluginName => {
if (pluginName.match(/^HtmlWebpackPlugin\-/)) {
webpackChain.plugins.delete(pluginName);
}
});
// add entries and HtmlWebpackPlugin instances
Object.keys(jsEntries).forEach(entryName => {
if (htmlEntries[entryName]) {
webpackChain.entry(entryName).add(jsEntries[entryName]);
webpackChain.plugin(`HtmlWebpackPlugin-${entryName}`).use(
...createHtmlWebpackPluginInstance({
entryName,
template: htmlEntries[entryName]
})
);
}
});
});
};