下面的代码似乎使用动态导入 https://webpack.js.org/guides/code-splitting/#dynamic-imports:
(function executeApplication(): void {
const loadDataButton: HTMLElement | null = document.getElementById("LoadDataButton");
if (loadDataButton !== null) {
loadDataButton.addEventListener("click", (): void => {
(async function handler(): Promise<void> {
console.log("Checkpoint 1");
const loadedValue: string = await loadDataOnDemand();
console.log(loadedValue);
})().catch((): void => { /* */ });
});
}
})();
async function loadDataOnDemand(): Promise<string> {
console.log("Checkpoint 2");
const MODULE: { default: string; DYNAMICALLY_LOADED_CONST_FROM_TS_MODULE: string; } =
await import("./DynamicLoadingTesting/TypeScriptModuleForDynamicLoading");
console.log("Checkpoint 3");
console.log(MODULE);
return MODULE.DYNAMICALLY_LOADED_CONST_FROM_TS_MODULE;
}
然而实际上TypeScriptModuleForDynamicLoading.ts
已捆绑到入口点:
FrontendLogicPreProcessingTesting:
asset FrontendLogicPreProcessingTesting.js 11.2 KiB [emitted] (name: FrontendLogicPreProcessingTesting)
./FrontendLogicPreProcessingTesting.ts 2.28 KiB [built] [code generated]
./DynamicLoadingTesting/TypeScriptModuleForDynamicLoading.ts 419 bytes [built] [code generated]
FrontendLogicPreProcessingTesting (webpack 5.4.0) compiled successfully in 710 ms
没有 webpack 错误,并且浏览器中的输出似乎是正确的:
我的网络包配置:
{
name: 'FrontendLogicPreProcessingTesting',
context: 'D:\\IntelliJ IDEA\\XXXXX\\ProjectBuildingCommonTest\\00-Source\\FrontendLogicPreProcessingTesting'
,
target: 'web',
entry: {
FrontendLogicPreProcessingTesting: 'D:/IntelliJ IDEA/XXXXX/ProjectBuildingCommonTest/00-Source/FrontendLogic
PreProcessingTesting/FrontendLogicPreProcessingTesting.ts'
},
output: {
path: 'D:\\IntelliJ IDEA\\XXXXX\\ProjectBuildingCommonTest\\01-DevelopmentBuild\\FrontendLogicPreProcessin
gTesting',
publicPath: './',
filename: '[name].js',
chunkFilename: 'loading_on_demand/partial__[id].js'
},
mode: 'development',
watch: true,
devtool: 'eval-cheap-source-map',
module: {
rules: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
]
},
resolve: {
extensions: [ '.mjs', '.js', '.ts' ],
alias: { vue: 'vue/dist/vue.common.js' }
},
plugins: [
DefinePlugin { definitions: [Object] },
ForkTsCheckerWebpackPlugin { options: [Object] },
],
optimization: { minimize: false, emitOnErrors: true }
}
我使用 webpack 5.x.x.,但在使用 4.x.x 时遇到同样的问题。
我的打字稿配置:
{
"compilerOptions": {
"target": "es2017",
"strict": true,
"module": "CommonJS",
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"allowJs": false,
"experimentalDecorators": true,
"baseUrl": "./",
"paths": {
},
"noUnusedParameters": true,
"noImplicitReturns": true
}
}
哪些动载条件没有得到满足?