Skip to content

Commit

Permalink
feat: add --import
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious committed May 22, 2024
1 parent d63ee3c commit d2345d3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- **feat**: Shim `import.meta.{dirname,filename}`.
- **refactor**: Use `"type": "module"` and change main file extension to `.js`.
- **refactor**: Use `--import` instead of `--loader` when possible.

## 0.10.5

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $ esbuild-dev -p:./plugin.ts main.ts
# build and run main.ts, with plugin from file ./plugin.ts
# longhand: --plugin

$ esbuild-dev --loader main.ts
$ esbuild-dev --import main.ts
# run main.ts with esm loader
# in this mode, --cjs, --watch and --plugin are not supported.
```
Expand Down
2 changes: 2 additions & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const EsbuildFlags: readonly FlagConfig[] = [
export const EsbuildDevFlags: readonly FlagConfig[] = [
["no-warnings", boolean],
["loader", boolean],
["import", boolean],
["cjs", boolean],
["shims", boolean],
["watch", boolean, { alias: ["w"] }],
Expand All @@ -108,6 +109,7 @@ export const EsbuildDevFlags: readonly FlagConfig[] = [
export interface EsbuildDevOptions {
noWarnings?: boolean;
loader?: boolean;
import?: boolean;
cjs?: boolean;
shims?: boolean;
watch?: boolean;
Expand Down
5 changes: 5 additions & 0 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function defaultCommand(entry: string, argsBeforeEntry: string[], a

const devOptions = { shims: true, ...devOptionsRaw } as EsbuildDevOptions;
const buildOptions = buildOptionsRaw as BuildOptions & { format: Format };
if (devOptions.import) devOptions.loader = true;
if (devOptions.loader && (devOptions.cjs || devOptions.plugin || devOptions.watch || devOptions.include)) {
throw new Error(`--cjs, --plugin, --watch and --include are not supported with --loader`);
}
Expand All @@ -35,7 +36,11 @@ export async function defaultCommand(entry: string, argsBeforeEntry: string[], a

let spawnArgs: string[];
if (devOptions.loader) {
const v = process.versions.node.split(".").map(e => Number.parseInt(e));
const register = v[0] > 20 || (v[0] === 20 && v[1] >= 6) || (v[0] === 18 && v[1] >= 19);

spawnArgs = ["--loader", loaderPath, "--enable-source-maps", entry, ...argsToEntry];
if (register) spawnArgs[0] = "--import";
if (devOptions.node) spawnArgs.splice(3, 0, ...devOptions.node);
if (devOptions.noWarnings) spawnArgs.unshift("--no-warnings");

Expand Down
8 changes: 4 additions & 4 deletions src/help.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

Usage:
esbuild-dev [--loader] [--cjs] [--watch] [--plugin:name] main.ts ...
esbuild-dev [--import] [--cjs] [--watch] [--plugin:name] main.ts ...
esbuild-dev external [--bare] main.ts ...
esbuild-dev temp

Options:
--loader Use `--loader` to run the file, which is helpful
when using other coverage tools.
--import Use `--import` to run the file, which is helpful
--loader when using other coverage tools.
In this mode, plugins are not supported.

--no-warnings Using experimental loader api will cause node
Expand All @@ -16,7 +16,7 @@ Options:
--cjs Change the outfile format to CJS. For example,
`__dirname` can only be used in CJS, and
`import.meta` can only be accessed in ESM.
This option cannot be used with `--loader`.
This option cannot be used with `--import`.

--shims Replace `import.meta.url` and `__dirname` with absolute
path in .[tj]s files. This is enabled by default.
Expand Down
6 changes: 6 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import module from "module";
import { Loader as EsbuildLoader, PartialMessage, formatMessages, transform } from "esbuild";
import { readFile } from "fs/promises";
import { dirname, extname } from "path";
Expand Down Expand Up @@ -140,3 +141,8 @@ export async function load(url: string, context: LoadContext, defaultLoad: Loade

return defaultLoad(url, context, defaultLoad);
}

if (module.register) {
process.setSourceMapsEnabled(true);
module.register(`./loader.js?${Date.now()}`, import.meta.url);
}

0 comments on commit d2345d3

Please sign in to comment.