From 7bc02d7f226bfafe33777c9ab536fa35f0a5e945 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Thu, 18 Nov 2021 11:34:50 +0800 Subject: [PATCH] fix: resolve `module` field before `main` field --- src/cli-main.ts | 3 +++ src/index.ts | 8 +++++++- src/options.ts | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cli-main.ts b/src/cli-main.ts index ce9fc402..07219bb7 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -77,6 +77,9 @@ export async function main(options: Options = {}) { ) .option('--pure ', 'Mark specific expressions as pure') .option('--metafile', 'Emit esbuild metafile (a JSON file)') + .option('--platform ', 'Target platform', { + default: 'node', + }) .action(async (files: string[], flags) => { const { build } = await import('.') Object.assign(options, { diff --git a/src/index.ts b/src/index.ts index e62fb57b..b1fb4939 100644 --- a/src/index.ts +++ b/src/index.ts @@ -99,12 +99,14 @@ export async function runEsbuild( ? options.splitting : format === 'esm' + const platform = options.platform || 'node' + try { result = await esbuild({ entryPoints: options.entryPoints, format: format === 'cjs' && splitting ? 'esm' : format, bundle: typeof options.bundle === 'undefined' ? true : options.bundle, - platform: 'node', + platform, globalName: options.globalName, jsxFactory: options.jsxFactory, jsxFragment: options.jsxFragment, @@ -112,6 +114,10 @@ export async function runEsbuild( target: options.target === 'es5' ? 'es2016' : options.target, footer: options.footer, banner: options.banner, + mainFields: + platform === 'node' + ? ['module', 'main'] + : ['browser', 'module', 'main'], plugins: [ { name: 'modify-options', diff --git a/src/options.ts b/src/options.ts index 9df7e547..3c974a21 100644 --- a/src/options.ts +++ b/src/options.ts @@ -96,4 +96,9 @@ export type Options = { metafile?: boolean footer?: BuildOptions['footer'] banner?: BuildOptions['banner'] + /** + * Target platform + * @default `node` + */ + platform?: 'node' | 'browser' }