我正在使用Webpack 4的动态导入。构建入口文件(app.js)时,在main.bundle.js文件中,与之关联的模块无法将其(模块)位置设置为'dist / {another_module}”。它设置在根文件夹中。
webpack.config.js
const path = require("path");
module.exports = {
entry: {
main: "./src/app.js"
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js",
chunkFilename: "[name].bundle.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
src / app.js
function getScript() {
return import(/* webpackChunkName: "script" */ "./script").then(res => {
console.log(res.default);
});
}
getScript();
src / script.js
export default function script() {
console.log("script file is loaded")
}
.babelrc
{
"plugins": ["@babel/plugin-syntax-dynamic-import"]
}
答案 0 :(得分:2)
我找到了答案,我只需要设置publicPath: "/dist/"
。
有关更多信息-https://github.com/webpack/webpack/issues/7417