Skip to content

Commit d1bfeb5

Browse files
committed
add post css
1 parent 2f44eef commit d1bfeb5

File tree

14 files changed

+257
-90
lines changed

14 files changed

+257
-90
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
node_modules/*
2-
build/*
2+
dist/*

bin/compile.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const debug = require('debug')('app:bin:compile');
2+
const fs = require('fs-extra');
3+
const compiler = require('../build/compiler');
4+
const env = require('../build/base-config/environment');
5+
const paths = require('../build/base-config/path');
6+
const webpackConfig = require('../build/webpack-config');
7+
8+
const startCompile = () => {
9+
debug(`Start compile with env '${process.env.NODE_ENV}'`);
10+
return Promise.resolve()
11+
.then(() => compiler(webpackConfig))
12+
.then((stats) => {
13+
if (stats.hasWarnings() && env.__PROD__) { // eslint-disable-line
14+
throw new Error('Production not allow warnig, exit .');
15+
}
16+
debug('Copying static assets to dist folder.');
17+
fs.copySync(paths.public(), paths.dist());
18+
return stats;
19+
})
20+
.then((stats) => {
21+
debug(stats.toString(env.config.stats));
22+
debug('Success compile !!');
23+
})
24+
.catch((error) => {
25+
debug('Compile with Error.', error);
26+
process.exit();
27+
});
28+
};
29+
30+
// =====
31+
// go!
32+
// =====
33+
34+
startCompile();

build/base-config/configs.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const cssnano = require('cssnano');
2+
3+
module.exports = {
4+
development: {
5+
performance: {
6+
hints: false,
7+
maxAssetSize: 2000000
8+
}
9+
},
10+
production: {
11+
performance: {
12+
hints: 'error',
13+
maxEntrypointSize: 1500000
14+
}
15+
},
16+
defaults: {
17+
stats: {
18+
chunkModules: false,
19+
colors: true,
20+
chunks: false
21+
},
22+
performance: {
23+
hints: 'warning'
24+
},
25+
vendors: [
26+
'react',
27+
'react-redux',
28+
'react-router',
29+
'redux',
30+
'react-dom'
31+
],
32+
postcss: [
33+
cssnano({
34+
autoprefixer: {
35+
add: true,
36+
remove: true,
37+
browsers: ['last 2 versions']
38+
},
39+
discardComments: {
40+
removeAll: true
41+
},
42+
discardUnused: false,
43+
mergeIdents: false,
44+
reduceIdents: false,
45+
safe: true,
46+
sourcemap: true
47+
})
48+
]
49+
}
50+
};

build/base-config/environment.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
21
const NODE_ENV = process.env.NODE_ENV || 'development';
2+
const envConfigs = require('./configs');
33

44
module.exports = {
5-
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
6-
__DEV__: NODE_ENV === 'development',
7-
__PROD__: NODE_ENV === 'production',
8-
__TEST__: NODE_ENV === 'test',
5+
/**
6+
* why? https://fb.me/react-minification ,http://stackoverflow.com/questions/30030031
7+
*/
8+
'process.env': {
9+
NODE_ENV: JSON.stringify(NODE_ENV)
10+
},
11+
env: NODE_ENV,
12+
isDev: NODE_ENV === 'development',
13+
isProd: NODE_ENV === 'production',
14+
isTest: NODE_ENV === 'test',
15+
config: Object.assign(envConfigs.defaults, envConfigs[NODE_ENV] || {})
916
};

build/base-config/path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const path = require('path');
33
const ROOT_PATH = path.resolve(__dirname, '../..');
44

55
function base() {
6-
const args = [ROOT_PATH].concat([].slice.call(arguments));
7-
return path.resolve.apply(path, args);
6+
const args = [ROOT_PATH].concat([].slice.call(arguments)); // eslint-disable-line
7+
return path.resolve.apply(path, args); // eslint-disable-line
88
}
99

1010
module.exports = {

build/compiler.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const webpack = require('webpack');
2+
const debug = require('debug')('app:build:compiler');
3+
const chalk = require('chalk');
4+
5+
const webpackCompiler = (webpackConfig) => {
6+
const compile = webpack(webpackConfig);
7+
return new Promise((resolve, reject) => {
8+
compile.run((err, stats) => {
9+
if (err) {
10+
debug('Webpack compiler with a fatal error.', err);
11+
return reject(err);
12+
}
13+
const jsonStats = stats.toJson();
14+
if (stats.hasErrors()) {
15+
debug('Webpack compiler with some errors.');
16+
debug(chalk.red(jsonStats.errors.join('\n')));
17+
return reject(new Error('Webpack compiler with some errors.'));
18+
} else if (stats.hasWarnings()) {
19+
debug('Webpack compiler with some warnings.');
20+
debug(chalk.yellow(jsonStats.warnings.join('\n')));
21+
} else {
22+
debug('Webpack compiler no Errors or no Warnings');
23+
}
24+
return resolve(stats);
25+
});
26+
});
27+
};
28+
29+
module.exports = webpackCompiler;

build/server.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
11
const express = require('express');
2-
const debug = require('debug')('app:server');
3-
const webpack = require('webpack');
4-
2+
const debug = require('debug')('app:build:server');
53
const env = require('./base-config/environment');
64
const paths = require('./base-config/path');
75

8-
const webpackConfig = require('./webpack-config');
96
const app = express();
107

118
// This rewrites all routes requests to the root /index.html file
12-
app.use(require('connect-history-api-fallback')())
9+
app.use(require('connect-history-api-fallback')());
1310

1411
// Apply gzip compression
15-
app.use(require('compression')())
12+
app.use(require('compression')());
1613

1714
// ------------------------------------
1815
// Apply Webpack HMR Middleware
1916
// ------------------------------------
20-
if (env.__DEV__) {
21-
const compiler = webpack(webpackConfig)
22-
debug('Enable webpack dev and HMR middleware(开启开发环境插件 webpack-dev 和 HRM 中间件)')
17+
if (env.isDev) {
18+
const webpack = require('webpack');
19+
const webpackConfig = require('./webpack-config');
20+
const compiler = webpack(webpackConfig);
21+
debug('Enable webpack dev and HMR middleware(开启开发环境插件 webpack-dev 和 HRM 中间件)');
2322
app.use(require('webpack-dev-middleware')(compiler, {
2423
publicPath: webpackConfig.output.publicPath,
2524
contentBase: paths.src(),
2625
lazy: false,
27-
stats: {
28-
chunkModules: false,
29-
colors: true,
30-
chunks: false
31-
}
32-
}))
33-
app.use(require('webpack-hot-middleware')(compiler))
26+
stats: env.config.stats
27+
}));
28+
app.use(require('webpack-hot-middleware')(compiler));
3429

35-
app.use(express.static(paths.public()))
30+
app.use(express.static(paths.public()));
3631
} else {
37-
debug(
38-
'Server is being run outside of live development mode ! ' +
39-
)
40-
app.use(express.static(paths.dist()))
32+
debug('Server is being run outside of live development mode !');
33+
app.use(express.static(paths.dist()));
4134
}
4235

4336
module.exports = app;

build/webpack-config/base.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
const debug = require('debug')('app:webpack:base');
22
const env = require('../base-config/environment');
33

4-
const __DEV__ = env.__DEV__;
4+
const isDev = env.isDev;
5+
const envConfig = env.config;
56

67
const pkg = require('../../package.json');
78

89
module.exports = (paths) => {
910
const App = [paths.src('main.js')];
10-
const Vendors = [
11-
'react',
12-
'react-redux',
13-
'react-router',
14-
'redux',
15-
'react-dom'
16-
].filter((dep) => {
11+
const Vendors = envConfig.vendors.filter((dep) => {
1712
if (pkg.dependencies[dep]) return true;
1813
return debug(
1914
`Package "${dep}" was not found as an npm dependency in package.json; ` +
2015
`it won't be included in the webpack vendor bundle.
2116
Consider removing it from "Vendors" in this file`
2217
);
2318
});
24-
if (__DEV__) {
19+
if (isDev) {
2520
App.unshift('webpack-hot-middleware/client');
2621
}
2722
return {
2823
context: paths.root(),
29-
entry_vendors: Vendors,
30-
entry_app: App,
31-
devtool: __DEV__ ? 'eval' : 'cheap-source-map',
24+
entry: {
25+
app: App,
26+
vendors: Vendors
27+
},
28+
devtool: isDev ? 'eval' : 'cheap-source-map',
3229
output: {
3330
filename: '[name].[hash:8].js',
3431
path: paths.dist(),
3532
publicPath: '/'
3633
},
37-
performance: {
38-
hints: false,
39-
maxAssetSize: 2000000,
40-
}
34+
performance: env.config.performance
4135
};
4236
};

build/webpack-config/index.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ const loaders = require('./loaders')();
77
const plugins = require('./plugins')(paths);
88

99
debug('Creating configuration.(创建配置)');
10-
module.exports = {
11-
entry: {
12-
app: base.entry_app,
13-
vendors: base.entry_vendors
14-
},
15-
output: base.output,
10+
module.exports = Object.assign({
11+
plugins,
1612
resolve: {
1713
alias
1814
},
19-
devtool: base.devtool,
2015
module: {
2116
loaders
22-
},
23-
performance: base.performance,
24-
devServer: base.devServer,
25-
plugins
26-
};
17+
}
18+
}, base);

build/webpack-config/loaders.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const debug = require('debug')('app:webpack:loaders');
22
const env = require('../base-config/environment');
33
const ExtractTextPlugin = require('extract-text-webpack-plugin');
44

5-
const __DEV__ = env.__DEV__; // eslint-disable-line
5+
const isDev = env.isDev;
66
module.exports = () => {
77
const loaders = [{
88
test: /\.(js|jsx)$/,
@@ -13,10 +13,10 @@ module.exports = () => {
1313
loader: 'json-loader'
1414
}, {
1515
test: /\.css$/,
16-
loaders: ['style-loader', 'css-loader']
16+
loaders: ['style-loader', 'css-loader', 'postcss-loader']
1717
}, {
1818
test: /\.less$/,
19-
loaders: ['style-loader', 'css-loader', 'less-loader']
19+
loaders: ['style-loader', 'css-loader', 'postcss-loader', 'less-loader']
2020
}, {
2121
test: /\.woff(\?.*)?$/,
2222
loader: 'url-loader?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/font-woff'
@@ -39,12 +39,12 @@ module.exports = () => {
3939
test: /\.(png|jpg)$/,
4040
loader: 'url-loader?limit=8192'
4141
}];
42-
if (!__DEV__) {
42+
if (!isDev) {
4343
debug('Apply ExtractTextPlugin to CSS loaders.(非开发环境应用ExtractTextPluginLoaders到css loaders)');
4444
loaders.filter(loader => loader.loaders && loader.loaders.find(name => /css/.test(name.split('?')[0]))).forEach((loader) => {
4545
const first = loader.loaders[0];
4646
const rest = loader.loaders.slice(1);
47-
loader.loader = ExtractTextPlugin.extract(first, rest.join('!'));
47+
loader.loader = ExtractTextPlugin.extract({ fallbackLoader: first, loader: rest.join('!') });
4848
delete loader.loaders;
4949
});
5050
}

build/webpack-config/plugins.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
44
const ExtractTextPlugin = require('extract-text-webpack-plugin');
55
const env = require('../base-config/environment');
66

7-
const __DEV__ = env.__DEV__;
8-
const __PROD__ = env.__PROD__;
7+
const isDev = env.isDev;
8+
const isProd = env.isProd;
99

1010
module.exports = (paths) => {
1111
const plugins = [
@@ -21,28 +21,33 @@ module.exports = (paths) => {
2121
}
2222
}),
2323
new webpack.optimize.CommonsChunkPlugin({
24-
names: ['common']
24+
names: ['vendors']
25+
}),
26+
new webpack.LoaderOptionsPlugin({
27+
options: {
28+
postcss: env.config.postcss
29+
}
2530
})
2631
];
27-
if (__DEV__) {
32+
if (isDev) {
2833
debug('Enable HMR,noErrors for development(开启开发环境插件)');
2934
plugins.push(
3035
new webpack.HotModuleReplacementPlugin(),
31-
new webpack.NoErrorsPlugin() //报错时不退出webpack进程
36+
new webpack.NoErrorsPlugin() // 报错时不退出webpack进程
3237
);
3338
} else {
3439
debug('Apply ExtractTextPlugin.(非开发环境开启ExtractTextPlugin)');
3540
plugins.push(
36-
new ExtractTextPlugin('[name].[contenthash].css', {
41+
new ExtractTextPlugin({
42+
filename: '[name].[hash:8].css',
3743
allChunks: true
3844
})
3945
);
4046
}
41-
if (__PROD__) {
42-
debug('Enable OccurenceOrder,Dedupe,UglifyJs for production(开启生产环境打包插件)');
47+
if (isProd) {
48+
debug('Enable OccurenceOrder,UglifyJs for production(开启生产环境打包插件)');
4349
plugins.push(
44-
new webpack.optimize.OccurrenceOrderPlugin(), //根据模块使用情况 排序模块序号
45-
new webpack.optimize.DedupePlugin(), //避免重复模块
50+
new webpack.optimize.OccurrenceOrderPlugin(), // 根据模块使用情况 排序模块序号
4651
new webpack.optimize.UglifyJsPlugin({
4752
compress: {
4853
unused: true,

0 commit comments

Comments
 (0)