这是我当前的webpack.production.config.js
文件:
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: './src_react/app/app.jsx',
output: {
path: __dirname + '/build',
filename: '[name]-[hash].js'
},
module: {
loaders: [
{
test: [/\.jsx?$/, /\.js?$/],
exclude: /(node_modules|bower_components)/,
loader: 'babel'
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract(
'style', 'css', 'resolve-url', 'sass?sourceMap'
)
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css?modules!postcss')
},
{
test: /\.png$/,
loader: 'url-loader?limit=100000'
},
{
test: /\.jpg$/,
loader: 'file-loader'
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=image/svg+xml'
},
{
test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader?name=fonts/[name].[ext]'
}
]
},
resolve: {
extensions: ['', '.js', '.jsx', '.css', '.scss'],
alias: {
'ie': 'component-ie'
}
},
postcss: [
require('autoprefixer')
],
plugins: [
new HtmlWebpackPlugin({
template: __dirname + '/src_react/index.tmpl.html'
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin(),
new ExtractTextPlugin('[name]-[hash].css')
]
};
这不起作用。以下是我在开发过程中使用的webpack.config.js
文件似乎工作正常:
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src_react/app/app.jsx',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
devtool: 'source-map',
devServer: {
colors: true,
inline: true
},
module: {
loaders: [
{
test: [/\.jsx?$/, /\.js?$/],
exclude: /(node_modules|bower_components)/,
loader: 'babel'
},
{
test: /\.scss$/,
loaders: ['style', 'css', 'resolve-url', 'sass?sourceMap']
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.png$/,
loader: 'url-loader?limit=100000'
},
{
test: /\.jpg$/,
loader: 'file-loader'
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=image/svg+xml'
},
{
test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader?name=fonts/[name].[ext]'
}
]
},
resolve: {
extensions: ['', '.js', '.jsx', '.css', '.scss'],
alias: {
'ie': 'component-ie'
}
},
postcss: [
require('autoprefixer')
],
plugins: [
new HtmlWebpackPlugin({
template: __dirname + "/src_react/index.tmpl.html"
}),
new webpack.HotModuleReplacementPlugin()
],
watch: true
};
两个文件之间的主要区别在于生产者尝试使用ExtractTextPlugin
,但每当我使用命令webpack -p --config ./webpack.production.config.js
运行生产配置时,我会收到如下错误:
ERROR in ./~/css-loader!./src_react/app/roles/components/job-button/JobButton.scss
Module not found: Error: Cannot resolve 'file' or 'directory' ../../../../sass/variables in /Users/Aaron/Documents/src_react/app/roles/components/job-button
@ ./~/css-loader!./src_react/app/roles/components/job-button/JobButton.scss 3:10-99
为什么它会尝试在错误的文件夹中查找我的_variables.scss
文件?它可以很好地运行开发配置。
按照Juho的建议改变我使用提取文本插件到ExtractTextPlugin.extract('style','css!resolve-url!sass?sourceMap')
的方式后,我得到了这个错误:
ERROR in ./~/bootstrap/dist/css/bootstrap.css
Module build failed: ReferenceError: window is not defined