使用extract-text-webpack-plugin时,窗口未定义错误

时间:2015-01-29 19:31:46

标签: reactjs webpack

我正在使用webpack来构建我的反应组件,我正在尝试使用extract-text-webpack-plugin将我的css与生成的js文件分开。但是,当我尝试构建组件时,我收到以下错误: Module build failed: ReferenceError: window is not defined

我的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')
  ]
}

4 个答案:

答案 0 :(得分:59)

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

这是本机实现:

    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

答案 1 :(得分:42)

没有看到原因的解释,所以我在这里发布了这个答案。

来自https://github.com/webpack/extract-text-webpack-plugin#api

  

ExtractTextPlugin.extract([notExtractLoader], loader, [options])   从现有的加载器创建一个提取加载器。

     

notExtractLoader(可选)在未提取css时应使用的加载器(即当allChunks:false时,在>附加块中)

     

loader应该用于将资源转换为css导出模块的加载程序。

     

options

     

publicPath会覆盖此加载程序的publicPath设置。

#extract方法应该会收到一个输出css的加载程序。发生的事情是它收到了一个style-loader输出 javascript代码,用于注入网页。此代码会尝试访问window

您不应将带有style的加载器字符串传递给#extract。但是......如果设置allChunks=false,则不会为非初始块构建CSS文件。因此,它需要知道用于注入页面的加载器。

提示:Webpack是一个真正需要深入理解的工具,或者你可能遇到很多奇怪的问题。

答案 2 :(得分:19)

Webpack 2

如果您使用的是Webpack 2,则此变体有效:

    rules: [{
        test: /\.css$/,
        exclude: '/node_modules/',
        use: ExtractTextPlugin.extract({
            fallback: [{
                loader: 'style-loader',
            }],
            use: [{
                loader: 'css-loader',
                options: {
                    modules: true,
                    localIdentName: '[name]__[local]--[hash:base64:5]',
                },
            }, {
                loader: 'postcss-loader',
            }],
        }),
    }]

新的提取方法不再需要三个参数,并且在从V1移动到V2时被列为重大变化。

https://webpack.js.org/guides/migrating/#extracttextwebpackplugin-breaking-change

答案 3 :(得分:12)

我找到了解决问题的方法:

您必须将每个加载器作为单独的参数传递给ExtractTextPlugin.extract('style-loader!css-loader')

,而不是将加载器相互交织(ExtractTextWebpackPlugin.extract('style-loader', 'css-loader'))。
相关问题