reactjs - 使用 extract-text-webpack-plugin React 时出现窗口未定义错误

标签 reactjs webpack

我正在使用 webpack 构建我的 React 组件,并尝试使用 extract-text-webpack-plugin 将我的 css 与生成的 js 文件分开。但是,当我尝试构建该组件时,出现以下错误: 模块构建失败:ReferenceError:窗口未定义

我的 webpack.config.js 文件如下所示:

var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: {
    MainComponent: './src/main.js'
  },
  output: {
    libraryTarget: 'var',
    library: 'MainComponent',
    path: './build',
    filename: '[name].js'
  },
  module: {
    loaders: [{
      test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader!css-loader')
    }]
  },
  plugins: [
    new ExtractTextPlugin('styles.css')
  ]
}

最佳答案

您可能希望在 extract 函数中使用 style-loader 作为 before 参数。

这是 native 实现:

    ExtractTextPlugin.extract = function(before, loader, options) {
        if(typeof loader === "string") {
            return [
                ExtractTextPlugin.loader(mergeOptions({omit: before.split("!").length, extract: true, remove: true}, options)),
                before,
                loader
            ].join("!");
        } else {
            options = loader;
            loader = before;
            return [
                ExtractTextPlugin.loader(mergeOptions({remove: true}, options)),
                loader
            ].join("!");
        }
    };

所以基本上你需要做的是:

{
    test: /\.sass$/,
    exclude: /node_modules/,
    loader: ExtractTextPlugin.extract('style-loader', 'css!sass?indentedSyntax=true&sourceMap=true')
},

例如,如果您使用sass

关于reactjs - 使用 extract-text-webpack-plugin React 时出现窗口未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28223040/

相关文章:

javascript - 如何阻止调用堆栈过载?

javascript - FunctionComponent HOC 的正确类型

javascript - webpack --env.production 和 --mode ="production"有什么区别

karma-runner - Webpack Karma 无法解析本地导入

javascript - 在 ReactJs 中使用 props 从父级到子级的数据无法正常工作

reactjs - React - 具有相同路径的路由,但不同的参数会在更新时更改数据

reactjs - 将framer-motion与React结合使用;代码无法编译

javascript - 我如何使用 ES6 保持 React 组件的状态

node.js - Webpack 仍然包含明确排除的文件夹

javascript - Webpack console.log 输出?