Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ node -r esbuild-register file.ts

It will use `jsxFactory`, `jsxFragmentFactory` and `target` options from your `tsconfig.json`

You can also pass a JSON string to specify configuration options for esbuild, such as if I want to pass in the banner configuration.

```bash
node -r esbuild-register file.ts --esbuild-config="{\"banner\": \"console.log('is banner')\"}"
```
Comment on lines +23 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like this idea, but personally would prefer an option to specify a config-file that exports an object matching the esbuild config options.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok, I'll resubmit when I have time, thanks for the reply

@ndelangen ndelangen Feb 5, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm not the package author @seho-dev, so maybe wait to see if they agree with my person opinion :)


### Experimental loader support

When using in a project with `type: "module"` in `package.json`, you need the `--loader` flag to load TypeScript files:
Expand Down
43 changes: 43 additions & 0 deletions register.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
const ESBUILD_CONFIG_NAME = '--esbuild-config'

// parse process.argv.slice(2) and return an object
const parseArgs = (argv) => {
const args = {}
argv.forEach(arg => {
const index = arg.indexOf('=')
if (index !== -1) {
const key = arg.slice(0, index)
const value = arg.slice(index + 1)
args[key] = value
}
})
return args
}

const args = parseArgs(process.argv.slice(2));

// Determine if a string is json
const isJson = (str) => {
let result;
try {
result = JSON.parse(str);
} catch (e) {
return false;
}
if (typeof result !== 'object' || Array.isArray(result) || result === null) {
return false;
}
return result;
}

let isJsonConfig;

if (typeof args[ESBUILD_CONFIG_NAME] === 'string') {
isJsonConfig = isJson(args[ESBUILD_CONFIG_NAME]);
if (!isJsonConfig) {
// not json
console.log('esbuild-config is not json');
}
}

require('./dist/node.js').register({
target: `node${process.version.slice(1)}`,
...(isJsonConfig || {}),
})