Skip to content

Commit

Permalink
fix: external node builtin modules (#6088)
Browse files Browse the repository at this point in the history
* fix: external node builtin modules

* fix: external node builtin modules
  • Loading branch information
ClarkXia authored Mar 20, 2023
1 parent 4a73cb2 commit 1ef827b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-moles-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/app': patch
---

fix: external node builtin modules
18 changes: 18 additions & 0 deletions packages/ice/src/esbuild/externalNodeBuiltin.ts
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 6 additions & 4 deletions packages/ice/src/service/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
}, {});
Expand Down

0 comments on commit 1ef827b

Please sign in to comment.