Idan Amit | a2c544d | 2018-07-19 12:05:50 +0300 | [diff] [blame] | 1 | var path = require("path"); |
| 2 | var webpack = require("webpack"); |
| 3 | var UglifyJsPlugin = require("uglifyjs-webpack-plugin"); |
| 4 | |
| 5 | var PATHS = { |
Idan Amit | bbeb590 | 2019-01-03 18:18:37 +0200 | [diff] [blame^] | 6 | entryPoint: path.resolve(__dirname, './index.ts'), |
Idan Amit | a2c544d | 2018-07-19 12:05:50 +0300 | [diff] [blame] | 7 | bundles: path.resolve(__dirname, 'dist'), |
Idan Amit | bbeb590 | 2019-01-03 18:18:37 +0200 | [diff] [blame^] | 8 | }; |
Idan Amit | a2c544d | 2018-07-19 12:05:50 +0300 | [diff] [blame] | 9 | |
| 10 | var config = { |
| 11 | // These are the entry point of our library. We tell webpack to use |
| 12 | // the name we assign later, when creating the bundle. We also use |
| 13 | // the name to filter the second entry point for applying code |
| 14 | // minification via UglifyJS |
| 15 | entry: { |
Idan Amit | bbeb590 | 2019-01-03 18:18:37 +0200 | [diff] [blame^] | 16 | 'sdc-pubsub': PATHS.entryPoint, |
| 17 | 'sdc-pubsub.min': PATHS.entryPoint |
Idan Amit | a2c544d | 2018-07-19 12:05:50 +0300 | [diff] [blame] | 18 | }, |
| 19 | // The output defines how and where we want the bundles. The special |
| 20 | // value `[name]` in `filename` tell Webpack to use the name we defined above. |
| 21 | // We target a UMD and name it MyLib. When including the bundle in the browser |
| 22 | // it will be accessible at `window.MyLib` |
| 23 | output: { |
| 24 | path: PATHS.bundles, |
| 25 | filename: '[name].js', |
| 26 | libraryTarget: 'umd', |
| 27 | library: 'sdcPubSub', |
| 28 | umdNamedDefine: true |
| 29 | }, |
| 30 | // Add resolve for `tsx` and `ts` files, otherwise Webpack would |
| 31 | // only look for common JavaScript file extension (.js) |
| 32 | resolve: { |
| 33 | extensions: ['.ts', '.js'] |
| 34 | }, |
| 35 | // Activate source maps for the bundles in order to preserve the original |
| 36 | // source when the user debugs the application |
| 37 | devtool: 'source-map', |
| 38 | plugins: [ |
| 39 | // Apply minification only on the second bundle by |
| 40 | // using a RegEx on the name, which must end with `.min.js` |
| 41 | // NB: Remember to activate sourceMaps in UglifyJsPlugin |
| 42 | // since they are disabled by default! |
| 43 | new UglifyJsPlugin({ |
| 44 | sourceMap: true, |
| 45 | include: /\.min\.js$/, |
| 46 | }) |
| 47 | ], |
| 48 | module: { |
| 49 | // Webpack doesn't understand TypeScript files and a loader is needed. |
| 50 | // `node_modules` folder is excluded in order to prevent problems with |
| 51 | // the library dependencies, as well as `__tests__` folders that |
| 52 | // contain the tests for the library |
| 53 | rules: [ |
| 54 | { |
| 55 | test: /\.ts?$/, |
| 56 | use: [ |
| 57 | { |
Idan Amit | bbeb590 | 2019-01-03 18:18:37 +0200 | [diff] [blame^] | 58 | loader: 'ts-loader', |
Idan Amit | a2c544d | 2018-07-19 12:05:50 +0300 | [diff] [blame] | 59 | options: { |
Idan Amit | bbeb590 | 2019-01-03 18:18:37 +0200 | [diff] [blame^] | 60 | configFile: 'tsconfig.json' |
Idan Amit | a2c544d | 2018-07-19 12:05:50 +0300 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | ] |
| 64 | }] |
| 65 | } |
Idan Amit | bbeb590 | 2019-01-03 18:18:37 +0200 | [diff] [blame^] | 66 | }; |
Idan Amit | a2c544d | 2018-07-19 12:05:50 +0300 | [diff] [blame] | 67 | |
| 68 | module.exports = config; |