@shuvi/toolpack 0.0.1-rc.9 → 1.0.0-rc.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/babel/plugins/auto-css-modules.js +1 -1
- package/lib/babel/plugins/{loadable-plugin.d.ts → dynamic-plugin.d.ts} +1 -1
- package/lib/babel/plugins/{loadable-plugin.js → dynamic-plugin.js} +9 -9
- package/lib/babel/plugins/optimize-hook-destructuring.d.ts +1 -1
- package/lib/babel/plugins/optimize-hook-destructuring.js +4 -4
- package/lib/babel/preset.js +25 -5
- package/lib/constants.d.ts +1 -0
- package/lib/constants.js +5 -1
- package/lib/utils/bundle-require.d.ts +33 -0
- package/lib/utils/bundle-require.js +143 -0
- package/lib/utils/forkTsCheckerWebpackPlugin.js +2 -1
- package/lib/utils/formatWebpackMessages.d.ts +18 -4
- package/lib/utils/formatWebpackMessages.js +17 -14
- package/lib/webpack/config/base.d.ts +17 -4
- package/lib/webpack/config/base.js +144 -99
- package/lib/webpack/config/browser.d.ts +3 -4
- package/lib/webpack/config/browser.js +132 -56
- package/lib/webpack/config/index.js +18 -7
- package/lib/webpack/config/node.d.ts +3 -4
- package/lib/webpack/config/node.js +21 -28
- package/lib/webpack/config/parts/external.d.ts +3 -2
- package/lib/webpack/config/parts/external.js +16 -10
- package/lib/webpack/config/parts/helpers.d.ts +3 -1
- package/lib/webpack/config/parts/helpers.js +14 -1
- package/lib/webpack/config/parts/resolve.js +1 -0
- package/lib/webpack/config/parts/style.d.ts +5 -2
- package/lib/webpack/config/parts/style.js +125 -60
- package/lib/webpack/dynamic-dll/bundler/index.d.ts +22 -0
- package/lib/webpack/dynamic-dll/bundler/index.js +211 -0
- package/lib/webpack/dynamic-dll/bundler/webpack-config.d.ts +16 -0
- package/lib/webpack/dynamic-dll/bundler/webpack-config.js +116 -0
- package/lib/webpack/dynamic-dll/constants.d.ts +6 -0
- package/lib/webpack/dynamic-dll/constants.js +9 -0
- package/lib/webpack/dynamic-dll/dep/getCJSExports.d.ts +3 -0
- package/lib/webpack/dynamic-dll/dep/getCJSExports.js +58 -0
- package/lib/webpack/dynamic-dll/dep/getExposeFromContent.d.ts +1 -0
- package/lib/webpack/dynamic-dll/dep/getExposeFromContent.js +80 -0
- package/lib/webpack/dynamic-dll/dep/getModuleExports.d.ts +7 -0
- package/lib/webpack/dynamic-dll/dep/getModuleExports.js +46 -0
- package/lib/webpack/dynamic-dll/dep/index.d.ts +13 -0
- package/lib/webpack/dynamic-dll/dep/index.js +43 -0
- package/lib/webpack/dynamic-dll/helper/check-not-in-node-modules.d.ts +1 -0
- package/lib/webpack/dynamic-dll/helper/check-not-in-node-modules.js +15 -0
- package/lib/webpack/dynamic-dll/index.d.ts +35 -0
- package/lib/webpack/dynamic-dll/index.js +198 -0
- package/lib/webpack/dynamic-dll/metadata.d.ts +13 -0
- package/lib/webpack/dynamic-dll/metadata.js +62 -0
- package/lib/webpack/dynamic-dll/moduleCollector.d.ts +28 -0
- package/lib/webpack/dynamic-dll/moduleCollector.js +64 -0
- package/lib/webpack/dynamic-dll/plugin/dynamic-dll-plugin.d.ts +24 -0
- package/lib/webpack/dynamic-dll/plugin/dynamic-dll-plugin.js +70 -0
- package/lib/webpack/dynamic-dll/utils.d.ts +6 -0
- package/lib/webpack/dynamic-dll/utils.js +29 -0
- package/lib/webpack/index.d.ts +15 -0
- package/lib/webpack/index.js +110 -0
- package/lib/webpack/loaders/parcel-css-loader/CssSyntaxError.d.ts +11 -0
- package/lib/webpack/loaders/parcel-css-loader/CssSyntaxError.js +40 -0
- package/lib/webpack/loaders/parcel-css-loader/index.d.ts +4 -0
- package/lib/webpack/loaders/parcel-css-loader/index.js +369 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/api.d.ts +2 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/api.js +85 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/getUrl.d.ts +2 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/getUrl.js +23 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/noSourceMaps.d.ts +2 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/noSourceMaps.js +2 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/sourceMaps.d.ts +2 -0
- package/lib/webpack/loaders/parcel-css-loader/runtime/sourceMaps.js +16 -0
- package/lib/webpack/loaders/parcel-css-loader/utils.d.ts +28 -0
- package/lib/webpack/loaders/parcel-css-loader/utils.js +869 -0
- package/lib/webpack/loaders/route-component-loader.js +25 -5
- package/lib/webpack/loaders/shuvi-swc-loader/getLoaderSWCOptions.d.ts +94 -0
- package/lib/webpack/loaders/shuvi-swc-loader/getLoaderSWCOptions.js +162 -0
- package/lib/webpack/loaders/shuvi-swc-loader/index.d.ts +2 -0
- package/lib/webpack/loaders/shuvi-swc-loader/index.js +119 -0
- package/lib/webpack/plugins/build-manifest-plugin.js +44 -23
- package/lib/webpack/plugins/dynamic-public-path-plugin.d.ts +9 -0
- package/lib/webpack/plugins/dynamic-public-path-plugin.js +50 -0
- package/lib/webpack/plugins/fix-watching-plugin.d.ts +4 -0
- package/lib/webpack/plugins/fix-watching-plugin.js +23 -0
- package/lib/webpack/plugins/jsconfig-paths-plugin.d.ts +44 -0
- package/lib/webpack/plugins/jsconfig-paths-plugin.js +228 -0
- package/lib/webpack/plugins/module-replace-plugin/index.js +5 -1
- package/lib/webpack/plugins/module-replace-plugin/plugin.d.ts +7 -6
- package/lib/webpack/plugins/module-replace-plugin/plugin.js +56 -33
- package/lib/webpack/plugins/module-replace-plugin/stub-loader.js +47 -7
- package/lib/webpack/plugins/require-cache-hot-reloader-plugin.js +9 -8
- package/lib/webpack/plugins/support-ts-extension-resolver-plugin.d.ts +4 -0
- package/lib/webpack/plugins/support-ts-extension-resolver-plugin.js +23 -0
- package/lib/webpack/types.d.ts +40 -0
- package/lib/webpack/types.js +2 -0
- package/package.json +46 -40
- package/lib/utils/emptyComponent.d.ts +0 -1
- package/lib/utils/emptyComponent.js +0 -7
- package/lib/utils/errorOverlayMiddleware.d.ts +0 -1
- package/lib/utils/errorOverlayMiddleware.js +0 -21
- package/lib/utils/hotDevClient/eventsource.d.ts +0 -1
- package/lib/utils/hotDevClient/eventsource.js +0 -63
- package/lib/utils/hotDevClient/index.d.ts +0 -4
- package/lib/utils/hotDevClient/index.js +0 -289
- package/lib/utils/verifyTypeScriptSetup.d.ts +0 -5
- package/lib/utils/verifyTypeScriptSetup.js +0 -229
- package/lib/webpack/loaders/export-global-loader.d.ts +0 -7
- package/lib/webpack/loaders/export-global-loader.js +0 -26
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.patternText = exports.matchedText = exports.isString = exports.matchPatternOrExact = exports.findBestPatternMatch = exports.tryParsePattern = exports.pathIsRelative = exports.hasZeroOrOneAsteriskCharacter = void 0;
|
|
27
|
+
/**
|
|
28
|
+
* This webpack resolver is largely based on TypeScript's "paths" handling
|
|
29
|
+
* The TypeScript license can be found here:
|
|
30
|
+
* https://github.com/microsoft/TypeScript/blob/214df64e287804577afa1fea0184c18c40f7d1ca/LICENSE.txt
|
|
31
|
+
*/
|
|
32
|
+
const path = __importStar(require("path"));
|
|
33
|
+
const asterisk = 0x2a;
|
|
34
|
+
function hasZeroOrOneAsteriskCharacter(str) {
|
|
35
|
+
let seenAsterisk = false;
|
|
36
|
+
for (let i = 0; i < str.length; i++) {
|
|
37
|
+
if (str.charCodeAt(i) === asterisk) {
|
|
38
|
+
if (!seenAsterisk) {
|
|
39
|
+
seenAsterisk = true;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// have already seen asterisk
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
exports.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter;
|
|
50
|
+
/**
|
|
51
|
+
* Determines whether a path starts with a relative path component (i.e. `.` or `..`).
|
|
52
|
+
*/
|
|
53
|
+
function pathIsRelative(testPath) {
|
|
54
|
+
return /^\.\.?($|[\\/])/.test(testPath);
|
|
55
|
+
}
|
|
56
|
+
exports.pathIsRelative = pathIsRelative;
|
|
57
|
+
function tryParsePattern(pattern) {
|
|
58
|
+
// This should be verified outside of here and a proper error thrown.
|
|
59
|
+
const indexOfStar = pattern.indexOf('*');
|
|
60
|
+
return indexOfStar === -1
|
|
61
|
+
? undefined
|
|
62
|
+
: {
|
|
63
|
+
prefix: pattern.slice(0, indexOfStar),
|
|
64
|
+
suffix: pattern.slice(indexOfStar + 1)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
exports.tryParsePattern = tryParsePattern;
|
|
68
|
+
function isPatternMatch({ prefix, suffix }, candidate) {
|
|
69
|
+
return (candidate.length >= prefix.length + suffix.length &&
|
|
70
|
+
candidate.startsWith(prefix) &&
|
|
71
|
+
candidate.endsWith(suffix));
|
|
72
|
+
}
|
|
73
|
+
/** Return the object corresponding to the best pattern to match `candidate`. */
|
|
74
|
+
function findBestPatternMatch(values, getPattern, candidate) {
|
|
75
|
+
let matchedValue;
|
|
76
|
+
// use length of prefix as betterness criteria
|
|
77
|
+
let longestMatchPrefixLength = -1;
|
|
78
|
+
for (const v of values) {
|
|
79
|
+
const pattern = getPattern(v);
|
|
80
|
+
if (isPatternMatch(pattern, candidate) &&
|
|
81
|
+
pattern.prefix.length > longestMatchPrefixLength) {
|
|
82
|
+
longestMatchPrefixLength = pattern.prefix.length;
|
|
83
|
+
matchedValue = v;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return matchedValue;
|
|
87
|
+
}
|
|
88
|
+
exports.findBestPatternMatch = findBestPatternMatch;
|
|
89
|
+
/**
|
|
90
|
+
* patternStrings contains both pattern strings (containing "*") and regular strings.
|
|
91
|
+
* Return an exact match if possible, or a pattern match, or undefined.
|
|
92
|
+
* (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.)
|
|
93
|
+
*/
|
|
94
|
+
function matchPatternOrExact(patternStrings, candidate) {
|
|
95
|
+
const patterns = [];
|
|
96
|
+
for (const patternString of patternStrings) {
|
|
97
|
+
if (!hasZeroOrOneAsteriskCharacter(patternString))
|
|
98
|
+
continue;
|
|
99
|
+
const pattern = tryParsePattern(patternString);
|
|
100
|
+
if (pattern) {
|
|
101
|
+
patterns.push(pattern);
|
|
102
|
+
}
|
|
103
|
+
else if (patternString === candidate) {
|
|
104
|
+
// pattern was matched as is - no need to search further
|
|
105
|
+
return patternString;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return findBestPatternMatch(patterns, _ => _, candidate);
|
|
109
|
+
}
|
|
110
|
+
exports.matchPatternOrExact = matchPatternOrExact;
|
|
111
|
+
/**
|
|
112
|
+
* Tests whether a value is string
|
|
113
|
+
*/
|
|
114
|
+
function isString(text) {
|
|
115
|
+
return typeof text === 'string';
|
|
116
|
+
}
|
|
117
|
+
exports.isString = isString;
|
|
118
|
+
/**
|
|
119
|
+
* Given that candidate matches pattern, returns the text matching the '*'.
|
|
120
|
+
* E.g.: matchedText(tryParsePattern("foo*baz"), "foobarbaz") === "bar"
|
|
121
|
+
*/
|
|
122
|
+
function matchedText(pattern, candidate) {
|
|
123
|
+
return candidate.substring(pattern.prefix.length, candidate.length - pattern.suffix.length);
|
|
124
|
+
}
|
|
125
|
+
exports.matchedText = matchedText;
|
|
126
|
+
function patternText({ prefix, suffix }) {
|
|
127
|
+
return `${prefix}*${suffix}`;
|
|
128
|
+
}
|
|
129
|
+
exports.patternText = patternText;
|
|
130
|
+
/**
|
|
131
|
+
* Calls the iterator function for each entry of the array
|
|
132
|
+
* until the first result or error is reached
|
|
133
|
+
*/
|
|
134
|
+
function forEachBail(array, iterator, callback) {
|
|
135
|
+
if (array.length === 0)
|
|
136
|
+
return callback();
|
|
137
|
+
let i = 0;
|
|
138
|
+
const next = () => {
|
|
139
|
+
let loop = undefined;
|
|
140
|
+
iterator(array[i++], (err, result) => {
|
|
141
|
+
if (err || result !== undefined || i >= array.length) {
|
|
142
|
+
return callback(err, result);
|
|
143
|
+
}
|
|
144
|
+
if (loop === false)
|
|
145
|
+
while (next())
|
|
146
|
+
;
|
|
147
|
+
loop = true;
|
|
148
|
+
});
|
|
149
|
+
if (!loop)
|
|
150
|
+
loop = false;
|
|
151
|
+
return loop;
|
|
152
|
+
};
|
|
153
|
+
while (next())
|
|
154
|
+
;
|
|
155
|
+
}
|
|
156
|
+
const NODE_MODULES_REGEX = /node_modules/;
|
|
157
|
+
/**
|
|
158
|
+
* Handles tsconfig.json or jsconfig.js "paths" option for webpack
|
|
159
|
+
* Largely based on how the TypeScript compiler handles it:
|
|
160
|
+
* https://github.com/microsoft/TypeScript/blob/1a9c8197fffe3dace5f8dca6633d450a88cba66d/src/compiler/moduleNameResolver.ts#L1362
|
|
161
|
+
*/
|
|
162
|
+
class JsConfigPathsPlugin {
|
|
163
|
+
constructor(paths, resolvedBaseUrl) {
|
|
164
|
+
this.paths = paths;
|
|
165
|
+
this.resolvedBaseUrl = resolvedBaseUrl;
|
|
166
|
+
}
|
|
167
|
+
apply(resolver) {
|
|
168
|
+
const paths = this.paths;
|
|
169
|
+
const pathsKeys = Object.keys(paths);
|
|
170
|
+
// If no aliases are added bail out
|
|
171
|
+
if (pathsKeys.length === 0) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const baseDirectory = this.resolvedBaseUrl;
|
|
175
|
+
const target = resolver.ensureHook('resolve');
|
|
176
|
+
resolver
|
|
177
|
+
.getHook('described-resolve')
|
|
178
|
+
.tapAsync('JsConfigPathsPlugin', (request, resolveContext, callback) => {
|
|
179
|
+
const moduleName = request.request;
|
|
180
|
+
// Exclude node_modules from paths support (speeds up resolving)
|
|
181
|
+
if (request.path.match(NODE_MODULES_REGEX)) {
|
|
182
|
+
return callback();
|
|
183
|
+
}
|
|
184
|
+
if (path.posix.isAbsolute(moduleName) ||
|
|
185
|
+
(process.platform === 'win32' && path.win32.isAbsolute(moduleName))) {
|
|
186
|
+
return callback();
|
|
187
|
+
}
|
|
188
|
+
if (pathIsRelative(moduleName)) {
|
|
189
|
+
return callback();
|
|
190
|
+
}
|
|
191
|
+
// If the module name does not match any of the patterns in `paths` we hand off resolving to webpack
|
|
192
|
+
const matchedPattern = matchPatternOrExact(pathsKeys, moduleName);
|
|
193
|
+
if (!matchedPattern) {
|
|
194
|
+
return callback();
|
|
195
|
+
}
|
|
196
|
+
const matchedStar = isString(matchedPattern)
|
|
197
|
+
? undefined
|
|
198
|
+
: matchedText(matchedPattern, moduleName);
|
|
199
|
+
const matchedPatternText = isString(matchedPattern)
|
|
200
|
+
? matchedPattern
|
|
201
|
+
: patternText(matchedPattern);
|
|
202
|
+
let triedPaths = [];
|
|
203
|
+
forEachBail(paths[matchedPatternText], (subst, pathCallback) => {
|
|
204
|
+
const curPath = matchedStar
|
|
205
|
+
? subst.replace('*', matchedStar)
|
|
206
|
+
: subst;
|
|
207
|
+
// Ensure .d.ts is not matched
|
|
208
|
+
if (curPath.endsWith('.d.ts')) {
|
|
209
|
+
// try next path candidate
|
|
210
|
+
return pathCallback();
|
|
211
|
+
}
|
|
212
|
+
const candidate = path.join(baseDirectory, curPath);
|
|
213
|
+
const obj = Object.assign({}, request, {
|
|
214
|
+
request: candidate
|
|
215
|
+
});
|
|
216
|
+
resolver.doResolve(target, obj, `Aliased with tsconfig.json or jsconfig.json ${matchedPatternText} to ${candidate}`, resolveContext, (resolverErr, resolverResult) => {
|
|
217
|
+
if (resolverErr || resolverResult === undefined) {
|
|
218
|
+
triedPaths.push(candidate);
|
|
219
|
+
// try next path candidate
|
|
220
|
+
return pathCallback();
|
|
221
|
+
}
|
|
222
|
+
return pathCallback(resolverErr, resolverResult);
|
|
223
|
+
});
|
|
224
|
+
}, callback);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
exports.default = JsConfigPathsPlugin;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = void 0;
|
|
3
7
|
var plugin_1 = require("./plugin");
|
|
4
|
-
exports
|
|
8
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(plugin_1).default; } });
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { Compiler, Plugin } from 'webpack';
|
|
2
2
|
export declare type ConfigItem = {
|
|
3
|
-
|
|
3
|
+
resourceQuery: Function | RegExp;
|
|
4
4
|
module: string;
|
|
5
5
|
};
|
|
6
6
|
export interface ModuleReplacePluginOptions {
|
|
7
7
|
modules: ConfigItem[];
|
|
8
8
|
}
|
|
9
|
+
export interface Loader {
|
|
10
|
+
loader: string;
|
|
11
|
+
options: Record<string, any>;
|
|
12
|
+
}
|
|
9
13
|
export interface ModuleInfo {
|
|
10
14
|
action: typeof ModuleAction[keyof typeof ModuleAction];
|
|
11
15
|
compiler: Compiler;
|
|
12
16
|
replacedModule: string;
|
|
13
|
-
loaders
|
|
14
|
-
loader: string;
|
|
15
|
-
options: Record<string, any>;
|
|
16
|
-
}[];
|
|
17
|
+
loaders: Loader[];
|
|
17
18
|
}
|
|
18
19
|
declare const ModuleAction: {
|
|
19
20
|
readonly REPLACE: "replace";
|
|
@@ -22,10 +23,10 @@ declare const ModuleAction: {
|
|
|
22
23
|
export default class ModuleReplacePlugin implements Plugin {
|
|
23
24
|
private _options;
|
|
24
25
|
static restoreModule(id: string): false | Promise<any>;
|
|
26
|
+
static replaceModule(id: string): false | void;
|
|
25
27
|
constructor(options: Partial<ModuleReplacePluginOptions>);
|
|
26
28
|
apply(compiler: Compiler): void;
|
|
27
29
|
private _handleBuildModule;
|
|
28
30
|
private _collectModules;
|
|
29
|
-
private _getReplacedModule;
|
|
30
31
|
}
|
|
31
32
|
export {};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const REPLACED = Symbol('replaced');
|
|
4
|
+
const stubLoader = require.resolve('./stub-loader');
|
|
5
|
+
const pitchLoader = stubLoader;
|
|
4
6
|
const toString = Object.prototype.toString;
|
|
5
7
|
function isRegExp(target) {
|
|
6
8
|
return toString.call(target) === `[object RegExp]`;
|
|
@@ -12,7 +14,25 @@ function getModuleId(wpModule) {
|
|
|
12
14
|
var _a;
|
|
13
15
|
return wpModule.rawRequest || ((_a = wpModule === null || wpModule === void 0 ? void 0 : wpModule.createData) === null || _a === void 0 ? void 0 : _a.rawRequest);
|
|
14
16
|
}
|
|
15
|
-
|
|
17
|
+
function isPitcher(loader) {
|
|
18
|
+
return loader.loader === pitchLoader;
|
|
19
|
+
}
|
|
20
|
+
function findReplacedModule(configs, query) {
|
|
21
|
+
for (let index = 0; index < configs.length; index++) {
|
|
22
|
+
const { resourceQuery, module } = configs[index];
|
|
23
|
+
let isMatch = false;
|
|
24
|
+
if (isRegExp(resourceQuery)) {
|
|
25
|
+
isMatch = resourceQuery.test(query);
|
|
26
|
+
}
|
|
27
|
+
else if (isFunction(resourceQuery)) {
|
|
28
|
+
isMatch = resourceQuery(query);
|
|
29
|
+
}
|
|
30
|
+
if (isMatch) {
|
|
31
|
+
return module;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
16
36
|
const ModuleAction = {
|
|
17
37
|
REPLACE: 'replace',
|
|
18
38
|
RESTORE: 'restore'
|
|
@@ -55,17 +75,37 @@ class ModuleReplacePlugin {
|
|
|
55
75
|
moduleInfos.forEach(moduleInfo => {
|
|
56
76
|
moduleInfo.action = ModuleAction.RESTORE;
|
|
57
77
|
handler.pending.set(moduleInfo.compiler, false);
|
|
58
|
-
moduleInfo.compiler.hooks.invalid.call('noop', new Date().getTime());
|
|
59
78
|
});
|
|
60
79
|
return new Promise(resolve => {
|
|
61
80
|
handler.resolve = resolve;
|
|
62
81
|
});
|
|
63
82
|
}
|
|
83
|
+
static replaceModule(id) {
|
|
84
|
+
const moduleInfos = getKnownModules(id);
|
|
85
|
+
if (moduleInfos.length < 1) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
moduleInfos.forEach(moduleInfo => {
|
|
89
|
+
moduleInfo.action = ModuleAction.REPLACE;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
64
92
|
apply(compiler) {
|
|
93
|
+
var _a;
|
|
94
|
+
const { modules } = this._options;
|
|
65
95
|
// init compiler info
|
|
66
96
|
compilerInfo.set(compiler, {
|
|
67
97
|
modules: new Map()
|
|
68
98
|
});
|
|
99
|
+
const pitcher = {
|
|
100
|
+
loader: pitchLoader,
|
|
101
|
+
resourceQuery(query) {
|
|
102
|
+
const find = findReplacedModule(modules, query);
|
|
103
|
+
return !!find;
|
|
104
|
+
},
|
|
105
|
+
options: {}
|
|
106
|
+
};
|
|
107
|
+
// replace original rules
|
|
108
|
+
(_a = compiler.options.module.rules) === null || _a === void 0 ? void 0 : _a.unshift(pitcher);
|
|
69
109
|
compiler.hooks.done.tap('done', () => {
|
|
70
110
|
const finished = [];
|
|
71
111
|
for (const [id, handler] of moduleHandler) {
|
|
@@ -105,22 +145,22 @@ class ModuleReplacePlugin {
|
|
|
105
145
|
const handler = moduleHandler.get(id);
|
|
106
146
|
if (handler) {
|
|
107
147
|
handler.pending.set(compiler, true);
|
|
108
|
-
|
|
148
|
+
}
|
|
149
|
+
const pitcher = (wpModule.loaders || []).find(isPitcher);
|
|
150
|
+
if (pitcher) {
|
|
151
|
+
pitcher.options = {};
|
|
109
152
|
}
|
|
110
153
|
return;
|
|
111
154
|
}
|
|
112
155
|
if (moduleInfo.action === ModuleAction.REPLACE) {
|
|
113
156
|
if (!wpModule.loaders || wpModule.loaders[REPLACED] !== true) {
|
|
114
|
-
moduleInfo.loaders = wpModule.loaders;
|
|
115
|
-
wpModule.loaders = [
|
|
116
|
-
{
|
|
117
|
-
loader: stubLoader,
|
|
118
|
-
options: {
|
|
119
|
-
module: moduleInfo.replacedModule
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
];
|
|
123
157
|
wpModule.loaders[REPLACED] = true;
|
|
158
|
+
const pitcher = (wpModule.loaders || []).find(isPitcher);
|
|
159
|
+
if (pitcher) {
|
|
160
|
+
pitcher.options = {
|
|
161
|
+
replacedModule: moduleInfo.replacedModule
|
|
162
|
+
};
|
|
163
|
+
}
|
|
124
164
|
}
|
|
125
165
|
}
|
|
126
166
|
}
|
|
@@ -130,33 +170,16 @@ class ModuleReplacePlugin {
|
|
|
130
170
|
if (knownModules.has(id) || !id) {
|
|
131
171
|
return;
|
|
132
172
|
}
|
|
133
|
-
const
|
|
173
|
+
const { createData: { resourceResolveData } } = wpModule;
|
|
174
|
+
const replacedModule = findReplacedModule(this._options.modules, resourceResolveData.query);
|
|
134
175
|
if (replacedModule) {
|
|
135
176
|
knownModules.set(id, {
|
|
136
177
|
action: ModuleAction.REPLACE,
|
|
137
178
|
replacedModule,
|
|
138
|
-
compiler
|
|
179
|
+
compiler,
|
|
180
|
+
loaders: []
|
|
139
181
|
});
|
|
140
182
|
}
|
|
141
183
|
}
|
|
142
|
-
_getReplacedModule(wpModule) {
|
|
143
|
-
const { request, dependencies } = wpModule;
|
|
144
|
-
if (dependencies.length <= 0)
|
|
145
|
-
return null;
|
|
146
|
-
const { modules } = this._options;
|
|
147
|
-
for (let index = 0; index < modules.length; index++) {
|
|
148
|
-
const { test, module } = modules[index];
|
|
149
|
-
let shouldReplace = false;
|
|
150
|
-
if (isRegExp(test)) {
|
|
151
|
-
shouldReplace = test.test(request);
|
|
152
|
-
}
|
|
153
|
-
else if (isFunction(test)) {
|
|
154
|
-
shouldReplace = test(wpModule);
|
|
155
|
-
}
|
|
156
|
-
if (shouldReplace)
|
|
157
|
-
return module;
|
|
158
|
-
}
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
184
|
}
|
|
162
185
|
exports.default = ModuleReplacePlugin;
|
|
@@ -1,14 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
4
24
|
};
|
|
5
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
26
|
+
const loaderUtils = __importStar(require("loader-utils"));
|
|
27
|
+
const isSelfLoader = (l) => l.path !== __filename;
|
|
28
|
+
const genRequest = (loaderCtx, loaders) => {
|
|
29
|
+
const loaderStrings = [];
|
|
30
|
+
loaders.forEach(loader => {
|
|
31
|
+
const request = typeof loader === 'string' ? loader : loader.request;
|
|
32
|
+
// loader.request contains both the resolved loader path and its options
|
|
33
|
+
// query (e.g. ??ref-0)
|
|
34
|
+
loaderStrings.push(request);
|
|
35
|
+
});
|
|
36
|
+
return loaderUtils.stringifyRequest(loaderCtx, '!' +
|
|
37
|
+
[...loaderStrings, loaderCtx.resourcePath + loaderCtx.resourceQuery].join('!'));
|
|
38
|
+
};
|
|
39
|
+
module.exports = (code) => code;
|
|
40
|
+
module.exports.pitch = function () {
|
|
10
41
|
this.cacheable(false);
|
|
42
|
+
const { replacedModule } = loaderUtils.getOptions(this) || {};
|
|
43
|
+
let loaders = this.loaders;
|
|
44
|
+
// remove self
|
|
45
|
+
loaders = loaders.filter(isSelfLoader);
|
|
46
|
+
const request = replacedModule
|
|
47
|
+
? loaderUtils.stringifyRequest(this, replacedModule)
|
|
48
|
+
: genRequest(this, loaders);
|
|
11
49
|
return `
|
|
12
|
-
|
|
50
|
+
import mod from ${request};
|
|
51
|
+
export * from ${request}
|
|
52
|
+
export default mod;
|
|
13
53
|
`.trim();
|
|
14
54
|
};
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// ref: https://github.com/vercel/next.js/blob/canary/packages/next/build/webpack/plugins/nextjs-require-cache-hot-reloader.ts
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
-
};
|
|
6
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const
|
|
4
|
+
const webpack_1 = require("webpack");
|
|
8
5
|
const fs_1 = require("fs");
|
|
9
|
-
const constants_1 = require("@shuvi/shared/lib/constants");
|
|
10
6
|
function deleteCache(filePath) {
|
|
11
7
|
try {
|
|
12
|
-
delete require.cache[fs_1.realpathSync(filePath)];
|
|
8
|
+
delete require.cache[(0, fs_1.realpathSync)(filePath)];
|
|
13
9
|
}
|
|
14
10
|
catch (e) {
|
|
15
11
|
if (e.code !== 'ENOENT')
|
|
@@ -27,13 +23,18 @@ class RequireCacheHotReloader {
|
|
|
27
23
|
this.currentOutputPathsWebpack5 = new Set();
|
|
28
24
|
}
|
|
29
25
|
apply(compiler) {
|
|
26
|
+
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
|
|
27
|
+
compilation.hooks.additionalTreeRuntimeRequirements.tap(PLUGIN_NAME, (_chunk, runtimeRequirements) => {
|
|
28
|
+
// add this so that the server.js would be emitted after every compilation
|
|
29
|
+
// @ts-ignore webpack typing bug
|
|
30
|
+
runtimeRequirements.add(webpack_1.RuntimeGlobals.getFullHash);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
30
33
|
compiler.hooks.assetEmitted.tap(PLUGIN_NAME, (_file, { targetPath }) => {
|
|
31
34
|
this.currentOutputPathsWebpack5.add(targetPath);
|
|
32
35
|
deleteCache(targetPath);
|
|
33
36
|
});
|
|
34
37
|
compiler.hooks.afterEmit.tap(PLUGIN_NAME, compilation => {
|
|
35
|
-
const serverRuntimePath = path_1.default.join(compilation.outputOptions.path, constants_1.BUILD_SERVER_FILE_SERVER);
|
|
36
|
-
deleteCache(serverRuntimePath);
|
|
37
38
|
for (const outputPath of this.previousOutputPathsWebpack5) {
|
|
38
39
|
if (!this.currentOutputPathsWebpack5.has(outputPath)) {
|
|
39
40
|
deleteCache(outputPath);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class SupportTsExtensionResolverPlugin {
|
|
4
|
+
apply(resolver) {
|
|
5
|
+
const exts = ['.tsx', '.ts'];
|
|
6
|
+
const target = resolver.ensureHook('file');
|
|
7
|
+
exts.forEach(ext => {
|
|
8
|
+
resolver
|
|
9
|
+
.getHook('raw-file')
|
|
10
|
+
.tapAsync('SupportTsExtensionResolverPlugin', (request, resolveContext, callback) => {
|
|
11
|
+
const { relativePath } = request;
|
|
12
|
+
if (relativePath) {
|
|
13
|
+
if (/\.shuvi\/(runtime|app)/.test(relativePath)) {
|
|
14
|
+
const newRequest = Object.assign(Object.assign({}, request), { path: request.path + ext, relativePath: request.relativePath && request.relativePath + ext });
|
|
15
|
+
return resolver.doResolve(target, newRequest, ext, resolveContext, callback);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return callback();
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.default = SupportTsExtensionResolverPlugin;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { WebpackChain } from './config';
|
|
2
|
+
export interface IModuleItem {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
}
|
|
6
|
+
export interface IModule {
|
|
7
|
+
files: string[];
|
|
8
|
+
children: IModuleItem[];
|
|
9
|
+
}
|
|
10
|
+
export declare type IAssetMap = {
|
|
11
|
+
js: string[];
|
|
12
|
+
css?: string[];
|
|
13
|
+
} & {
|
|
14
|
+
[ext: string]: string[];
|
|
15
|
+
};
|
|
16
|
+
export interface IChunk {
|
|
17
|
+
file: string;
|
|
18
|
+
request: string;
|
|
19
|
+
}
|
|
20
|
+
export interface IManifest {
|
|
21
|
+
entries: {
|
|
22
|
+
[s: string]: IAssetMap;
|
|
23
|
+
};
|
|
24
|
+
bundles: {
|
|
25
|
+
[name: string]: string;
|
|
26
|
+
};
|
|
27
|
+
chunkRequest: {
|
|
28
|
+
[file: string]: string;
|
|
29
|
+
};
|
|
30
|
+
loadble: {
|
|
31
|
+
[s: string]: IModule;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export declare type ExternalsFunction = (data: {
|
|
35
|
+
context: string;
|
|
36
|
+
request: string;
|
|
37
|
+
}, callback: (err: Error | null, result: string | undefined) => void) => void;
|
|
38
|
+
export interface IWebpackHelpers {
|
|
39
|
+
addExternals: (chain: WebpackChain, externalsFn: ExternalsFunction) => void;
|
|
40
|
+
}
|