-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
webpack-env-tests.ts
157 lines (121 loc) · 4.1 KB
/
webpack-env-tests.ts
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
/// <reference types="node"/>
interface SomeModule {
someMethod(): void;
}
let someModule = require<SomeModule>('./someModule');
someModule.someMethod();
let otherModule = require('./otherModule');
otherModule.otherMethod();
let context = require.context('./somePath', true);
let contextModule = context<SomeModule>('./someModule');
const contextId: string = require.context('./somePath').id;
require(['./someModule', './otherModule'], (someModule: SomeModule, otherModule: any) => {
});
async function testChunkLoad(): Promise<void> {
const module = await __webpack_chunk_load__("./someModule");
}
// check if HMR is enabled
if(module.hot) {
// accept update of dependency without a callback
module.hot.accept("./handler.js");
// accept update of dependency
module.hot.accept("./handler.js", function() {
//...
});
// accept update of dependency with an error handler
module.hot.accept(
"./handler.js",
function() {
//...
},
function(err: Error) {
// ...
}
);
}
module.exports = null;
// check if HMR is enabled
if(module.hot) {
// accept itself
module.hot.accept();
// dispose handler
module.hot.dispose(function() {
// revoke the side effect
//...
});
}
class ModuleData {
updated: boolean;
}
if (module.hot) {
module.hot.accept((err: Error) => {
//...
});
module.hot.decline("./someModule");
module.hot.dispose((data: ModuleData) => {
data.updated = true;
// ...
});
let disposeHandler: ((data: ModuleData) => void) = data => {
// ...
};
module.hot.addDisposeHandler(disposeHandler);
module.hot.removeDisposeHandler(disposeHandler);
module.hot.check(true, (err: Error, outdatedModules: (string|number)[]) => {
// ...
});
module.hot.apply({ ignoreUnaccepted: true }, (err: Error, outdatedModules: (string|number)[]) => {
// ...
});
let status: string = module.hot.status();
let statusHandler: ((status: string) => void) = status => {
// ...
};
module.hot.status(statusHandler);
module.hot.addStatusHandler(statusHandler);
module.hot.removeStatusHandler(statusHandler);
}
require.ensure([], (require) => {
require("some/module");
});
require.ensure([], (require) => {
require("some/module");
}, (e) => {}, 'chunkWithErrorHandling')
require.ensure([], (require) => {
require("some/module");
}, 'chunkWithoutErrorHandling');
// since `compilerOptions["module"] === "commonjs"` is required, add this to test `import.meta` fields.
declare const importMeta: ImportMeta;
if (importMeta.webpack >= 5 && importMeta.webpackHot) {
importMeta.webpackHot.accept((err: Error) => {
//...
});
importMeta.webpackHot.decline('./someModule');
importMeta.webpackHot.dispose((data: ModuleData) => {
data.updated = true;
// ...
});
let disposeHandler: ((data: ModuleData) => void) = data => {
// ...
};
importMeta.webpackHot.addDisposeHandler(disposeHandler);
importMeta.webpackHot.removeDisposeHandler(disposeHandler);
importMeta.webpackHot.check(true, (err: Error, outdatedModules: (string|number)[]) => {
// ...
});
importMeta.webpackHot.apply({ ignoreUnaccepted: true }, (err: Error, outdatedModules: (string|number)[]) => {
// ...
});
let status: string = importMeta.webpackHot.status();
let statusHandler: ((status: string) => void) = status => {
// ...
};
importMeta.webpackHot.status(statusHandler);
importMeta.webpackHot.addStatusHandler(statusHandler);
importMeta.webpackHot.removeStatusHandler(statusHandler);
}
if (importMeta.webpack >= 5 && importMeta.webpackContext) {
let context = importMeta.webpackContext('./somePath', { recursive: true, regExp: /some/, include: /someModule/, exclude: /noNeedModuel/, preload: true, prefetch: true, chunkName: "[request]", exports: "default", mode: "weak" });
let contextModule = context<SomeModule>('./someModule');
const contextId: string = importMeta.webpackContext('./somePath').id;
}