【问题标题】:Can not export webpack.config when modules:falsemodules:false 时无法导出 webpack.config
【发布时间】:2017-11-05 12:57:59
【问题描述】:

在我的 index.js 中,我正在使用 webpack-dev-middleware/webpack-hot-middleware,我需要 webpack.config 并将其用于编译器。

index.js

  const webpack = require('webpack')
  const webpackConfig = require('../../webpack.config.js')
  const compiler = webpack(webpackConfig)
  const webpackDevMiddleware = require('webpack-dev-middleware')

  app.use(webpackDevMiddleware(compiler, {
    publicPath: webpackConfig.output.publicPath,
    hot: true,
    noInfo: true,
    stats: {
      colors: true
    }
  }))

  app.use(require('webpack-hot-middleware')(compiler))

我尝试通过 requiremodule.exports 使用 Common.js 导出我的 webpack.config,但出现错误

TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

webpack.config

'use strict'

const path = require('path')
const webpack = require('webpack')
const publicPath = path.resolve(__dirname, './src/client')
const buildPath = path.resolve(__dirname, './src')


process.noDeprecation = true

module.exports = {
  devtool: 'source-maps',
  performance: {
    hints: false
  },
  context: publicPath,
  entry: {
    bundle: [
      'react-hot-loader/patch',
      'webpack-hot-middleware/client?reload=false&noInfo=true',
      'script-loader!jquery/dist/jquery.min.js',
      'script-loader!tether/dist/js/tether.min.js',
      'script-loader!bootstrap/dist/js/bootstrap.min.js',
      './app.js'
    ]
  },
  output: {
    path: path.join(buildPath, 'dist'),
    filename: '[name].js',
    publicPath: '/'
  },
  resolve: {
    extensions: ['.js', '.jsx'],
    alias: {
      CountdownForm: path.resolve(__dirname, 'src/client/scenes/countdown/components/CountdownForm.jsx'),
      Countdown: path.resolve(__dirname, 'src/client/scenes/countdown/index.jsx'),
     ..
    }
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules|dist|build/,
        loader: 'babel-loader',
        options: {
          plugins: [
            [
              'babel-plugin-react-css-modules',
              {
                context: publicPath,
                filetypes: {
                  '.scss': 'postcss-scss'
                }
              }
            ]
          ]
        }
      },
      {
        test: /\.local\.(css|scss)$/,
        use: [
          'style-loader',
          'css-loader?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]',
          'postcss-loader',
          'sass-loader',
          {
            loader: 'sass-resources-loader',
            options: {
              resources: path.resolve(__dirname, './src/client/styles/global/sass-resources.scss')
            }
          }
        ]
      },
      {
        test: /^((?!\.local).)+\.(css|scss)$/,
        use: [
          'style-loader',
          'css-loader',
          'postcss-loader',
          'sass-loader'
        ]
      }
    ]
  },
  plugins: [
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      jquery: 'jquery'
    }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('development')
      }
    })
  ]
}

如果我使用 ES6(我正在使用 babel,这通常有效)在顶部使用 import 语句而不是 require 和 export default 而不是 module.exports 我得到这个错误

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration misses the property 'entry'.

所有这一切都是因为我的 .babelrc 中的 modules:false 如果我删除 Common.js 的方式有效,但我需要这个。如何使用模块将 webpack.config 导出到编译器:false

{
  "presets": [
    "react",
    ["es2015", { "modules": false }],
    "stage-0"
  ],
  "plugins": [
    "react-hot-loader/babel",
    "transform-runtime"
  ]
}

【问题讨论】:

    标签: reactjs module webpack webpack-dev-middleware


    【解决方案1】:

    transform-runtime 添加import 并导致import 与您的module.exports 混合。

    简单的解决方法是将 module.exports 替换为 es6 export

    module.exports = { ...webpackconfig } 
    

    变成

    export default { ...webpackconfig }
    

    并更新您的 index.js 以使用默认导出

    const webpackConfig = require('../../webpack.config.js').default
    

    您可以找到有关这些问题的更多信息

    https://github.com/webpack/webpack/issues/4039 https://github.com/webpack/webpack/issues/3917

    【讨论】:

    • 谢谢rzr。有用。我们可以详细说明一下,看看我是否明白。所以,我可以在我使用 module.exports 的地方使用 common.js,但我不能将它与同一个文件中的导入混合?如果我使用导入,我必须使用导出默认值并在最后使用 .default 吗?我可以将 require 与 module.exports 或 export default 一起使用,但对于 module.exports 我不需要在最后使用 .default 吗?最后,它们不能在一个文件中混合,但可以跨项目混合?
    猜你喜欢
    • 2018-07-31
    • 2017-07-08
    • 2020-10-15
    • 2017-11-07
    • 2020-03-03
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多