【问题标题】:Webpack - multiple entrypoints, require bundles between themWebpack - 多个入口点,它们之间需要捆绑
【发布时间】:2020-05-04 14:33:13
【问题描述】:

我有一个带有多个入口点的 webpack 配置,看起来或多或少像这样:

const config = {
    entry: {
        'components/index': './src/components',
        'helpers/index': './src/helpers',
    },
}

module.exports = config;

这会生成两个捆绑文件:components/index.jshelpers/index.js。这很好,但问题是components/index.js 包含来自helpers 的整个代码...这是因为组件正在导入辅助函数。

例子:

  • src/helpers 导出名为 foo 的函数。
  • src/components 从助手导入 foo 函数。

现在,在 components/index.js 包中,我看到声明了 foo 函数。有没有办法告诉 webpack 它应该使用 require() 函数并导入这个 foo 函数?

所以component/index.js 的输出看起来像这样:

var foo = require('../helpers/index.js')
/* rest of the code */

【问题讨论】:

    标签: javascript webpack bundler rollup


    【解决方案1】:

    我切换到 rollup.js,默认情况下它似乎是这样工作的,没有任何额外的配置:

    // rollup.config.js
    
    export default {
        input: {
            'components/index': './src/components',
            'helpers/index': './src/helpers',
        },
        output: {
            dir: 'dist',
            format: 'esm',
        },
    };
    

    【讨论】:

      猜你喜欢
      • 2018-11-22
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多