Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 8, 2022
0 parents commit cd47d8a
Show file tree
Hide file tree
Showing 42 changed files with 4,828 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test
on:
push:
branches: [develop]
pull_request:
branches: [master, develop, next]
jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version-file: '.nvmrc'
- name: Install dependencies
run: npx ci
- name: Test
run: npm run test
- name: Lint
if: ${{ matrix.os == 'ubuntu-latest' }}
run: npm run lint
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# macOS
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Dependency directories
/node_modules/

# Output of 'npm pack'
*.tgz

# dotenv environment variables file
.env
.env.test

# VSCode
.vscode

# Distribution
dist

# Link config
link.config.json
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.14.2
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# esm-loader

Node.js `import` hook to transform TypeScript to ESM on demand using [esbuild](https://esbuild.github.io/).

### Features
- Converts TypeScript to ESM
- Supports new extensions `.cjs` + `.mjs` (and `.cts` &`.mts`)
- Supports Node.js 12.20.0 and up
- Handles `node:` import prefixes
- Sourcemap support
- Cached for performance boost

> **Tip:**
>
> _esm-loader_ doesn't hook into `require()` calls.
>
> Use this with [cjs-loader](https://github.com/esbuild-kit/cjs-loader) for `require()` support. Alternatively, use [esb](https://github.com/esbuild-kit/esb) to handle them both automatically.
## Install

```sh
npm install --save-dev @esbuild-kit/esm-loader
```

## Usage

Pass `@esbuild/esm-loader` into the [`--loader`](https://nodejs.org/api/cli.html#--experimental-loadermodule) flag.
```sh
node --loader @esbuild/esm-loader ./file.js
```

### TypeScript configuration
The following properties are used from `tsconfig.json` in the working directory:
- `jsxFactory`
- `jsxFragmentFactory`

### Cache
Modules transformations are cached in the system cache directory ([`TMPDIR`](https://en.wikipedia.org/wiki/TMPDIR)). Transforms are cached by content hash so duplicate dependencies are not re-transformed.

Set environment variable `ESBK_DISABLE_CACHE` to a truthy value to disable the cache:

```sh
ESBK_DISABLE_CACHE=1 node --loader @esbuild/esm-loader ./file.js
```

## FAQ

### Can it import JSON modules?
Yes. This loader enables importing native [JSON modules](https://nodejs.org/api/esm.html#json-modules).

### Can it import ESM modules over network?

Node.js has built-in support for network imports [behind the `--experimental-network-imports` flag](https://nodejs.org/api/esm.html#network-based-loading-is-not-enabled-by-default).

You can pass it in with `esm-loader`:

```sh
node --loader @esbuild/esm-loader --experimental-network-imports ./file.js
```

### Can it resolve files without an extension?

In ESM, import paths must be explicit (must include file name and extension).

For backwards compatibility, this loader adds support for classic Node resolution for extensions: `.js`, `.json`, `.ts`, `.tsx`, `.jsx`. Resolving a `index` file by the directory name works too.

```js
import file from './file' // -> ./file.js
import directory from './directory' // -> ./directory/index.js
```

## Related

- [@esbuild-kit/esb](https://github.com/esbuild-kit/esb) - Node.js runtime powered by esbuild using `@esbuild-kit/cjs-loader` and `@esbuild-kit/esb-loader`.

- [@esbuild-kit/cjs-loader](https://github.com/esbuild-kit/cjs-loader) - TypeScript & ESM to CJS transpiler using the Node.js loader API.
64 changes: 64 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@esbuild-kit/esm-loader",
"version": "0.0.0-semantic-release",
"description": "Node.js loader for compiling TypeScript modules to ESM",
"keywords": [
"esbuild",
"loader",
"node",
"esm",
"typescript"
],
"license": "MIT",
"repository": "esbuild-kit/esm-loader",
"author": {
"name": "Hiroki Osame",
"email": "hiroki.osame@gmail.com"
},
"type": "module",
"files": [
"dist"
],
"main": "./dist/index.js",
"exports": "./dist/index.js",
"scripts": {
"build": "pkgroll --minify --target=node12.19",
"lint": "eslint .",
"pretest": "npm run build",
"test": "node --loader @esbuild-kit/esm-loader tests"
},
"dependencies": {
"@esbuild-kit/core-utils": "github:esbuild-kit/core-utils#built/develop",
"es-module-lexer": "^0.10.5",
"get-tsconfig": "^3.0.1"
},
"devDependencies": {
"@pvtnbr/eslint-config": "^0.21.0",
"@types/node": "^17.0.31",
"@types/semver": "^7.3.9",
"eslint": "^8.15.0",
"execa": "^6.1.0",
"get-node": "^12.1.0",
"manten": "^0.0.3",
"pkgroll": "^1.2.2",
"semver": "^7.3.7",
"typescript": "^4.6.4"
},
"eslintConfig": {
"extends": "@pvtnbr",
"ignorePatterns": [
"tests/fixtures"
],
"rules": {
"import/no-unresolved": "off",
"@typescript-eslint/no-shadow": [
"error",
{
"allow": [
"describe"
]
}
]
}
}
}
Loading

0 comments on commit cd47d8a

Please sign in to comment.