javascript - Webpack-merge 不访问公共(public)配置变量

标签 javascript webpack production-environment

据我了解,webpack-merge 帮助我们将 webpack.config 文件分解为更易于管理的 block ,添加与环境相关的配置。

While we will separate the production and development specific bits out, note that we'll still maintain a "common" configuration to keep things DRY. In order to merge these configurations together, we'll use a utility called webpack-merge. With the "common" configuration in place, we won't have to duplicate code within the environment-specific configurations.
- Webpack - production

我的 webpack.prod.js 中的代码如下所示:

const merge = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'production',
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
            'file-loader'
        ],
        exclude: [
          path.resolve(__dirname, "node_modules") //NEED TO ACCCESS PATH VARIABLE HERE
        ]
      }
  ]
}

在我的 webpack.common.js 中,是我认为 webpack.prod.js 可以访问的变量 path 。我假设它不会,因为我遇到了错误:

ReferenceError: path is not defined

问题

如何访问通用配置?我是否误解了 webpack-merge 的概念?

最佳答案

webpack-merge 将获取两个 js 对象并使用 lodash mergeWith 合并它们。 。所以基本上它返回一个包含两个对象属性的对象。

它无法为您提供path变量或任何其他变量。您需要将其隐式导入到 webpack.prod.js 文件中。

关于javascript - Webpack-merge 不访问公共(public)配置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50290924/

相关文章:

javascript - 指定一个我可能不会在 Vue.js 的动态组件中使用的值会降低性能吗?

javascript - Vue.js : Failed to mount component: template or render function not defined

javascript - 滚动上多个固定标题

javascript - 如何在弹出窗口中显示消息

Javascript Instagram API 在浏览器中显示答案,但不在 JS 中显示答案

node.js - 在 Node.js 中动态导入 webpack

webpack - 如何使用 Next.js、Ant Design、Less 和 TypeScript 配置 Storybook.js Webpack

javascript - Meteor.js 是否足够稳定以用于生产?

flutter - 如何防止生产 flutter 应用程序在模拟器中运行?

node.js - NodeJS、NPM 和 package.json 依赖项