-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnormalizeOptions.test.ts
More file actions
162 lines (152 loc) · 4.24 KB
/
normalizeOptions.test.ts
File metadata and controls
162 lines (152 loc) · 4.24 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { type RspackOptions, rspack } from '@rspack/core';
import { type Configuration, RspackDevServer } from '@rspack/dev-server';
import ReactRefreshPlugin from '@rspack/plugin-react-refresh';
// The aims of use a cutstom value rather than
// default is to avoid stack overflow trigged
// by `webpack/schemas/WebpackOption.check.js` in debug mode
const ENTRY = './placeholder.js';
const ENTRY1 = './placeholder1.js';
// TODO: node 20 works but node 18 and 16 failed
describe.skip('normalize options snapshot', () => {
it('no options', async () => {
await match({});
});
it('port string', async () => {
await match({
devServer: {
port: '9000',
},
});
});
it('additional entires should added', async () => {
expect(
await getAdditionEntries({}, { entry: ['something'] }),
).toMatchSnapshot();
});
it("shouldn't have reactRefreshEntry.js by default when in production mode", async () => {
const reactRefreshEntry =
'plugin-react-refresh/client/reactRefreshEntry.js';
const entries1 = await getAdditionEntries(
{},
{
mode: 'production',
entry: ['something'],
},
);
expect(entries1.undefined).not.toContain(reactRefreshEntry);
const entries2 = await getAdditionEntries(
{},
{
mode: 'production',
entry: ['something'],
plugins: [new ReactRefreshPlugin({ forceEnable: true })],
},
);
expect(entries2.undefined).toContain(reactRefreshEntry);
const entries3 = await getAdditionEntries(
{},
{
mode: 'development',
entry: ['something'],
plugins: [new ReactRefreshPlugin()],
},
);
expect(entries3.undefined).toContain(reactRefreshEntry);
const entries4 = await getAdditionEntries(
{},
{
mode: 'production',
entry: ['something'],
plugins: [new ReactRefreshPlugin()],
},
);
expect(entries4.undefined).not.toContain(reactRefreshEntry);
});
it('should apply HMR plugin by default', async () => {
const compiler = rspack({
entry: ENTRY,
stats: 'none',
});
const server = new RspackDevServer({}, compiler);
await server.start();
const hmrPlugins = compiler.__internal__builtinPlugins.filter(
(p) => p.name === 'HotModuleReplacementPlugin',
);
expect(hmrPlugins.length).toBe(1);
expect(server.options.hot).toBe(true);
await server.stop();
});
it('should support multi-compiler', async () => {
const compiler = rspack([
{
entry: ENTRY,
stats: 'none',
},
{
entry: ENTRY1,
stats: 'none',
},
]);
const server = new RspackDevServer({}, compiler);
await server.start();
await server.stop();
});
});
async function match(config: RspackOptions) {
const compiler = rspack({
...config,
entry: ENTRY,
stats: 'none',
});
const server = new RspackDevServer(
compiler.options.devServer ?? {},
compiler,
);
await server.start();
// it will break ci
//@ts-ignore
server.options.port = undefined;
expect(server.options).toMatchSnapshot();
await server.stop();
}
async function getAdditionEntries(
serverConfig: Configuration,
config: RspackOptions,
) {
const compiler = rspack({
...config,
stats: 'none',
entry: ENTRY,
});
const server = new RspackDevServer(serverConfig, compiler);
await server.start();
const entries = compiler.__internal__builtinPlugins
.filter((p) => p.name === 'EntryPlugin')
.map((p) => p.options)
.reduce<Record<string, any>>((acc: any, cur: any) => {
const name = cur.options.name;
const request = cur.entry;
if (acc[name]) {
acc[name].import.push(request);
} else {
acc[name] = { import: [request] };
}
return acc;
}, {});
// some hack for snapshot
const value = Object.fromEntries(
Object.entries(entries).map(([key, item]) => {
// @ts-expect-error
const replaced = item.import?.map((entry) => {
const array = entry
.replace(/\\/g, '/')
.replace(/port=\d+/g, '')
.split('/');
return `<prefix>/${array.slice(-3).join('/')}`;
});
return [key, replaced];
}),
);
await server.stop();
return value;
}