I am trying to get css requires to work in webpack using the ExtractTextPlugin but with no success
I want a separate css file rather than inlining any css.
ExtractTextPlugin
needs to be added in two spots: in the Loader, and as a Plugin. Here's the example pulled from the 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")
]
}
Using css-loader
and style-loader
together first parse your CSS, then turn it into style nodes, which can be imported in Webpack just like code. I don't understand why you'd want this artificial relationship built between your JavaScript and your CSS.
The above approach emits CSS again in the end. Why put your code through a round trip like that? Use raw-loader and add your main CSS file to your entry points. You lose any error-checking that css-loader
performs, but your compilation happens much faster. But if you're using something like sass-loader
, you'll still get all the error checking.
Here's a webpack.config.js that works. I don't use the same directory names you do, but I think you can see the differences and make the needed changes. I'm also including my current module versions.
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;
// and modules:
"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"
}
I have modified your config filenames and how you include them in page
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')
}]
}
};
Following is the html page
<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>