Webpack not copying css into dist

白昼怎懂夜的黑 提交于 2019-12-10 11:46:57

问题


I have the following css files:

<link rel="stylesheet" href="style/select.css">
<link rel="stylesheet" href="style/site.css">

and the following webpack config

var path   = require("path");
module.exports = {
  entry: {
    app: './src/index.js'
  },

  output: {
    path: path.resolve(__dirname + '/dist'),
    filename: '[name].js',
  },

  module: {
    rules: [
      {
        test: /\.(css|scss)$/,
        use: [
          'style-loader',
          'css-loader',
        ]
      },
      {
        test:    /\.html$/,
        exclude: /node_modules/,
        loader:  'file-loader?name=[name].[ext]',
      },
      {
        test:    /\.elm$/,
        exclude: [/elm-stuff/, /node_modules/],
        loader:  'elm-webpack-loader?verbose=true&warn=true',
          options: {debug: true, warn: true},
      },
      {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'url-loader?limit=10000&mimetype=application/font-woff',
      },
      {
        test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'file-loader',
      },
    ],

    noParse: /\.elm$/,
  },

  devServer: {
    inline: true,
    stats: { colors: true }
  },
  resolve: {
    mainFields: [ 'main'],
  }
};

when I use webpack-dev-server, the in memory file server seems to have the correct css files. However when I call

yarn build

it does not copy the css files in my dist folder and therefore fails to load the css files.


回答1:


Are you importing the css files in your modules? I think you need to use the ExtractTextWebpackPlugin which extract the css from the js bundle into a separate css file.

This will not work with webpack-dev-server though, so you need to disable the plugin in the configuration that you use for the dev server. There is an option that allows you to do that:

  new ExtractTextPlugin({
    filename: '[name].css',
    disable: true,
  }),



回答2:


Try this config to webpack 4, For loader:

use: [
          MiniCssExtractPlugin.loader,
          { loader: 'css-loader', options: {modules:true, importLoaders: 1 } },
          { loader: 'postcss-loader', options: {
              ident: 'postcss',
              plugins: () => [
                postcssPresetEnv(/* pluginOptions */)
              ]
            } }
        ]

For plugin section:

 new MiniCssExtractPlugin({
      filename: '[name].css' }),


来源:https://stackoverflow.com/questions/48041902/webpack-not-copying-css-into-dist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!