diff --git a/.changeset/hip-moles-cheat.md b/.changeset/hip-moles-cheat.md new file mode 100644 index 0000000000..407f2b1d9a --- /dev/null +++ b/.changeset/hip-moles-cheat.md @@ -0,0 +1,5 @@ +--- +'@ice/app': patch +--- + +fix: external node builtin modules diff --git a/packages/ice/src/esbuild/externalNodeBuiltin.ts b/packages/ice/src/esbuild/externalNodeBuiltin.ts new file mode 100644 index 0000000000..d4990382ce --- /dev/null +++ b/packages/ice/src/esbuild/externalNodeBuiltin.ts @@ -0,0 +1,18 @@ +import { isNodeBuiltin } from 'mlly'; +import type { Plugin, PluginBuild } from 'esbuild'; + +const externalBuiltinPlugin = (): Plugin => { + return { + name: 'esbuild-external-node-builtin', + setup(build: PluginBuild) { + build.onResolve({ filter: /.*/ }, (args) => { + const id = args.path; + if (isNodeBuiltin(id)) { + return { path: id, external: true }; + } + }); + }, + }; +}; + +export default externalBuiltinPlugin; diff --git a/packages/ice/src/service/config.ts b/packages/ice/src/service/config.ts index bb44072763..004a5bf1cc 100644 --- a/packages/ice/src/service/config.ts +++ b/packages/ice/src/service/config.ts @@ -8,6 +8,7 @@ import dynamicImport from '../utils/dynamicImport.js'; import formatPath from '../utils/formatPath.js'; import { RUNTIME_TMP_DIR, CACHE_DIR } from '../constant.js'; import { createLogger } from '../utils/logger.js'; +import externalBuiltinPlugin from '../esbuild/externalNodeBuiltin.js'; type GetOutfile = (entry: string, exportNames: string[]) => string; @@ -45,11 +46,12 @@ class Config { const { error } = await serverCompiler({ entryPoints: [entry], format: 'esm', - platform: 'node', - // Don't add banner for config file, it will cause name conflict when bundled by server entry. - banner: undefined, outfile, - plugins: [removeTopLevelCode(keepExports, transformInclude)], + plugins: [ + removeTopLevelCode(keepExports, transformInclude), + // External node builtin modules, such as `fs`, it will be imported by weex document. + externalBuiltinPlugin(), + ], sourcemap: false, logLevel: 'silent', // The main server compiler process will log it. }, {});