|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// Silence webpack2 deprecation warnings |
| 4 | +// https://github.com/vuejs/vue-loader/issues/666 |
| 5 | +process.noDeprecation = true |
| 6 | + |
| 7 | +const HtmlWebpackPlugin = require('html-webpack-plugin') |
| 8 | +const path = require('path') |
| 9 | +const webpack = require('webpack') |
| 10 | +const { CheckerPlugin } = require('awesome-typescript-loader') |
| 11 | + |
| 12 | +// Paths to be used for webpack configuration |
| 13 | +const paths = { |
| 14 | + appSrc: path.join(process.cwd(), 'src'), |
| 15 | + appIndex: path.join(process.cwd(), 'src', 'index.ts'), |
| 16 | + appBuild: path.join(process.cwd(), 'build'), |
| 17 | + public: '/' |
| 18 | +} |
| 19 | + |
| 20 | +module.exports = { |
| 21 | + entry: { |
| 22 | + main: [ |
| 23 | + // Include an alternative client for WebpackDevServer. A client's job is to |
| 24 | + // connect to WebpackDevServer by a socket and get notified about changes. |
| 25 | + // When you save a file, the client will either apply hot updates (in case |
| 26 | + // of CSS changes), or refresh the page (in case of JS changes). When you |
| 27 | + // make a syntax error, this client will display a syntax error overlay. |
| 28 | + // Note: instead of the default WebpackDevServer client, we use a custom one |
| 29 | + // to bring better experience from Create React App users. You can replace |
| 30 | + // the line below with these two lines if you prefer the stock client: |
| 31 | + // require.resolve('webpack-dev-server/client') + '?/', |
| 32 | + // require.resolve('webpack/hot/dev-server'), |
| 33 | + require.resolve('react-dev-utils/webpackHotDevClient'), |
| 34 | + // Your app's code |
| 35 | + paths.appIndex |
| 36 | + ] |
| 37 | + }, |
| 38 | + resolve: { |
| 39 | + extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'] |
| 40 | + }, |
| 41 | + output: { |
| 42 | + // This does not produce a real file. It's just the virtual path that is |
| 43 | + // served by WebpackDevServer in development. This is the JS bundle |
| 44 | + // containing code from all our entry points, and the Webpack runtime. |
| 45 | + filename: 'static/js/bundle.js', |
| 46 | + // Not used in dev but WebpackDevServer crashes without it: |
| 47 | + path: paths.appBuild, |
| 48 | + // The URL that app is served from. We use "/" in development. |
| 49 | + publicPath: paths.public |
| 50 | + }, |
| 51 | + module: { |
| 52 | + rules: [ |
| 53 | + { |
| 54 | + // We use babel-loader to transipile every .js or .jsx file |
| 55 | + test: /\.jsx?$/, |
| 56 | + loader: 'babel-loader', |
| 57 | + // Including over excluding as a whitelist is easier to maintain than a blacklist. |
| 58 | + // as per http://stackoverflow.com/questions/31675025/how-to-exclude-nested-node-module-folders-from-a-loader-in-webpack |
| 59 | + include: paths.appSrc, |
| 60 | + options: { |
| 61 | + // This is a feature of `babel-loader` for webpack (not Babel itself). |
| 62 | + // It enables caching results in ./node_modules/.cache/babel-loader/ |
| 63 | + // directory for faster rebuilds. |
| 64 | + cacheDirectory: true, |
| 65 | + // Instead of relying on a babelrc file to configure babel (or in package.json configs) |
| 66 | + // We speficy here which presets to use. In the future this could be moved to it's own |
| 67 | + // package as create-react-app does with their 'babel-preset-react-app module |
| 68 | + babelrc: false, |
| 69 | + presets: [ |
| 70 | + [ 'env', { |
| 71 | + 'targets': { |
| 72 | + 'browsers': ['last 2 versions'] |
| 73 | + } |
| 74 | + }] |
| 75 | + ], |
| 76 | + plugins: [ |
| 77 | + // https://cycle.js.org/getting-started.html#getting-started-coding-consider-jsx |
| 78 | + // This allow us to use JSX to create virtual dom elements instead of Snabbdom helpers like div(), input(), .. |
| 79 | + ['transform-react-jsx', { pragma: 'Snabbdom.createElement' }], |
| 80 | + // Allow Babel to transform rest properties for object destructuring assignment and spread properties for object literals. |
| 81 | + ['transform-object-rest-spread'] |
| 82 | + ] |
| 83 | + } |
| 84 | + }, |
| 85 | + { |
| 86 | + test: /\.tsx?$/, |
| 87 | + loader: 'awesome-typescript-loader' |
| 88 | + } |
| 89 | + ] |
| 90 | + }, |
| 91 | + plugins: [ |
| 92 | + new CheckerPlugin(), |
| 93 | + // This is necessary to emit hot updates (currently CSS only): |
| 94 | + new webpack.HotModuleReplacementPlugin(), |
| 95 | + // Generates an `index.html` file with the <script> injected. |
| 96 | + new HtmlWebpackPlugin({ |
| 97 | + template: 'public/index.html', |
| 98 | + inject: true, |
| 99 | + favicon: 'public/favicon.png', |
| 100 | + hash: true |
| 101 | + }), |
| 102 | + // Makes environment variables available to the JS code, fallback to 'development' |
| 103 | + new webpack.DefinePlugin({ |
| 104 | + DEVELOPMENT: JSON.stringify(process.env.NODE_ENV === 'development') |
| 105 | + }), |
| 106 | + // To be used for JSX support |
| 107 | + new webpack.ProvidePlugin({ |
| 108 | + Snabbdom: 'snabbdom-pragma' |
| 109 | + }) |
| 110 | + ], |
| 111 | + devtool: 'inline-source-map' |
| 112 | +} |
0 commit comments