| import TerserPlugin from "terser-webpack-plugin"; |
| import { fileURLToPath } from "url"; |
| import path from "path"; |
| import fs from "fs"; |
|
|
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
|
|
| |
| |
| |
| |
| |
| |
| class PostBuildPlugin { |
|
|
| apply(compiler) { |
| compiler.hooks.done.tap('PostBuildPlugin', () => { |
| const dist = path.join(__dirname, 'dist'); |
| const ORT_JSEP_FILE = 'ort-wasm-simd-threaded.jsep.mjs'; |
| const ORT_BUNDLE_FILE = 'ort.bundle.min.mjs'; |
|
|
| |
| { |
| const file = path.join(dist, ORT_BUNDLE_FILE); |
| if (fs.existsSync(file)) fs.unlinkSync(file); |
| } |
| |
| |
| { |
| const src = path.join(__dirname, 'node_modules/onnxruntime-web/dist', ORT_JSEP_FILE); |
| const dest = path.join(dist, ORT_JSEP_FILE); |
| fs.copyFileSync(src, dest); |
| } |
|
|
| |
| { |
| const files = ['transformers.js', 'transformers.min.js']; |
| for (const file of files) { |
| const filePath = path.join(dist, file); |
| let content = fs.readFileSync(filePath, 'utf8'); |
| content = content.replace( |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| new RegExp('new URL\\(["\']\\.\\\/["\'],\\s*import\\.meta\\.url\\)', 'gm'), |
| "new URL(import.meta.url)", |
| ); |
| fs.writeFileSync(filePath, content, 'utf8'); |
| } |
| } |
| }); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function buildConfig({ |
| name = "", |
| suffix = ".js", |
| type = "module", // 'module' | 'commonjs' |
| ignoreModules = [], |
| externalModules = [], |
| plugins = [], |
| } = {}) { |
| const outputModule = type === "module"; |
|
|
| const alias = Object.fromEntries( |
| ignoreModules.map((module) => [module, false]), |
| ); |
|
|
| |
| 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}$`), |
|
|
| |
| |
| terserOptions: { |
| output: { |
| comments: false, |
| }, |
| }, |
| extractComments: false, |
| }), |
| ], |
| }, |
| experiments: { |
| outputModule, |
| }, |
| resolve: { alias }, |
|
|
| externals: externalModules, |
|
|
| |
| devServer: { |
| static: { |
| directory: __dirname, |
| }, |
| port: 8080, |
| }, |
| plugins, |
| }; |
|
|
| if (outputModule) { |
| config.module = { |
| parser: { |
| javascript: { |
| importMeta: false, |
| }, |
| }, |
| }; |
| } else { |
| config.externalsType = "commonjs"; |
| } |
|
|
| return config; |
| } |
|
|
| |
| |
| const NODE_IGNORE_MODULES = ["onnxruntime-web"]; |
|
|
| |
| |
| |
| const NODE_EXTERNAL_MODULES = [ |
| "onnxruntime-node", |
| "sharp", |
| "fs", |
| "path", |
| "url", |
| ]; |
|
|
| |
| const WEB_BUILD = buildConfig({ |
| type: "module", |
| plugins: [new PostBuildPlugin()], |
| }); |
|
|
| |
| const NODE_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, |
| }), |
| ]; |
|
|
| |
| const BUILDS = process.env.WEBPACK_SERVE |
| ? [WEB_BUILD] |
| : [WEB_BUILD, ...NODE_BUILDS]; |
| export default BUILDS; |
|
|