webpack.config.js 2.44 KB
Newer Older
gaoqiong's avatar
gaoqiong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require("terser-webpack-plugin");

function terserEcmaVersionFromWebpackTarget(target) {
  switch (target) {
    case 'es5':
      return 5;
    case 'es6':
    case 'es2015':
      return 2015;
    case 'es2017':
      return 2017;
    default:
      throw new RangeError(`not supported ECMA version: ${target}`);
  }
}

function addCopyrightBannerPlugin(mode, target) {
  const VERSION = require(path.join(__dirname, 'package.json')).version;
  const COPYRIGHT_BANNER = `/*!
 * ONNX Runtime Common v${VERSION}
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */`;

  if (mode === 'production') {
    return new TerserPlugin({
      extractComments: false,
      terserOptions: {
        ecma: terserEcmaVersionFromWebpackTarget(target),
        format: {
          preamble: COPYRIGHT_BANNER,
          comments: false,
        },
        compress: {
          passes: 2
        }
      }
    });
  } else {
    return new webpack.BannerPlugin({ banner: COPYRIGHT_BANNER, raw: true });
  }
}

function buildConfig({
  suffix = '',
  format = 'umd',
  target = 'es2017',
  mode = 'production',
  devtool = 'source-map'
}) {
  return {
    target: [format === 'commonjs' ? 'node' : 'web', target],
    entry: path.resolve(__dirname, 'lib/index.ts'),
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: `ort-common${suffix}.js`,
      library: {
        name: format === 'commonjs' ? undefined : 'ort',
        type: format
      }
    },
    resolve: { extensions: ['.ts', '.js'] },
    plugins: [
      new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/] }),
      addCopyrightBannerPlugin(mode, target),
    ],
    module: {
      rules: [{
        test: /\.tsx?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              compilerOptions: { target }
            }
          }
        ]
      }]
    },
    mode: mode,
    devtool: devtool,
  };
}

module.exports = (env, argv) => {
  return [
    buildConfig({ suffix: '.es5.min', target: 'es5' }),
    buildConfig({ suffix: '.es6.min', target: 'es6' }),
    buildConfig({ suffix: '.min' }),
    buildConfig({ mode: 'development', devtool: 'inline-source-map' }),
    buildConfig({ format: 'commonjs', suffix: '.node' }),
  ];
};