forked from huggingface/transformers.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
116 lines (103 loc) · 2.91 KB
/
webpack.config.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import TerserPlugin from "terser-webpack-plugin";
import { fileURLToPath } from "url";
import path from "path";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Helper function to create webpack configurations.
* @param {Object} options Options for creating a webpack target.
* @param {string} options.name Name of output file.
* @param {string} options.suffix Suffix of output file.
* @param {string} options.type Type of library.
* @param {string} options.ignoreModules The list of modules to ignore.
* @param {string} options.externalModules The list of modules to set as external.
* @returns {import('webpack').Configuration} One webpack target.
*/
function buildConfig({
name = "",
suffix = ".js",
type = "module", // 'module' | 'commonjs'
ignoreModules = [],
externalModules = [],
} = {}) {
const outputModule = type === "module";
const alias = Object.fromEntries(
ignoreModules.map((module) => {
return [module, false];
}),
);
/** @type {import('webpack').Configuration} */
const config = {
mode: 'development',
devtool: 'source-map',
entry: {
[`transformers${name}`]: './src/transformers.js',
[`transformers${name}.min`]: './src/transformers.js',
},
output: {
filename: `[name]${suffix}`,
path: path.join(__dirname, 'dist'),
library: {
type,
},
assetModuleFilename: '[name][ext]',
chunkFormat: 'module',
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin({
test: new RegExp(`\\.min\\${suffix}$`),
extractComments: false,
})],
},
experiments: {
outputModule,
},
resolve: { alias },
externals: externalModules,
// Development server
devServer: {
static: {
directory: __dirname,
},
port: 8080,
},
};
if (outputModule) {
config.module = {
parser: {
javascript: {
importMeta: false
}
}
}
} else {
config.externalsType = 'commonjs';
}
return config;
}
// Do not bundle onnxruntime-web when packaging for Node.js.
// Instead, we use the native library (onnxruntime-node).
const NODE_IGNORE_MODULES = ["onnxruntime-web", "onnxruntime-web/webgpu"];
// Do not bundle the following modules with webpack (mark as external)
// NOTE: This is necessary for both type="module" and type="commonjs",
// and will be ignored when building for web (only used for node/deno)
const NODE_EXTERNAL_MODULES = ["onnxruntime-node", "sharp", "fs", "path", "url"];
export default [
// Web-only build
buildConfig({
type: "module",
}),
// Node-compatible builds
buildConfig({
suffix: ".mjs",
type: "module",
ignoreModules: NODE_IGNORE_MODULES,
externalModules: NODE_EXTERNAL_MODULES,
}),
buildConfig({
suffix: ".cjs",
type: "commonjs",
ignoreModules: NODE_IGNORE_MODULES,
externalModules: NODE_EXTERNAL_MODULES,
}),
];