Skip to content

Commit

Permalink
fix: resolve module field before main field
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 18, 2021
1 parent 795d065 commit 7bc02d7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cli-main.ts
Expand Up @@ -77,6 +77,9 @@ export async function main(options: Options = {}) {
)
.option('--pure <express>', 'Mark specific expressions as pure')
.option('--metafile', 'Emit esbuild metafile (a JSON file)')
.option('--platform <platform>', 'Target platform', {
default: 'node',
})
.action(async (files: string[], flags) => {
const { build } = await import('.')
Object.assign(options, {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Expand Up @@ -99,19 +99,25 @@ 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,
sourcemap: options.sourcemap,
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',
Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Expand Up @@ -96,4 +96,9 @@ export type Options = {
metafile?: boolean
footer?: BuildOptions['footer']
banner?: BuildOptions['banner']
/**
* Target platform
* @default `node`
*/
platform?: 'node' | 'browser'
}

1 comment on commit 7bc02d7

@vercel
Copy link

@vercel vercel bot commented on 7bc02d7 Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.