From b5d7a000badf72c8051864c8040e19227e4aa73e Mon Sep 17 00:00:00 2001 From: ambar Date: Thu, 11 Apr 2024 14:49:06 +0800 Subject: [PATCH] feat: mark node modules as external --- package.json | 1 + src/extension/bundle.ts | 17 ++++++++++---- yarn.lock | 52 ++++++++++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index d171683..7f61ef4 100644 --- a/package.json +++ b/package.json @@ -181,6 +181,7 @@ "@vscode/webview-ui-toolkit": "^1.4.0", "console-feed": "^3.6.0", "es-jest": "^2.1.0", + "esbuild-node-externals": "^1.13.0", "is-promise": "^4.0.0", "jest": "^29.7.0", "ldrs": "^1.0.1", diff --git a/src/extension/bundle.ts b/src/extension/bundle.ts index 3639cc0..3d80b18 100644 --- a/src/extension/bundle.ts +++ b/src/extension/bundle.ts @@ -1,6 +1,7 @@ import path from 'path' import type * as Esbuild from 'esbuild' import {fetchBuilder, MemoryCache} from 'node-fetch-cache' +import {nodeExternalsPlugin} from 'esbuild-node-externals' // @ts-expect-error ESM import httpPlugin from '../../scripts/httpPlugin.mjs' @@ -99,10 +100,18 @@ export default async function bundle( }, // ensure there is only one copy of React, see https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react // external: isWeb ? ['react', 'react-dom'] : [], - plugins: [ - isNode && httpPlugin({fetch}), - isWeb && externalGlobalPlugin({react: 'React', 'react-dom': 'ReactDOM'}), - ].filter(Boolean), + plugins: isNode + ? [ + // + httpPlugin({fetch}) as unknown as Esbuild.Plugin, + nodeExternalsPlugin(), + ] + : isWeb + ? [ + // + externalGlobalPlugin({react: 'React', 'react-dom': 'ReactDOM'}), + ] + : [], }) const outputs = result.outputFiles const js = outputs.find((x) => x.path.endsWith('.js'))?.text diff --git a/yarn.lock b/yarn.lock index 642e43f..d35610d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2388,6 +2388,18 @@ __metadata: languageName: node linkType: hard +"esbuild-node-externals@npm:^1.13.0": + version: 1.13.0 + resolution: "esbuild-node-externals@npm:1.13.0" + dependencies: + find-up: ^5.0.0 + tslib: ^2.4.1 + peerDependencies: + esbuild: 0.12 - 0.20 + checksum: 516303edeb5b8124cbd2d9b33bcbeeb3fe0dfe38eb3183b253793862ecb889badcb87abde8ebb415e9c33292032c2c71bd0f9f713d38083c65d4ae6d615628fd + languageName: node + linkType: hard + "esbuild@npm:^0, esbuild@npm:^0.20.2": version: 0.20.2 resolution: "esbuild@npm:0.20.2" @@ -2783,6 +2795,16 @@ __metadata: languageName: node linkType: hard +"find-up@npm:^5.0.0": + version: 5.0.0 + resolution: "find-up@npm:5.0.0" + dependencies: + locate-path: ^6.0.0 + path-exists: ^4.0.0 + checksum: 07955e357348f34660bde7920783204ff5a26ac2cafcaa28bace494027158a97b9f56faaf2d89a6106211a8174db650dd9f503f9c0d526b1202d5554a00b9095 + languageName: node + linkType: hard + "fs-minipass@npm:^2.0.0": version: 2.1.0 resolution: "fs-minipass@npm:2.1.0" @@ -3868,6 +3890,7 @@ __metadata: console-feed: ^3.6.0 es-jest: ^2.1.0 esbuild: ^0.20.2 + esbuild-node-externals: ^1.13.0 is-promise: ^4.0.0 jest: ^29.7.0 ldrs: ^1.0.1 @@ -3893,6 +3916,15 @@ __metadata: languageName: node linkType: hard +"locate-path@npm:^6.0.0": + version: 6.0.0 + resolution: "locate-path@npm:6.0.0" + dependencies: + p-locate: ^5.0.0 + checksum: 72eb661788a0368c099a184c59d2fee760b3831c9c1c33955e8a19ae4a21b4116e53fa736dc086cdeb9fce9f7cc508f2f92d2d3aae516f133e16a2bb59a39f5a + languageName: node + linkType: hard + "locko@npm:0.0.3": version: 0.0.3 resolution: "locko@npm:0.0.3" @@ -4280,7 +4312,7 @@ __metadata: languageName: node linkType: hard -"p-limit@npm:^3.1.0": +"p-limit@npm:^3.0.2, p-limit@npm:^3.1.0": version: 3.1.0 resolution: "p-limit@npm:3.1.0" dependencies: @@ -4298,6 +4330,15 @@ __metadata: languageName: node linkType: hard +"p-locate@npm:^5.0.0": + version: 5.0.0 + resolution: "p-locate@npm:5.0.0" + dependencies: + p-limit: ^3.0.2 + checksum: 1623088f36cf1cbca58e9b61c4e62bf0c60a07af5ae1ca99a720837356b5b6c5ba3eb1b2127e47a06865fee59dd0453cad7cc844cda9d5a62ac1a5a51b7c86d3 + languageName: node + linkType: hard + "p-map@npm:^4.0.0": version: 4.0.0 resolution: "p-map@npm:4.0.0" @@ -5123,14 +5164,7 @@ __metadata: languageName: node linkType: hard -"tslib@npm:^2.0.1": - version: 2.3.1 - resolution: "tslib@npm:2.3.1" - checksum: de17a98d4614481f7fcb5cd53ffc1aaf8654313be0291e1bfaee4b4bb31a20494b7d218ff2e15017883e8ea9626599b3b0e0229c18383ba9dce89da2adf15cb9 - languageName: node - linkType: hard - -"tslib@npm:^2.6.2": +"tslib@npm:^2.0.1, tslib@npm:^2.4.1, tslib@npm:^2.6.2": version: 2.6.2 resolution: "tslib@npm:2.6.2" checksum: 329ea56123005922f39642318e3d1f0f8265d1e7fcb92c633e0809521da75eeaca28d2cf96d7248229deb40e5c19adf408259f4b9640afd20d13aecc1430f3ad