使用extract-text-webpack-plugin

时间:2017-10-02 10:30:29

标签: css webpack less extracttextwebpackplugin

文件夹结构:

/resources
  /assets
    /css
      /config.less
    /js
      /index.js
      /components
        /Test
          /Presentation.jsx
          /styles.less

config.less我想存储一些变量。在Test/styles.less我需要包含config.less

@import 'config.less';

编译器返回此错误:

  

@import' config.less';
  ^
  无法解决' ./ config.less'

(注意:它会尝试加载./config.less而不是config.less

这包括作品:

@import '../../../css/config.less';

但这非常不方便。这是我的webpack配置:

module.exports = {
    output: {
        filename: 'bundle.js',
        publicPath: '/'
    },
    module: {
        loaders: [
            {
                test: /\.(js|jsx)$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: [
                        ['react'],
                        ['es2015'],
                        ['stage-0']
                    ],
                    plugins: [
                        ['transform-decorators-legacy'],
                        ['transform-runtime'],
                        ['transform-react-remove-prop-types'],
                        ['transform-react-constant-elements'],
                        ['transform-react-inline-elements']
                    ]
                }
            },
            {
                test: /\.less$/,
                loader: extractTextPlugin.extract({
                    publicPath: '../public',
                    use: [
                        {
                            loader: 'css-loader'
                        },
                        {
                            loader: 'less-loader',
                            options: {
                                plugins: [
                                    new lessPluginLists()
                                ]
                            }
                        }
                    ]
                }),
            }
        ],
    },
    plugins: [
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"production"'
        }),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"development"'
        }),
        new webpack.ProvidePlugin({
            'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
        }),
        new webpack.optimize.UglifyJsPlugin({
            beautify: false,
            comments: false,
            sourceMap: true,
            compress: {
                sequences: true,
                booleans: true,
                loops: true,
                unused: true,
                warnings: false,
                drop_console: true,
                unsafe: true
            }
        }),
        new extractTextPlugin({
            filename: 'bundle.css',
            allChunks: true
        }),
        new optimizeCssAssetsPlugin({
            assetNameRegExp: /\.css$/g,
            cssProcessor: require('cssnano'),
            cssProcessorOptions: {
                discardComments: {
                    removeAll: true
                }
            },
            canPrint: true
        })
    ],
    resolve: {
        modules: [
            './node_modules',
            path.resolve(__dirname, 'resources/assets/js'),
            path.resolve(__dirname, 'resources/assets/css')
        ],
        extensions: ['.less', '.css', '.js']
    }
};

似乎resolve.modules配置在这种情况下不起作用。 (我也尝试使用webpack的resolveLoader设置)

1 个答案:

答案 0 :(得分:3)

解决。 只需添加前缀~

@import '~config.less'