【问题标题】:VueLayers - Using webpack externals for internal filesVueLayers - 对内部文件使用 webpack 外部
【发布时间】:2020-10-28 17:35:00
【问题描述】:

我正在尝试以创建 3 个文件的方式配置 webpack。

app.js - 我的所有代码都捆绑在一起 chunk-vendors.js - 来自 node_modules 的代码被捆绑在一起,有一个例外 vuelayers.js - 用于地图,占用太多空间,而且由于它在单个组件中使用,理想情况下它应该与其他所有内容分开加载。

我正在尝试通过外部实现这一点,但我不确定这是正确的方法,因为我仍然想加载本地版本的 VueLayers,而不是通过 CDN。 I saw some code examples dynamically creating script tags on mounted event, but I would like those scripts to be loaded from node_modules.

我也尝试过这样配置webpack,但是当然不行,因为我没有足够的经验。

module.exports = {

configureWebpack: {

    output: {

        filename: 'app.js',

    },

    externals:{

        moment: 'moment',

        'vuelayers': require('vuelayers/lib/index.umd')

    },
}

【问题讨论】:

标签: vue.js webpack openlayers vue-cli-3 vuelayers


【解决方案1】:

类似的东西(未经测试):

module.exports = {
    //...
    optimization: {
        splitChunks: {
            cacheGroups: {
                chunkVendors: {
                    name: 'chunk-vendors',
                    chunks: 'all',
                    test(module, chunks) {
                        // `module.resource` contains the absolute path of the file on disk.
                        return module.resource.includes('node_modules') && !module.resource.includes('vuelayers');
                    }
                    priority: -10
                },
            }
        }
    }
}

【讨论】:

  • 我试过了,现在我把所有东西都捆绑在 app.js 中,根本没有块供应商
  • 试试去掉优先级,觉得没必要。
  • 你让我走上了正轨。我最终做的是将名称更改为“大型模块”,并将测试功能设置为仅包含来自 node_modules 的 vuelayers 和 ol。谢谢,问题解决了。
猜你喜欢
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2015-12-07
  • 1970-01-01
  • 2019-03-23
  • 2016-04-26
相关资源
最近更新 更多