Skip to content

Commit f4f14ce

Browse files
mikegreilingSpaceK33z
authored andcommitted
Fix support for DynamicEntryPlugin (#1319)
* Fix incorrect use of assert library and unnecessary done callback * Add tests for each type of entry option * Add proper support for webpack's DynamicEntryPlugin
1 parent 398c773 commit f4f14ce

File tree

2 files changed

+113
-12
lines changed

2 files changed

+113
-12
lines changed

lib/util/addDevServerEntrypoints.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@ module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptio
1919

2020
if (devServerOptions.hotOnly) { devClient.push('webpack/hot/only-dev-server'); } else if (devServerOptions.hot) { devClient.push('webpack/hot/dev-server'); }
2121

22-
[].concat(webpackOptions).forEach((wpOpt) => {
23-
if (typeof wpOpt.entry === 'object' && !Array.isArray(wpOpt.entry)) {
24-
Object.keys(wpOpt.entry).forEach((key) => {
25-
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
22+
const prependDevClient = (entry) => {
23+
if (typeof entry === 'function') {
24+
return () => Promise.resolve(entry()).then(prependDevClient);
25+
}
26+
if (typeof entry === 'object' && !Array.isArray(entry)) {
27+
const entryClone = {};
28+
Object.keys(entry).forEach((key) => {
29+
entryClone[key] = devClient.concat(entry[key]);
2630
});
27-
} else if (typeof wpOpt.entry === 'function') {
28-
wpOpt.entry = wpOpt.entry(devClient);
29-
} else {
30-
wpOpt.entry = devClient.concat(wpOpt.entry || './src');
31+
return entryClone;
3132
}
33+
return devClient.concat(entry);
34+
};
35+
36+
[].concat(webpackOptions).forEach((wpOpt) => {
37+
wpOpt.entry = prependDevClient(wpOpt.entry || './src');
3238
});
3339
}
3440
};

test/Entry.test.js

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,112 @@
22

33
const assert = require('assert');
44
const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
5+
const config = require('./fixtures/simple-config/webpack.config');
56

67
describe('Entry', () => {
7-
it('default to src if no entry point is given', (done) => {
8+
it('adds devServer entry points to a single entry point', () => {
9+
const webpackOptions = Object.assign({}, config);
10+
const devServerOptions = {};
11+
12+
addDevServerEntrypoints(webpackOptions, devServerOptions);
13+
14+
assert.equal(webpackOptions.entry.length, 2);
15+
assert(webpackOptions.entry[0].indexOf('client/index.js?') !== -1);
16+
assert.equal(webpackOptions.entry[1], './foo.js');
17+
});
18+
19+
it('adds devServer entry points to a multi-module entry point', () => {
20+
const webpackOptions = Object.assign({}, config, {
21+
entry: ['./foo.js', './bar.js']
22+
});
23+
const devServerOptions = {};
24+
25+
addDevServerEntrypoints(webpackOptions, devServerOptions);
26+
27+
assert.equal(webpackOptions.entry.length, 3);
28+
assert(webpackOptions.entry[0].indexOf('client/index.js?') !== -1);
29+
assert.equal(webpackOptions.entry[1], './foo.js');
30+
assert.equal(webpackOptions.entry[2], './bar.js');
31+
});
32+
33+
it('adds devServer entry points to a multi entry point object', () => {
34+
const webpackOptions = Object.assign({}, config, {
35+
entry: {
36+
foo: './foo.js',
37+
bar: './bar.js'
38+
}
39+
});
40+
const devServerOptions = {};
41+
42+
addDevServerEntrypoints(webpackOptions, devServerOptions);
43+
44+
assert.equal(webpackOptions.entry.foo.length, 2);
45+
assert(webpackOptions.entry.foo[0].indexOf('client/index.js?') !== -1);
46+
assert.equal(webpackOptions.entry.foo[1], './foo.js');
47+
assert.equal(webpackOptions.entry.bar[1], './bar.js');
48+
});
49+
50+
it('defaults to src if no entry point is given', () => {
851
const webpackOptions = {};
952
const devServerOptions = {};
1053

1154
addDevServerEntrypoints(webpackOptions, devServerOptions);
1255

13-
assert(webpackOptions.entry.length, 2);
14-
assert(webpackOptions.entry[1], './src');
56+
assert.equal(webpackOptions.entry.length, 2);
57+
assert.equal(webpackOptions.entry[1], './src');
58+
});
59+
60+
it('preserves dynamic entry points', (done) => {
61+
let i = 0;
62+
const webpackOptions = {
63+
// simulate dynamic entry
64+
entry: () => {
65+
i += 1;
66+
return `./src-${i}.js`;
67+
}
68+
};
69+
const devServerOptions = {};
70+
71+
addDevServerEntrypoints(webpackOptions, devServerOptions);
72+
73+
assert(typeof webpackOptions.entry, 'function');
74+
75+
webpackOptions.entry().then(entryFirstRun => (
76+
webpackOptions.entry().then((entrySecondRun) => {
77+
assert.equal(entryFirstRun.length, 2);
78+
assert.equal(entryFirstRun[1], './src-1.js');
79+
80+
assert.equal(entrySecondRun.length, 2);
81+
assert.equal(entrySecondRun[1], './src-2.js');
82+
done();
83+
})
84+
)).catch(done);
85+
});
86+
87+
it('preserves asynchronous dynamic entry points', (done) => {
88+
let i = 0;
89+
const webpackOptions = {
90+
// simulate async dynamic entry
91+
entry: () => new Promise((resolve) => {
92+
i += 1;
93+
resolve(`./src-${i}.js`);
94+
})
95+
};
96+
const devServerOptions = {};
97+
98+
addDevServerEntrypoints(webpackOptions, devServerOptions);
99+
100+
assert(typeof webpackOptions.entry, 'function');
101+
102+
webpackOptions.entry().then(entryFirstRun => (
103+
webpackOptions.entry().then((entrySecondRun) => {
104+
assert.equal(entryFirstRun.length, 2);
105+
assert.equal(entryFirstRun[1], './src-1.js');
15106

16-
done();
107+
assert.equal(entrySecondRun.length, 2);
108+
assert.equal(entrySecondRun[1], './src-2.js');
109+
done();
110+
})
111+
)).catch(done);
17112
});
18113
});

0 commit comments

Comments
 (0)