Skip to content

Commit

Permalink
fix(addEntries): only add client entry to web and webworker targets
Browse files Browse the repository at this point in the history
  • Loading branch information
mlrawlings committed Apr 9, 2019
1 parent 99e8db0 commit b03d176
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 14 deletions.
42 changes: 28 additions & 14 deletions lib/utils/addEntries.js
Expand Up @@ -17,36 +17,40 @@ function addEntries(config, options, server) {

const domain = createDomain(options, app);
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
const entries = [
`${require.resolve('../../client/')}?${domain}${sockPath}`,
];
const clientEntry = `${require.resolve(
'../../client/'
)}?${domain}${sockPath}`;
let hotEntry;

if (options.hotOnly) {
entries.push(require.resolve('webpack/hot/only-dev-server'));
hotEntry = require.resolve('webpack/hot/only-dev-server');
} else if (options.hot) {
entries.push(require.resolve('webpack/hot/dev-server'));
hotEntry = require.resolve('webpack/hot/dev-server');
}

const prependEntry = (entry) => {
if (typeof entry === 'function') {
return () => Promise.resolve(entry()).then(prependEntry);
const prependEntry = (originalEntry, additionalEntries) => {
if (typeof originalEntry === 'function') {
return () =>
Promise.resolve(originalEntry()).then((entry) =>
prependEntry(entry, additionalEntries)
);
}

if (typeof entry === 'object' && !Array.isArray(entry)) {
if (typeof originalEntry === 'object' && !Array.isArray(originalEntry)) {
const clone = {};

Object.keys(entry).forEach((key) => {
Object.keys(originalEntry).forEach((key) => {
// entry[key] should be a string here
clone[key] = prependEntry(entry[key]);
clone[key] = prependEntry(originalEntry[key], additionalEntries);
});

return clone;
}

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

// eslint-disable-next-line no-shadow
[].concat(config).forEach((config) => {
config.entry = prependEntry(config.entry || './src');
const webTarget =
config.target === 'web' ||
config.target === 'webworker' ||
config.target == null;
const additionalEntries = webTarget ? [clientEntry] : [];

if (hotEntry) {
additionalEntries.push(hotEntry);
}

config.entry = prependEntry(config.entry || './src', additionalEntries);

if (options.hot || options.hotOnly) {
config.plugins = config.plugins || [];
Expand Down
48 changes: 48 additions & 0 deletions test/UniversalCompiler.test.js
@@ -0,0 +1,48 @@
'use strict';

const request = require('supertest');
const helper = require('./helper');
const config = require('./fixtures/universal-compiler-config/webpack.config');

describe('UniversalCompiler', () => {
let server;
let req;
beforeAll((done) => {
server = helper.start(config, { inline: true }, done);
req = request(server.app);
});

afterAll(helper.close);

it('client bundle should have the inlined the client runtime', (done) => {
req
.get('/client.js')
.expect('Content-Type', 'application/javascript; charset=UTF-8')
.expect(200)
.end((err, res) => {
if (err) {
return done(err);
}
expect(res.text).toContain('Hello from the client');
expect(res.text).toContain('webpack-dev-server/client');
done();
});
});

it('server bundle should NOT have the inlined the client runtime', (done) => {
// we wouldn't normally request a server bundle
// but we'll do it here to check the contents
req
.get('/server.js')
.expect('Content-Type', 'application/javascript; charset=UTF-8')
.expect(200)
.end((err, res) => {
if (err) {
return done(err);
}
expect(res.text).toContain('Hello from the server');
expect(res.text).not.toContain('webpack-dev-server/client');
done();
});
});
});
3 changes: 3 additions & 0 deletions test/fixtures/universal-compiler-config/client.js
@@ -0,0 +1,3 @@
'use strict';

console.log('Hello from the client');
3 changes: 3 additions & 0 deletions test/fixtures/universal-compiler-config/server.js
@@ -0,0 +1,3 @@
'use strict';

console.log('Hello from the server');
25 changes: 25 additions & 0 deletions test/fixtures/universal-compiler-config/webpack.config.js
@@ -0,0 +1,25 @@
'use strict';

module.exports = [
{
mode: 'development',
context: __dirname,
entry: './client.js',
output: {
path: '/',
filename: 'client.js',
},
node: false,
},
{
mode: 'development',
context: __dirname,
target: 'node',
entry: './server.js',
output: {
path: '/',
filename: 'server.js',
},
node: false,
},
];

0 comments on commit b03d176

Please sign in to comment.