@taro-hmr-compile/plugin-webpack 0.0.1-alpha.6 → 0.0.1-alpha.7
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { LoaderDefinitionFunction } from 'webpack';
|
|
2
2
|
export declare const injectAppHmrRuntime: (source: string, debugNamespaces?: string) => string;
|
|
3
|
-
export declare const transformPageConfig: (source: string, sourceFileName?: string) => string
|
|
3
|
+
export declare const transformPageConfig: (source: string, sourceFileName?: string) => string;
|
|
4
4
|
interface LoaderOptions {
|
|
5
5
|
miniType: 'PAGE' | 'ENTRY';
|
|
6
6
|
debugNamespaces?: string;
|
|
@@ -45,8 +45,9 @@ const t = __importStar(require("@babel/types"));
|
|
|
45
45
|
const webpack_1 = __importDefault(require("webpack"));
|
|
46
46
|
const CREATE_PAGE_CONFIG = 'createPageConfig';
|
|
47
47
|
const WITH_HMR_PAGE_CONFIG = 'withHmrPageConfig';
|
|
48
|
-
const
|
|
48
|
+
const TARO_RUNTIME_REQUEST = '@tarojs/runtime';
|
|
49
49
|
const LOG_REQUEST = require.resolve('../log');
|
|
50
|
+
const CORE_RUNTIME_REQUEST = require.resolve('@taro-hmr-compile/core/runtime');
|
|
50
51
|
const injectAppHmrRuntime = (source, debugNamespaces = '') => {
|
|
51
52
|
const ast = parser.parse(source, { sourceType: 'module' });
|
|
52
53
|
const buildHmrCode = template_1.default.statements(`
|
|
@@ -74,7 +75,7 @@ const injectAppHmrRuntime = (source, debugNamespaces = '') => {
|
|
|
74
75
|
(0, traverse_1.default)(ast, {
|
|
75
76
|
Program(path) {
|
|
76
77
|
const body = path.get('body');
|
|
77
|
-
const hasInjected = body.some(item => item.isImportDeclaration() && item.node.source
|
|
78
|
+
const hasInjected = body.some(item => item.isImportDeclaration() && t.isStringLiteral(item.node.source, { value: CORE_RUNTIME_REQUEST }));
|
|
78
79
|
if (hasInjected)
|
|
79
80
|
return;
|
|
80
81
|
const nodes = buildHmrCode();
|
|
@@ -90,54 +91,61 @@ const injectAppHmrRuntime = (source, debugNamespaces = '') => {
|
|
|
90
91
|
exports.injectAppHmrRuntime = injectAppHmrRuntime;
|
|
91
92
|
const transformPageConfig = (source, sourceFileName) => {
|
|
92
93
|
const ast = parser.parse(source, { sourceType: 'module', plugins: ['typescript', 'jsx'] });
|
|
93
|
-
|
|
94
|
+
const hasImportWithHmrPageConfig = (programPath) => {
|
|
95
|
+
return programPath.node.body.some(node => {
|
|
96
|
+
if (!t.isImportDeclaration(node))
|
|
97
|
+
return false;
|
|
98
|
+
if (!t.isStringLiteral(node.source, { value: CORE_RUNTIME_REQUEST }))
|
|
99
|
+
return false;
|
|
100
|
+
return node.specifiers.some(specifier => {
|
|
101
|
+
return t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported, { name: WITH_HMR_PAGE_CONFIG });
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
};
|
|
94
105
|
(0, traverse_1.default)(ast, {
|
|
95
106
|
Program(programPath) {
|
|
107
|
+
// 判断是否存在 createPageConfig 引用
|
|
96
108
|
const createPageConfigBinding = programPath.scope.getBinding(CREATE_PAGE_CONFIG);
|
|
97
109
|
if (!createPageConfigBinding || !createPageConfigBinding.path.isImportSpecifier())
|
|
98
110
|
return;
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
// 判断 createPageConfig 引用是否来自 @tarojs/runtime
|
|
112
|
+
const { parentPath: createPageConfigImportDeclaration } = createPageConfigBinding.path;
|
|
113
|
+
if (!t.isImportDeclaration(createPageConfigImportDeclaration.node))
|
|
114
|
+
return;
|
|
115
|
+
if (!t.isStringLiteral(createPageConfigImportDeclaration.node.source, { value: TARO_RUNTIME_REQUEST }))
|
|
101
116
|
return;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
117
|
+
// 判断是否已经注入了 withHmrPageConfig
|
|
118
|
+
if (hasImportWithHmrPageConfig(programPath))
|
|
119
|
+
return;
|
|
120
|
+
// 判断是否存在 withHmrPageConfig 引用
|
|
121
|
+
const hasWithHmrPageConfigBinding = programPath.scope.hasBinding(WITH_HMR_PAGE_CONFIG);
|
|
122
|
+
// 生成 withHmrPageConfig 的别名
|
|
123
|
+
const wrapperIdentifier = hasWithHmrPageConfigBinding ? programPath.scope.generateUidIdentifier(WITH_HMR_PAGE_CONFIG) : t.identifier(WITH_HMR_PAGE_CONFIG);
|
|
124
|
+
// 生成 runtime import
|
|
125
|
+
const runtimeImport = (() => {
|
|
126
|
+
if (!hasWithHmrPageConfigBinding)
|
|
127
|
+
return template_1.default.statement(`import { ${WITH_HMR_PAGE_CONFIG} } from ${JSON.stringify(CORE_RUNTIME_REQUEST)};`)();
|
|
128
|
+
return template_1.default.statement(`import { ${WITH_HMR_PAGE_CONFIG} as %%WRAPPER%% } from ${JSON.stringify(CORE_RUNTIME_REQUEST)};`)({ WRAPPER: wrapperIdentifier });
|
|
129
|
+
})();
|
|
105
130
|
programPath.traverse({
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const init = variablePath.node.init;
|
|
110
|
-
if (!t.isCallExpression(init) || !t.isIdentifier(init.callee, { name: CREATE_PAGE_CONFIG }))
|
|
111
|
-
return;
|
|
112
|
-
if (init.arguments.length < 2 || t.isSpreadElement(init.arguments[1]))
|
|
131
|
+
CallExpression(CallExpressionPath) {
|
|
132
|
+
const { node: CallExpressionNode } = CallExpressionPath;
|
|
133
|
+
if (!t.isIdentifier(CallExpressionNode.callee, { name: CREATE_PAGE_CONFIG }))
|
|
113
134
|
return;
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
135
|
+
const [_, pageName] = CallExpressionNode.arguments;
|
|
136
|
+
const wrappedPageConfig = template_1.default.expression(`%%WRAPPER%%(%%PAGE_CONFIG%%, %%PAGE_NAME%%)`)({
|
|
137
|
+
WRAPPER: wrapperIdentifier,
|
|
138
|
+
PAGE_CONFIG: CallExpressionNode,
|
|
139
|
+
PAGE_NAME: pageName,
|
|
140
|
+
});
|
|
141
|
+
if (!hasImportWithHmrPageConfig(programPath))
|
|
142
|
+
programPath.unshiftContainer('body', runtimeImport);
|
|
143
|
+
CallExpressionPath.replaceWith(wrappedPageConfig);
|
|
144
|
+
CallExpressionPath.skip();
|
|
145
|
+
}
|
|
124
146
|
});
|
|
125
|
-
const runtimeImport = wrapperIdentifier.name === WITH_HMR_PAGE_CONFIG
|
|
126
|
-
? template_1.default.statement(`import { withHmrPageConfig } from ${JSON.stringify(CORE_RUNTIME_REQUEST)}`)()
|
|
127
|
-
: template_1.default.statement(`import { withHmrPageConfig as %%WRAPPER%% } from ${JSON.stringify(CORE_RUNTIME_REQUEST)}`)({ WRAPPER: wrapperIdentifier });
|
|
128
|
-
if (!t.isImportDeclaration(runtimeImport))
|
|
129
|
-
throw new TypeError('Expected runtime import template to create an import declaration');
|
|
130
|
-
const hasNamespaceSpecifier = (existingImport === null || existingImport === void 0 ? void 0 : existingImport.isImportDeclaration()) && existingImport.node.specifiers.some(specifier => t.isImportNamespaceSpecifier(specifier));
|
|
131
|
-
if ((existingImport === null || existingImport === void 0 ? void 0 : existingImport.isImportDeclaration()) && !hasNamespaceSpecifier) {
|
|
132
|
-
existingImport.pushContainer('specifiers', runtimeImport.specifiers[0]);
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
programPath.unshiftContainer('body', runtimeImport);
|
|
136
|
-
}
|
|
137
147
|
},
|
|
138
148
|
});
|
|
139
|
-
if (!transformed)
|
|
140
|
-
return;
|
|
141
149
|
return (0, generator_1.default)(ast, { sourceMaps: true, sourceFileName }, source).code;
|
|
142
150
|
};
|
|
143
151
|
exports.transformPageConfig = transformPageConfig;
|
|
@@ -148,7 +156,7 @@ const hmrCompilerLoader = function (source) {
|
|
|
148
156
|
return (0, exports.injectAppHmrRuntime)(source, debugNamespaces);
|
|
149
157
|
if (miniType === 'PAGE')
|
|
150
158
|
return (_a = (0, exports.transformPageConfig)(source, this.resourcePath)) !== null && _a !== void 0 ? _a : source;
|
|
151
|
-
|
|
159
|
+
return source;
|
|
152
160
|
};
|
|
153
161
|
exports.default = hmrCompilerLoader;
|
|
154
162
|
//# sourceMappingURL=hmr-compiler-loader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hmr-compiler-loader.js","sourceRoot":"","sources":["../../src/compile/hmr-compiler-loader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iEAAwC;AACxC,sDAAuC;AACvC,+DAAsC;AACtC,+
|
|
1
|
+
{"version":3,"file":"hmr-compiler-loader.js","sourceRoot":"","sources":["../../src/compile/hmr-compiler-loader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iEAAwC;AACxC,sDAAuC;AACvC,+DAAsC;AACtC,+DAAkD;AAClD,gDAAiC;AAEjC,sDAA6B;AAE7B,MAAM,kBAAkB,GAAG,kBAAkB,CAAA;AAE7C,MAAM,oBAAoB,GAAG,mBAAmB,CAAA;AAEhD,MAAM,oBAAoB,GAAG,iBAAiB,CAAA;AAE9C,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;AAE7C,MAAM,oBAAoB,GAAG,OAAO,CAAC,OAAO,CAAC,gCAAgC,CAAC,CAAA;AAEvE,MAAM,mBAAmB,GAAG,CAAC,MAAc,EAAE,eAAe,GAAG,EAAE,EAAE,EAAE;IAGxE,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,EAAC,UAAU,EAAE,QAAQ,EAAC,CAAC,CAAA;IAExD,MAAM,YAAY,GAAG,kBAAQ,CAAC,UAAU,CAAC;8EACiC,IAAI,CAAC,SAAS,CAAC,oBAAoB,CAAC;8DACpD,IAAI,CAAC,SAAS,CAAC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AAE,iBAAiB;wBAC1B,WAAW,EAAE,kBAAkB;wBAC/B,SAAS,EAAE,QAAQ;qBACtB,CAAC,CAAA;oBAEF,IAAI,CAAC,0BAA0B,CAAC,WAAW,CAAC;wBAAE,WAAW,CAAC,gBAAgB,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;oBAEjG,kBAAkB,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAA;oBACjD,kBAAkB,CAAC,IAAI,EAAE,CAAA;gBAC7B,CAAC;aACJ,CAAC,CAAA;QAEN,CAAC;KACJ,CAAC,CAAA;IAEF,OAAO,IAAA,mBAAS,EAAC,GAAG,EAAE,EAAC,UAAU,EAAE,IAAI,EAAE,cAAc,EAAC,EAAE,MAAM,CAAC,CAAC,IAAI,CAAA;AAC1E,CAAC,CAAA;AA/DY,QAAA,mBAAmB,uBA+D/B;AAOD,MAAM,iBAAiB,GAA4C,UAAU,MAAM;;IAC/E,MAAM,EAAC,QAAQ,EAAE,eAAe,EAAC,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;IACrD,IAAI,QAAQ,KAAK,OAAO;QAAE,OAAO,IAAA,2BAAmB,EAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC7E,IAAI,QAAQ,KAAK,MAAM;QAAE,OAAO,MAAA,IAAA,2BAAmB,EAAC,MAAM,EAAE,IAAI,CAAC,YAAY,CAAC,mCAAI,MAAM,CAAA;IACxF,OAAO,MAAM,CAAA;AACjB,CAAC,CAAA;AAED,kBAAe,iBAAiB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taro-hmr-compile/plugin-webpack",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@babel/template": "^7.29.7",
|
|
45
45
|
"@babel/traverse": "^7.29.8",
|
|
46
46
|
"@babel/types": "^7.29.8",
|
|
47
|
-
"@taro-hmr-compile/core": "0.0.1-alpha.
|
|
47
|
+
"@taro-hmr-compile/core": "0.0.1-alpha.7",
|
|
48
48
|
"debug": "^4.4.3",
|
|
49
49
|
"tapable": "^2.3.3",
|
|
50
50
|
"webpack-sources": "^3.5.1"
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import generator from '@babel/generator'
|
|
2
2
|
import * as parser from '@babel/parser'
|
|
3
3
|
import template from '@babel/template'
|
|
4
|
-
import traverse from '@babel/traverse'
|
|
4
|
+
import traverse, {NodePath} from '@babel/traverse'
|
|
5
5
|
import * as t from '@babel/types'
|
|
6
6
|
import type {LoaderDefinitionFunction} from 'webpack'
|
|
7
7
|
import webpack from 'webpack'
|
|
8
8
|
|
|
9
9
|
const CREATE_PAGE_CONFIG = 'createPageConfig'
|
|
10
|
+
|
|
10
11
|
const WITH_HMR_PAGE_CONFIG = 'withHmrPageConfig'
|
|
11
|
-
|
|
12
|
+
|
|
13
|
+
const TARO_RUNTIME_REQUEST = '@tarojs/runtime'
|
|
14
|
+
|
|
12
15
|
const LOG_REQUEST = require.resolve('../log')
|
|
13
16
|
|
|
17
|
+
const CORE_RUNTIME_REQUEST = require.resolve('@taro-hmr-compile/core/runtime')
|
|
18
|
+
|
|
14
19
|
export const injectAppHmrRuntime = (source: string, debugNamespaces = '') => {
|
|
20
|
+
|
|
21
|
+
|
|
15
22
|
const ast = parser.parse(source, {sourceType: 'module'})
|
|
16
23
|
|
|
17
24
|
const buildHmrCode = template.statements(`
|
|
@@ -40,7 +47,7 @@ export const injectAppHmrRuntime = (source: string, debugNamespaces = '') => {
|
|
|
40
47
|
traverse(ast, {
|
|
41
48
|
Program(path) {
|
|
42
49
|
const body = path.get('body')
|
|
43
|
-
const hasInjected = body.some(item => item.isImportDeclaration() && item.node.source
|
|
50
|
+
const hasInjected = body.some(item => item.isImportDeclaration() && t.isStringLiteral(item.node.source, {value: CORE_RUNTIME_REQUEST}))
|
|
44
51
|
if (hasInjected) return
|
|
45
52
|
|
|
46
53
|
const nodes = buildHmrCode()
|
|
@@ -55,54 +62,67 @@ export const injectAppHmrRuntime = (source: string, debugNamespaces = '') => {
|
|
|
55
62
|
}
|
|
56
63
|
|
|
57
64
|
export const transformPageConfig = (source: string, sourceFileName?: string) => {
|
|
65
|
+
|
|
58
66
|
const ast = parser.parse(source, {sourceType: 'module', plugins: ['typescript', 'jsx']})
|
|
59
|
-
|
|
67
|
+
|
|
68
|
+
const hasImportWithHmrPageConfig = (programPath: NodePath<t.Program>) => {
|
|
69
|
+
return programPath.node.body.some(node => {
|
|
70
|
+
if (!t.isImportDeclaration(node)) return false
|
|
71
|
+
if (!t.isStringLiteral(node.source, {value: CORE_RUNTIME_REQUEST})) return false
|
|
72
|
+
return node.specifiers.some(specifier => {
|
|
73
|
+
return t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported, {name: WITH_HMR_PAGE_CONFIG})
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
}
|
|
60
77
|
|
|
61
78
|
traverse(ast, {
|
|
62
79
|
Program(programPath) {
|
|
80
|
+
|
|
81
|
+
// 判断是否存在 createPageConfig 引用
|
|
63
82
|
const createPageConfigBinding = programPath.scope.getBinding(CREATE_PAGE_CONFIG)
|
|
64
83
|
if (!createPageConfigBinding || !createPageConfigBinding.path.isImportSpecifier()) return
|
|
65
|
-
const importDeclaration = createPageConfigBinding.path.parentPath
|
|
66
|
-
if (!importDeclaration.isImportDeclaration() || importDeclaration.node.source.value !== '@tarojs/runtime') return
|
|
67
84
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
85
|
+
// 判断 createPageConfig 引用是否来自 @tarojs/runtime
|
|
86
|
+
const {parentPath: createPageConfigImportDeclaration} = createPageConfigBinding.path
|
|
87
|
+
if (!t.isImportDeclaration(createPageConfigImportDeclaration.node)) return
|
|
88
|
+
if (!t.isStringLiteral(createPageConfigImportDeclaration.node.source, {value: TARO_RUNTIME_REQUEST})) return
|
|
71
89
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
transformed = true
|
|
81
|
-
variablePath.stop()
|
|
82
|
-
},
|
|
83
|
-
})
|
|
90
|
+
// 判断是否已经注入了 withHmrPageConfig
|
|
91
|
+
if (hasImportWithHmrPageConfig(programPath)) return
|
|
92
|
+
|
|
93
|
+
// 判断是否存在 withHmrPageConfig 引用
|
|
94
|
+
const hasWithHmrPageConfigBinding = programPath.scope.hasBinding(WITH_HMR_PAGE_CONFIG)
|
|
95
|
+
|
|
96
|
+
// 生成 withHmrPageConfig 的别名
|
|
97
|
+
const wrapperIdentifier = hasWithHmrPageConfigBinding ? programPath.scope.generateUidIdentifier(WITH_HMR_PAGE_CONFIG) : t.identifier(WITH_HMR_PAGE_CONFIG)
|
|
84
98
|
|
|
85
|
-
|
|
99
|
+
// 生成 runtime import
|
|
100
|
+
const runtimeImport = (() => {
|
|
101
|
+
if (!hasWithHmrPageConfigBinding) return template.statement(`import { ${WITH_HMR_PAGE_CONFIG} } from ${JSON.stringify(CORE_RUNTIME_REQUEST)};`)()
|
|
102
|
+
return template.statement(`import { ${WITH_HMR_PAGE_CONFIG} as %%WRAPPER%% } from ${JSON.stringify(CORE_RUNTIME_REQUEST)};`)({WRAPPER: wrapperIdentifier})
|
|
103
|
+
})()
|
|
86
104
|
|
|
87
|
-
|
|
88
|
-
|
|
105
|
+
programPath.traverse({
|
|
106
|
+
CallExpression(CallExpressionPath) {
|
|
107
|
+
const {node: CallExpressionNode} = CallExpressionPath
|
|
108
|
+
if (!t.isIdentifier(CallExpressionNode.callee, {name: CREATE_PAGE_CONFIG})) return
|
|
109
|
+
const [_, pageName] = CallExpressionNode.arguments
|
|
110
|
+
const wrappedPageConfig = template.expression(`%%WRAPPER%%(%%PAGE_CONFIG%%, %%PAGE_NAME%%)`)({
|
|
111
|
+
WRAPPER: wrapperIdentifier,
|
|
112
|
+
PAGE_CONFIG: CallExpressionNode,
|
|
113
|
+
PAGE_NAME: pageName,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
if (!hasImportWithHmrPageConfig(programPath)) programPath.unshiftContainer('body', runtimeImport)
|
|
117
|
+
|
|
118
|
+
CallExpressionPath.replaceWith(wrappedPageConfig)
|
|
119
|
+
CallExpressionPath.skip()
|
|
120
|
+
}
|
|
89
121
|
})
|
|
90
|
-
|
|
91
|
-
? template.statement(`import { withHmrPageConfig } from ${JSON.stringify(CORE_RUNTIME_REQUEST)}`)()
|
|
92
|
-
: template.statement(`import { withHmrPageConfig as %%WRAPPER%% } from ${JSON.stringify(CORE_RUNTIME_REQUEST)}`)({WRAPPER: wrapperIdentifier})
|
|
93
|
-
if (!t.isImportDeclaration(runtimeImport)) throw new TypeError('Expected runtime import template to create an import declaration')
|
|
94
|
-
const hasNamespaceSpecifier = existingImport?.isImportDeclaration() && existingImport.node.specifiers.some(specifier => t.isImportNamespaceSpecifier(specifier))
|
|
95
|
-
|
|
96
|
-
if (existingImport?.isImportDeclaration() && !hasNamespaceSpecifier) {
|
|
97
|
-
existingImport.pushContainer('specifiers', runtimeImport.specifiers[0])
|
|
98
|
-
} else {
|
|
99
|
-
programPath.unshiftContainer('body', runtimeImport)
|
|
100
|
-
}
|
|
122
|
+
|
|
101
123
|
},
|
|
102
124
|
})
|
|
103
125
|
|
|
104
|
-
if (!transformed) return
|
|
105
|
-
|
|
106
126
|
return generator(ast, {sourceMaps: true, sourceFileName}, source).code
|
|
107
127
|
}
|
|
108
128
|
|
|
@@ -113,11 +133,9 @@ interface LoaderOptions {
|
|
|
113
133
|
|
|
114
134
|
const hmrCompilerLoader: LoaderDefinitionFunction<LoaderOptions> = function (source) {
|
|
115
135
|
const {miniType, debugNamespaces} = this.getOptions()
|
|
116
|
-
|
|
117
136
|
if (miniType === 'ENTRY') return injectAppHmrRuntime(source, debugNamespaces)
|
|
118
137
|
if (miniType === 'PAGE') return transformPageConfig(source, this.resourcePath) ?? source
|
|
119
|
-
|
|
120
|
-
throw new Error(`unsupported HMR entry miniType: ${miniType}`)
|
|
138
|
+
return source
|
|
121
139
|
}
|
|
122
140
|
|
|
123
141
|
export default hmrCompilerLoader
|