让你的 webpack.config.js 与此类似...
const path = require('path');
const postStylus = require('poststylus');
const webpack = require('webpack');
module.exports = {
context: __dirname,
entry: [
'./source/ClientApp.jsx',
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080/',
'webpack/hot/only-dev-server',
],
devtool: 'cheap-eval-source-map',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js',
publicPath: '/public/',
hotUpdateChunkFilename: 'hot/hot-update.js',
hotUpdateMainFilename: 'hot/hot-update.json'
},
devServer: {
hot: true,
publicPath: '/public/',
historyApiFallback: true,
},
watch: true,
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
// new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.LoaderOptionsPlugin({
options: {
stylus: {
preferPathResolver: 'webpack',
},
}
}),
],
resolve: {
extensions: ['.js', '.jsx', '.json', '.styl'],
modules: [
path.resolve('./source'),
path.resolve('./node_modules'),
],
},
stats: {
colors: true,
reasons: true,
chunks: true,
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
loader: 'eslint-loader',
exclude: /node_modules/,
},
{
test: /\.jsx?$/,
loader: 'babel-loader',
},
{
test: /\.styl$/,
use: [
'style-loader',
'css-loader',
{
loader: 'stylus-loader',
options: {
use: [
postStylus(['autoprefixer']),
],
modules: true,
},
},
],
exclude: /node_modules/,
},
],
},
};
如果您需要参考,这是我的整个 package.json...
{
"scripts": {
"lint": "eslint **/*.{js,jsx} --quiet",
"clean": "rm -r public/bundle.js",
"build": "rm -r public/bundle.js && webpack",
"dev": "webpack-dev-server --watch --progress --colors",
"watch": "webpack --watch"
},
"dependencies": {
"autoprefixer": "^7.1.2",
"autoprefixer-stylus": "^0.14.0",
"axios": "^0.16.2",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.1.1",
"babel-plugin-dynamic-import-node": "^1.0.2",
"babel-plugin-dynamic-import-webpack": "^1.0.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"babel-preset-airbnb": "^2.4.0",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"concurrently": "^3.5.0",
"css-loader": "^0.28.4",
"d3": "^4.10.0",
"enzyme": "^2.9.1",
"eslint": "3.19.0",
"eslint-config-airbnb": "^15.0.2",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-config-react": "^1.1.7",
"eslint-loader": "^1.9.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.0.3",
"eslint-plugin-react": "^7.1.0",
"express": "^4.15.3",
"extract-text-webpack-plugin": "^3.0.0",
"firebase": "^4.2.0",
"firebase-tools": "^3.9.2",
"json-loader": "^0.5.7",
"node-sass": "^4.5.3",
"nodemon": "^1.11.0",
"poststylus": "^0.2.3",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-date-range": "^0.9.4",
"react-dom": "^15.6.1",
"react-hot-loader": "3.0.0-beta.6",
"react-router-dom": "^4.1.2",
"react-test-renderer": "^15.6.1",
"resolve-url-loader": "^2.1.0",
"sass-loader": "^6.0.6",
"sort-package-json": "^1.7.0",
"style-loader": "^0.18.2",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.1",
"webpack": "2.6.1",
"webpack-combine-loaders": "^2.0.3",
"webpack-dev-middleware": "^1.11.0",
"webpack-dev-server": "^2.6.1",
"webpack-hot-middleware": "^2.18.2"
},
}