Skip to content

Commit c7e53b0

Browse files
authored
Fix: ES5 client (#2658)
1 parent ab7f6dd commit c7e53b0

File tree

9 files changed

+170
-40
lines changed

9 files changed

+170
-40
lines changed

client-src/default/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ window.process = window.process || {};
66
window.process.env = window.process.env || {};
77

88
/* global __resourceQuery WorkerGlobalScope self */
9-
const stripAnsi = require('strip-ansi');
9+
const stripAnsi = require('../transpiled-modules/strip-ansi');
1010
const socket = require('./socket');
1111
const overlay = require('./overlay');
1212
const { log, setLogLevel } = require('./utils/log');

client-src/default/utils/log.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const log = require('webpack/lib/logging/runtime');
3+
const log = require('../../transpiled-modules/log');
44

55
const name = 'webpack-dev-server';
66
// default level is set on the client side, so it does not need

client-src/transpiled-modules/log.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('webpack/lib/logging/runtime');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('strip-ansi');
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const merge = require('webpack-merge');
5+
6+
const base = {
7+
mode: 'production',
8+
output: {
9+
path: path.resolve(__dirname, '../../client/transpiled-modules'),
10+
libraryTarget: 'commonjs2',
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.js$/,
16+
use: [
17+
{
18+
loader: 'babel-loader',
19+
},
20+
],
21+
},
22+
],
23+
},
24+
};
25+
26+
module.exports = [
27+
merge(base, {
28+
entry: path.join(__dirname, 'log.js'),
29+
output: {
30+
filename: 'log.js',
31+
},
32+
}),
33+
merge(base, {
34+
entry: path.join(__dirname, 'strip-ansi.js'),
35+
output: {
36+
filename: 'strip-ansi.js',
37+
},
38+
}),
39+
];

package-lock.json

Lines changed: 38 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"build:client:clients": "babel client-src/clients --out-dir client/clients",
3030
"build:client:index": "webpack --color --config client-src/default/webpack.config.js",
3131
"build:client:sockjs": "webpack --color --config client-src/sockjs/webpack.config.js",
32+
"build:client:transpiled-modules": "webpack --color --config client-src/transpiled-modules/webpack.config.js",
3233
"build:client": "rimraf ./client/* && npm-run-all -s -l -p \"build:client:**\"",
3334
"webpack-dev-server": "node examples/run-example.js",
3435
"release": "standard-version"
@@ -74,6 +75,7 @@
7475
"@commitlint/cli": "^9.1.2",
7576
"@commitlint/config-conventional": "^10.0.0",
7677
"@jest/test-sequencer": "^26.3.0",
78+
"acorn": "^8.0.1",
7779
"babel-jest": "^26.3.0",
7880
"babel-loader": "^8.1.0",
7981
"body-parser": "^1.19.0",
@@ -107,7 +109,8 @@
107109
"typescript": "^3.9.7",
108110
"url-loader": "^4.1.0",
109111
"webpack": "^4.44.1",
110-
"webpack-cli": "^3.3.12"
112+
"webpack-cli": "^3.3.12",
113+
"webpack-merge": "^4.2.2"
111114
},
112115
"peerDependencies": {
113116
"webpack": "^4.0.0 || ^5.0.0"

test/client/bundle.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const acorn = require('acorn');
6+
const request = require('supertest');
7+
const testServer = require('../helpers/test-server');
8+
const config = require('../fixtures/simple-config/webpack.config');
9+
const port = require('../ports-map').bundle;
10+
const isWebpack5 = require('../helpers/isWebpack5');
11+
12+
describe('bundle', () => {
13+
// the ES5 check test for the bundle will not work on webpack@5,
14+
// because webpack@5 bundle output uses some ES6 syntax that can
15+
// only be avoided with babel-loader
16+
const runBundleTest = isWebpack5 ? describe.skip : describe;
17+
18+
runBundleTest('index.bundle.js bundled output', () => {
19+
it('should parse with ES5', () => {
20+
const bundleStr = fs.readFileSync(
21+
path.resolve(__dirname, '../../client/default/index.bundle.js'),
22+
'utf8'
23+
);
24+
expect(() => {
25+
acorn.parse(bundleStr, {
26+
ecmaVersion: 5,
27+
});
28+
}).not.toThrow();
29+
});
30+
});
31+
32+
runBundleTest('main.js bundled output', () => {
33+
let server;
34+
let req;
35+
36+
beforeAll((done) => {
37+
server = testServer.start(config, { port }, done);
38+
req = request(server.app);
39+
});
40+
41+
afterAll(testServer.close);
42+
43+
it('should get full user bundle and parse with ES5', async () => {
44+
const { text } = await req
45+
.get('/main.js')
46+
.expect('Content-Type', 'application/javascript; charset=utf-8')
47+
.expect(200);
48+
49+
expect(() => {
50+
let evalStep = 0;
51+
acorn.parse(text, {
52+
ecmaVersion: 5,
53+
onToken: (token) => {
54+
// a webpack bundle is a series of evaluated JavaScript
55+
// strings like this: eval('...')
56+
// if we want the bundle to work using ES5, we need to
57+
// check that these strings are good with ES5 as well
58+
59+
// this can be done by waiting for tokens during the main parse
60+
// then when we hit a string in an 'eval' function we also try
61+
// to parse that string with ES5
62+
if (token.type.label === 'name' && token.value === 'eval') {
63+
evalStep += 1;
64+
} else if (token.type.label === '(' && evalStep === 1) {
65+
evalStep += 1;
66+
} else if (token.type.label === 'string' && evalStep === 2) {
67+
const program = token.value;
68+
acorn.parse(program, {
69+
ecmaVersion: 5,
70+
});
71+
72+
evalStep = 0;
73+
}
74+
},
75+
});
76+
}).not.toThrow();
77+
});
78+
});
79+
});

test/ports-map.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const portsList = {
4242
Iframe: 1,
4343
SocketInjection: 1,
4444
'static-publicPath-option': 1,
45+
'contentBasePublicPath-option': 1,
46+
bundle: 1,
4547
};
4648

4749
let startPort = 8089;

0 commit comments

Comments
 (0)