From e66e88802c42610441dd9acfd193a9ff8e022231 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 16 Nov 2022 18:32:59 -0800 Subject: [PATCH] Change NPM Bundle to ESM PiperOrigin-RevId: 489091370 --- mediapipe/tasks/web/BUILD | 80 ++++++------------- mediapipe/tasks/web/audio.ts | 8 +- mediapipe/tasks/web/audio/BUILD | 12 --- mediapipe/tasks/web/audio/index.ts | 17 ---- mediapipe/tasks/web/package.json | 12 +-- mediapipe/tasks/web/rollup.config.iife.mjs | 21 ----- ...ollup.config.cjs.mjs => rollup.config.mjs} | 4 +- mediapipe/tasks/web/text.ts | 10 ++- mediapipe/tasks/web/text/BUILD | 13 --- mediapipe/tasks/web/text/index.ts | 18 ----- mediapipe/tasks/web/vision.ts | 22 ++++- mediapipe/tasks/web/vision/BUILD | 16 ---- mediapipe/tasks/web/vision/index.ts | 21 ----- 13 files changed, 67 insertions(+), 187 deletions(-) delete mode 100644 mediapipe/tasks/web/audio/index.ts delete mode 100644 mediapipe/tasks/web/rollup.config.iife.mjs rename mediapipe/tasks/web/{rollup.config.cjs.mjs => rollup.config.mjs} (86%) delete mode 100644 mediapipe/tasks/web/text/index.ts delete mode 100644 mediapipe/tasks/web/vision/index.ts diff --git a/mediapipe/tasks/web/BUILD b/mediapipe/tasks/web/BUILD index b8777e785..e9703e37a 100644 --- a/mediapipe/tasks/web/BUILD +++ b/mediapipe/tasks/web/BUILD @@ -24,35 +24,25 @@ mediapipe_files(srcs = [ mediapipe_ts_library( name = "audio_lib", srcs = ["audio.ts"], - deps = ["//mediapipe/tasks/web/audio:audio_lib"], -) - -rollup_bundle( - name = "audio_cjs_bundle", - config_file = "rollup.config.cjs.mjs", - entry_point = "audio.ts", - format = "cjs", - output_dir = False, deps = [ - ":audio_lib", - "@npm//@rollup/plugin-commonjs", - "@npm//@rollup/plugin-node-resolve", - "@npm//@rollup/plugin-replace", + "//mediapipe/tasks/web/audio/audio_classifier", ], ) rollup_bundle( - name = "audio_iife_bundle", - config_file = "rollup.config.iife.mjs", + name = "audio_bundle", + config_file = "rollup.config.mjs", entry_point = "audio.ts", - format = "iife", + format = "esm", output_dir = False, + sourcemap = "false", deps = [ ":audio_lib", "@npm//@rollup/plugin-commonjs", "@npm//@rollup/plugin-node-resolve", "@npm//@rollup/plugin-replace", "@npm//@rollup/plugin-terser", + "@npm//google-protobuf", ], ) @@ -69,8 +59,7 @@ pkg_npm( deps = [ "wasm/audio_wasm_internal.js", "wasm/audio_wasm_internal.wasm", - ":audio_cjs_bundle", - ":audio_iife_bundle", + ":audio_bundle", ], ) @@ -79,35 +68,26 @@ pkg_npm( mediapipe_ts_library( name = "text_lib", srcs = ["text.ts"], - deps = ["//mediapipe/tasks/web/text:text_lib"], -) - -rollup_bundle( - name = "text_cjs_bundle", - config_file = "rollup.config.cjs.mjs", - entry_point = "text.ts", - format = "cjs", - output_dir = False, deps = [ - ":text_lib", - "@npm//@rollup/plugin-commonjs", - "@npm//@rollup/plugin-node-resolve", - "@npm//@rollup/plugin-replace", + "//mediapipe/tasks/web/text/text_classifier", + "//mediapipe/tasks/web/text/text_embedder", ], ) rollup_bundle( - name = "text_iife_bundle", - config_file = "rollup.config.iife.mjs", + name = "text_bundle", + config_file = "rollup.config.mjs", entry_point = "text.ts", - format = "iife", + format = "esm", output_dir = False, + sourcemap = "false", deps = [ ":text_lib", "@npm//@rollup/plugin-commonjs", "@npm//@rollup/plugin-node-resolve", "@npm//@rollup/plugin-replace", "@npm//@rollup/plugin-terser", + "@npm//google-protobuf", ], ) @@ -124,8 +104,7 @@ pkg_npm( deps = [ "wasm/text_wasm_internal.js", "wasm/text_wasm_internal.wasm", - ":text_cjs_bundle", - ":text_iife_bundle", + ":text_bundle", ], ) @@ -134,35 +113,29 @@ pkg_npm( mediapipe_ts_library( name = "vision_lib", srcs = ["vision.ts"], - deps = ["//mediapipe/tasks/web/vision:vision_lib"], -) - -rollup_bundle( - name = "vision_cjs_bundle", - config_file = "rollup.config.cjs.mjs", - entry_point = "vision.ts", - format = "cjs", - output_dir = False, deps = [ - ":vision_lib", - "@npm//@rollup/plugin-commonjs", - "@npm//@rollup/plugin-node-resolve", - "@npm//@rollup/plugin-replace", + "//mediapipe/tasks/web/vision/gesture_recognizer", + "//mediapipe/tasks/web/vision/hand_landmarker", + "//mediapipe/tasks/web/vision/image_classifier", + "//mediapipe/tasks/web/vision/image_embedder", + "//mediapipe/tasks/web/vision/object_detector", ], ) rollup_bundle( - name = "vision_iife_bundle", - config_file = "rollup.config.iife.mjs", + name = "vision_bundle", + config_file = "rollup.config.mjs", entry_point = "vision.ts", - format = "iife", + format = "esm", output_dir = False, + sourcemap = "false", deps = [ ":vision_lib", "@npm//@rollup/plugin-commonjs", "@npm//@rollup/plugin-node-resolve", "@npm//@rollup/plugin-replace", "@npm//@rollup/plugin-terser", + "@npm//google-protobuf", ], ) @@ -179,7 +152,6 @@ pkg_npm( deps = [ "wasm/vision_wasm_internal.js", "wasm/vision_wasm_internal.wasm", - ":vision_cjs_bundle", - ":vision_iife_bundle", + ":vision_bundle", ], ) diff --git a/mediapipe/tasks/web/audio.ts b/mediapipe/tasks/web/audio.ts index 4a3b80594..764fd8393 100644 --- a/mediapipe/tasks/web/audio.ts +++ b/mediapipe/tasks/web/audio.ts @@ -14,4 +14,10 @@ * limitations under the License. */ -export * from '../../tasks/web/audio/index'; +import {AudioClassifier as AudioClassifierImpl} from '../../tasks/web/audio/audio_classifier/audio_classifier'; + +// Declare the variables locally so that Rollup in OSS includes them explcilty +// as exports. +const AudioClassifier = AudioClassifierImpl; + +export {AudioClassifier}; diff --git a/mediapipe/tasks/web/audio/BUILD b/mediapipe/tasks/web/audio/BUILD index 4f6e48b28..69b0408e9 100644 --- a/mediapipe/tasks/web/audio/BUILD +++ b/mediapipe/tasks/web/audio/BUILD @@ -1,13 +1 @@ # This contains the MediaPipe Audio Tasks. - -load("//mediapipe/framework/port:build_config.bzl", "mediapipe_ts_library") - -package(default_visibility = ["//mediapipe/tasks:internal"]) - -mediapipe_ts_library( - name = "audio_lib", - srcs = ["index.ts"], - deps = [ - "//mediapipe/tasks/web/audio/audio_classifier", - ], -) diff --git a/mediapipe/tasks/web/audio/index.ts b/mediapipe/tasks/web/audio/index.ts deleted file mode 100644 index a5083b326..000000000 --- a/mediapipe/tasks/web/audio/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2022 The MediaPipe Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export * from '../../../tasks/web/audio/audio_classifier/audio_classifier'; diff --git a/mediapipe/tasks/web/package.json b/mediapipe/tasks/web/package.json index 1870f18a6..89c9a599e 100644 --- a/mediapipe/tasks/web/package.json +++ b/mediapipe/tasks/web/package.json @@ -2,20 +2,10 @@ "name": "@mediapipe/tasks-__NAME__", "version": "__VERSION__", "description": "__DESCRIPTION__", - "main": "__NAME___cjs_bundle.js", - "module": "__NAME___cjs_bundle.js", - "jsdeliver": "__NAME___iife_bundle.js", - "exports": { - ".": "./__NAME___cjs_bundle.js", - "./loader": "./wasm/__NAME___wasm_internal.js", - "./wasm": "./wasm/__NAME___wasm_internal.wasm" - }, + "main": "__NAME___bundle.js", "author": "mediapipe@google.com", "license": "Apache-2.0", "types": "__TYPES__", - "dependencies": { - "google-protobuf": "^3.21.2" - }, "homepage": "http://mediapipe.dev", "keywords": [ "AR", "ML", "Augmented", "MediaPipe", "MediaPipe Tasks" ] } diff --git a/mediapipe/tasks/web/rollup.config.iife.mjs b/mediapipe/tasks/web/rollup.config.iife.mjs deleted file mode 100644 index 1320927aa..000000000 --- a/mediapipe/tasks/web/rollup.config.iife.mjs +++ /dev/null @@ -1,21 +0,0 @@ -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.cjs.mjs b/mediapipe/tasks/web/rollup.config.mjs similarity index 86% rename from mediapipe/tasks/web/rollup.config.cjs.mjs rename to mediapipe/tasks/web/rollup.config.mjs index 5f8ca1848..e633bf702 100644 --- a/mediapipe/tasks/web/rollup.config.cjs.mjs +++ b/mediapipe/tasks/web/rollup.config.mjs @@ -1,6 +1,7 @@ import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import replace from '@rollup/plugin-replace'; +import terser from '@rollup/plugin-terser'; export default { plugins: [ @@ -10,6 +11,7 @@ export default { delimiters: ['', ''] }), resolve(), - commonjs() + commonjs(), + terser() ] } diff --git a/mediapipe/tasks/web/text.ts b/mediapipe/tasks/web/text.ts index f8a0b6457..39d101237 100644 --- a/mediapipe/tasks/web/text.ts +++ b/mediapipe/tasks/web/text.ts @@ -14,4 +14,12 @@ * limitations under the License. */ -export * from '../../tasks/web/text/index'; +import {TextClassifier as TextClassifierImpl} from '../../tasks/web/text/text_classifier/text_classifier'; +import {TextEmbedder as TextEmbedderImpl} from '../../tasks/web/text/text_embedder/text_embedder'; + +// Declare the variables locally so that Rollup in OSS includes them explcilty +// as exports. +const TextClassifier = TextClassifierImpl; +const TextEmbedder = TextEmbedderImpl; + +export {TextClassifier, TextEmbedder}; diff --git a/mediapipe/tasks/web/text/BUILD b/mediapipe/tasks/web/text/BUILD index 4b465b0f5..edd23c7d4 100644 --- a/mediapipe/tasks/web/text/BUILD +++ b/mediapipe/tasks/web/text/BUILD @@ -1,14 +1 @@ # This contains the MediaPipe Text Tasks. - -load("//mediapipe/framework/port:build_config.bzl", "mediapipe_ts_library") - -package(default_visibility = ["//mediapipe/tasks:internal"]) - -mediapipe_ts_library( - name = "text_lib", - srcs = ["index.ts"], - deps = [ - "//mediapipe/tasks/web/text/text_classifier", - "//mediapipe/tasks/web/text/text_embedder", - ], -) diff --git a/mediapipe/tasks/web/text/index.ts b/mediapipe/tasks/web/text/index.ts deleted file mode 100644 index d50db209c..000000000 --- a/mediapipe/tasks/web/text/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Copyright 2022 The MediaPipe Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export * from '../../../tasks/web/text/text_classifier/text_classifier'; -export * from '../../../tasks/web/text/text_embedder/text_embedder'; diff --git a/mediapipe/tasks/web/vision.ts b/mediapipe/tasks/web/vision.ts index 6ff8f725b..4e4fab43f 100644 --- a/mediapipe/tasks/web/vision.ts +++ b/mediapipe/tasks/web/vision.ts @@ -14,4 +14,24 @@ * limitations under the License. */ -export * from '../../tasks/web/vision/index'; +import {GestureRecognizer as GestureRecognizerImpl} from '../../tasks/web/vision/gesture_recognizer/gesture_recognizer'; +import {HandLandmarker as HandLandmarkerImpl} from '../../tasks/web/vision/hand_landmarker/hand_landmarker'; +import {ImageClassifier as ImageClassifierImpl} from '../../tasks/web/vision/image_classifier/image_classifier'; +import {ImageEmbedder as ImageEmbedderImpl} from '../../tasks/web/vision/image_embedder/image_embedder'; +import {ObjectDetector as ObjectDetectorImpl} from '../../tasks/web/vision/object_detector/object_detector'; + +// Declare the variables locally so that Rollup in OSS includes them explcilty +// as exports. +const GestureRecognizer = GestureRecognizerImpl; +const HandLandmarker = HandLandmarkerImpl; +const ImageClassifier = ImageClassifierImpl; +const ImageEmbedder = ImageEmbedderImpl; +const ObjectDetector = ObjectDetectorImpl; + +export { + GestureRecognizer, + HandLandmarker, + ImageClassifier, + ImageEmbedder, + ObjectDetector +}; diff --git a/mediapipe/tasks/web/vision/BUILD b/mediapipe/tasks/web/vision/BUILD index 3c45fbfa6..7267744e2 100644 --- a/mediapipe/tasks/web/vision/BUILD +++ b/mediapipe/tasks/web/vision/BUILD @@ -1,17 +1 @@ # This contains the MediaPipe Vision Tasks. - -load("//mediapipe/framework/port:build_config.bzl", "mediapipe_ts_library") - -package(default_visibility = ["//mediapipe/tasks:internal"]) - -mediapipe_ts_library( - name = "vision_lib", - srcs = ["index.ts"], - deps = [ - "//mediapipe/tasks/web/vision/gesture_recognizer", - "//mediapipe/tasks/web/vision/hand_landmarker", - "//mediapipe/tasks/web/vision/image_classifier", - "//mediapipe/tasks/web/vision/image_embedder", - "//mediapipe/tasks/web/vision/object_detector", - ], -) diff --git a/mediapipe/tasks/web/vision/index.ts b/mediapipe/tasks/web/vision/index.ts deleted file mode 100644 index d68c00cc7..000000000 --- a/mediapipe/tasks/web/vision/index.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Copyright 2022 The MediaPipe Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export * from '../../../tasks/web/vision/image_classifier/image_classifier'; -export * from '../../../tasks/web/vision/image_embedder/image_embedder'; -export * from '../../../tasks/web/vision/gesture_recognizer/gesture_recognizer'; -export * from '../../../tasks/web/vision/hand_landmarker/hand_landmarker'; -export * from '../../../tasks/web/vision/object_detector/object_detector';