zuiidea / antd-admin

An excellent front-end solution for enterprise applications built upon Ant Design and UmiJS
https://antd-admin.zuiidea.com
MIT License
9.64k stars 2.52k forks source link

npm run build 后文件过大1.4M左右 #65

Closed cheartx closed 7 years ago

cheartx commented 7 years ago

基于antd-admin 开发,可是发现build 后的index.js 有 1.4M左右,太大了,请帮忙看一下问题出在了哪里,如何优化呢?

wx20170208-201728 2x

package.json

{ "private": true, "entry": { "index": "./src/index.js" }, "dependencies": { "antd": "^2.5.0", "classnames": "^2.2.5", "dva": "^1.1.0", "js-cookie": "^2.1.3", "md5": "^2.2.1", "qs": "^6.3.0", "react": "^15.4.1", "react-cookie": "^1.0.4", "react-countup": "^1.3.0", "react-dom": "^15.4.1", "recharts": "^0.20.6" }, "devDependencies": { "atool-build": "^0.7.6", "axios": "^0.15.3", "babel-eslint": "^6.0.4", "babel-plugin-dev-expression": "^0.2.1", "babel-plugin-dva-hmr": "^0.1.0", "babel-plugin-import": "^1.0.1", "babel-plugin-transform-runtime": "^6.9.0", "babel-runtime": "^6.9.2", "dora": "0.3.x", "dora-plugin-proxy": "^0.7.0", "dora-plugin-webpack": "0.6.x", "dora-plugin-webpack-hmr": "^0.1.0", "eslint": "^2.13.1", "eslint-config-airbnb": "^9.0.1", "eslint-plugin-import": "^1.8.1", "eslint-plugin-jsx-a11y": "^1.4.2", "eslint-plugin-react": "^5.1.1", "glob": "^7.0.5", "mockjs": "^1.0.1-beta3", "redbox-react": "^1.2.10", "robe-ajax": "^1.0.0", "watchjs": "^0.0.0" }, "scripts": { "start": "dora --plugins \"proxy,webpack,webpack-hmr\"", "dev": "dora --plugins \"webpack,webpack-hmr\"", "lint": "eslint --fix --ext .js,.jsx src/components/layout", "build": "atool-build", "build-online": "SELF=online atool-build" }, "theme": "./src/theme.js" }

webpack.config.js

const fs = require('fs') const path = require('path') const webpack = require('atool-build/lib/webpack')

module.exports = function(webpackConfig, env) { webpackConfig.babel.plugins.push('transform-runtime') webpackConfig.babel.plugins.push(['import', { libraryName: 'antd', style: true, }]);

// Support hmr if (env === 'development') { webpackConfig.devtool = '#eval' webpackConfig.babel.plugins.push(['dva-hmr', { entries: [ './src/index.js', ], }]) } else { webpackConfig.babel.plugins.push('dev-expression') }

// Don't extract common.js and common.css webpackConfig.plugins = webpackConfig.plugins.filter(function(plugin) { return !(plugin instanceof webpack.optimize.CommonsChunkPlugin); });

if (process.env.NODE_ENV) { webpackConfig.plugins = [...webpackConfig.plugins, new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV), }), new webpack.optimize.UglifyJsPlugin(), new webpack.optimize.DedupePlugin(), ]; }

// Support CSS Modules // Parse all less files as css module. webpackConfig.module.loaders.forEach(function(loader, index) { if (typeof loader.test === 'function' && loader.test.toString().indexOf('\.less$') > -1) { loader.include = /node_modules/ loader.test = /.less$/ } if (loader.test.toString() === '/\.module\.less$/') { loader.exclude = /node_modules/ loader.test = /.less$/ } if (typeof loader.test === 'function' && loader.test.toString().indexOf('\.css$') > -1) { loader.include = /node_modules/ loader.test = /.css$/ } if (loader.test.toString() === '/\.module\.css$/') { loader.exclude = /node_modules/ loader.test = /.css$/ } })

return webpackConfig }

cheartx commented 7 years ago

原来readme中已经提到

生产环境中,已有数据接口,请将src/utils/index.js第四行 require('./mock.js')注释

又将项目中原有的router,service等文件删了,重新npm i 一下就好了,现在是700K,还得继续

hjtgzh commented 7 years ago

在package.json里把script对象的build改成下面试一下: "build": "set NODE_ENV=production&&atool-build",