Skip to content

Commit

Permalink
feat(register): experimental esm loader (#643)
Browse files Browse the repository at this point in the history
* feat: experimental esm loader

* Add tests

* ci
  • Loading branch information
Brooooooklyn committed Feb 10, 2023
1 parent 593ee50 commit 0b4d305
Show file tree
Hide file tree
Showing 26 changed files with 413 additions and 257 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ rules:
# 0 = off, 1 = warn, 2 = error
'space-before-function-paren': 0
'no-useless-constructor': 0
'no-redeclare': 0
'no-undef': 2
'no-console': [2, { allow: ['error', 'warn', 'info', 'assert'] }]
'comma-dangle': ['error', 'only-multiline']
Expand Down Expand Up @@ -56,6 +57,8 @@ rules:
},
]

'@typescript-eslint/no-redeclare': 2

'@typescript-eslint/adjacent-overload-signatures': 2

'@typescript-eslint/await-thenable': 2
Expand Down
15 changes: 5 additions & 10 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: pnpm/action-setup@v2

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 18

- name: Cache NPM dependencies
uses: actions/cache@v3
with:
path: ~/.pnpm-store
key: npm-cache-${{ matrix.os }}-node@18
cache: 'pnpm'

- name: 'Install dependencies'
run: |
npm install -g corepack
corepack enable
pnpm install -r
run: pnpm install -r

- name: Build TypeScript
run: pnpm build
Expand All @@ -44,6 +38,7 @@ jobs:
run: |
pnpm test
pnpm test:jest
pnpm test:module
publish:
name: Publish
Expand Down
19 changes: 2 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,15 @@ Run TypeScript with node, without compilation or typechecking:
```bash
npm i -D @swc-node/register
node -r @swc-node/register script.ts
node -r @swc-node/register/esm script.ts # for esm project
```

Set environment variable SWCRC=true when you would like to load .swcrc file

```bash
SWCRC=true node -r @swc-node/register script.ts
```

## Support matrix

| | node10 | node12 | node14 | node16 |
| ------------------- | ------ | ------ | ------ | ------ |
| Windows x64 |||||
| Windows x32 |||||
| Windows arm64 |||||
| macOS x64 |||||
| macOS arm64 |||||
| Linux x64 gnu |||||
| Linux x64 musl |||||
| Linux arm gnueabihf |||||
| Linux arm64 gnu |||||
| Linux arm64 musl |||||
| Android arm64 |||||
| FreeBSD x64 |||||

## @swc-node/core

Fastest `TypeScript` transformer.
Expand Down
35 changes: 24 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
"tsc"
],
"private": true,
"workspaces": ["packages/*"],
"workspaces": [
"packages/*"
],
"repository": "git@github.com:swc/swc-node.git",
"author": "LongYinan <github@lyn.one>",
"license": "MIT",
"scripts": {
"bench": "node -r @swc-node/register ./bench/index.ts",
"build": "tsc -b tsconfig.project.json",
"format": "prettier . -w",
"lint": "eslint -c ./.eslintrc.yml \"packages/**/*.{js,ts,tsx}\"",
"format": "prettier --config ./package.json . -w",
"lint": "eslint -c ./.eslintrc.yml .",
"test": "ava",
"test:jest": "jest --config jest.config.js",
"test:module": "cross-env SWC_NODE_PROJECT=packages/integrate-module/tsconfig.json node --loader=@swc-node/register/esm packages/integrate-module/src/index.ts",
"version": "pnpm install && git add .",
"postinstall": "husky install"
},
Expand All @@ -50,6 +53,7 @@
"ava": "^5.2.0",
"benchmark": "^2.1.4",
"colorette": "^2.0.19",
"cross-env": "^7.0.3",
"esbuild": "^0.17.6",
"eslint": "^8.33.0",
"eslint-config-prettier": "^8.6.0",
Expand All @@ -68,10 +72,12 @@
"typescript": "^4.9.5"
},
"lint-staged": {
"*.@(js|ts|tsx)": ["prettier --write"],
"*.@(yml|yaml)": ["prettier --parser yaml --write"],
"*.md": ["prettier --parser markdown --write"],
"*.json": ["prettier --parser json --write"]
"*.@(js|ts|tsx|mts)": [
"eslint --fix"
],
"*.@(js|ts|tsx|mts|yml|yaml|md|json)": [
"prettier --write"
]
},
"prettier": {
"printWidth": 120,
Expand All @@ -81,12 +87,19 @@
"arrowParens": "always"
},
"ava": {
"extensions": ["ts", "tsx"],
"require": ["@swc-node/register"],
"files": ["packages/**/*.spec.{ts,tsx}"],
"extensions": [
"ts",
"tsx"
],
"require": [
"@swc-node/register"
],
"files": [
"packages/**/*.spec.{ts,tsx}"
],
"environmentVariables": {
"SWC_NODE_PROJECT": "./tsconfig.test.json"
}
},
"packageManager": "pnpm@7.26.3"
"packageManager": "pnpm@7.27.0"
}
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function transformOption(path: string, options?: Options, jest = false): SwcOpti
minify: false,
isModule: true,
module: {
type: 'commonjs',
type: options?.module ?? 'commonjs',
noInterop: !opts.esModuleInterop,
},
sourceMaps: typeof opts.sourcemap === 'undefined' ? sourceMapDefault : opts.sourcemap,
Expand Down
7 changes: 6 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"license": "MIT",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": ["lib"],
"files": [
"lib"
],
"engines": {
"node": ">= 10"
},
Expand All @@ -41,6 +43,9 @@
"peerDependencies": {
"@swc/core": ">= 1.3"
},
"devDependencies": {
"@swc/core": "^1.3.35"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
Expand Down
15 changes: 15 additions & 0 deletions packages/integrate-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "integrate-module",
"type": "module",
"version": "0.0.0",
"private": true,
"dependencies": {
"file-type": "^18.2.0",
"p-timeout": "^6.1.0"
},
"devDependencies": {
"@swc/core": "^1.3.35",
"@swc-node/register": "workspace:*",
"typescript": "^4.9.5"
}
}
9 changes: 9 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint import/order: off */
import assert from 'node:assert'
import test from 'node:test'

import { supportedExtensions } from 'file-type'

await test('file-type should work', () => {
assert.ok(supportedExtensions.has('jpg'))
})
10 changes: 10 additions & 0 deletions packages/integrate-module/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"composite": true,
"outDir": "dist"
},
"include": ["src"]
}
6 changes: 5 additions & 1 deletion packages/integrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "@swc-node/integrate",
"version": "0.1.1",
"description": "Integrate testing",
"keywords": ["ava", "swc", "ts-node"],
"keywords": [
"ava",
"swc",
"ts-node"
],
"author": "LongYinan <github@lyn.one>",
"homepage": "https://github.com/swc-project/swc-node",
"license": "MIT",
Expand Down
6 changes: 3 additions & 3 deletions packages/jest/__test__/hoist-top-level.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ Generated by [AVA](https://avajs.dev).
jest.mock('./foo');␊
jest.mock('./foo/bar', ()=>'bar');␊
jest.deepUnmock('./foo');␊
jest.mock('./foo').mock('./bar');␊
const foo = 'foo';␊
console.log(foo);␊
jest.unmock('./bar/foo').dontMock('./bar/bar');␊
jest.mock('./foo').mock('./bar');␊
const func = ()=>{␊
jest.unmock('./foo');␊
jest.mock('./bar');␊
jest.mock('./bar/foo', ()=>'foo');␊
jest.unmock('./foo/bar');␊
jest.deepUnmock('./bar');␊
jest.mock('./foo').mock('./bar');␊
const bar = 'bar';␊
console.log(bar);␊
jest.unmock('./bar/foo').dontMock('./bar/bar');␊
jest.mock('./foo').mock('./bar');␊
};␊
const func2 = ()=>{␊
jest.mock('./bar');␊
jest.unmock('./foo/bar');␊
jest.mock('./bar/foo', ()=>'foo');␊
jest.unmock('./foo');␊
jest.deepUnmock('./bar');␊
jest.mock('./foo').mock('./bar');␊
const bar = 'bar';␊
console.log(bar);␊
jest.unmock('./bar/foo').dontMock('./bar/bar');␊
jest.mock('./foo').mock('./bar');␊
};␊
`
Binary file modified packages/jest/__test__/hoist-top-level.spec.ts.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/jest/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = { transform: { '^.+\\.(t|j)sx?$': '@swc-node/jest' } }
module.exports = { transform: { '^.+\\.((m|c)?t|(m|c)?j)sx?$': '@swc-node/jest' } }
21 changes: 19 additions & 2 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
"name": "@swc-node/jest",
"version": "1.5.9",
"description": "swc preprocessor for jest with source map support",
"keywords": ["swc", "jest", "ts-jest", "napi", "N-API", "typescript", "node-rs", "napi-rs", "ts-node"],
"keywords": [
"swc",
"jest",
"ts-jest",
"napi",
"N-API",
"typescript",
"node-rs",
"napi-rs",
"ts-node"
],
"author": "LongYinan <github@lyn.one>",
"homepage": "https://github.com/swc-project/swc-node",
"license": "MIT",
"main": "./lib/index.js",
"files": ["lib", "jest-preset.js", "LICENSE"],
"files": [
"lib",
"jest-preset.js",
"LICENSE"
],
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
Expand All @@ -21,6 +35,9 @@
"@swc/core": ">= 1.3",
"typescript": ">= 4.3"
},
"devDependencies": {
"@swc/core": "^1.3.35"
},
"repository": {
"type": "git",
"url": "git+https://github.com/swc-project/swc-node.git"
Expand Down
7 changes: 5 additions & 2 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"homepage": "https://github.com/swc-project/swc-node",
"license": "MIT",
"main": "./index.js",
"files": ["lib", "LICENSE", "index.js"],
"files": [
"lib",
"LICENSE",
"index.js"
],
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
Expand All @@ -28,7 +32,6 @@
"@swc-node/register": "^1.5.8"
},
"peerDependencies": {
"@swc/core": ">= 1.3",
"typescript": ">= 4.3",
"webpack": ">= 5.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/register/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
esm/
2 changes: 2 additions & 0 deletions packages/register/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ switch (target) {
case ts.ScriptTarget.ES2019:
return 'es2019'
case ts.ScriptTarget.ES2020:
case ts.ScriptTarget.ES2021:
case ts.ScriptTarget.ES2022:
case ts.ScriptTarget.ESNext:
case ts.ScriptTarget.Latest:
return 'es2020'
Expand Down
Loading

0 comments on commit 0b4d305

Please sign in to comment.