使用 webpack ExtractTextPlugin 获取 css 输出
Getting css output using webpack ExtractTextPlugin
我正在尝试 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 而只是在配置中执行此操作:
module: {
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
然后我将 css 正确应用到页面,但显然这不是来自 css 文件
这是我第一次尝试使用 webpack,所以可能犯了一些菜鸟错误
有什么想法吗?
ExtractTextPlugin
需要在两个位置添加:在加载程序中和作为插件。这是从 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")
]
}
我已经修改了您的配置文件名以及您如何将它们包含在页面中
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>
这是一个有效的 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"
}
使用css-loader
和style-loader
先解析你的CSS,然后把它变成样式节点,可以像代码一样导入到Webpack中。我不明白你为什么要在你的 JavaScript 和你的 CSS 之间建立这种人为的关系。
上述方法最后再次发出 CSS。为什么要让你的代码像这样往返?使用 raw-loader
并将您的主 CSS 文件添加到您的入口点。你失去了 css-loader
执行的任何错误检查,但你的编译速度更快。但是如果您使用 sass-loader
之类的东西,您仍然会得到所有的错误检查。
我正在尝试 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 而只是在配置中执行此操作:
module: {
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
然后我将 css 正确应用到页面,但显然这不是来自 css 文件
这是我第一次尝试使用 webpack,所以可能犯了一些菜鸟错误
有什么想法吗?
ExtractTextPlugin
需要在两个位置添加:在加载程序中和作为插件。这是从 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")
]
}
我已经修改了您的配置文件名以及您如何将它们包含在页面中
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>
这是一个有效的 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"
}
使用css-loader
和style-loader
先解析你的CSS,然后把它变成样式节点,可以像代码一样导入到Webpack中。我不明白你为什么要在你的 JavaScript 和你的 CSS 之间建立这种人为的关系。
上述方法最后再次发出 CSS。为什么要让你的代码像这样往返?使用 raw-loader
并将您的主 CSS 文件添加到您的入口点。你失去了 css-loader
执行的任何错误检查,但你的编译速度更快。但是如果您使用 sass-loader
之类的东西,您仍然会得到所有的错误检查。