Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
105 lines (93 loc) · 3.13 KB
/
Copy pathtest.js
File metadata and controls
105 lines (93 loc) · 3.13 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const fs = require('node:fs');
const test = require('ava');
const webpack = require('p-webpack');
const GopeedPolyfillPlugin = require('./index.js');
test('main', async t => {
await webpack({
entry: './fixture',
output: {
library: {
type: 'commonjs-module',
},
filename: '1.js',
},
plugins: [
new GopeedPolyfillPlugin({
excludeAliases: ['process'],
}),
],
});
t.is(require('./dist/1.js'), 'Hello World');
// https://github.com/defunctzombie/node-process/blob/77caa43cdaee4ea710aa14d11cea1705293c0ef3/browser.js#L16C8-L16C9
t.false(fs.readFileSync('./dist/1.js').toString().includes('clearTimeout has not been defined'));
// https://github.com/feross/buffer/blob/master/index.js#L80
t.true(fs.readFileSync('./dist/1.js').toString().includes('is invalid for option "size"'));
});
test('includeAliases', async t => {
await webpack({
entry: './fixture',
output: {
library: {
type: 'commonjs-module',
},
filename: '2.js',
},
plugins: [
new GopeedPolyfillPlugin({
includeAliases: ['process'],
}),
],
});
t.is(require('./dist/2.js'), 'Hello World');
// https://github.com/defunctzombie/node-process/blob/77caa43cdaee4ea710aa14d11cea1705293c0ef3/browser.js#L16C8-L16C9
t.true(fs.readFileSync('./dist/2.js').toString().includes('clearTimeout has not been defined'));
// https://github.com/feross/buffer/blob/master/index.js#L80
t.false(fs.readFileSync('./dist/2.js').toString().includes('is invalid for option "size"'));
});
test('includeAliases and excludeAliases used at the same time', t => {
t.throws(() => new GopeedPolyfillPlugin({
includeAliases: ['process'],
excludeAliases: ['crypto'],
}), {instanceOf: Error});
});
for (const mode of ['development', 'production']) {
test(`URL exports (${mode})`, async t => {
await webpack({
mode,
entry: './fixture-url.js',
output: {
library: {type: 'commonjs-module'},
filename: `url-${mode}.js`,
},
plugins: [new GopeedPolyfillPlugin({includeAliases: ['url']})],
});
const url = require(`./dist/url-${mode}.js`);
t.is(url.URL, globalThis.URL);
t.is(url.NodeURL, globalThis.URL);
t.is(url.URLSearchParams, globalThis.URLSearchParams);
t.is(url.NodeURLSearchParams, globalThis.URLSearchParams);
t.is(url.default.URL, globalThis.URL);
t.is(new url.URL('/video', 'https://example.com').href, 'https://example.com/video');
t.is(new url.URLSearchParams('p=2').get('p'), '2');
t.is(url.parse('https://example.com/video').hostname, 'example.com');
t.is(url.fileURLToPath('file:///tmp/video.mp4'), '/tmp/video.mp4');
});
}
for (const options of [{excludeAliases: ['url']}, {includeAliases: ['process']}]) {
test(`URL polyfill disabled by ${Object.keys(options)[0]}`, async t => {
const filename = `url-disabled-${Object.keys(options)[0]}.js`;
await webpack({
mode: 'development',
entry: './fixture-url.js',
output: {
library: {type: 'commonjs-module'},
filename,
},
plugins: [new GopeedPolyfillPlugin(options)],
});
const url = require(`./dist/${filename}`);
t.is(url.URL, undefined);
t.is(url.NodeURL, undefined);
t.is(url.parse('https://example.com/video').hostname, 'example.com');
});
}