forked from Derek-Hu/react-app-rewire-multiple-entry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (87 loc) · 3.07 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
'use strict';
const path = require('path');
const fs = require('fs');
const pwd = process.cwd();
const XXH = require('xxhashjs');
const H = XXH.h32(0xABCD) // seed = 0xABCD
const appIndexJs = path.resolve(pwd, 'src/index.js');
const appHtml = path.resolve(pwd, 'public/index.html');
const formatName = function (name) {
return name.split('/').reverse()[0].match(/^[^.]*/)[0] + '.' + H.update(name).digest().toString(16);
};
const checkFileExist = function(file){
if (!fs.existsSync(file)) {
throw new Error('File not found: ' + file);
}
}
const defaultEntryName = 'main';
module.exports = function (params) {
// Prepare Data for Multiple Entry
const isArray = Object.prototype.toString.call(params) === '[object Array]';
const validElements = params.filter(function(entry){
return entry && Object.keys(entry).length;
});
const entries = isArray && validElements && validElements.map(function (entry) {
if (!entry.entry) {
throw new Error('Missing attribute [entry], Received '+JSON.stringify(entry));
}
if (!entry.template) {
entry.template = appHtml;
} else {
entry.template = path.resolve(pwd, entry.template);
}
if (!entry.outPath) {
entry.outPath = path.relative(pwd, entry.template).replace(/\\/g, '/')
}
entry.outPath = entry.outPath.replace(/^\//, '').replace(/\/$/, '');
checkFileExist(entry.template);
const entryPath = path.resolve(pwd, entry.entry);
checkFileExist(entryPath);
return {
name: formatName(entry.entry),
entry: entryPath,
template: entry.template,
outPath: entry.outPath,
proxyPath: '/build/' + entry.outPath,
pattern: new RegExp('^' + entry.outPath.replace(/[-/\\^$&*+?.()|[\]{}]/g, '\\$&')),
}
});
return {
addMultiEntry: function (config) {
if(!entries || !entries.length){
return config;
}
// Mulitple Entry JS
const defaultEntryHTMLPlugin = config.plugins.filter(function(plugin){
return plugin.constructor.name === 'HtmlWebpackPlugin'
})[0];
defaultEntryHTMLPlugin.options.chunks = [defaultEntryName];
const necessaryEntry = config.entry.filter(function(file){
return file !== appIndexJs;
});
const multipleEntry = {};
multipleEntry[defaultEntryName] = config.entry;
entries.forEach(_entry => {
multipleEntry[_entry.name] = necessaryEntry.concat(_entry.entry);
// Multiple Entry HTML Plugin
config.plugins.push(
new defaultEntryHTMLPlugin.constructor(
Object.assign({}, defaultEntryHTMLPlugin.options, {
filename: _entry.outPath,
template: _entry.template,
chunks: [_entry.name]
})
)
);
});
config.entry = multipleEntry;
// Multiple Entry Output File
let names = config.output.filename.split('/').reverse();
if (names[0].indexOf('[name]') === -1) {
names[0] = '[name].' + names[0];
config.output.filename = names.reverse().join('/');
}
return config;
}
}
};