Skip to content

Commit e9f4819

Browse files
committed
Add proper support for webpack's DynamicEntryPlugin
1 parent 3034914 commit e9f4819

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
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: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('assert');
44
const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
55
const config = require('./fixtures/simple-config/webpack.config');
66

7-
describe('Entry', () => {
7+
describe.only('Entry', () => {
88
it('adds devServer entry points to a single entry point', () => {
99
const webpackOptions = Object.assign({}, config);
1010
const devServerOptions = {};
@@ -56,4 +56,58 @@ describe('Entry', () => {
5656
assert.equal(webpackOptions.entry.length, 2);
5757
assert.equal(webpackOptions.entry[1], './src');
5858
});
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');
106+
107+
assert.equal(entrySecondRun.length, 2);
108+
assert.equal(entrySecondRun[1], './src-2.js');
109+
done();
110+
})
111+
)).catch(done);
112+
});
59113
});

0 commit comments

Comments
 (0)