【问题标题】:Webpack 4 - Dynamic Import can't set proper locationWebpack 4 - 动态导入无法设置正确的位置
【发布时间】:2018-11-27 04:35:14
【问题描述】:

我正在使用 Webpack 4 的动态导入。当我构建我的入口文件(app.js)时,在 main.bundle.js 文件中,与之关联的模块无法将其(模块)位置设置为 'dist/ {另一个模块}'。它设置在根文件夹中。

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"]
}

【问题讨论】:

    标签: javascript webpack


    【解决方案1】:

    我找到了答案,我只需要设置publicPath: "/dist/"。 欲了解更多信息 - https://github.com/webpack/webpack/issues/7417

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多