|
1 |
| -var path = require('path'); |
2 |
| -var webpack = require('webpack'); |
3 |
| - |
4 |
| -var packageJSON = require('./package.json'); |
5 |
| - |
6 |
| -var getBanner = function (compressed) { |
7 |
| - return packageJSON.name + (compressed ? ' (compressed)' : ' (uncompressed)') + '\n' + |
8 |
| - packageJSON.homepage + '\n' + |
9 |
| - 'Version: ' + packageJSON.version + ' -- ' + (new Date()).toISOString() + '\n' + |
10 |
| - 'License: ' + packageJSON.license; |
11 |
| -}; |
12 |
| - |
13 |
| -module.exports.config = { |
14 |
| - entry: { |
15 |
| - 'ui-scroll': './src/ui-scroll.js', |
16 |
| - 'ui-scroll-grid': './src/ui-scroll-grid.js' |
17 |
| - }, |
18 |
| - output: { |
19 |
| - path: path.join(__dirname, 'temp'), |
20 |
| - filename: '[name].js' |
21 |
| - }, |
22 |
| - cache: false, |
23 |
| - devtool: 'source-map', |
24 |
| - module: { |
25 |
| - loaders: [ |
26 |
| - { |
27 |
| - test: /\.js$/, |
28 |
| - exclude: /node_modules/, |
29 |
| - loader: 'babel-loader?presets[]=es2015' |
30 |
| - } |
31 |
| - ] |
32 |
| - }, |
33 |
| - plugins: [ |
34 |
| - new webpack.BannerPlugin(getBanner(false)) |
35 |
| - ] |
36 |
| -}; |
37 |
| - |
38 |
| -module.exports.compressedPlugins = [ |
39 |
| - new webpack.optimize.UglifyJsPlugin({ |
40 |
| - sourceMap: true, |
41 |
| - compress: { |
42 |
| - warnings: true, |
43 |
| - }, |
44 |
| - output: { |
45 |
| - comments: false, |
46 |
| - }, |
47 |
| - }), |
48 |
| - new webpack.BannerPlugin(getBanner(true)) |
49 |
| -]; |
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | +const webpack = require('webpack'); |
| 4 | +const CleanWebpackPlugin = require('clean-webpack-plugin'); |
| 5 | +const CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 6 | + |
| 7 | +const packageJSON = require('./package.json'); |
| 8 | + |
| 9 | +const getBanner = () => |
| 10 | + packageJSON.name + '\n' + |
| 11 | + packageJSON.homepage + '\n' + |
| 12 | + 'Version: ' + packageJSON.version + ' -- ' + (new Date()).toISOString() + '\n' + |
| 13 | + 'License: ' + packageJSON.license; |
| 14 | + |
| 15 | +const ENV = (process.env.npm_lifecycle_event.indexOf('dev') === 0) ? 'development' : 'production'; |
| 16 | +const isTest = (process.env.npm_lifecycle_event.indexOf('dev-test') !== -1) ? true : false; |
| 17 | +console.log('********** webpack runs in ' + ENV + ' environment **********\n'); |
| 18 | + |
| 19 | +let configEnv; |
| 20 | + |
| 21 | +if (ENV === 'development') { |
| 22 | + configEnv = { |
| 23 | + output: { |
| 24 | + filename: '[name].js', |
| 25 | + publicPath: '/' |
| 26 | + }, |
| 27 | + |
| 28 | + rules: [{ |
| 29 | + enforce: 'pre', |
| 30 | + test: /Spec\.js$/, |
| 31 | + include: path.resolve(__dirname, 'test'), |
| 32 | + use: [{ |
| 33 | + loader: 'jshint-loader' |
| 34 | + }] |
| 35 | + }], |
| 36 | + |
| 37 | + devtool: 'inline-source-map', |
| 38 | + |
| 39 | + entry: {}, |
| 40 | + |
| 41 | + plugins: [], |
| 42 | + |
| 43 | + devServer: !isTest ? { |
| 44 | + historyApiFallback: { |
| 45 | + rewrites: [ |
| 46 | + { from: '/dist/ui-scroll.js', to: (context) => '/ui-scroll.js' }, |
| 47 | + { from: '/dist/ui-scroll-grid.js', to: (context) => '/ui-scroll-grid.js' }, |
| 48 | + { from: /\/*\/*\.html$/, to: (context) => '/demo' + context.parsedUrl.pathname }, |
| 49 | + { from: /\/*\/*\.css$/, to: (context) => '/demo' + context.parsedUrl.pathname }, |
| 50 | + { from: /\/*\/*\.js$/, to: (context) => '/demo' + context.parsedUrl.pathname }, |
| 51 | + { from: /\/ui-scroll-demo\.gif$/, to: '/demo/ui-scroll-demo.gif' }, |
| 52 | + { from: /^\/$/, to: '/demo/index.html' } |
| 53 | + ] |
| 54 | + }, |
| 55 | + inline: true, |
| 56 | + quiet: false, |
| 57 | + port: 5005, |
| 58 | + stats: 'errors-only', |
| 59 | + publicPath: '/' |
| 60 | + } : {}, |
| 61 | + |
| 62 | + watch: true |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +if (ENV === 'production') { |
| 67 | + configEnv = { |
| 68 | + output: { |
| 69 | + path: path.join(__dirname, 'dist'), |
| 70 | + filename: '[name].js' |
| 71 | + }, |
| 72 | + |
| 73 | + rules: [], |
| 74 | + |
| 75 | + devtool: 'source-map', |
| 76 | + |
| 77 | + entry: { |
| 78 | + 'ui-scroll.min': path.resolve(__dirname, 'src/ui-scroll.js'), |
| 79 | + 'ui-scroll-grid.min': path.resolve(__dirname, 'src/ui-scroll-grid.js') |
| 80 | + }, |
| 81 | + |
| 82 | + plugins: [ |
| 83 | + new CleanWebpackPlugin('dist', { |
| 84 | + root: __dirname |
| 85 | + }), |
| 86 | + new webpack.optimize.UglifyJsPlugin({ |
| 87 | + sourceMap: true, |
| 88 | + compress: { |
| 89 | + warnings: true, |
| 90 | + }, |
| 91 | + output: { |
| 92 | + comments: false, |
| 93 | + }, |
| 94 | + include: /\.min\.js$/ |
| 95 | + }), |
| 96 | + new CopyWebpackPlugin([ |
| 97 | + {from: 'src/ui-scroll-jqlite.js', to: 'ui-scroll-jqlite.min.js'}, |
| 98 | + {from: 'src/ui-scroll-jqlite.js', to: 'ui-scroll-jqlite.js'} |
| 99 | + ], {copyUnmodified: true}), |
| 100 | + new webpack.BannerPlugin(getBanner()) |
| 101 | + ], |
| 102 | + |
| 103 | + devServer: {}, |
| 104 | + |
| 105 | + watch: false |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = { |
| 110 | + entry: Object.assign({ |
| 111 | + 'ui-scroll': path.resolve(__dirname, 'src/ui-scroll.js'), |
| 112 | + 'ui-scroll-grid': path.resolve(__dirname, 'src/ui-scroll-grid.js') |
| 113 | + }, configEnv.entry), |
| 114 | + |
| 115 | + output: configEnv.output, |
| 116 | + |
| 117 | + cache: false, |
| 118 | + |
| 119 | + devtool: configEnv.devtool, |
| 120 | + |
| 121 | + module: { |
| 122 | + rules: [...configEnv.rules, |
| 123 | + { |
| 124 | + test: /\.js$/, |
| 125 | + exclude: /node_modules/, |
| 126 | + loader: 'babel-loader', |
| 127 | + options: { |
| 128 | + presets: ['es2015'] |
| 129 | + } |
| 130 | + }, |
| 131 | + { |
| 132 | + enforce: 'pre', |
| 133 | + test: /\.js$/, |
| 134 | + include: path.resolve(__dirname, 'src'), |
| 135 | + use: [{ |
| 136 | + loader: 'jshint-loader' |
| 137 | + }] |
| 138 | + } |
| 139 | + ] |
| 140 | + }, |
| 141 | + |
| 142 | + plugins: configEnv.plugins, |
| 143 | + |
| 144 | + devServer: configEnv.devServer, |
| 145 | + |
| 146 | + watch: configEnv.watch |
| 147 | +}; |
0 commit comments