css - 如何使用 webpack 2 分离 css 文件中的 less 文件?

标签 css webpack less extract loader

我可以编译<style></style>内页面上的less内容通过 webpack2。但我无法将 less 文件编译成 CSS 文件。

webpack.config.js:

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

var ENV = process.env.NODE_ENV;
var port = '10101';

var commonAttr = ['common', 'markerFactory', 'sceneTransform', 'sparFactory', 'upload'];
var vendorArr = [];
for (var i = 0, l = commonAttr.length; i < l; i++) {
    vendorArr.push(path.resolve(__dirname + '/script/common/', commonAttr[i] + '.js'))
}

var config = {
    entry: {
        vendor: vendorArr,
        app: './script/app',
    },
    output: {
        path: path.resolve(__dirname, 'wds'),
        filename: '[name].bundle.js',
        publicPath: '/wds/'
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: 'babel-loader'
        },
        // // this option will compile the less to css, and append style tag to the page
        {
            test: /\.less$/,
            use: [
                "style-loader",
                "css-loader",
                "less-loader"
            ]
        },

        // I tried to split the css file into a indenpendent file, but it didn't work
        {
            test: /\.less$/,
            use: {
                loader: ExtractTextPlugin.extract({
                    fallbackLoader: "style-loader",
                    loader: "css-loader!less-loader",
                })
            }
        },
        {
            test: /\.html$/,
            use: "handlebars-loader?helperDirs[]=" + __dirname + "/script/helpers"
        }]
    },
    plugins: [
        new ExtractTextPlugin('[name].bundle.css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendor",
            filename: "vendor.js"
        })
    ],
    watchOptions: {
        aggregateTimeout: 300,
        poll: 1000
    },
    devServer: {
        compress: true,
        // hot: true,
        publicPath: '/wds/', //可访问的路径地址
        port: port
    }
}

if (ENV == 'development') {
    config.devtool = 'inline-source-map'; // 将模块单独编译 成 单个文件 浏览器可调试
}
else {
    config.devtool = 'eval-source-map'; // 将模块压缩一个文件一行 打包进bundle
}

var compiler = webpack(config);

module.exports = config;

但它给出了以下错误:

enter image description here

enter image description here

如果我不使用ExtractTextPluginrules use loader ,它可以编译为样式标签。这哪里出了问题?

最佳答案

抱歉,我已经修复了!

定义:

    var extractLESS = new ExtractTextPlugin('[name].bundle.css');

模块:

    rules:[{
            test: /\.less$/,
            use: extractLESS.extract([ 'css-loader', 'less-loader' ])
        }]

插件:

plugins: [
    extractLESS,
    new webpack.optimize.CommonsChunkPlugin({
        name: "vendor",
        filename: "vendor.js"
    })
],

关于css - 如何使用 webpack 2 分离 css 文件中的 less 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246408/

相关文章:

css - 如何将标题与内容分开

css - 将 CSS 显示属性重置为默认值

npm - 无法安装 WebPack CLI

javascript - 如何修复 Gulp 生成的文件太大

html - 如何使用 after 和 before 属性选择 div 的所有外侧

angular - 类型错误 : Cannot read property 'request' of undefined with angular-cli

less - Symfony 2 开发环境中的 Assets 路径。路径有效,但没有应用样式

css - 超出范围

css - LESS mixin - 不带引号的输出值

javascript - 复杂的网站,需要更新与 jQuery 共享类的按钮