-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathbundle.test.js
84 lines (70 loc) · 2.31 KB
/
bundle.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
const webpack = require('webpack');
const acorn = require('acorn');
const request = require('supertest');
const Server = require('../../lib/Server');
const config = require('../fixtures/simple-config/webpack.config');
const port = require('../ports-map').bundle;
const isWebpack5 = require('../helpers/isWebpack5');
describe('bundle', () => {
describe('main.js bundled output', () => {
let server;
let req;
beforeAll(async () => {
const compiler = webpack({
...config,
target: isWebpack5 ? ['es5', 'web'] : 'web',
});
server = new Server({ port }, compiler);
await new Promise((resolve, reject) => {
server.listen(port, '127.0.0.1', (error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
req = request(server.app);
});
afterAll(async () => {
await new Promise((resolve) => {
server.close(() => {
resolve();
});
});
});
it('should get full user bundle and parse with ES5', async () => {
const { text } = await req
.get('/main.js')
.expect('Content-Type', 'application/javascript; charset=utf-8')
.expect(200);
expect(() => {
let evalStep = 0;
acorn.parse(text, {
ecmaVersion: 5,
onToken: (token) => {
// a webpack bundle is a series of evaluated JavaScript
// strings like this: eval('...')
// if we want the bundle to work using ES5, we need to
// check that these strings are good with ES5 as well
// this can be done by waiting for tokens during the main parse
// then when we hit a string in an 'eval' function we also try
// to parse that string with ES5
if (token.type.label === 'name' && token.value === 'eval') {
evalStep += 1;
} else if (token.type.label === '(' && evalStep === 1) {
evalStep += 1;
} else if (token.type.label === 'string' && evalStep === 2) {
const program = token.value;
acorn.parse(program, {
ecmaVersion: 5,
});
evalStep = 0;
}
},
});
}).not.toThrow();
});
});
});