Webpack 2生成空CSS文件

时间:2016-12-07 21:36:29

标签: webpack webpack-dev-server webpack-2

在阅读numerous SO questions(以及在线研究)后,我仍然无法使用Webpack 2生成CSS文件(其中包含CSS)(v2。 1.0-beta.27),ExtractText插件(​​v2.0.0-beta.4),sass-loader(v3.2.0),css-loader(v0.26.1)和style-loader(v0.13.1)。

目前,Webpack 2已成功完成并按预期生成我的JS文件,但我的app.css文件为空(Chrome会为该文件抛出404),但根据Chrome中的“来源”面板生成了正确的位置。这让我相信css-loader或其他加载器存在问题。

我当前的 webpack.config.js 文件如下所示:

const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const autoprefixer = require('autoprefixer');
const path = require('path');
const resolve = path.resolve;

const rootDir = resolve(__dirname);
const dist = resolve(rootDir, 'coolAppName/secured_assets');
const assetIndex = resolve(rootDir, 'src/app');
const vendorIndex = resolve(rootDir, 'webpack.vendor.js');
const styleIndex = resolve(rootDir, 'webpack.style.js');
const fontPath = '../static/fonts/';

module.exports = {

  watchOptions: {
    poll: 3000,
    aggregateTimeout: 1000,
  },

  devServer: {
    port: 8080,
    publicPath: 'http://localhost:8080/secured_assets/',
  },

  devtool: 'eval-source-map',

  entry: {
    app: assetIndex,
    style: styleIndex,
    vendor: vendorIndex,
  },

  output: {
    path: dist,
    filename: '[name].js',
    publicPath: 'http://localhost:8080/secured_assets/'
  },

  module: {
    loaders: [

      // CSS files
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallbackLoader: 'style-loader',
          loader: [
            {
              loader: 'css-loader',
            }
          ]
        }),
      },

      // SCSS files
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract({
          fallbackLoader: 'style-loader',
          loader: [
            {
              loader: 'css-loader'
            },
            {
              loader: 'sass-loader'
            }
          ]
        })
      },

      {
        test: /\.(gif|png)$/,
        loaders: [
          {
            loader: 'url-loader',
            query: {
              mimetype: 'image/png'
            },
          },
        ],
      },

      {
        test: /\.js/,
        loaders: 'babel-loader',
        include: [
          assetIndex,
        ],
        exclude: /(node_modules|bower_components)/,
        query: {
          cacheDirectory: true,
        },
      },      

      {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loaders: 'url-loader',
        query: {
          limit: 10000,
          mimetype: 'application/font-woff',
          name: `${fontPath}[name].[ext]`,
        },
      },

      {
        test: /\.(ttf|eot|svg|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loaders: 'file-loader',
        query: {
          name: `${fontPath}[name].[ext]`,
        },
      },

      {
        test: /\.json$/,
        loaders: 'json-loader',
      },

      {
        test: /\.html$/,
        loaders: 'html-loader',
      },

    ],
  },

  plugins: [
    new ExtractTextPlugin({
      filename: '/css/app.css',
      allChunks: true,
    })
  ],

};

我的 webpack.style.js 文件如下所示:

// STYLE entrypoint
// vendor (CSS)
require('./coolAppName/static/fonts/custom-font-icons/styles.css');
require('font-awesome/css/font-awesome.css');
require('simple-line-icons/css/simple-line-icons.css');
require('lato-font');
require('./node_modules/bootswatch/yeti/bootstrap.min.css');
require('./node_modules/angularjs-toaster/toaster.css');
require('./node_modules/angular-busy2/dist/angular-busy.css');
require('./node_modules/perfect-scrollbar/dist/css/perfect-scrollbar.css');
require('./node_modules/angular-bootstrap-lightbox/dist/angular-bootstrap-lightbox.css');
require('./node_modules/angularjs-slider/dist/rzslider.min.css');
require('./node_modules/ui-select/dist/select.css');
require('./coolAppName/static/fonts/noto/noto.css');
// SCSS
require('./src/app/css/templates.scss');

我的文件结构看起来像这样(这是正确的):

file structure

在尝试迁移到Webpack 2之前,一切都按预期工作。唯一改变的是Webpack 2升级,所有文件路径和我使用的加载器是相同的(除了升级版本)。

这是重要的部分与Webpack 1相似(我在上面的例子中删除了postcss-loader)

{
    test: /\.css$/,
    loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!postcss-loader'),
},
{
    test: /\.scss$/,
    loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!sass-loader?sourceMap!postcss-loader'),
},

最终我想在/secured_assets/css/app.css中创建一个CSS文件,其中包含requirewebpack.style.js的所有样式。此时,我没有收到任何错误,app.css没有内容,我在“网络”面板中看到了404(虽然它显示在Chrome中的来源中)。

更新

当我只是运行webpack --config ./webpack.dev.js时,它会正确创建我的app.css。当我运行webpack-dev-server --config ./webpack.dev.js时它没有。所以看起来是一个可能的webpack-dev-server问题......

2 个答案:

答案 0 :(得分:0)

您应该使用查询参数importLoaders(doc:importing and chained loaders):

// SCSS files
{
  test: /\.scss$/,
  loader: ExtractTextPlugin.extract({
    fallbackLoader: 'style-loader',
    loader: [
      {
        loader: 'css-loader',
        query: {
          importLoaders: 1
        }
      },
      {
        loader: 'sass-loader'
      }
    ]
  })
}

答案 1 :(得分:0)

问题是在我的/数组中以ExtractTextPlugin filename为前缀的斜杠(plugins)。

在:

new ExtractTextPlugin({
  filename: '/css/app.css',
  allChunks: true,
})

后:

new ExtractTextPlugin({
  filename: 'css/app.css',
  allChunks: true,
})
进行此更改后,

webpack-dev-server能够正确找到此文件。