From a12bc3fd0e947fd8ad5d209ffa3a89dd7c8522b1 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 14 Nov 2022 16:49:58 -0800 Subject: [PATCH] Add IIFE bundles for NPM packages PiperOrigin-RevId: 488504360 --- mediapipe/tasks/web/BUILD | 69 ++++++++++++++++--- mediapipe/tasks/web/package.json | 7 +- mediapipe/tasks/web/rollup.config.cjs.mjs | 15 ++++ mediapipe/tasks/web/rollup.config.iife.mjs | 21 ++++++ mediapipe/tasks/web/rollup.config.mjs | 9 --- package.json | 2 + yarn.lock | 80 +++++++++++++++++++++- 7 files changed, 181 insertions(+), 22 deletions(-) create mode 100644 mediapipe/tasks/web/rollup.config.cjs.mjs create mode 100644 mediapipe/tasks/web/rollup.config.iife.mjs delete mode 100644 mediapipe/tasks/web/rollup.config.mjs diff --git a/mediapipe/tasks/web/BUILD b/mediapipe/tasks/web/BUILD index 2c0ea57ef..ddc35ab21 100644 --- a/mediapipe/tasks/web/BUILD +++ b/mediapipe/tasks/web/BUILD @@ -28,8 +28,8 @@ mediapipe_ts_library( ) rollup_bundle( - name = "audio_bundle", - config_file = "rollup.config.mjs", + name = "audio_cjs_bundle", + config_file = "rollup.config.cjs.mjs", entry_point = "audio.ts", format = "cjs", output_dir = False, @@ -37,6 +37,22 @@ rollup_bundle( ":audio_lib", "@npm//@rollup/plugin-commonjs", "@npm//@rollup/plugin-node-resolve", + "@npm//@rollup/plugin-replace", + ], +) + +rollup_bundle( + name = "audio_iife_bundle", + config_file = "rollup.config.iife.mjs", + entry_point = "audio.ts", + format = "iife", + output_dir = False, + deps = [ + ":audio_lib", + "@npm//@rollup/plugin-commonjs", + "@npm//@rollup/plugin-node-resolve", + "@npm//@rollup/plugin-replace", + "@npm//@rollup/plugin-terser", ], ) @@ -52,7 +68,8 @@ pkg_npm( deps = [ "wasm/audio_wasm_internal.js", "wasm/audio_wasm_internal.wasm", - ":audio_bundle", + ":audio_cjs_bundle", + ":audio_iife_bundle", ], ) @@ -65,8 +82,8 @@ mediapipe_ts_library( ) rollup_bundle( - name = "text_bundle", - config_file = "rollup.config.mjs", + name = "text_cjs_bundle", + config_file = "rollup.config.cjs.mjs", entry_point = "text.ts", format = "cjs", output_dir = False, @@ -74,6 +91,22 @@ rollup_bundle( ":text_lib", "@npm//@rollup/plugin-commonjs", "@npm//@rollup/plugin-node-resolve", + "@npm//@rollup/plugin-replace", + ], +) + +rollup_bundle( + name = "text_iife_bundle", + config_file = "rollup.config.iife.mjs", + entry_point = "text.ts", + format = "iife", + output_dir = False, + deps = [ + ":text_lib", + "@npm//@rollup/plugin-commonjs", + "@npm//@rollup/plugin-node-resolve", + "@npm//@rollup/plugin-replace", + "@npm//@rollup/plugin-terser", ], ) @@ -89,7 +122,8 @@ pkg_npm( deps = [ "wasm/text_wasm_internal.js", "wasm/text_wasm_internal.wasm", - ":text_bundle", + ":text_cjs_bundle", + ":text_iife_bundle", ], ) @@ -102,8 +136,8 @@ mediapipe_ts_library( ) rollup_bundle( - name = "vision_bundle", - config_file = "rollup.config.mjs", + name = "vision_cjs_bundle", + config_file = "rollup.config.cjs.mjs", entry_point = "vision.ts", format = "cjs", output_dir = False, @@ -111,6 +145,22 @@ rollup_bundle( ":vision_lib", "@npm//@rollup/plugin-commonjs", "@npm//@rollup/plugin-node-resolve", + "@npm//@rollup/plugin-replace", + ], +) + +rollup_bundle( + name = "vision_iife_bundle", + config_file = "rollup.config.iife.mjs", + entry_point = "vision.ts", + format = "iife", + output_dir = False, + deps = [ + ":vision_lib", + "@npm//@rollup/plugin-commonjs", + "@npm//@rollup/plugin-node-resolve", + "@npm//@rollup/plugin-replace", + "@npm//@rollup/plugin-terser", ], ) @@ -126,6 +176,7 @@ pkg_npm( deps = [ "wasm/vision_wasm_internal.js", "wasm/vision_wasm_internal.wasm", - ":vision_bundle", + ":vision_cjs_bundle", + ":vision_iife_bundle", ], ) diff --git a/mediapipe/tasks/web/package.json b/mediapipe/tasks/web/package.json index d7d484ca4..5b2dce245 100644 --- a/mediapipe/tasks/web/package.json +++ b/mediapipe/tasks/web/package.json @@ -2,10 +2,11 @@ "name": "@mediapipe/tasks-__NAME__", "version": "__VERSION__", "description": "__DESCRIPTION__", - "main": "__NAME___bundle.js", - "module": "__NAME___bundle.js", + "main": "__NAME___cjs_bundle.js", + "module": "__NAME___cjs_bundle.js", + "jsdeliver": "__NAME___iife_bundle.js", "exports": { - ".": "./__NAME___bundle.js", + ".": "./__NAME___cjs_bundle.js", "./loader": "./wasm/__NAME___wasm_internal.js", "./wasm": "./wasm/__NAME___wasm_internal.wasm" }, diff --git a/mediapipe/tasks/web/rollup.config.cjs.mjs b/mediapipe/tasks/web/rollup.config.cjs.mjs new file mode 100644 index 000000000..5f8ca1848 --- /dev/null +++ b/mediapipe/tasks/web/rollup.config.cjs.mjs @@ -0,0 +1,15 @@ +import resolve from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; +import replace from '@rollup/plugin-replace'; + +export default { + plugins: [ + // Workaround for https://github.com/protocolbuffers/protobuf-javascript/issues/151 + replace({ + 'var calculator_options_pb = {};': 'var calculator_options_pb = {}; var mediapipe_framework_calculator_options_pb = calculator_options_pb;', + delimiters: ['', ''] + }), + resolve(), + commonjs() + ] +} diff --git a/mediapipe/tasks/web/rollup.config.iife.mjs b/mediapipe/tasks/web/rollup.config.iife.mjs new file mode 100644 index 000000000..1320927aa --- /dev/null +++ b/mediapipe/tasks/web/rollup.config.iife.mjs @@ -0,0 +1,21 @@ +import resolve from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; +import terser from '@rollup/plugin-terser'; +import replace from '@rollup/plugin-replace'; + +export default { + output: { + name: 'bundle', + sourcemap: false + }, + plugins: [ + // Workaround for https://github.com/protocolbuffers/protobuf-javascript/issues/151 + replace({ + 'var calculator_options_pb = {};': 'var calculator_options_pb = {}; var mediapipe_framework_calculator_options_pb = calculator_options_pb;', + delimiters: ['', ''] + }), + resolve({browser: true}), + commonjs(), + terser() + ] +} diff --git a/mediapipe/tasks/web/rollup.config.mjs b/mediapipe/tasks/web/rollup.config.mjs deleted file mode 100644 index 392b235fc..000000000 --- a/mediapipe/tasks/web/rollup.config.mjs +++ /dev/null @@ -1,9 +0,0 @@ -import resolve from '@rollup/plugin-node-resolve'; -import commonjs from '@rollup/plugin-commonjs'; - -export default { - plugins: [ - resolve(), - commonjs() - ] -} diff --git a/package.json b/package.json index 298157cbc..22a035b74 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "@bazel/typescript": "^5.7.1", "@rollup/plugin-commonjs": "^23.0.2", "@rollup/plugin-node-resolve": "^15.0.1", + "@rollup/plugin-replace": "^5.0.1", + "@rollup/plugin-terser": "^0.1.0", "@types/google-protobuf": "^3.15.6", "@types/offscreencanvas": "^2019.7.0", "google-protobuf": "^3.21.2", diff --git a/yarn.lock b/yarn.lock index a5ec6fb13..19c32e322 100644 --- a/yarn.lock +++ b/yarn.lock @@ -31,6 +31,46 @@ dependencies: google-protobuf "^3.6.1" +"@jridgewell/gen-mapping@^0.3.0": + version "0.3.2" + resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz#c1aedc61e853f2bb9f5dfe6d4442d3b565b253b9" + integrity sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A== + dependencies: + "@jridgewell/set-array" "^1.0.1" + "@jridgewell/sourcemap-codec" "^1.4.10" + "@jridgewell/trace-mapping" "^0.3.9" + +"@jridgewell/resolve-uri@3.1.0": + version "3.1.0" + resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.0.tgz#2203b118c157721addfe69d47b70465463066d78" + integrity sha512-F2msla3tad+Mfht5cJq7LSXcdudKTWCVYUgw6pLFOOHSTtZlj6SWNYAp+AhuqLmWdBO2X5hPrLcu8cVP8fy28w== + +"@jridgewell/set-array@^1.0.1": + version "1.1.2" + resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.2.tgz#7c6cf998d6d20b914c0a55a91ae928ff25965e72" + integrity sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw== + +"@jridgewell/source-map@^0.3.2": + version "0.3.2" + resolved "https://registry.yarnpkg.com/@jridgewell/source-map/-/source-map-0.3.2.tgz#f45351aaed4527a298512ec72f81040c998580fb" + integrity sha512-m7O9o2uR8k2ObDysZYzdfhb08VuEml5oWGiosa1VdaPZ/A6QyPkAJuwN0Q1lhULOf6B7MtQmHENS743hWtCrgw== + dependencies: + "@jridgewell/gen-mapping" "^0.3.0" + "@jridgewell/trace-mapping" "^0.3.9" + +"@jridgewell/sourcemap-codec@1.4.14", "@jridgewell/sourcemap-codec@^1.4.10": + version "1.4.14" + resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.14.tgz#add4c98d341472a289190b424efbdb096991bb24" + integrity sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw== + +"@jridgewell/trace-mapping@^0.3.9": + version "0.3.17" + resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.17.tgz#793041277af9073b0951a7fe0f0d8c4c98c36985" + integrity sha512-MCNzAp77qzKca9+W/+I0+sEpaUnZoeasnghNeVc41VZCEKaCH73Vq3BZZ/SzWIgrqE4H4ceI+p+b6C0mHf9T4g== + dependencies: + "@jridgewell/resolve-uri" "3.1.0" + "@jridgewell/sourcemap-codec" "1.4.14" + "@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2": version "1.1.2" resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf" @@ -108,6 +148,21 @@ is-module "^1.0.0" resolve "^1.22.1" +"@rollup/plugin-replace@^5.0.1": + version "5.0.1" + resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-5.0.1.tgz#49a57af3e6df111a9e75dea3f3572741f4c5c83e" + integrity sha512-Z3MfsJ4CK17BfGrZgvrcp/l6WXoKb0kokULO+zt/7bmcyayokDaQ2K3eDJcRLCTAlp5FPI4/gz9MHAsosz4Rag== + dependencies: + "@rollup/pluginutils" "^5.0.1" + magic-string "^0.26.4" + +"@rollup/plugin-terser@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-terser/-/plugin-terser-0.1.0.tgz#7530c0f11667637419d71820461646c418526041" + integrity sha512-N2KK+qUfHX2hBzVzM41UWGLrEmcjVC37spC8R3c9mt3oEDFKh3N2e12/lLp9aVSt86veR0TQiCNQXrm8C6aiUQ== + dependencies: + terser "^5.15.1" + "@rollup/pluginutils@^5.0.1": version "5.0.2" resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-5.0.2.tgz#012b8f53c71e4f6f9cb317e311df1404f56e7a33" @@ -165,7 +220,7 @@ acorn-jsx@^5.3.2: resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937" integrity sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ== -acorn@^8.8.0: +acorn@^8.5.0, acorn@^8.8.0: version "8.8.1" resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.1.tgz#0a3f9cbecc4ec3bea6f0a80b66ae8dd2da250b73" integrity sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA== @@ -244,6 +299,11 @@ color-name@~1.1.4: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== +commander@^2.20.0: + version "2.20.3" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" + integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== + commondir@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" @@ -676,6 +736,14 @@ source-map-support@0.5.9: buffer-from "^1.0.0" source-map "^0.6.0" +source-map-support@~0.5.20: + version "0.5.21" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f" + integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w== + dependencies: + buffer-from "^1.0.0" + source-map "^0.6.0" + source-map@^0.6.0, source-map@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" @@ -708,6 +776,16 @@ taffydb@2.6.2: resolved "https://registry.yarnpkg.com/taffydb/-/taffydb-2.6.2.tgz#7cbcb64b5a141b6a2efc2c5d2c67b4e150b2a268" integrity sha512-y3JaeRSplks6NYQuCOj3ZFMO3j60rTwbuKCvZxsAraGYH2epusatvZ0baZYA01WsGqJBq/Dl6vOrMUJqyMj8kA== +terser@^5.15.1: + version "5.15.1" + resolved "https://registry.yarnpkg.com/terser/-/terser-5.15.1.tgz#8561af6e0fd6d839669c73b92bdd5777d870ed6c" + integrity sha512-K1faMUvpm/FBxjBXud0LWVAGxmvoPbZbfTCYbSgaaYQaIXI3/TdI7a7ZGA73Zrou6Q8Zmz3oeUTsp/dj+ag2Xw== + dependencies: + "@jridgewell/source-map" "^0.3.2" + acorn "^8.5.0" + commander "^2.20.0" + source-map-support "~0.5.20" + tmp@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.2.1.tgz#8457fc3037dcf4719c251367a1af6500ee1ccf14"