diff --git a/bin/wxt.cjs b/bin/wxt.cjs deleted file mode 100755 index d894e69c..00000000 --- a/bin/wxt.cjs +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env node -require('../dist/cli.cjs'); diff --git a/bin/wxt.mjs b/bin/wxt.mjs new file mode 100755 index 00000000..7c3d1548 --- /dev/null +++ b/bin/wxt.mjs @@ -0,0 +1,2 @@ +#!/usr/bin/env node +import '../dist/cli.js'; diff --git a/package.json b/package.json index a0365599..fbf06a2c 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "bin", "dist" ], - "bin": "./bin/wxt.cjs", + "bin": "./bin/wxt.mjs", "main": "./dist/index.cjs", "module": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/scripts/build.ts b/scripts/build.ts index db35122c..499b7c4f 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -93,7 +93,7 @@ const config: tsup.Options[] = [ entry: { cli: 'src/cli.ts', }, - format: ['cjs'], + format: ['esm'], }, ]; diff --git a/src/core/utils/building/get-internal-config.ts b/src/core/utils/building/get-internal-config.ts index 3663e103..7a835ea5 100644 --- a/src/core/utils/building/get-internal-config.ts +++ b/src/core/utils/building/get-internal-config.ts @@ -37,6 +37,9 @@ export async function getInternalConfig( name: 'wxt', cwd: inlineConfig.root ?? process.cwd(), rcFile: false, + jitiOptions: { + esmResolve: true, + }, }); userConfig = loadedConfig ?? {}; userConfigMetadata = metadata; diff --git a/src/core/utils/building/import-entrypoint.ts b/src/core/utils/building/import-entrypoint.ts index 1b745a43..9fbc6272 100644 --- a/src/core/utils/building/import-entrypoint.ts +++ b/src/core/utils/building/import-entrypoint.ts @@ -2,11 +2,12 @@ import createJITI, { TransformOptions as JitiTransformOptions } from 'jiti'; import { InternalConfig } from '~/types'; import { createUnimport } from 'unimport'; import fs from 'fs-extra'; -import { resolve } from 'path'; +import { resolve } from 'node:path'; import { getUnimportOptions } from '~/core/utils/unimport'; import { removeProjectImportStatements } from '~/core/utils/strings'; import { normalizePath } from '~/core/utils/paths'; import { TransformOptions, transformSync } from 'esbuild'; +import { fileURLToPath } from 'node:url'; /** * Get the value from the default export of a `path`. @@ -45,30 +46,44 @@ export async function importEntrypointFile( ['Text:', text, 'No imports:', textNoImports, 'Code:', code].join('\n'), ); - const jiti = createJITI(__filename, { - cache: false, - debug: config.debug, - esmResolve: true, - alias: { - 'webextension-polyfill': resolve( - config.root, - 'node_modules/wxt/dist/virtual/mock-browser.js', - ), + const jiti = createJITI( + typeof __filename !== 'undefined' + ? __filename + : fileURLToPath(import.meta.url), + { + cache: false, + debug: config.debug, + esmResolve: true, + alias: { + 'webextension-polyfill': resolve( + config.root, + 'node_modules/wxt/dist/virtual/mock-browser.js', + ), + }, + // Continue using node to load TS files even if `bun run --bun` is detected. Jiti does not + // respect the custom transform function when using it's native bun option. + experimentalBun: false, + // List of extensions to transform with esbuild + extensions: [ + '.ts', + '.cts', + '.mts', + '.tsx', + '.js', + '.cjs', + '.mjs', + '.jsx', + ], + transform(opts) { + const isEntrypoint = opts.filename === normalPath; + return transformSync( + // Use modified source code for entrypoints + isEntrypoint ? code : opts.source, + getEsbuildOptions(opts), + ); + }, }, - // Continue using node to load TS files even if `bun run --bun` is detected. Jiti does not - // respect the custom transform function when using it's native bun option. - experimentalBun: false, - // List of extensions to transform with esbuild - extensions: ['.ts', '.cts', '.mts', '.tsx', '.js', '.cjs', '.mjs', '.jsx'], - transform(opts) { - const isEntrypoint = opts.filename === normalPath; - return transformSync( - // Use modified source code for entrypoints - isEntrypoint ? code : opts.source, - getEsbuildOptions(opts), - ); - }, - }); + ); try { const res = await jiti(path);