/
webpack.dev.js
159 lines (138 loc) · 5.11 KB
/
webpack.dev.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* webpack.dev.js
*/
const common = require('./webpack.common.js');
const fs = require('fs');
const webpack = require('webpack');
const merge = require('webpack-merge');
const jsdom = require('jsdom');
// webpack main configration
const webpackConfig = {
mode: 'development',
plugins:[
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
new webpack.optimize.MinChunkSizePlugin({minChunkSize: 1000}),
new webpack.DefinePlugin({
'process.env': {
TEST_ENV: JSON.stringify(process.env.TEST_ENV),
}
})
],
devtool: 'inline-source-map' // add inline source map
};
// export main configuration adjusted to various environments
module.exports = (env, argv) => {
if (argv.mode !== 'development') throw new Error('Not development mode!!');
////////////////////////////////////////////////////////////////////////
// library build setting
const config = merge.smart(common.webpackConfig, webpackConfig);
////////////////////////////////////////////////////////////////////////
// test bundle build setting
if(process.env.TEST_ENV){
// when NODE_ENV = test is set, only test bundles are generated.
// this is only the case where the bundled js files are generated for test using html file.
// NOTE Karma does not refer to config.entry, and it pre-process the spec files written in karma.conf.js
if(process.env.NODE_ENV === 'test'){
config.entry = getEntriesForHTMLTest(config);
createNewHtml(config.entry); // Edit HTML here
}
// for require through dynamic variables in webpack
config.plugins.push(
new webpack.ContextReplacementPlugin(
RegExp('./src'), RegExp(common.entryName)// only src/index.js is allowed to get imported.
),
new webpack.ContextReplacementPlugin(
RegExp('./dist'), RegExp(common.bundleName)// only dist/xxx.bundle.js is allowed to get imported.
)
);
}
return config;
};
const typeList = {
source: {filePrefix: ''},
bundle: {filePrefix: 'fromBundled'},
window: {filePrefix: 'fromWindow'},
};
const testHtmlName = './test/html/test.html';
// get test file names for test with static html
function getEntriesForHTMLTest (config) {
const parentDir = './test';
const testFiles = getFilteredFileList(parentDir, new RegExp('.*\\.spec\\.js$'));
config.entry = {};
testFiles.map( (file) => {
const prefix = file.slice(0, -8);
config.entry[`.${parentDir}/html/${prefix}`] = [ `${parentDir}/${file}` ];
});
if(process.env.TEST_ENV === 'bundle'){
const newEntry = {};
Object.keys(config.entry).map( (key) => {
const newKey = `${key}.${typeList['bundle'].filePrefix}`;
newEntry[newKey] = config.entry[key];
});
config.entry = newEntry;
}
else if(process.env.TEST_ENV === 'window'){
const newEntry = {};
Object.keys(config.entry).map( (key) => {
const newKey = `${key}.${typeList['window'].filePrefix}`;
newEntry[newKey] = config.entry[key];
});
config.entry = newEntry;
}
return config.entry;
}
function createNewHtml(newEntry){
// list new test bundle files
const files = Object.keys(newEntry).map( (elem) => {
const x = elem.split('/');
return `${x[x.length-1]}.bundle.js`;
});
// filter existing test bundle files
const newList = getFilteredFileList('./test/html', new RegExp('.*\\.bundle\\.js$'));
// merge existing and new file lists
files.map( (f) => { if (newList.indexOf(f) < 0) newList.push(f); });
// create dom
const domForTest= new jsdom.JSDOM(testHtmlTemplate);
// import test bundle files
newList.map( (f) => {
const p = domForTest.window.document.createElement('p');
// get test bundle file type
const type = (f.match(/Bundled/)) ? 'bundle'
: (f.match(/Window/)) ? 'window' : 'source';
// window import
if (type === 'window') {
const bundleScript = domForTest.window.document.createElement('script');
bundleScript.src = `../../dist/${common.bundleName}`;
p.appendChild(bundleScript);
}
// script import
const script = domForTest.window.document.createElement('script');
script.src = `./${f}`;
p.appendChild(script);
domForTest.window.document.body.querySelector('#test').appendChild(p);
});
// generate html
const html = domForTest.window.document.documentElement.outerHTML;
fs.writeFileSync(testHtmlName, html, 'utf8');
}
function getFilteredFileList(parentDir, fileRegExp){
const files = fs.readdirSync(parentDir);
return files.filter((file) => fs.statSync(`${parentDir}/${file}`).isFile() && fileRegExp.test(file));
}
const testHtmlTemplate =
'<!DOCTYPE html>\n' +
'<html lang="en">\n' +
'<head>\n' +
' <meta charset="UTF-8">\n' +
' <title>Title</title>\n' +
' <link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />\n' +
' <script src="../../node_modules/@babel/polyfill/browser.js"></script>\n' +
'</head>\n' +
'<body>\n' +
'<div id="mocha"></div>\n' +
'<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>\n' +
'<script>mocha.setup(\'bdd\')</script>\n' +
'<div id="test"></div>\n' +
'<script>mocha.run();</script>\n' +
'</body>\n' +
'</html>';