|
| 1 | +const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); |
| 2 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 3 | +const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); |
| 4 | +const path = require('path'); |
| 5 | +const webpack = require('webpack'); |
| 6 | + |
| 7 | +module.exports = env => { |
| 8 | + const nodeEnv = process.env.NODE_ENV || 'development'; |
| 9 | + const isProd = nodeEnv === 'production'; |
| 10 | + const isLegacy = !!process.env.legacy && !(process.env.legacy == "false"); |
| 11 | + console.log(">> webpack nodeEnv=" + nodeEnv); |
| 12 | + console.log(">> webpack isProd=" + isProd); |
| 13 | + console.log(">> webpack isLegacy=" + isLegacy); |
| 14 | + const presets = [ |
| 15 | + ["@babel/preset-env", { |
| 16 | + "useBuiltIns": "usage", |
| 17 | + "corejs": 3, |
| 18 | + "targets": { |
| 19 | + "browsers": isLegacy ? ["defaults"] : [ |
| 20 | + "last 2 Chrome versions", |
| 21 | + "last 2 Safari versions", |
| 22 | + "last 2 iOS versions", |
| 23 | + "last 2 Firefox versions", |
| 24 | + "last 2 Edge versions"] |
| 25 | + } |
| 26 | + }], |
| 27 | + "@babel/preset-typescript" |
| 28 | + ]; |
| 29 | + |
| 30 | + return { |
| 31 | + entry: isLegacy ? [ |
| 32 | + path.resolve(__dirname, 'node_modules/@webcomponents/custom-elements'), |
| 33 | + path.resolve(__dirname, 'node_modules/@webcomponents/template'), |
| 34 | + path.resolve(__dirname, 'src') |
| 35 | + ] : path.resolve(__dirname, 'src'), |
| 36 | + devtool: isProd ? false : 'source-map', |
| 37 | + output: { |
| 38 | + filename: isProd ? '[fullhash].bundle.js' : '[fullhash].bundle.js', |
| 39 | + globalObject: 'this', |
| 40 | + path: path.resolve(__dirname, 'dist'), |
| 41 | + }, |
| 42 | + |
| 43 | + resolve: { |
| 44 | + mainFields: ['esm2015', 'module', 'main'], |
| 45 | + extensions: ['.ts', '.js', '.json'], |
| 46 | + plugins: [new TsconfigPathsPlugin({ |
| 47 | + configFile: './tsconfig.json', |
| 48 | + extensions: ['.ts', '.js'], |
| 49 | + mainFields: ['esm2015', 'module', 'main'] |
| 50 | + })] |
| 51 | + }, |
| 52 | + |
| 53 | + module: { |
| 54 | + rules: [ |
| 55 | + { test: /\.(png|svg|jpg|gif)$/, use: ['file-loader'] }, |
| 56 | + { test: /\.(csv|tsv)$/, use: ['csv-loader'] }, |
| 57 | + { test: /\.xml$/, use: ['xml-loader'] }, |
| 58 | + { test: /\.css$/, sideEffects: true, use: ['style-loader', 'css-loader'] }, |
| 59 | + { |
| 60 | + test: /worker\.(ts|js)$/, |
| 61 | + use: [ |
| 62 | + { loader: 'worker-loader' }, |
| 63 | + { |
| 64 | + loader: 'babel-loader', options: { |
| 65 | + "compact": isProd ? true : false, |
| 66 | + "presets": presets, |
| 67 | + "plugins": [ |
| 68 | + "@babel/plugin-proposal-class-properties", |
| 69 | + "@babel/plugin-transform-runtime" |
| 70 | + ] |
| 71 | + } |
| 72 | + } |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + test: /\.(ts|js)$/, loader: 'babel-loader', |
| 77 | + options: { |
| 78 | + "compact": isProd ? true : false, |
| 79 | + "presets": presets, |
| 80 | + "plugins": [ |
| 81 | + "@babel/plugin-proposal-class-properties", |
| 82 | + "@babel/plugin-transform-runtime" |
| 83 | + ] |
| 84 | + }, |
| 85 | + exclude: |
| 86 | + function (modulePath) { |
| 87 | + return /node_modules/.test(modulePath) && |
| 88 | + !/igniteui-webcomponents/.test(modulePath) && |
| 89 | + !/lit-html/.test(modulePath); |
| 90 | + } |
| 91 | + }], |
| 92 | + }, |
| 93 | + |
| 94 | + plugins: [ |
| 95 | + new webpack.DefinePlugin({ |
| 96 | + 'process.env.NODE_ENV': JSON.stringify(nodeEnv) |
| 97 | + }), |
| 98 | + new HtmlWebpackPlugin({ |
| 99 | + title: 'for-cs', |
| 100 | + template: 'index.html' |
| 101 | + }), |
| 102 | + new ForkTsCheckerWebpackPlugin() |
| 103 | + ] |
| 104 | + }; |
| 105 | +}; |
0 commit comments