简体   繁体   English

webpack输出CSS文件

[英]webpack output css file

I'm trying to get webpack to output a css file for my less instead of inline styles. 我试图让webpack为我的少而不是内联样式输出一个css文件。 I've been using the extract-text-webpack-plugin . 我一直在使用extract-text-webpack-plugin

I've set it up based on the documentation and have had a look at similar questions on stackoverflow but can't figure out why my below webpack config file is not outputting a file or putting anything in index.html 我已经根据文档进行了设置,并查看了关于stackoverflow的类似问题,但无法弄清楚为什么我下面的webpack配置文件不输出文件或在index.html中放入任何内容

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

module.exports = {
    entry: [
      'webpack/hot/only-dev-server',
      './app/main.js'
    ],
    output: {
        path: __dirname + '/dist',
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
              test: /\.js?$/,
              loaders: ['react-hot', 'babel'],
              exclude: /node_modules/
            },
            {
              test: /\.js$/,
              exclude: /node_modules/,
              loader: 'babel-loader'
            },
            {
              test: /\.less$/,
              loader: ExtractTextPlugin.extract('style', 'css!less')
              //loader: "style!css!less"
            }
        ]
    },
    plugins: [
      new webpack.NoErrorsPlugin(),
      new ExtractTextPlugin('[name].css', {
            allChunks: true
        })
    ]

};

In current state, indeed, you need to embed your CSS manually. 实际上,在当前状态下,您需要手动嵌入CSS。

However, if you use HTML Webpack Plugin (and I strongly recommend it (-:), it will be automatically injected, as explained by documentation: 但是,如果您使用HTML Webpack插件 (强烈建议使用(-:),则会自动注入它,如文档所述):

If you have any css assets in webpack's output (for example, css extracted with the ExtractTextPlugin) then these will be included with <link> tags in the HTML head. 如果您在webpack的输出中有任何CSS资源(例如,用ExtractTextPlugin提取的CSS),则这些资源将包含在HTML头中的<link>标记中。

From what i understand use the loader as such: 据我了解使用这样的装载机:

{
   test: /\.less$/,
   loaders: ['style', 'css', 'less']
}

installing both css-loader, style-loader and less-loader via npm. 通过npm安装css-loader,style-loader和less-loader。 in your entry point, require the less files you want and they will be applied as styles to your index.html file. 在您的入口点,需要的文件更少,它们将作为样式应用于您的index.html文件。 No need to add any html tags. 无需添加任何html标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM