This repository has been archived by the owner on Aug 23, 2021. It is now read-only.
forked from Derek-Hu/react-app-rewire-multiple-entry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (98 loc) · 3.56 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
'use strict';
const path = require('path');
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);
};
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(/\/$/, '');
return {
name: formatName(entry.entry),
entry: path.resolve(pwd, entry.entry),
template: entry.template,
outPath: entry.outPath,
proxyPath: '/build/' + entry.outPath,
pattern: new RegExp('^' + entry.outPath.replace(/[-/\\^$&*+?.()|[\]{}]/g, '\\$&')),
}
});
return {
addEntryProxy: function (configFunction) {
if(!entries || !entries.length){
return configFunction;
}
if (!configFunction.historyApiFallback) {
configFunction.historyApiFallback = {};
}
if (!configFunction.historyApiFallback.rewrites) {
configFunction.historyApiFallback.rewrites = [];
}
configFunction.historyApiFallback.rewrites = configFunction.historyApiFallback.rewrites.concat(entries.map(function (entry) {
return {
from: entry.pattern,
to: entry.proxyPath
};
}));
return configFunction;
},
addMultiEntry: function (config) {
if(!entries || !entries.length){
return config;
}
// Mulitple Entry JS
const defaultEntryPath = 'src/index.js';
const defaulEntryName = formatName(defaultEntryPath);
const defaultEntryHTMLPlugin = config.plugins.filter(function(plugin){
return plugin.constructor.name === 'HtmlWebpackPlugin'
})[0];
defaultEntryHTMLPlugin.options.chunks = [defaulEntryName];
const necessaryEntry = config.entry.filter(function(file){
return file !== appIndexJs;
});
const multipleEntry = {};
multipleEntry[defaulEntryName] = config.entry;
entries.forEach(_entry => {
multipleEntry[_entry.name] = necessaryEntry.concat(_entry.entry);
let baseEntry = _entry.base ? { base: _entry.base } : {};
// Multiple Entry HTML Plugin
config.plugins.push(
new defaultEntryHTMLPlugin.constructor(
Object.assign({}, defaultEntryHTMLPlugin.options, {
filename: _entry.outPath,
template: _entry.template,
chunks: [_entry.name]
}, baseEntry)
)
);
});
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;
}
}
};