1 | # Webpack Configuration
|
2 | Webpack configuration with following features:
|
3 | * Hot Reloading in Development
|
4 | * Production Optimized Build
|
5 | * JSX and Vue
|
6 | * Babel
|
7 | * Eslint
|
8 | * Sane Project Structure
|
9 |
|
10 |
|
11 | ## How To Use
|
12 |
|
13 | Install this package
|
14 | ```bash
|
15 | npm install sm-webpack-config --save-dev
|
16 | ```
|
17 |
|
18 | **NOTE**: You need to install `node-sass` if you want to use sass, and `compression-webpack-plugin` if you want to use gzip option.
|
19 |
|
20 | ## How to Use
|
21 | ```js
|
22 | const smWebpack = require('sm-webpack-config');
|
23 |
|
24 | // Set Configuration [Optional]
|
25 | const config = {
|
26 | sourcePath: 'res',
|
27 | destPath: 'static/dist',
|
28 | publicUrl: '/static/dist',
|
29 | sourceMap: true,
|
30 | devServerPort: 3001,
|
31 | appPort: 3000,
|
32 | };
|
33 |
|
34 | // Run Developement Server With Hot Reloading
|
35 | smWebpack.runDevServer({config});
|
36 |
|
37 | // Build For Production
|
38 | smWebpack.runProdWebpack({config});
|
39 |
|
40 | // If you want to go advanced and override some webpack configuration
|
41 | const webpackConfig = {
|
42 | plugins: [
|
43 | new webpack.optimize.UglifyJsPlugin({
|
44 | compress: {
|
45 | warnings: true
|
46 | }
|
47 | }),
|
48 | ],
|
49 | };
|
50 |
|
51 | // Run Developement Server With Hot Reloading
|
52 | smWebpack.runDevServer({config, webpackConfig});
|
53 |
|
54 | // Build For Production
|
55 | smWebpack.runProdWebpack({config, webpackConfig});
|
56 |
|
57 | // NOTE: Both config & webpackConfig are optional
|
58 | ```
|
59 |
|
60 | ## Using with Gulp
|
61 | You can also use it in gulp using the following gulpfile
|
62 | ```js
|
63 | const smWebpack = require('sm-webpack-config');
|
64 | const gulp = require('gulp');
|
65 |
|
66 | // The development server (the recommended option for development)
|
67 | gulp.task('default', function(callback) {
|
68 | smWebpack.runDevServer().then(callback);
|
69 | });
|
70 |
|
71 | // Build files for production
|
72 | gulp.task('build', function(callback) {
|
73 | smWebpack.runProdWebpack().then(callback);
|
74 | });
|
75 | ```
|
76 |
|
77 | Now you can use `gulp` to run dev-server and `gulp build` to build for production.
|
78 |
|
79 |
|
80 | ## Default Project Structure
|
81 | * Keep all your client side code in `res`
|
82 | * all javascript in `js`
|
83 | * vue components in `components`
|
84 | * 3rd party scripts in `vendor` (though they should generally be installed from npm)
|
85 | * css / scss in `css`
|
86 | * images in `img`
|
87 | * other assests in `assests`
|
88 | * eg. fonts can be kept in `assests/fonts`
|
89 | * directly served static assests in `public`
|
90 | * assests in this directory will not be compiled and copied directly to the `public` folder in dist
|
91 | * this directory should be used as public root directory in your server config
|
92 | * `index.html` as entry point (no need to include js/css, they will automatically be injected by webpack).
|
93 |
|
94 | * all the compiled code will be put in `static/dist`
|
95 | * point your static server to `static` directory
|
96 | * use public path as `/static`
|
97 | * api endpoints should start with `/api`
|
98 | * Keep your server running at port `3000`
|
99 | * Dev server will run at port `3001`
|
100 |
|
101 | ## Webpack Aliases
|
102 | The following aliases are defined
|
103 | ```js
|
104 | // by default config.sourcePath points to `res`, you can override it in your config
|
105 | {
|
106 | '@': config.sourcePath,
|
107 | 'res': config.sourcePath,
|
108 | 'js': path.join(config.sourcePath, 'js'),
|
109 | 'assets': path.join(config.sourcePath, 'assets'),
|
110 | 'components': path.join(config.sourcePath, 'js', 'components'),
|
111 | 'css': path.join(config.sourcePath, 'css'),
|
112 | 'img': path.join(config.sourcePath, 'img'),
|
113 | }
|
114 | ```
|
115 |
|
116 | So you can write `import Hello from 'components/Hello.vue'` from anywhere
|
117 | If you want to use images in your vue templates use them as `~img/logo.png` (append a ~)
|
118 | See https://github.com/vuejs/vue-loader/issues/193
|
119 |
|
120 | ## Configuration Options
|
121 | ```js
|
122 | const config = {
|
123 | // folder containing source files
|
124 | sourcePath: 'res',
|
125 |
|
126 | // where to put the compiled files
|
127 | destPath: 'static/dist',
|
128 |
|
129 | // at what url are these files accessible
|
130 | // eg. if using a CDN, you can give http://static.smartprix.com/dist
|
131 | publicUrl: '/static/dist',
|
132 |
|
133 | // whether to generate source maps or not
|
134 | sourceMap: true,
|
135 |
|
136 | // whether to enable eslint or not
|
137 | eslint: true,
|
138 |
|
139 | // entry points for webpack (relative to sourcePath)
|
140 | entry: {
|
141 | app: 'js/index.js',
|
142 | },
|
143 |
|
144 | // html entry file, generated files will be auto-injected into this
|
145 | entryHtml: 'index.html',
|
146 |
|
147 | // whether the exported file should be a library
|
148 | library: false,
|
149 |
|
150 | // append hash of the file to the filename
|
151 | appendHash: true,
|
152 |
|
153 | // whether to minify the output or not
|
154 | // false in developement, true in production
|
155 | minify: false,
|
156 |
|
157 | // whether to pre gzip the files
|
158 | // makes a .gz file for each bundle produced
|
159 | gzip: false,
|
160 |
|
161 | // whether to not display much info while running dev server
|
162 | quiet: true,
|
163 |
|
164 | // babel config
|
165 | babel: {
|
166 | // options for @babel/preset-env
|
167 | // https://babeljs.io/docs/en/babel-preset-env.html
|
168 | envOptions: {
|
169 | // target these browsers
|
170 | // default targets is based on browsers that support async-await
|
171 | targets: {chrome: '55'},
|
172 | },
|
173 |
|
174 | // extra presets to include
|
175 | includePresets: [],
|
176 |
|
177 | // don't include these presets (if included by default)
|
178 | excludePresets: [],
|
179 |
|
180 | // extra plugins to include
|
181 | includePlugins: [],
|
182 |
|
183 | // don't include these plugins (if included by default)
|
184 | excludePlugins: [],
|
185 |
|
186 | // transform imports of the given modules to reduce code size
|
187 | // see: https://www.npmjs.com/package/babel-plugin-transform-imports
|
188 | transformImports: {},
|
189 |
|
190 | // run babel in debug mode
|
191 | debug: false,
|
192 | },
|
193 |
|
194 | // whether to generate analyzer report with the generated bundle
|
195 | // false in development, true in production
|
196 | analyzeBundle: false,
|
197 |
|
198 | // whether this bundle is meant for server (vue ssr)
|
199 | ssr: false,
|
200 |
|
201 | // enable css modules support for all css files
|
202 | // NOT: the following files already have module support regardless of this setting
|
203 | // <style module> in .vue
|
204 | // .module.css files
|
205 | // .css?module files
|
206 | cssModules: false,
|
207 |
|
208 | // dev server and hot reloading options
|
209 | devServer: {
|
210 | // dev server middlewares
|
211 | before(app, server) {},
|
212 | after(app, server) {},
|
213 |
|
214 | // dev server host
|
215 | host: '0.0.0.0',
|
216 |
|
217 | // dev server port
|
218 | port: 3001,
|
219 |
|
220 | // use https
|
221 | https: false,
|
222 |
|
223 | // whether to open the web browser
|
224 | open: true,
|
225 |
|
226 | // what port the app server is running
|
227 | appPort: 3000,
|
228 |
|
229 | // the paths to proxy on the dev-server
|
230 | proxy: {
|
231 | '/api': 'http://localhost:<appPort>',
|
232 | '/static': 'http://localhost:<appPort>',
|
233 | '/uploads': 'http://localhost:<appPort>',
|
234 | },
|
235 |
|
236 | // notify using os-native notification whenever an error occurs
|
237 | notifyOnError: true,
|
238 | },
|
239 |
|
240 | // overrides for production environment
|
241 | $env_production: {
|
242 | minify: true,
|
243 | eslint: false,
|
244 | },
|
245 |
|
246 | // overrides for development environment
|
247 | $env_development: {
|
248 | eslint: true,
|
249 | },
|
250 | };
|
251 | ```
|
252 |
|
253 | # Rollup
|
254 | sm-webpack-config also includes rollup. You can use it as:
|
255 | ```js
|
256 | const smWebpack = require('sm-webpack-config');
|
257 |
|
258 | const config = {
|
259 | entry: 'src/index.js',
|
260 | dest: 'dest/index.js',
|
261 | library: 'vutils',
|
262 | libraryFormat: 'es',
|
263 | minify: false,
|
264 | sourceMap: false,
|
265 |
|
266 | // overrides for production environment
|
267 | $env_production: {
|
268 | minify: true,
|
269 | },
|
270 |
|
271 | // overrides for development environment
|
272 | $env_development: {
|
273 | minify: false,
|
274 | },
|
275 | };
|
276 |
|
277 | smWebpack.runRollup({config}).then(() => {
|
278 | console.log("Done!");
|
279 | });
|
280 | ```
|
281 |
|
282 | #### Configuration Options
|
283 | ```js
|
284 | const config = {
|
285 | // entry point of the script (source)
|
286 | entry: 'src/index.js',
|
287 |
|
288 | // where to put compiled file
|
289 | dest: 'dist/index.js',
|
290 |
|
291 | // library name to give to the module
|
292 | library: 'lib',
|
293 |
|
294 | // what format should the compiled file be in
|
295 | // this can be umd, amd, cjs (node like format), es (import / export), iife (for using in <script>)
|
296 | libraryFormat: 'umd',
|
297 |
|
298 | // whether to uglify the output
|
299 | minify: false,
|
300 |
|
301 | // whether to generate a sourcemap or not
|
302 | sourceMap: false,
|
303 | };
|
304 | ```
|