UNPKG

4.84 kBMarkdownView Raw
1#Minimalist boilerplate for Webpack +/- Express
2
3 * Hot loading for both **client** & **server**
4 * Code in es2015
5 * CSS module + postcss + autoprefixer
6 * Assets minification for production
7 * Client files hashing for caching
8 * Focus on your app logics, leave the build tools to others
9
10###
11
12To start
13```shell
14mkdir myApp
15cd myApp
16
17npm init
18npm i -D mwb # install (i) mwb as devDependency (-D)
19
20npm run mwb init # generate the boilerplate with an express server
21npm run mwb initMin # generate the boilerplate without a server
22
23npm run dev # start live coding & editing in development mode
24
25npm run bundle # bundling the app for production
26```
27
28##Tested in node 8+, npm 5+
29
30----------
31###Directory structure:
32```
33App
34 ├─ /build/
35 ├─ /tool/
36 └─ /src/
37 ├─ /client/
38 | └─ entry.js
39 ├─ /share/
40 ├─ /server/
41 | ├─ entry.js
42 | ├─ app.js
43 | └─ main.js
44 └─ /public/
45 └─ favicon.ico
46
47
48```
49---
50###How it works:
51* After initiation, an entry.js file is placed in the src/client and src/server folder
52* All default webpack config files are in the tool folder, you can override these
53* `npm run dev` uses webpack to compile and watch these files and output them into the build folder -> clientBundle.js and serverBundle.js
54* serverBundle.js is run automatically and will be served at localhost:3000 (default using express js)
55* When you edit the files in the source folder, webpack re-compile required files
56* To enable hot module replacement, add `if (module.hot) {module.hot.accept()}` in your code
57* Default webpack.config.js is in the tool directory
58
59---
60###Details
61
62####Included loaders:
63* [`babel-loader`](https://github.com/babel/babel-loader) with presets (env, stage-0, react), plugins (transform-runtime) and cacheDirectory (true).
64* `css!postcss` loaders for css with `autoprefixer`
65* `url-loader` for everything else with limit=10000 & name=[name]_[hash:7].[ext]
66
67Style sheet is **extracted** by `extract-text-webpack-plugin` using `css?minimize&localIdentName=[local]_[hash:7]!postcss` for the initial chunk. The subsequent chunks will be inlined using `style-loader` in the client. `css?module` is applied to files with name xxx.module.css, don't mix global also local css, i.e. in global.css don't do @import './local.module.css'; and vice versa, in local.module.css don't do @import './global.css'. On server, `null-loader` is applied to global.css but `css?module` to local.module.css, the extracted styles.css will be deleted on server when built for production
68
69###Included plugins
70* `extract-text-webpack-plugin`
71* `webpack.optimize.AggressiveMergingPlugin`
72* `webpack.optimize.UglifyJsPlugin({compress: {warnings: false, sourceMap: false, comments: false})`
73* `webpack.DefinePlugin({ 'process.env.APP_ENV': '"node"' })`for server
74* `webpack.DefinePlugin({ 'process.env.APP_ENV': '"web"' })` for clients (browser and cordova)
75* `webpack.DefinePlugin({ 'process.env.CORDOVA': true })` for cordova app
76* `webpack.DefinePlugin({ 'process.env.TEST': true })` for testing
77* `webpack.DefinePlugin({ 'process.env.NODE_ENV': '"development"' })` in development mode
78* `webpack.DefinePlugin({ 'process.env.NODE_ENV': '"production"' })` in production mode to aid dead code elimination during minification
79
80### Express server
81* Listen to 3000 by default or `process.env.PORT`
82* Use `compression` middleware, then `static('./build/public')` middleware
83* Server code is hot replaced in development mode, but NOT in production mode
84* All native modules and assets.json are excluded (treated as external) by webpack using `/^[@a-z][a-z/\.\-0-9]*$/i,` and `/^.?assets\.json$/i` in server, this speeds up build time
85
86
87### Misc
88* All codes wrapped inside `if (process.env.NODE_ENV !== 'production') {}` or `if (process.env.NODE_ENV == 'development') {}` or `if(module.hot) {}` are removed for production
89* source map is set to `cheap-module-eval-source-map` for development
90* source map is **NOT** included in production mode
91* `client_[chunkhash:7].js` `vendor_[chunkhash:7].js` & `styles_[contenthash:7].css` in production mode for caching
92
93
94### Mongodb
95
96```shell
97npm run mwb initMongo
98```
99This add the latest mongodb native driver to the app.
100This also add a file named `mongo.js` into the src/server folder.
101The mongo.js file contains and export the default connection.
102In an imported file, use async & await to retrive the connection and db.
103
104
105### [React & Redux](./doc/react.md)
106
107### [Writing tests](./doc/writingTests.md)
108
109### [Examples](./examples)
110
111### [Cordova integration](./doc/cordova.md)
112
113### Update
114
115You can update simply by typing
116```shell
117npm i -D mwb
118```
119The `tool` directory will be renamed to tool.{timestamp} so your modification will still be preserved. A new tool directory will be created with the updated settings.
\No newline at end of file