webpack构建较少的文件输出一个css minify文件 [英] webpack build less files output one css minify file

查看:126
本文介绍了webpack构建较少的文件输出一个css minify文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

webpack是我需要将少量文件传递到一个缩小的CSS文件中的工具吗?

如果是这样,我不确定我在下面的代码中做错了什么?

有没有办法输出到不同的文件路径,现在我的js文件输出到'./ assets / javascripts / bundle /',我想要我的要输出到'./ assets / stylesheets / bundle /'的css文件,我该怎么做?

Is webpack the tool that I need to pass several less files into one minified CSS file?
if so, I'm not sure what I'm doing wrong in the code below?
Is there a way of outputting to different file paths, right now my js file outputs to './assets/javascripts/bundle/', I would like my css file to output to './assets/stylesheets/bundle/' , how would I do this?

更新

我做了一个测试,我可以将较少的文件构建到一个css文件,但仍然无法找到如何为输出文件夹设置多个路径,现在我有了注释掉js条目部分并更改输出路径......

Update
I did a test and I can build my less files to one css file but still can't find out how to set multiple paths for the output folder, now I have to comment out the js entry part and change output path...

webpack config

webpack config

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

module.exports = {
  entry: {
    'MediaIndex':['./assets/javascripts/Media/Index/Base.js'],
    // stylesheets
    'MediaIndexStyleSheet':['./assets/stylesheets/Media/Index/Base.js']
  },
  output: {
    path: './assets/javascripts/bundle/',
    filename: '[name].js'
  },
  resolve: {
    alias: {
      jquery: path.join(__dirname, "assets/javascripts/lib/jquery-1.11.1.min.js"),
      "jquery-ui": path.join(__dirname, "assets/javascripts/lib/jquery-ui.min.js"),
    }
  },
  plugins: [
    new webpack.ProvidePlugin({
      jQuery: "jquery",
      $: "jquery"
    }),
    new ExtractTextPlugin("[name].css")
  ],
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader")
      },
      {
        test: /\.less$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")
      }
    ]
  },
};

assets / stylesheets / Media / Index / Base.js

assets/stylesheets/Media/Index/Base.js

require('../../../Global/Config.less');
require('../../../Global/Fp.less');
require('../../../Global/Urb.less');
require('../../../Global/Ad1.less');
require('./Nav0.less');
require('./Index.less');
require('./Content.less');

Config.less

Config.less

@color: 'red';

Index.less

Index.less

body {
  background-color: @{color};
}


推荐答案

你快到了。就像您已经做过的那样,您可以使用 ExtractTextPlugin 将所有样式放入一个css文件中。要将js和css放入不同的目录,只需在 ExtractTextPlugin output.filename 中定义一个路径即可相对于 output.path 。例如:

You're almost there. Like you already did, you can get all your styling into one css file using ExtractTextPlugin. To put your js and css into a different directories, you can just define a path in ExtractTextPlugin or output.filename that is relative to your output.path. For example:

output: {
    path: './assets',
    filename: './javascripts/bundle/[name].js'
},
plugins: [
    new ExtractTextPlugin("./stylesheets/bundle/[name].css")
]

这篇关于webpack构建较少的文件输出一个css minify文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆