使用webpack ExtractTextPlugin获取css输出

时间:2015-04-19 13:12:24

标签: webpack

我试图让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,所以可能会做一些noob错误

有什么想法吗?

4 个答案:

答案 0 :(得分:28)

ExtractTextPlugin需要添加到两个位置:在Loader中,以及作为插件。以下是从stylesheets documentation拉出的示例。

// 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")
    ]
}

答案 1 :(得分:2)

我修改了您的配置文件名以及如何将其包含在页面

   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: 'scripts/bundle.js',
    publicPath: '/scripts/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new ExtractTextPlugin('styles/styles.css', {
      publicPath: '/styles/',
      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')
    }]
  }
};

以下是html页面

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

答案 2 :(得分:2)

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

上述方法最终会再次发出CSS。为什么要把你的代码通过这样的往返?使用raw-loader并将主CSS文件添加到您的入口点。您丢失了css-loader执行的任何错误检查,但您的编译发生得更快。但是,如果您使用的是sass-loader之类的内容,您仍会收到所有错误检查。

答案 3 :(得分:1)

这是一个有效的webpack.config.js。我没有使用您所使用的相同目录名称,但我认为您可以看到差异并进行必要的更改。我还包括我当前的模块版本。

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

const config = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js',
    publicPath: 'build/'
  },
  module: {
    rules: [
      {
        use: 'babel-loader',
        test: /\.js$/
      },
      {
        loader: ExtractTextPlugin.extract({fallback: 'style-loader', use: 'css-loader'}),
        test: /\.css$/
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/,
        use: [
          {
            loader: 'url-loader',
            options: { limit: 40000 }
          },
          'image-webpack-loader?bypassOnDebug'
        ]
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin({filename: 'style.css',
      allChunks: true
    })
  ]
};

module.exports = config;

//和模块:

"devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^6.4.1",
    "babel-preset-env": "^1.3.3",
    "css-loader": "^0.28.0",
    "extract-text-webpack-plugin": "^2.0.0-beta.4",
    "file-loader": "^0.11.1",
    "image-webpack-loader": "^3.3.0",
    "style-loader": "^0.16.1",
    "url-loader": "^0.5.8",
    "webpack": "^2.2.0-rc.0"
  }