【发布时间】:2016-09-29 01:19:32
【问题描述】:
我一直在尝试从ReactJS Tutorials 学习 React 和 Webpack。 我的问题是当我从所有文件的顶级目录中运行 webpack 时,通常需要 62392ms 左右。但是,在教程中,他需要 1394ms。我读过 exclude 应该有助于提高性能时间,但我已经排除了 node_modules。我尝试将我的 loader 切换为 babel,它使我的 webpack 时间减少到 13183ms,但这似乎仍然非常慢,尤其是当它只是转译一个文件(client.js)。有没有人知道为什么运行 webpack 命令这么慢,因为大多数其他 stackoverflow 答案似乎都指向包含/排除路径/目录,而我已经这样做了?
我的文件结构如下:
node_modules
src
-> js
->-> client.js
-> client.min.js
-> index.html
.gitignore
package.json
webpack.config.js
我的 webpack.config.js 看起来像:
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
module.exports = {
context: __dirname + "/src",
devtool: debug ? "inline-sourcemap" : null,
entry: "./js/client.js",
module: {
loaders: [
{
target: 'node',
test: /\.jsx?$/,
//include: [path.resolve(__dirname, "./src")],
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0'],
plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
}
}
]
},
output: {
path: __dirname + "/src/",
filename: "client.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
};
我的 package.json 看起来像:
{
"name": "module-loaders",
"version": "1.0.0",
"description": "Learning React and Webpack through LearnCode.academy",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dustinchang/React_Learning_LearnCode.Academy.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/dustinchang/React_Learning_LearnCode.Academy/issues"
},
"homepage": "https://github.com/dustinchang/React_Learning_LearnCode.Academy#readme",
"dependencies": {
"babel-core": "^6.9.1",
"babel-loader": "^6.2.0",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-react-html-attrs": "^2.0.0",
"babel-plugin-transform-class-properties": "^6.3.13",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"react": "^0.14.6",
"react-dom": "^0.14.6",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
}
}
我非常感谢任何建议。
【问题讨论】:
-
sourcemap -> 慢,babel -> 慢,UglifyJsPlugin -> 慢。尝试运行 webpack --json --progress --profile > stats.json 并将其加载到分析工具中以查看更多详细信息
-
你说教程比你项目快:他们之间有什么区别?如果你有比教程项目更多的文件和更多的依赖项,它可以解释差异。
-
唯一的依赖差异是我添加了 babel-core,因为我在某处读到尝试只使用 babel 而不是 babel-loader,这确实在一定程度上提高了性能,但仍然很慢,然后是 webpack-dev-服务器,在本教程中稍微深入一点。
-
开发版中
process.env.NODE_ENV的值是否设置正确? -
我不相信,教程中唯一一次这样做是在命令行中说
process.env.NODE_ENV=productionwebpack,但除此之外我们只是在使用 webpack,所以我没有'认为我不必这样做。
标签: reactjs webpack package.json