使用Webpack ExtractTextPlugin获取CSS输出

网络包 Webpack

小宇宙猴子

2020-03-24

我试图让CSS要求使用ExtractTextPlugin在webpack中工作,但没有成功

我想要一个单独的CSS文件,而不是内联任何CSS。

这是我的webpack配置:

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

module.exports = {
  devtool: 'eval',
  entry: [
    'webpack-dev-server/client?http://localhost:3000',
    'webpack/hot/only-dev-server',
    './scripts/index'
  ],
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'bundle.js',
    publicPath: '/scripts/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new ExtractTextPlugin('styles/styles.css', {
      allChunks: true
    })
  ],
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [{
      test: /\.jsx?$/,
      loaders: ['react-hot', 'babel'],
      include: path.join(__dirname, 'scripts')
    },
    {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
    }]
  }
};

index.js:

import React from 'react';
import App from './App';

React.render(<App />, document.getElementById('root'));

App.js:

import React from 'react';

require('../styles/app.css');

export default class App extends React.Component {
  render() {
    return (
      <h1>Hello, world.</h1>
    );
  }
}

index.html:

<html>
  <head>
    <link rel="stylesheet" href="/styles/styles.css">
  </head>
  <body>
    <div id='root'></div>
  </body>
  <script src="/scripts/bundle.js"></script>
</html>

styles.css返回404

知道这里可能出什么问题了。如果我不使用ExtractTextPlugin,而只需在config中执行此操作:

module: {
        loaders: [
            { test: /\.css$/, loader: "style-loader!css-loader" }
        ]
    }

然后我正确地将css应用于页面,但是显然这不是来自css文件

这是我第一次尝试使用webpack,因此可能会出现一些菜鸟错误

有任何想法吗?

第3460篇《使用Webpack ExtractTextPlugin获取CSS输出》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
小胖Gil 2020.03.24

ExtractTextPlugin需要在两个位置添加:在Loader中,以及作为插件。这是从样式表文档中提取的示例

// webpack.config.js
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
    // The standard entry point and output config
    entry: {
        posts: "./posts",
        post: "./post",
        about: "./about"
    },
    output: {
        filename: "[name].js",
        chunkFilename: "[id].js"
    },
    module: {
        loaders: [
            // Extract css files
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract("style-loader", "css-loader")
            },
            // Optionally extract less files
            // or any other compile-to-css language
            {
                test: /\.less$/,
                loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")
            }
            // You could also use other loaders the same way. I. e. the autoprefixer-loader
        ]
    },
    // Use the plugin to specify the resulting filename (and add needed behavior to the compiler)
    plugins: [
        new ExtractTextPlugin("[name].css")
    ]
}
樱小胖Mandy 2020.03.24

一起使用css-loaderstyle-loader一起解析CSS,然后将其转换为样式节点,可以像代码一样将其导入Webpack。我不明白您为什么要在JavaScript和CSS之间建立这种人为的关系。

最后,以上方法再次发出CSS。为什么要像这样往返进行代码?使用raw-loader主CSS文件并将其添加到入口点。您会丢失css-loader执行的所有错误检查,但是编译会快得多。但是,如果您使用sass-loader,则仍然会得到所有错误检查。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android