Skip to content

Commit 7e3cff7

Browse files
committed
fix(addEntries): only add client entry to web targets
1 parent 99e8db0 commit 7e3cff7

File tree

5 files changed

+107
-14
lines changed

5 files changed

+107
-14
lines changed

lib/utils/addEntries.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,40 @@ function addEntries(config, options, server) {
1717

1818
const domain = createDomain(options, app);
1919
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
20-
const entries = [
21-
`${require.resolve('../../client/')}?${domain}${sockPath}`,
22-
];
20+
const clientEntry = `${require.resolve(
21+
'../../client/'
22+
)}?${domain}${sockPath}`;
23+
let hotEntry;
2324

2425
if (options.hotOnly) {
25-
entries.push(require.resolve('webpack/hot/only-dev-server'));
26+
hotEntry = require.resolve('webpack/hot/only-dev-server');
2627
} else if (options.hot) {
27-
entries.push(require.resolve('webpack/hot/dev-server'));
28+
hotEntry = require.resolve('webpack/hot/dev-server');
2829
}
2930

30-
const prependEntry = (entry) => {
31-
if (typeof entry === 'function') {
32-
return () => Promise.resolve(entry()).then(prependEntry);
31+
const prependEntry = (originalEntry, additionalEntries) => {
32+
if (typeof originalEntry === 'function') {
33+
return () =>
34+
Promise.resolve(originalEntry()).then((entry) =>
35+
prependEntry(entry, additionalEntries)
36+
);
3337
}
3438

35-
if (typeof entry === 'object' && !Array.isArray(entry)) {
39+
if (typeof originalEntry === 'object' && !Array.isArray(originalEntry)) {
3640
const clone = {};
3741

38-
Object.keys(entry).forEach((key) => {
42+
Object.keys(originalEntry).forEach((key) => {
3943
// entry[key] should be a string here
40-
clone[key] = prependEntry(entry[key]);
44+
clone[key] = prependEntry(originalEntry[key], additionalEntries);
4145
});
4246

4347
return clone;
4448
}
4549

4650
// in this case, entry is a string or an array.
4751
// make sure that we do not add duplicates.
48-
const entriesClone = entries.slice(0);
49-
[].concat(entry).forEach((newEntry) => {
52+
const entriesClone = additionalEntries.slice(0);
53+
[].concat(originalEntry).forEach((newEntry) => {
5054
if (!entriesClone.includes(newEntry)) {
5155
entriesClone.push(newEntry);
5256
}
@@ -56,7 +60,17 @@ function addEntries(config, options, server) {
5660

5761
// eslint-disable-next-line no-shadow
5862
[].concat(config).forEach((config) => {
59-
config.entry = prependEntry(config.entry || './src');
63+
const webTarget =
64+
config.target === 'web' ||
65+
config.target === 'webworker' ||
66+
config.target == null;
67+
const additionalEntries = webTarget ? [clientEntry] : [];
68+
69+
if (hotEntry) {
70+
additionalEntries.push(hotEntry);
71+
}
72+
73+
config.entry = prependEntry(config.entry || './src', additionalEntries);
6074

6175
if (options.hot || options.hotOnly) {
6276
config.plugins = config.plugins || [];

test/UniversalCompiler.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const request = require('supertest');
4+
const helper = require('./helper');
5+
const config = require('./fixtures/universal-compiler-config/webpack.config');
6+
7+
describe('UniversalCompiler', () => {
8+
let server;
9+
let req;
10+
beforeAll((done) => {
11+
server = helper.start(config, { inline: true }, done);
12+
req = request(server.app);
13+
});
14+
15+
afterAll(helper.close);
16+
17+
it('client bundle should have the inlined the client runtime', (done) => {
18+
req
19+
.get('/client.js')
20+
.expect('Content-Type', 'application/javascript; charset=UTF-8')
21+
.expect(200)
22+
.end((err, res) => {
23+
if (err) {
24+
return done(err);
25+
}
26+
expect(res.text).toContain('Hello from the client');
27+
expect(res.text).toContain('webpack-dev-server/client');
28+
done();
29+
});
30+
});
31+
32+
it('server bundle should NOT have the inlined the client runtime', (done) => {
33+
// we wouldn't normally request a server bundle
34+
// but we'll do it here to check the contents
35+
req
36+
.get('/server.js')
37+
.expect('Content-Type', 'application/javascript; charset=UTF-8')
38+
.expect(200)
39+
.end((err, res) => {
40+
if (err) {
41+
return done(err);
42+
}
43+
expect(res.text).toContain('Hello from the server');
44+
expect(res.text).not.toContain('webpack-dev-server/client');
45+
done();
46+
});
47+
});
48+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('Hello from the client');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('Hello from the server');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
module.exports = [
4+
{
5+
mode: 'development',
6+
context: __dirname,
7+
entry: './client.js',
8+
output: {
9+
path: '/',
10+
filename: 'client.js',
11+
},
12+
node: false,
13+
},
14+
{
15+
mode: 'development',
16+
context: __dirname,
17+
target: 'node',
18+
entry: './server.js',
19+
output: {
20+
path: '/',
21+
filename: 'server.js',
22+
},
23+
node: false,
24+
},
25+
];

0 commit comments

Comments
 (0)