├── .changeset ├── great-goats-trade.md ├── ninety-singers-roll.md ├── rude-bugs-invite.md ├── smooth-ties-count.md ├── spotty-mails-relate.md └── three-rockets-jump.md ├── .eslintignore ├── .eslintrc.yml ├── .github ├── FUNDING.yml └── workflows │ └── CI.yml ├── .gitignore ├── .husky ├── .gitignore └── pre-commit ├── .npmrc ├── .prettierignore ├── LICENSE ├── README.md ├── bench └── index.ts ├── jest.config.js ├── lerna.json ├── package.json ├── packages ├── core │ ├── CHANGELOG.md │ ├── README.md │ ├── index.ts │ ├── package.json │ └── tsconfig.json ├── integrate-module │ ├── package.json │ ├── src │ │ ├── cjs │ │ │ ├── index.d.ts │ │ │ ├── index.js │ │ │ └── package.json │ │ ├── common.cjs │ │ ├── common.d.cts │ │ ├── compiled.d.ts │ │ ├── compiled.js │ │ ├── component.tsx │ │ ├── foo.mts │ │ ├── index.ts │ │ ├── js-module.mjs │ │ ├── mocked.ts │ │ └── subdirectory │ │ │ ├── bar.mts │ │ │ ├── baz.mts │ │ │ └── index.mts │ └── tsconfig.json ├── integrate │ ├── CHANGELOG.md │ ├── README.md │ ├── __tests__ │ │ ├── class-property-order │ │ │ ├── class-property-order.spec.ts │ │ │ └── class-property-order.ts │ │ ├── declare-file │ │ │ ├── declare.d.ts │ │ │ └── declare.spec.ts │ │ ├── hygiene │ │ │ ├── class.ts │ │ │ └── hygiene.spec.ts │ │ ├── name-shadowed │ │ │ ├── name-shadowed.spec.ts │ │ │ ├── shadowed.ts │ │ │ └── url.ts │ │ ├── optional-call │ │ │ └── optional-chain-function-call.spec.ts │ │ ├── paths │ │ │ ├── js-paths.spec.js │ │ │ └── paths.spec.ts │ │ ├── react-pragma │ │ │ ├── __snapshots__ │ │ │ │ ├── react-pragma.spec.ts.md │ │ │ │ └── react-pragma.spec.ts.snap │ │ │ └── react-pragma.spec.ts │ │ ├── sourcemaps │ │ │ ├── __snapshots__ │ │ │ │ ├── sourcemaps.spec.ts.md │ │ │ │ └── sourcemaps.spec.ts.snap │ │ │ └── sourcemaps.spec.ts │ │ └── super │ │ │ └── class-super.spec.ts │ ├── jest │ │ └── inline-snapshot │ │ │ ├── inline-snapshot.test.ts │ │ │ └── jsx-runtime.test.tsx │ ├── package.json │ ├── src │ │ └── fixture.ts │ └── tsconfig.json ├── jest │ ├── CHANGELOG.md │ ├── README.md │ ├── __test__ │ │ ├── hoist-top-level.spec.ts │ │ ├── hoist-top-level.spec.ts.md │ │ └── hoist-top-level.spec.ts.snap │ ├── index.ts │ ├── jest-preset.js │ ├── package.json │ └── tsconfig.json ├── loader │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ ├── src │ │ └── index.ts │ └── tsconfig.json ├── register │ ├── .gitignore │ ├── CHANGELOG.md │ ├── README.md │ ├── __test__ │ │ ├── read-default-config.spec.ts │ │ ├── subdirectory │ │ │ └── tsconfig.extend.json │ │ ├── ts-compiler-options-to-swc-config.spec.ts │ │ └── tsconfig.spec.json │ ├── esm-register.mts │ ├── esm.mts │ ├── index.js │ ├── package.json │ ├── read-default-tsconfig.d.ts │ ├── read-default-tsconfig.ts │ ├── register.d.ts │ ├── register.ts │ ├── tsconfig.esm.json │ └── tsconfig.json └── sourcemap-support │ ├── CHANGELOG.md │ ├── README.md │ ├── index.ts │ ├── package.json │ └── tsconfig.json ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── renovate.json ├── tsconfig.json ├── tsconfig.lint.json └── tsconfig.test.json /.changeset/great-goats-trade.md: -------------------------------------------------------------------------------- 1 | --- 2 | "@fake-scope/fake-pkg": patch 3 | --- 4 | 5 | fix(register): fix esm entry resolver for third-party executor, close #762 6 | -------------------------------------------------------------------------------- /.changeset/ninety-singers-roll.md: -------------------------------------------------------------------------------- 1 | --- 2 | "@fake-scope/fake-pkg": patch 3 | --- 4 | 5 | fix: fix ts extension detect regex, close #775 #774 #772 6 | -------------------------------------------------------------------------------- /.changeset/rude-bugs-invite.md: -------------------------------------------------------------------------------- 1 | --- 2 | "@fake-scope/fake-pkg": patch 3 | --- 4 | 5 | doc: update readme 6 | -------------------------------------------------------------------------------- /.changeset/smooth-ties-count.md: -------------------------------------------------------------------------------- 1 | --- 2 | '@fake-scope/fake-pkg': patch 3 | --- 4 | 5 | fix: support compile js files. close #761 6 | fix: update minimal `@swc/core` to `1.4.13` because of https://github.com/swc-project/swc/pull/8784 7 | -------------------------------------------------------------------------------- /.changeset/spotty-mails-relate.md: -------------------------------------------------------------------------------- 1 | --- 2 | '@fake-scope/fake-pkg': patch 3 | --- 4 | 5 | fix: convert fileUrl to path before compile, close #753 6 | fix: remove baseUrl from esm to keep module import specifier, cause it use tsc resolver. 7 | -------------------------------------------------------------------------------- /.changeset/three-rockets-jump.md: -------------------------------------------------------------------------------- 1 | --- 2 | "@fake-scope/fake-pkg": patch 3 | --- 4 | 5 | fix: add default tsconfig.baseUrl to align with tsc behavior 6 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | lib 4 | esm 5 | next 6 | coverage 7 | output 8 | static 9 | temp 10 | .nyc_output 11 | *.d.ts 12 | __mock__ 13 | target 14 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | parser: '@typescript-eslint/parser' 2 | 3 | parserOptions: 4 | ecmaFeatures: 5 | jsx: true 6 | ecmaVersion: 2020 7 | sourceType: module 8 | project: ./tsconfig.lint.json 9 | 10 | env: 11 | browser: true 12 | es6: true 13 | node: true 14 | jest: true 15 | 16 | plugins: 17 | - import 18 | 19 | extends: 20 | - eslint:recommended 21 | - prettier 22 | rules: 23 | # 0 = off, 1 = warn, 2 = error 24 | 'space-before-function-paren': 0 25 | 'no-useless-constructor': 0 26 | 'no-redeclare': 0 27 | 'no-undef': 2 28 | 'no-console': [2, { allow: ['error', 'warn', 'info', 'assert'] }] 29 | 'comma-dangle': ['error', 'only-multiline'] 30 | 'no-unused-vars': 0 31 | 'no-var': 2 32 | 'one-var-declaration-per-line': 2 33 | 'prefer-const': 2 34 | 'no-const-assign': 2 35 | 'no-duplicate-imports': 2 36 | 'no-use-before-define': [2, { 'functions': false, 'classes': false }] 37 | 'eqeqeq': [2, 'always', { 'null': 'ignore' }] 38 | 'no-case-declarations': 0 39 | 'no-dupe-class-members': 0 40 | 41 | 'import/first': 2 42 | 'import/newline-after-import': 2 43 | 'import/no-extraneous-dependencies': 2 44 | 'import/order': 45 | [ 46 | 2, 47 | { 48 | 'newlines-between': 'always', 49 | 'alphabetize': { 'order': 'asc', 'caseInsensitive': true }, 50 | 'pathGroups': 51 | [ 52 | { 'pattern': '@slardar/**', 'group': 'internal', 'position': 'before' }, 53 | { 'pattern': '@perfkit/**', 'group': 'internal', 'position': 'before' }, 54 | { 'pattern': '@maiev/**', 'group': 'internal', 'position': 'before' }, 55 | ], 56 | }, 57 | ] 58 | 59 | '@typescript-eslint/no-redeclare': 2 60 | 61 | '@typescript-eslint/adjacent-overload-signatures': 2 62 | 63 | '@typescript-eslint/await-thenable': 2 64 | 65 | '@typescript-eslint/consistent-type-assertions': 2 66 | 67 | '@typescript-eslint/ban-types': 68 | [ 69 | 'error', 70 | { 71 | 'types': 72 | { 73 | 'String': { 'message': 'Use string instead', 'fixWith': 'string' }, 74 | 'Number': { 'message': 'Use number instead', 'fixWith': 'number' }, 75 | 'Boolean': { 'message': 'Use boolean instead', 'fixWith': 'boolean' }, 76 | 'Function': { 'message': 'Use explicit type instead' }, 77 | }, 78 | }, 79 | ] 80 | 81 | '@typescript-eslint/explicit-member-accessibility': 82 | [ 83 | 'error', 84 | { 85 | accessibility: 'explicit', 86 | overrides: 87 | { 88 | accessors: 'no-public', 89 | constructors: 'no-public', 90 | methods: 'no-public', 91 | properties: 'no-public', 92 | parameterProperties: 'explicit', 93 | }, 94 | }, 95 | ] 96 | 97 | '@typescript-eslint/method-signature-style': 2 98 | 99 | '@typescript-eslint/no-floating-promises': 2 100 | 101 | '@typescript-eslint/no-implied-eval': 2 102 | 103 | '@typescript-eslint/no-for-in-array': 2 104 | 105 | '@typescript-eslint/no-inferrable-types': 2 106 | 107 | '@typescript-eslint/no-invalid-void-type': 2 108 | 109 | '@typescript-eslint/no-misused-new': 2 110 | 111 | '@typescript-eslint/no-misused-promises': 2 112 | 113 | '@typescript-eslint/no-namespace': 2 114 | 115 | '@typescript-eslint/no-non-null-asserted-optional-chain': 2 116 | 117 | '@typescript-eslint/no-throw-literal': 2 118 | 119 | '@typescript-eslint/no-unnecessary-boolean-literal-compare': 2 120 | 121 | '@typescript-eslint/prefer-for-of': 2 122 | 123 | '@typescript-eslint/prefer-nullish-coalescing': 2 124 | 125 | '@typescript-eslint/switch-exhaustiveness-check': 2 126 | 127 | '@typescript-eslint/prefer-optional-chain': 2 128 | 129 | '@typescript-eslint/prefer-readonly': 2 130 | 131 | '@typescript-eslint/prefer-string-starts-ends-with': 0 132 | 133 | '@typescript-eslint/no-array-constructor': 2 134 | 135 | '@typescript-eslint/require-await': 2 136 | 137 | '@typescript-eslint/return-await': 2 138 | 139 | '@typescript-eslint/ban-ts-comment': 140 | [2, { 'ts-expect-error': false, 'ts-ignore': true, 'ts-nocheck': true, 'ts-check': false }] 141 | 142 | '@typescript-eslint/naming-convention': 143 | [ 144 | 2, 145 | { 146 | selector: 'memberLike', 147 | format: ['camelCase', 'PascalCase'], 148 | modifiers: ['private'], 149 | leadingUnderscore: 'forbid', 150 | }, 151 | ] 152 | 153 | '@typescript-eslint/no-unused-vars': 154 | [2, { varsIgnorePattern: '^_', argsIgnorePattern: '^_', ignoreRestSiblings: true }] 155 | '@typescript-eslint/member-ordering': 156 | [ 157 | 2, 158 | { 159 | default: 160 | [ 161 | 'public-static-field', 162 | 'protected-static-field', 163 | 'private-static-field', 164 | 'public-static-method', 165 | 'protected-static-method', 166 | 'private-static-method', 167 | 'public-instance-field', 168 | 'protected-instance-field', 169 | 'private-instance-field', 170 | 'public-constructor', 171 | 'protected-constructor', 172 | 'private-constructor', 173 | 'public-instance-method', 174 | 'protected-instance-method', 175 | 'private-instance-method', 176 | ], 177 | }, 178 | ] 179 | 180 | overrides: 181 | - files: 182 | - ./{scripts,bench}/**/*.js 183 | plugins: 184 | - '@typescript-eslint' 185 | parserOptions: 186 | project: ./tsconfig.js.json 187 | rules: 188 | '@typescript-eslint/prefer-nullish-coalescing': 0 189 | '@typescript-eslint/prefer-optional-chain': 0 190 | '@typescript-eslint/no-non-null-asserted-optional-chain': 0 191 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [Brooooooklyn] 2 | -------------------------------------------------------------------------------- /.github/workflows/CI.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master, develop] 6 | pull_request: 7 | 8 | env: 9 | OXC_LOG: 'debug' 10 | DEBUG: '@swc-node' 11 | 12 | jobs: 13 | test: 14 | strategy: 15 | fail-fast: false 16 | matrix: 17 | os: [ubuntu-latest, macos-latest, windows-latest] 18 | node: [18, 20, 22] 19 | 20 | name: stable - ${{ matrix.os }} - node@${{ matrix.node }} 21 | runs-on: ${{ matrix.os }} 22 | 23 | steps: 24 | - uses: actions/checkout@v4 25 | 26 | - uses: pnpm/action-setup@v4 27 | 28 | - name: Setup node 29 | uses: actions/setup-node@v4 30 | with: 31 | node-version: ${{ matrix.node }} 32 | cache: 'pnpm' 33 | 34 | - name: 'Install dependencies' 35 | run: pnpm install -r 36 | 37 | - name: Build TypeScript 38 | run: pnpm build 39 | 40 | - name: Test bindings 41 | run: | 42 | pnpm test 43 | pnpm test:jest 44 | pnpm test:module 45 | 46 | publish: 47 | name: Publish 48 | permissions: 49 | contents: write 50 | id-token: write 51 | if: "startsWith(github.event.head_commit.message, 'chore(release): publish')" 52 | runs-on: ubuntu-latest 53 | needs: 54 | - test 55 | 56 | steps: 57 | - uses: actions/checkout@v4 58 | 59 | - uses: pnpm/action-setup@v4 60 | 61 | - name: Setup node 62 | uses: actions/setup-node@v4 63 | with: 64 | node-version: 20 65 | cache: 'pnpm' 66 | 67 | - name: 'Install dependencies' 68 | run: pnpm install 69 | 70 | - name: Build TypeScript 71 | run: | 72 | pnpm build 73 | rm ./packages/**/*.tsbuildinfo 74 | 75 | - name: Lerna publish 76 | run: | 77 | echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc 78 | npm config set provenance true 79 | pnpm publish -r --access public 80 | env: 81 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 82 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 83 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/macos 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=macos 4 | 5 | ### macOS ### 6 | # General 7 | .DS_Store 8 | .AppleDouble 9 | .LSOverride 10 | 11 | # Icon must end with two \r 12 | Icon 13 | 14 | # Thumbnails 15 | ._* 16 | 17 | # Files that might appear in the root of a volume 18 | .DocumentRevisions-V100 19 | .fseventsd 20 | .Spotlight-V100 21 | .TemporaryItems 22 | .Trashes 23 | .VolumeIcon.icns 24 | .com.apple.timemachine.donotpresent 25 | 26 | # Directories potentially created on remote AFP share 27 | .AppleDB 28 | .AppleDesktop 29 | Network Trash Folder 30 | Temporary Items 31 | .apdisk 32 | 33 | # End of https://www.toptal.com/developers/gitignore/api/macos 34 | 35 | # Created by https://www.toptal.com/developers/gitignore/api/nodejs 36 | # Edit at https://www.toptal.com/developers/gitignore?templates=nodejs 37 | 38 | # Created by https://www.toptal.com/developers/gitignore/api/node 39 | # Edit at https://www.toptal.com/developers/gitignore?templates=node 40 | 41 | ### Node ### 42 | # Logs 43 | logs 44 | *.log 45 | npm-debug.log* 46 | yarn-debug.log* 47 | yarn-error.log* 48 | lerna-debug.log* 49 | 50 | # Diagnostic reports (https://nodejs.org/api/report.html) 51 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 52 | 53 | # Runtime data 54 | pids 55 | *.pid 56 | *.seed 57 | *.pid.lock 58 | 59 | # Directory for instrumented libs generated by jscoverage/JSCover 60 | lib-cov 61 | 62 | # Coverage directory used by tools like istanbul 63 | coverage 64 | *.lcov 65 | 66 | # nyc test coverage 67 | .nyc_output 68 | 69 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 70 | .grunt 71 | 72 | # Bower dependency directory (https://bower.io/) 73 | bower_components 74 | 75 | # node-waf configuration 76 | .lock-wscript 77 | 78 | # Compiled binary addons (https://nodejs.org/api/addons.html) 79 | build/Release 80 | 81 | # Dependency directories 82 | node_modules/ 83 | jspm_packages/ 84 | 85 | # TypeScript v1 declaration files 86 | typings/ 87 | 88 | # TypeScript cache 89 | *.tsbuildinfo 90 | 91 | # Optional npm cache directory 92 | .npm 93 | 94 | # Optional eslint cache 95 | .eslintcache 96 | 97 | # Microbundle cache 98 | .rpt2_cache/ 99 | .rts2_cache_cjs/ 100 | .rts2_cache_es/ 101 | .rts2_cache_umd/ 102 | 103 | # Optional REPL history 104 | .node_repl_history 105 | 106 | # Output of 'npm pack' 107 | *.tgz 108 | 109 | # Yarn Integrity file 110 | .yarn-integrity 111 | 112 | # dotenv environment variables file 113 | .env 114 | .env.test 115 | 116 | # parcel-bundler cache (https://parceljs.org/) 117 | .cache 118 | 119 | # Next.js build output 120 | .next 121 | 122 | # Nuxt.js build / generate output 123 | .nuxt 124 | dist 125 | 126 | # Gatsby files 127 | .cache/ 128 | # Comment in the public line in if your project uses Gatsby and not Next.js 129 | # https://nextjs.org/blog/next-9-1#public-directory-support 130 | # public 131 | 132 | # vuepress build output 133 | .vuepress/dist 134 | 135 | # Serverless directories 136 | .serverless/ 137 | 138 | # FuseBox cache 139 | .fusebox/ 140 | 141 | # DynamoDB Local files 142 | .dynamodb/ 143 | 144 | # TernJS port file 145 | .tern-port 146 | 147 | # Stores VSCode versions used for testing VSCode extensions 148 | .vscode-test 149 | 150 | # End of https://www.toptal.com/developers/gitignore/api/node 151 | 152 | 153 | #Added by cargo 154 | 155 | /target 156 | 157 | 158 | #Added by cargo 159 | # 160 | #already existing elements were commented out 161 | 162 | #/target 163 | Cargo.lock 164 | 165 | # idea 166 | .idea/ 167 | 168 | *.node 169 | lib 170 | artifacts 171 | packages/core/index.js 172 | packages/core/index.d.ts 173 | packages/core/index.js.map -------------------------------------------------------------------------------- /.husky/.gitignore: -------------------------------------------------------------------------------- 1 | _ 2 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | pnpm lint-staged -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | auto-install-peers = true 2 | recursive-install = true 3 | link-workspace-packages = true -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | esm 3 | lib 4 | dist 5 | temp 6 | target 7 | pnpm-lock.yaml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020-present LongYinan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `swc-node` 2 | 3 | > 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn) 4 | 5 | **_Fast `TypeScript/JavaScript` transformer without `node-gyp` and postinstall script_**. 6 | 7 |

8 | Build Status 9 | Downloads 10 | License 11 |

12 | 13 | ## Usage 14 | 15 | Run TypeScript with node, without compilation or typechecking: 16 | 17 | ```bash 18 | npm i -D @swc-node/register 19 | node -r @swc-node/register script.ts 20 | node --import @swc-node/register/esm-register script.ts # for esm project with node>=20.6 21 | node --loader @swc-node/register/esm script.ts # for esm project with node<=20.5, deprecated 22 | ``` 23 | 24 | Pass `--enable-source-maps` to node for esm projects 25 | 26 | Set environment variable SWCRC=true when you would like to load .swcrc file 27 | 28 | ```bash 29 | SWCRC=true node -r @swc-node/register script.ts 30 | ``` 31 | 32 | ```typescript 33 | #!/usr/bin/env -S node --import @swc-node/register/esm-register 34 | 35 | // your code 36 | ``` 37 | 38 | run with shebang, add `TS_NODE_PROJECT=null`(`#!/usr/bin/env TS_NODE_PROJECT=null node --import @swc-node/register/esm-register`) to use ignore tsconfig.json 39 | 40 | ## @swc-node/core 41 | 42 | Fastest `TypeScript` transformer. 43 | 44 | Detail: [@swc-node/core](./packages/core) 45 | 46 | ### Benchmark 47 | 48 | > transform RxJS `AjaxObservable.ts` to ES2015 & CommonJS `JavaScript`. Benchmark code: [bench](./bench/index.ts) 49 | 50 | **Hardware info**: 51 | 52 | ``` 53 | Model Name: MacBook Pro 54 | Model Identifier: MacBookPro15,1 55 | Processor Name: 6-Core Intel Core i7 56 | Processor Speed: 2.6 GHz 57 | Number of Processors: 1 58 | Total Number of Cores: 6 59 | L2 Cache (per Core): 256 KB 60 | L3 Cache: 12 MB 61 | Hyper-Threading Technology: Enabled 62 | Memory: 16 GB 63 | ``` 64 | 65 | #### `transformSync` 66 | 67 | ```bash 68 | esbuild x 510 ops/sec ±1.28% (88 runs sampled) 69 | @swc-node/core x 438 ops/sec ±1.00% (88 runs sampled) 70 | typescript x 28.83 ops/sec ±10.20% (52 runs sampled) 71 | babel x 24.21 ops/sec ±10.66% (46 runs sampled) 72 | Transform rxjs/AjaxObservable.ts benchmark bench suite: Fastest is esbuild 73 | ``` 74 | 75 | #### `transform` parallel 76 | 77 | `UV_THREADPOOL_SIZE=11 yarn bench` 78 | 79 | ```bash 80 | @swc-node/core x 1,253 ops/sec ±0.90% (75 runs sampled) 81 | esbuild x 914 ops/sec ±1.31% (77 runs sampled) 82 | Transform rxjs/AjaxObservable.ts parallel benchmark bench suite: Fastest is @swc-node/core 83 | ``` 84 | 85 | `yarn bench` 86 | 87 | ```bash 88 | @swc-node/core x 1,123 ops/sec ±0.95% (77 runs sampled) 89 | esbuild x 847 ops/sec ±3.74% (71 runs sampled) 90 | Transform rxjs/AjaxObservable.ts parallel benchmark bench suite: Fastest is @swc-node/core 91 | ``` 92 | 93 | ## @swc-node/jest 94 | 95 | Fastest jest `TypeScript` transformer. 96 | 97 | Detail: [@swc-node/jest](./packages/jest) 98 | 99 | ### Performance glance 100 | 101 | > Testing in pure `TypeScript` project, compile target is `ES2018`. 102 | > Running with `npx jest --no-cache`, `ts-jest` was configured with `isolatedModules: true` 103 | 104 | #### ts-jest 105 | 106 | ``` 107 | Test Suites: 49 passed, 49 total 108 | Tests: 254 passed, 254 total 109 | Snapshots: 53 passed, 53 total 110 | Time: 54.631 s 111 | Ran all test suites. 112 | ✨ Done in 62.71s. 113 | ``` 114 | 115 | #### @swc-node/jest 116 | 117 | ``` 118 | Test Suites: 49 passed, 49 total 119 | Tests: 254 passed, 254 total 120 | Snapshots: 53 passed, 53 total 121 | Time: 10.511 s 122 | Ran all test suites. 123 | ✨ Done in 14.34s. 124 | ``` 125 | 126 | ## @swc-node/register 127 | 128 | Faster `ts-node/register/transpile-only` alternative. 129 | 130 | Detail: [@swc-node/register](./packages/register) 131 | 132 | ## Development 133 | 134 | ### Install dependencies 135 | 136 | - `pnpm i` 137 | 138 | ### Build and Test 139 | 140 | - `pnpm build` 141 | 142 | - `pnpm test` 143 | 144 | ## Sponsors 145 | 146 |

147 | sponsors 148 |

149 | -------------------------------------------------------------------------------- /bench/index.ts: -------------------------------------------------------------------------------- 1 | import fs from 'fs' 2 | import os from 'os' 3 | import { join } from 'path' 4 | 5 | import * as babel from '@babel/core' 6 | // @ts-expect-error 7 | import * as envPreset from '@babel/preset-env' 8 | // @ts-expect-error 9 | import * as tsPreset from '@babel/preset-typescript' 10 | import { transformSync as transformSyncNapi, transform as transformNapi } from '@swc-node/core' 11 | import Benchmark, { Suite } from 'benchmark' 12 | import { green } from 'colorette' 13 | import { transformSync as transformSyncEsbuild, transform as transformEsbuild } from 'esbuild' 14 | import ts from 'typescript' 15 | 16 | const cpuCount = os.cpus().length - 1 17 | 18 | const syncSuite = new Suite('Transform rxjs/AjaxObservable.ts benchmark') 19 | 20 | const asyncSuite = new Suite('Transform rxjs/AjaxObservable.ts async benchmark') 21 | 22 | const parallelSuite = new Suite('Transform rxjs/AjaxObservable.ts parallel benchmark') 23 | 24 | const SOURCE_PATH = join(__dirname, '..', 'node_modules', 'rxjs', 'src', 'internal', 'ajax', 'ajax.ts') 25 | const SOURCE_CODE = fs.readFileSync(SOURCE_PATH, 'utf-8') 26 | 27 | async function run() { 28 | let defer: () => void 29 | const task = new Promise((resolve) => { 30 | defer = resolve 31 | }) 32 | 33 | syncSuite 34 | .add('esbuild', () => { 35 | transformSyncEsbuild(SOURCE_CODE, { 36 | sourcefile: SOURCE_PATH, 37 | loader: 'ts', 38 | sourcemap: true, 39 | minify: false, 40 | target: 'es2015', 41 | }) 42 | }) 43 | .add('@swc-node/core', () => { 44 | transformSyncNapi(SOURCE_CODE, SOURCE_PATH, { 45 | // SWC target es2016 is es2015 in TypeScript 46 | target: 'es2016', 47 | module: 'commonjs', 48 | sourcemap: true, 49 | }) 50 | }) 51 | .add('typescript', () => { 52 | ts.transpileModule(SOURCE_CODE, { 53 | fileName: SOURCE_PATH, 54 | compilerOptions: { 55 | target: ts.ScriptTarget.ES2015, 56 | module: ts.ModuleKind.CommonJS, 57 | isolatedModules: true, 58 | sourceMap: true, 59 | }, 60 | }) 61 | }) 62 | .add('babel', () => { 63 | babel.transform(SOURCE_CODE, { 64 | filename: SOURCE_PATH, 65 | presets: [ 66 | tsPreset, 67 | [envPreset, { useBuiltIns: false, loose: true, targets: 'Chrome > 52', modules: 'commonjs' }], 68 | ], 69 | configFile: false, 70 | babelrc: false, 71 | sourceMaps: true, 72 | }) 73 | }) 74 | .on('cycle', function (event: Benchmark.Event) { 75 | console.info(String(event.target)) 76 | }) 77 | .on('complete', function (this: Benchmark.Target & Benchmark.Suite) { 78 | console.info( 79 | `${this.name} bench suite: Fastest is ${green( 80 | this.filter('fastest') 81 | .map((s: Benchmark.Target) => s.name) 82 | .join(''), 83 | )}`, 84 | ) 85 | defer() 86 | }) 87 | .run() 88 | 89 | await task 90 | } 91 | 92 | async function runAsync(parallel = 1, suite = asyncSuite) { 93 | let defer: () => void 94 | const task = new Promise((resolve) => { 95 | defer = resolve 96 | }) 97 | suite 98 | .add({ 99 | name: '@swc-node/core', 100 | fn: (deferred: any) => { 101 | Promise.all( 102 | Array.from({ length: parallel }).map(() => { 103 | return transformNapi(SOURCE_CODE, SOURCE_PATH, { 104 | target: 'es2016', 105 | module: 'commonjs', 106 | sourcemap: true, 107 | }) 108 | }), 109 | ) 110 | .then(() => { 111 | deferred.resolve() 112 | }) 113 | .catch((e) => { 114 | console.error(e) 115 | }) 116 | }, 117 | defer: true, 118 | async: true, 119 | queued: true, 120 | }) 121 | .add({ 122 | name: 'esbuild', 123 | fn: (deferred: any) => { 124 | Promise.all( 125 | Array.from({ length: parallel }).map(() => 126 | transformEsbuild(SOURCE_CODE, { 127 | sourcefile: SOURCE_PATH, 128 | loader: 'ts', 129 | sourcemap: true, 130 | minify: false, 131 | target: 'es2015', 132 | }), 133 | ), 134 | ) 135 | .then(() => { 136 | deferred.resolve() 137 | }) 138 | .catch((e) => { 139 | console.error(e) 140 | }) 141 | }, 142 | defer: true, 143 | async: true, 144 | queued: true, 145 | }) 146 | .on('cycle', function (event: Benchmark.Event) { 147 | event.target.hz = event.target!.hz! * parallel 148 | console.info(String(event.target)) 149 | }) 150 | .on('complete', function (this: Benchmark.Target & Benchmark.Suite) { 151 | console.info( 152 | `${this.name} bench suite: Fastest is ${green( 153 | this.filter('fastest') 154 | .map((t: Benchmark.Target) => t.name) 155 | .join(''), 156 | )}`, 157 | ) 158 | defer() 159 | }) 160 | .run() 161 | 162 | await task 163 | } 164 | 165 | run() 166 | .then(() => runAsync(cpuCount, parallelSuite)) 167 | .catch(console.error) 168 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: '@swc-node/jest', 3 | moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], 4 | testMatch: ['/packages/integrate/jest/**/*.test.ts', '/packages/integrate/jest/**/*.test.tsx'], 5 | testPathIgnorePatterns: ['/packages/jest'], 6 | } 7 | -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "npmClient": "pnpm", 3 | "version": "independent", 4 | "command": { 5 | "version": { 6 | "message": "chore(release): publish" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "swc-node", 3 | "version": "0.0.0", 4 | "description": "Faster swc nodejs binding", 5 | "keywords": [ 6 | "swc", 7 | "babel", 8 | "esbuild", 9 | "rust", 10 | "n-api", 11 | "napi", 12 | "node-rs", 13 | "uglify", 14 | "terser", 15 | "webpack", 16 | "ts-node", 17 | "typescript", 18 | "tsc" 19 | ], 20 | "private": true, 21 | "workspaces": [ 22 | "packages/*" 23 | ], 24 | "repository": "git@github.com:swc/swc-node.git", 25 | "author": "LongYinan ", 26 | "license": "MIT", 27 | "scripts": { 28 | "bench": "node -r @swc-node/register ./bench/index.ts", 29 | "build": "tsc -b tsconfig.json", 30 | "format": "prettier --config ./package.json . -w", 31 | "lint": "oxlint .", 32 | "test": "ava", 33 | "test:jest": "jest --config jest.config.js", 34 | "test:module": "cross-env SWC_NODE_PROJECT=packages/integrate-module/tsconfig.json node --import=@swc-node/register/esm-register packages/integrate-module/src/index.ts", 35 | "version": "pnpm install && git add .", 36 | "postinstall": "husky" 37 | }, 38 | "devDependencies": { 39 | "@babel/core": "^7.24.7", 40 | "@babel/plugin-transform-typescript": "^7.24.7", 41 | "@babel/preset-env": "^7.24.7", 42 | "@babel/preset-typescript": "^7.24.7", 43 | "@swc-node/core": "workspace:*", 44 | "@swc-node/jest": "workspace:*", 45 | "@swc-node/register": "workspace:*", 46 | "@types/babel__core": "^7.20.5", 47 | "@types/benchmark": "^2.1.5", 48 | "@types/lodash": "^4.17.6", 49 | "@types/node": "^22.0.0", 50 | "@types/sinon": "^17.0.3", 51 | "ava": "^6.1.3", 52 | "benchmark": "^2.1.4", 53 | "colorette": "^2.0.20", 54 | "cross-env": "^7.0.3", 55 | "esbuild": "^0.25.0", 56 | "husky": "^9.0.11", 57 | "jest": "^29.7.0", 58 | "lerna": "8.2.2", 59 | "lint-staged": "^15.2.7", 60 | "lodash": "^4.17.21", 61 | "oxlint": "^0.16.0", 62 | "prettier": "^3.3.2", 63 | "react": "^19.0.0", 64 | "rxjs": "^7.8.1", 65 | "sinon": "^20.0.0", 66 | "tslib": "^2.6.3", 67 | "typescript": "^5.5.3" 68 | }, 69 | "lint-staged": { 70 | "*.@(js|ts|tsx|mts)": [ 71 | "oxlint --fix" 72 | ], 73 | "*.@(js|ts|tsx|mts|yml|yaml|md|json)": [ 74 | "prettier --write" 75 | ] 76 | }, 77 | "prettier": { 78 | "printWidth": 120, 79 | "semi": false, 80 | "trailingComma": "all", 81 | "singleQuote": true, 82 | "arrowParens": "always" 83 | }, 84 | "ava": { 85 | "extensions": [ 86 | "js", 87 | "ts", 88 | "tsx" 89 | ], 90 | "require": [ 91 | "@swc-node/register" 92 | ], 93 | "cache": false, 94 | "files": [ 95 | "packages/**/*.spec.{js,ts,tsx}" 96 | ], 97 | "environmentVariables": { 98 | "SWC_NODE_PROJECT": "./tsconfig.test.json" 99 | } 100 | }, 101 | "packageManager": "pnpm@10.10.0" 102 | } 103 | -------------------------------------------------------------------------------- /packages/core/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | ## [1.13.3](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.13.2...@swc-node/core@1.13.3) (2024-07-14) 7 | 8 | ### Bug Fixes 9 | 10 | - **register:** enable keepImportAttributes in SWC options ([#816](https://github.com/swc-project/swc-node/issues/816)) ([33568ee](https://github.com/swc-project/swc-node/commit/33568ee42dfb580b4b739ecf89ba7ebb85c45330)) 11 | - **register:** resolve internal cjs module ([#811](https://github.com/swc-project/swc-node/issues/811)) ([9aad5b0](https://github.com/swc-project/swc-node/commit/9aad5b0a86dbe58aed8cba9c57524ad8e553f21c)) 12 | 13 | ## [1.13.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.13.1...@swc-node/core@1.13.2) (2024-07-05) 14 | 15 | **Note:** Version bump only for package @swc-node/core 16 | 17 | ## [1.13.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.13.0...@swc-node/core@1.13.1) (2024-05-01) 18 | 19 | ### Bug Fixes 20 | 21 | - support compile js files. close [#761](https://github.com/swc-project/swc-node/issues/761) ([#767](https://github.com/swc-project/swc-node/issues/767)) ([016f1aa](https://github.com/swc-project/swc-node/commit/016f1aab2a17d2512d30b5a12848ed1941b59e49)) 22 | 23 | # [1.13.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.11.0...@swc-node/core@1.13.0) (2024-03-05) 24 | 25 | ### Features 26 | 27 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 28 | 29 | # [1.12.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.11.0...@swc-node/core@1.12.0) (2024-02-01) 30 | 31 | ### Features 32 | 33 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 34 | 35 | # [1.11.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.10.6...@swc-node/core@1.11.0) (2024-02-01) 36 | 37 | ### Bug Fixes 38 | 39 | - **core:** respect useDefineForClassFields tsconfig value ([#740](https://github.com/swc-project/swc-node/issues/740)) ([9330c1a](https://github.com/swc-project/swc-node/commit/9330c1a1183723638b3c83cff63ec6f18a09294c)) 40 | - respect sourceMaps swcrc value ([#742](https://github.com/swc-project/swc-node/issues/742)) ([df125c8](https://github.com/swc-project/swc-node/commit/df125c8335c25a4dd75c56f5210f822541707d93)) 41 | 42 | ### Features 43 | 44 | - **core:** add `ignoreDynamic` option ([4d32c17](https://github.com/swc-project/swc-node/commit/4d32c1700bb2bdb5c56afd26539598b0968dda84)) 45 | 46 | ## [1.10.6](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.10.5...@swc-node/core@1.10.6) (2023-09-26) 47 | 48 | **Note:** Version bump only for package @swc-node/core 49 | 50 | ## [1.10.5](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.10.4...@swc-node/core@1.10.5) (2023-08-18) 51 | 52 | ### Bug Fixes 53 | 54 | - **core:** assign `baseUrl` property ([55c7b20](https://github.com/swc-project/swc-node/commit/55c7b204ddeb1ad2bfc701918ed76f5374414ebe)) 55 | - **core:** Pass jsc.baseUrl ([#721](https://github.com/swc-project/swc-node/issues/721)) ([877bd58](https://github.com/swc-project/swc-node/commit/877bd58f44072d2f44f6164960271dc9e3fda873)) 56 | 57 | ## [1.10.4](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.10.3...@swc-node/core@1.10.4) (2023-06-21) 58 | 59 | ### Bug Fixes 60 | 61 | - tsCompilerOptionsToSwcConfig should not override default swc config for jest ([#714](https://github.com/swc-project/swc-node/issues/714)) ([60ea642](https://github.com/swc-project/swc-node/commit/60ea64284582ce3164ca3705976b4dc4215c2504)) 62 | 63 | ## [1.10.3](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.10.2...@swc-node/core@1.10.3) (2023-04-10) 64 | 65 | **Note:** Version bump only for package @swc-node/core 66 | 67 | ## [1.10.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.10.1...@swc-node/core@1.10.2) (2023-03-31) 68 | 69 | ### Bug Fixes 70 | 71 | - **core,register:** ts Compiler to Swc Config: respects decorators config and SWCRC=true ([#702](https://github.com/swc-project/swc-node/issues/702)) ([d421ca8](https://github.com/swc-project/swc-node/commit/d421ca8aa02a07ea01b1f97e2f38d696d84e4531)) 72 | 73 | ## [1.10.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.10.0...@swc-node/core@1.10.1) (2023-02-15) 74 | 75 | ### Bug Fixes 76 | 77 | - **core:** enforce sourcemap: 'inline' for jest ([#695](https://github.com/swc-project/swc-node/issues/695)) ([2439b7e](https://github.com/swc-project/swc-node/commit/2439b7e2cd28fee7d0d1038b91baeb042a32c146)) 78 | 79 | # [1.10.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.9.2...@swc-node/core@1.10.0) (2023-02-10) 80 | 81 | ### Features 82 | 83 | - **register:** experimental esm loader ([#643](https://github.com/swc-project/swc-node/issues/643)) ([0b4d305](https://github.com/swc-project/swc-node/commit/0b4d30505408f6f07c1ff8ea5c1953e1d22bb4e1)) 84 | 85 | ## [1.9.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.9.1...@swc-node/core@1.9.2) (2023-01-05) 86 | 87 | ### Bug Fixes 88 | 89 | - **jest:** add externalHelpers:true for jest ([#673](https://github.com/swc-project/swc-node/issues/673)) ([e353c1a](https://github.com/swc-project/swc-node/commit/e353c1a4843671fc7a8f5ccc5727ab260e060565)) 90 | 91 | ## [1.9.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.9.0...@swc-node/core@1.9.1) (2022-09-22) 92 | 93 | ### Bug Fixes 94 | 95 | - **core:** move @swc/core to peerDependencies ([821542b](https://github.com/swc-project/swc-node/commit/821542bb254c7f840443bc45f0505fa478983496)) 96 | 97 | # [1.9.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.8.2...@swc-node/core@1.9.0) (2022-04-27) 98 | 99 | ### Features 100 | 101 | - **jest:** read tsconfig for default jest transform options ([8c180e6](https://github.com/swc-project/swc-node/commit/8c180e68abbc66aa68f83b401d985a6c8617baa9)) 102 | 103 | ## [1.8.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.8.1...@swc-node/core@1.8.2) (2021-12-15) 104 | 105 | **Note:** Version bump only for package @swc-node/core 106 | 107 | ## [1.8.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.8.0...@swc-node/core@1.8.1) (2021-12-13) 108 | 109 | **Note:** Version bump only for package @swc-node/core 110 | 111 | # [1.8.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.7.1...@swc-node/core@1.8.0) (2021-11-08) 112 | 113 | ### Features 114 | 115 | - **core:** enable inlineSourcesContent by default ([c2d1d3f](https://github.com/swc-project/swc-node/commit/c2d1d3f34e4f3c9b5f974c641075877e02e28ebb)) 116 | 117 | ## [1.7.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.7.0...@swc-node/core@1.7.1) (2021-10-29) 118 | 119 | **Note:** Version bump only for package @swc-node/core 120 | 121 | # [1.7.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.6.0...@swc-node/core@1.7.0) (2021-10-16) 122 | 123 | ### Features 124 | 125 | - **core:** add swc option to pass raw SwcOptions ([dbee70c](https://github.com/swc-project/swc-node/commit/dbee70c4157ee6c03fe3f2ae7a834195487dfe38)) 126 | 127 | # [1.6.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.5.1...@swc-node/core@1.6.0) (2021-09-11) 128 | 129 | ### Bug Fixes 130 | 131 | - typecheck ([d39561d](https://github.com/swc-project/swc-node/commit/d39561d3215b5ce2c81f59f51045a1ac7535fe18)) 132 | - **register:** outdated ts->swc config ([cff217b](https://github.com/swc-project/swc-node/commit/cff217b9b45199c580e9ed308f3826b577776bb3)) 133 | 134 | ### Features 135 | 136 | - add typescript path support ([54615b8](https://github.com/swc-project/swc-node/commit/54615b880d70bc41c547cd13cacc2ebefd0bd82c)) 137 | 138 | ## [1.5.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.5.0...@swc-node/core@1.5.1) (2021-06-07) 139 | 140 | ### Bug Fixes 141 | 142 | - **register:** always enable dynamicImport ([0eb1bf2](https://github.com/swc-project/swc-node/commit/0eb1bf2e0bce97ca70d72dc13c51c8eac221029d)) 143 | 144 | # [1.5.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.4.0...@swc-node/core@1.5.0) (2021-05-21) 145 | 146 | ### Features 147 | 148 | - **loader:** implement tsconfig compatible loader ([8c1cd85](https://github.com/swc-project/swc-node/commit/8c1cd858a64a6b6ec6ff23811bafab7dfe30554d)) 149 | 150 | # [1.4.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.3.0...@swc-node/core@1.4.0) (2021-04-28) 151 | 152 | ### Features 153 | 154 | - **core:** expose inline and both config for sourcemap ([780f2bb](https://github.com/swc-project/swc-node/commit/780f2bb81053af6fc6af865a979059ffff470eac)) 155 | 156 | # [1.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.2.0...@swc-node/core@1.3.0) (2021-03-04) 157 | 158 | ### Features 159 | 160 | - **core:** support keepClassNames in Config ([fcbadd5](https://github.com/swc-project/swc-node/commit/fcbadd59d1752e26f1838d29ba98c2b525dda110)) 161 | 162 | # [1.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.1.2...@swc-node/core@1.2.0) (2021-01-25) 163 | 164 | ### Features 165 | 166 | - **jest:** cache transpile result to avoid source code to be compiled mult times ([77cc3d8](https://github.com/swc-project/swc-node/commit/77cc3d8ea82728b9b486e1b5fd898f82180c3f37)) 167 | 168 | ## [1.1.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.1.1...@swc-node/core@1.1.2) (2021-01-04) 169 | 170 | **Note:** Version bump only for package @swc-node/core 171 | 172 | ## [1.1.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.1.0...@swc-node/core@1.1.1) (2020-11-18) 173 | 174 | **Note:** Version bump only for package @swc-node/core 175 | 176 | # [1.1.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@1.0.0...@swc-node/core@1.1.0) (2020-10-21) 177 | 178 | ### Features 179 | 180 | - **core:** support react config in transformOptions ([313e021](https://github.com/swc-project/swc-node/commit/313e02128f833b09f4bf6dd9200b82819cb734cc)) 181 | 182 | ## [0.7.6](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.7.4...@swc-node/core@0.7.6) (2020-09-16) 183 | 184 | ### Bug Fixes 185 | 186 | - **core:** cjs import order and resolver issues ([b61e241](https://github.com/swc-project/swc-node/commit/b61e241613b376d77278ac087f768c72f84ac807)) 187 | 188 | ## [0.7.5](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.7.4...@swc-node/core@0.7.5) (2020-09-13) 189 | 190 | ### Bug Fixes 191 | 192 | - **core:** cjs import order and resolver issues ([deca648](https://github.com/swc-project/swc-node/commit/deca64873c897bddffae4a3c3186966626d274c4)) 193 | 194 | ## [0.7.4](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.7.3...@swc-node/core@0.7.4) (2020-09-11) 195 | 196 | ### Bug Fixes 197 | 198 | - **core:** add support for esModuleInterop ([a7f3331](https://github.com/swc-project/swc-node/commit/a7f3331f06d597e39cb44be8a8d73f264a417a71)) 199 | 200 | ## [0.7.3](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.7.2...@swc-node/core@0.7.3) (2020-09-09) 201 | 202 | ### Bug Fixes 203 | 204 | - **core:** conditional expr in callee & do not rename new keywords in meta properties ([c893622](https://github.com/swc-project/swc-node/commit/c8936229782164fe54bd864ecc2d8049827fd1ef)) 205 | 206 | ## [0.7.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.7.1...@swc-node/core@0.7.2) (2020-09-07) 207 | 208 | ### Bug Fixes 209 | 210 | - **core:** wrong assertion in hygiene ([b28cc10](https://github.com/swc-project/swc-node/commit/b28cc10875a0c98db78f402dfa5d3762ec77cd31)) 211 | 212 | ## [0.7.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.7.0...@swc-node/core@0.7.1) (2020-09-06) 213 | 214 | ### Bug Fixes 215 | 216 | - **core:** wrong this in function call expr in optional chains ([5d9966f](https://github.com/swc-project/swc-node/commit/5d9966f9eb4b1026356a3f824568f9804faa05ff)) 217 | 218 | # [0.7.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.6.1...@swc-node/core@0.7.0) (2020-09-04) 219 | 220 | ### Features 221 | 222 | - **core:** upgrade to napi@0.5 ([bf5f2c4](https://github.com/swc-project/swc-node/commit/bf5f2c4b9efc074e0b1ff62f8d7ee2b1c578228f)) 223 | 224 | ## [0.6.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.6.0...@swc-node/core@0.6.1) (2020-09-01) 225 | 226 | ### Bug Fixes 227 | 228 | - **core:** macOS 10.13 compatible issue ([c5917ac](https://github.com/swc-project/swc-node/commit/c5917ac54678757afeca1600ff2a9c459190dfd1)) 229 | 230 | # [0.6.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.4.2...@swc-node/core@0.6.0) (2020-08-31) 231 | 232 | ### Features 233 | 234 | - **core:** provide jest hoist plugin ([f3638f2](https://github.com/swc-project/swc-node/commit/f3638f2004b9fb323261a301b6fe354255846965)) 235 | 236 | # [0.5.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.4.2...@swc-node/core@0.5.0) (2020-08-28) 237 | 238 | ### Features 239 | 240 | - **core:** provide jest hoist plugin ([f3638f2](https://github.com/swc-project/swc-node/commit/f3638f2004b9fb323261a301b6fe354255846965)) 241 | 242 | ## [0.4.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.4.1...@swc-node/core@0.4.2) (2020-08-24) 243 | 244 | ### Bug Fixes 245 | 246 | - **core:** linux musl addon loader ([c6e8a78](https://github.com/swc-project/swc-node/commit/c6e8a7858f504eaabf07254cf7e3ec42eee432eb)) 247 | 248 | ## [0.4.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.4.0...@swc-node/core@0.4.1) (2020-08-23) 249 | 250 | ### Bug Fixes 251 | 252 | - wrong benchmark setup ([7bd298d](https://github.com/swc-project/swc-node/commit/7bd298d1d4cf3dddf770caa75671e681066e5b83)) 253 | 254 | # [0.4.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.3.0...@swc-node/core@0.4.0) (2020-08-23) 255 | 256 | ### Bug Fixes 257 | 258 | - **core:** version.js ([f8ba87d](https://github.com/swc-project/swc-node/commit/f8ba87d13bb9768c1725512fba5168027a3319ac)) 259 | 260 | ### Features 261 | 262 | - **core:** support linux musl ([9e1bac7](https://github.com/swc-project/swc-node/commit/9e1bac7a55d4c6dd9f99eaaf1acbade16339c8fe)) 263 | 264 | # [0.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.9...@swc-node/core@0.3.0) (2020-08-21) 265 | 266 | ### Features 267 | 268 | - **core:** TypeScript 4.0 ([c18ab85](https://github.com/swc-project/swc-node/commit/c18ab85f6911ca44e6db70894ee21a0653695411)) 269 | 270 | ## [0.2.9](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.8...@swc-node/core@0.2.9) (2020-08-20) 271 | 272 | ### Bug Fixes 273 | 274 | - **core:** transform api ([11f2601](https://github.com/swc-project/swc-node/commit/11f26018a0860afcf33b7e86dcc44975096489e4)) 275 | 276 | ## [0.2.8](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.7...@swc-node/core@0.2.8) (2020-08-20) 277 | 278 | **Note:** Version bump only for package @swc-node/core 279 | 280 | ## [0.2.7](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.6...@swc-node/core@0.2.7) (2020-08-20) 281 | 282 | ### Features 283 | 284 | - **core:** add filename to error message if compile failed ([dddf0ac](https://github.com/swc-project/swc-node/commit/dddf0acac53723db382d79a82cb6153b5dd10dd6)) 285 | 286 | ## [0.2.6](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.5...@swc-node/core@0.2.6) (2020-08-18) 287 | 288 | ### Performance Improvements 289 | 290 | - **core:** around ~25 performance gain ([4d6fc06](https://github.com/swc-project/swc-node/commit/4d6fc0687e2890dcfdbd0fe33c3d45c1d743876f)) 291 | 292 | ## [0.2.5](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.4...@swc-node/core@0.2.5) (2020-08-14) 293 | 294 | ### Bug Fixes 295 | 296 | - **core:** Allow referencing global idents even when it's injected ([dcf44e5](https://github.com/swc-project/swc-node/commit/dcf44e5631cfaed68310e2447882ff3aa88b652a)) 297 | 298 | ## [0.2.4](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.3...@swc-node/core@0.2.4) (2020-08-13) 299 | 300 | ### Bug Fixes 301 | 302 | - **core:** bugfix from upstream ([2bcb946](https://github.com/swc-project/swc-node/commit/2bcb9461d7d3c2dba9944f1c3bc746dc5c375ca3)) 303 | 304 | ## [0.2.3](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.2...@swc-node/core@0.2.3) (2020-08-10) 305 | 306 | ### Bug Fixes 307 | 308 | - **core:** strip TypeScript class properties without value assigned ([88f7022](https://github.com/swc-project/swc-node/commit/88f7022bb555cc2063c4f95743a88633a6aadb46)) 309 | 310 | ## [0.2.2](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.1...@swc-node/core@0.2.2) (2020-08-10) 311 | 312 | ### Bug Fixes 313 | 314 | - **core:** class properties transform ([de3d564](https://github.com/swc-project/swc-node/commit/de3d5647c48202ceb12cd90cd59311d8bc1607f4)) 315 | 316 | ## [0.2.1](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.2.0...@swc-node/core@0.2.1) (2020-08-09) 317 | 318 | ### Bug Fixes 319 | 320 | - **core:** missing jsx option ([0518375](https://github.com/swc-project/swc-node/commit/0518375485974258461910b78e73b49ef5b4f67b)) 321 | 322 | # [0.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/core@0.1.12...@swc-node/core@0.2.0) (2020-08-09) 323 | 324 | ### Features 325 | 326 | - **core:** add support for emitDecoratorMetadata ([edd2fd5](https://github.com/swc-project/swc-node/commit/edd2fd575bf43bf4206a49b5b078945de5eae95a)) 327 | -------------------------------------------------------------------------------- /packages/core/README.md: -------------------------------------------------------------------------------- 1 | # `@swc-node/core` 2 | 3 | Downloads 4 | 5 | > 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn) 6 | 7 | ## Benchmark 8 | 9 | > transform AjaxObservable.ts to ES2015 & CommonJS `JavaScript`. 10 | 11 | ``` 12 | @swc-node/core x 151 ops/sec ±3.74% (77 runs sampled) 13 | @swc/core x 107 ops/sec ±0.51% (78 runs sampled) 14 | esbuild x 52.89 ops/sec ±1.58% (67 runs sampled) 15 | typescript x 21.08 ops/sec ±9.68% (40 runs sampled) 16 | Transform rxjs/AjaxObservable.ts benchmark bench suite: Fastest is @swc-node/core 17 | ``` 18 | 19 | ## Usage 20 | 21 | ```ts 22 | export interface Options { 23 | target?: 'es3' | 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' 24 | module?: 'commonjs' | 'umd' | 'amd' | 'es6' 25 | sourcemap?: boolean | 'inline' 26 | experimentalDecorators?: boolean 27 | emitDecoratorMetadata?: boolean 28 | dynamicImport?: boolean 29 | esModuleInterop?: boolean 30 | keepClassNames?: boolean 31 | react?: Partial 32 | paths?: { 33 | [from: string]: [string] 34 | } 35 | } 36 | 37 | export interface ReactConfig { 38 | /** 39 | * Replace the function used when compiling JSX expressions. 40 | * 41 | * Defaults to `React.createElement`. 42 | */ 43 | pragma: string 44 | /** 45 | * Replace the component used when compiling JSX fragments. 46 | * 47 | * Defaults to `React.Fragment` 48 | */ 49 | pragmaFrag: string 50 | /** 51 | * Toggles whether or not to throw an error if a XML namespaced tag name is used. For example: 52 | * `` 53 | * 54 | * Though the JSX spec allows this, it is disabled by default since React's 55 | * JSX does not currently have support for it. 56 | * 57 | */ 58 | throwIfNamespace: boolean 59 | /** 60 | * Toggles plugins that aid in development, such as @swc/plugin-transform-react-jsx-self 61 | * and @swc/plugin-transform-react-jsx-source. 62 | * 63 | * Defaults to `false`, 64 | * 65 | */ 66 | development: boolean 67 | /** 68 | * Use `Object.assign()` instead of `_extends`. Defaults to false. 69 | */ 70 | useBuiltins: boolean 71 | } 72 | 73 | export function transformSync( 74 | source: string | Buffer, 75 | path: string, 76 | options?: Options, 77 | ): { 78 | code: string 79 | map: string 80 | } 81 | 82 | export function transform( 83 | source: string | Buffer, 84 | path: string, 85 | options?: Options, 86 | ): Promise<{ 87 | code: string 88 | map: string 89 | }> 90 | ``` 91 | -------------------------------------------------------------------------------- /packages/core/index.ts: -------------------------------------------------------------------------------- 1 | import { 2 | transform as swcTransform, 3 | transformSync as swcTransformSync, 4 | Options as SwcOptions, 5 | ReactConfig, 6 | Config, 7 | JscTarget, 8 | } from '@swc/core' 9 | 10 | // Oldest LTS Node.js supported target 11 | const DEFAULT_ES_TARGET: JscTarget = 'es2018' 12 | 13 | export interface Options { 14 | target?: JscTarget 15 | module?: 'commonjs' | 'umd' | 'amd' | 'es6' 16 | sourcemap?: Config['sourceMaps'] 17 | jsx?: boolean 18 | experimentalDecorators?: boolean 19 | emitDecoratorMetadata?: boolean 20 | useDefineForClassFields?: boolean 21 | dynamicImport?: boolean 22 | esModuleInterop?: boolean 23 | keepClassNames?: boolean 24 | externalHelpers?: boolean 25 | react?: Partial 26 | baseUrl?: string 27 | paths?: { 28 | [from: string]: [string] 29 | } 30 | swc?: SwcOptions 31 | ignoreDynamic?: boolean 32 | } 33 | 34 | function transformOption(path: string, options?: Options, jest = false): SwcOptions { 35 | const opts = options ?? {} 36 | opts.esModuleInterop = opts.esModuleInterop ?? true 37 | const moduleType = options?.module ?? 'commonjs' 38 | return { 39 | filename: path, 40 | jsc: options?.swc?.swcrc 41 | ? undefined 42 | : { 43 | target: opts.target ?? DEFAULT_ES_TARGET, 44 | externalHelpers: jest ? true : Boolean(opts.externalHelpers), 45 | parser: { 46 | syntax: 'typescript' as const, 47 | tsx: typeof opts.jsx !== 'undefined' ? opts.jsx : path.endsWith('.tsx'), 48 | decorators: Boolean(opts.experimentalDecorators), 49 | dynamicImport: Boolean(opts.dynamicImport), 50 | }, 51 | transform: { 52 | legacyDecorator: Boolean(opts.experimentalDecorators), 53 | decoratorMetadata: Boolean(opts.emitDecoratorMetadata), 54 | useDefineForClassFields: Boolean(opts.useDefineForClassFields), 55 | react: options?.react, 56 | // @ts-expect-error 57 | hidden: { 58 | jest, 59 | }, 60 | }, 61 | keepClassNames: opts.keepClassNames, 62 | paths: opts.paths, 63 | baseUrl: opts.baseUrl, 64 | experimental: { 65 | keepImportAttributes: true, 66 | }, 67 | }, 68 | minify: false, 69 | isModule: true, 70 | module: options?.swc?.swcrc 71 | ? undefined 72 | : { 73 | type: moduleType, 74 | ...(moduleType === 'commonjs' || moduleType === 'umd' || moduleType === 'amd' 75 | ? { 76 | noInterop: !opts.esModuleInterop, 77 | ignoreDynamic: opts.ignoreDynamic, 78 | } 79 | : undefined), 80 | }, 81 | sourceMaps: options?.swc?.swcrc 82 | ? undefined 83 | : jest || typeof opts.sourcemap === 'undefined' 84 | ? 'inline' 85 | : opts.sourcemap, 86 | inlineSourcesContent: true, 87 | swcrc: false, 88 | ...options?.swc, 89 | } 90 | } 91 | 92 | export function transformSync(source: string, path: string, options?: Options) { 93 | return swcTransformSync(source, transformOption(path, options)) 94 | } 95 | 96 | export function transformJest(source: string, path: string, options?: Options) { 97 | return swcTransformSync(source, transformOption(path, options, true)) 98 | } 99 | 100 | export function transform(source: string, path: string, options?: Options) { 101 | return swcTransform(source, transformOption(path, options)) 102 | } 103 | -------------------------------------------------------------------------------- /packages/core/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@swc-node/core", 3 | "version": "1.13.3", 4 | "description": "Faster swc nodejs binding", 5 | "keywords": [ 6 | "swc", 7 | "babel", 8 | "esbuild", 9 | "rust", 10 | "n-api", 11 | "napi", 12 | "node-rs", 13 | "uglify", 14 | "napi-rs", 15 | "terser", 16 | "webpack", 17 | "ts-node", 18 | "typescript", 19 | "tsc" 20 | ], 21 | "author": "LongYinan ", 22 | "homepage": "https://github.com/swc-project/swc-node", 23 | "license": "MIT", 24 | "main": "lib/index.js", 25 | "typings": "lib/index.d.ts", 26 | "files": [ 27 | "lib" 28 | ], 29 | "engines": { 30 | "node": ">= 10" 31 | }, 32 | "publishConfig": { 33 | "registry": "https://registry.npmjs.org/", 34 | "access": "public" 35 | }, 36 | "repository": { 37 | "type": "git", 38 | "url": "git+https://github.com/swc-project/swc-node.git" 39 | }, 40 | "bugs": { 41 | "url": "https://github.com/swc-project/swc-node/issues" 42 | }, 43 | "peerDependencies": { 44 | "@swc/core": ">= 1.4.13", 45 | "@swc/types": ">= 0.1" 46 | }, 47 | "devDependencies": { 48 | "@swc/core": "^1.6.6", 49 | "@swc/types": "^0.1.9" 50 | }, 51 | "funding": { 52 | "type": "github", 53 | "url": "https://github.com/sponsors/Brooooooklyn" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /packages/core/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "composite": true, 5 | "rootDir": ".", 6 | "outDir": "./lib", 7 | }, 8 | "include": ["."], 9 | "exclude": ["./lib"], 10 | } 11 | -------------------------------------------------------------------------------- /packages/integrate-module/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "integrate-module", 3 | "type": "module", 4 | "version": "0.0.0", 5 | "private": true, 6 | "dependencies": { 7 | "file-type": "^20.0.0", 8 | "p-timeout": "^6.1.2" 9 | }, 10 | "devDependencies": { 11 | "@napi-rs/simple-git": "^0.1.16", 12 | "@swc/core": "^1.6.6", 13 | "@swc-node/register": "workspace:*", 14 | "@types/react": "^19.0.0", 15 | "@types/react-dom": "^19.0.0", 16 | "esmock": "^2.6.6", 17 | "ipaddr.js": "^2.2.0", 18 | "postgres": "^3.4.4", 19 | "react": "^19.0.0", 20 | "react-dom": "^19.0.0", 21 | "simple-git": "^3.25.0", 22 | "typescript": "^5.5.3" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /packages/integrate-module/src/cjs/index.d.ts: -------------------------------------------------------------------------------- 1 | declare const exports: { 2 | default: () => string 3 | } 4 | 5 | export default exports 6 | -------------------------------------------------------------------------------- /packages/integrate-module/src/cjs/index.js: -------------------------------------------------------------------------------- 1 | // this undesirable output was commonly generated by babel w browser-breaking 2 | // defineProperty and default.default definitions 3 | 4 | /* eslint-disable */ 5 | 'use strict' 6 | 7 | Object.defineProperty(exports, '__esModule', { 8 | value: true, 9 | }) 10 | exports['default'] = void 0 11 | 12 | function _typeof(obj) { 13 | '@babel/helpers - typeof' 14 | if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') { 15 | _typeof = function _typeof(obj) { 16 | return typeof obj 17 | } 18 | } else { 19 | _typeof = function _typeof(obj) { 20 | return obj && typeof Symbol === 'function' && obj.constructor === Symbol && obj !== Symbol.prototype 21 | ? 'symbol' 22 | : typeof obj 23 | } 24 | } 25 | return _typeof(obj) 26 | } 27 | 28 | var _default = function _default() { 29 | return 'default.default' 30 | } 31 | 32 | exports['default'] = _default 33 | /* eslint-enable */ 34 | -------------------------------------------------------------------------------- /packages/integrate-module/src/cjs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "problematic cjs file type generated by babel", 3 | "main": "./index.js", 4 | "types": "./index.d.ts", 5 | "private": true 6 | } 7 | -------------------------------------------------------------------------------- /packages/integrate-module/src/common.cjs: -------------------------------------------------------------------------------- 1 | module.exports.common = "common"; -------------------------------------------------------------------------------- /packages/integrate-module/src/common.d.cts: -------------------------------------------------------------------------------- 1 | export declare const common: string 2 | -------------------------------------------------------------------------------- /packages/integrate-module/src/compiled.d.ts: -------------------------------------------------------------------------------- 1 | export declare class CompiledClass { 2 | name: string 3 | } 4 | -------------------------------------------------------------------------------- /packages/integrate-module/src/compiled.js: -------------------------------------------------------------------------------- 1 | export class CompiledClass { 2 | constructor() { 3 | this.name = 'CompiledClass' 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /packages/integrate-module/src/component.tsx: -------------------------------------------------------------------------------- 1 | export function Component() { 2 | return
Component
3 | } 4 | -------------------------------------------------------------------------------- /packages/integrate-module/src/foo.mts: -------------------------------------------------------------------------------- 1 | export function foo() { 2 | return 'foo' 3 | } 4 | -------------------------------------------------------------------------------- /packages/integrate-module/src/index.ts: -------------------------------------------------------------------------------- 1 | /* eslint import/order: off */ 2 | import assert from 'node:assert' 3 | import test from 'node:test' 4 | 5 | import { RepositoryState } from '@napi-rs/simple-git' 6 | import { bar as subBar } from '@subdirectory/bar.mjs' 7 | import esmock from 'esmock' 8 | import { supportedExtensions } from 'file-type' 9 | import { renderToString } from 'react-dom/server' 10 | import { simpleGit } from 'simple-git' 11 | import ipaddr from 'ipaddr.js' 12 | import postgres from 'postgres' 13 | 14 | import { CompiledClass } from './compiled.js' 15 | import cjs from './cjs' 16 | import { foo } from './foo.mjs' 17 | import { bar } from './subdirectory/bar.mjs' 18 | import { baz } from './subdirectory/index.mjs' 19 | import { Component } from './component.js' 20 | import { common } from './common.cjs' 21 | import './js-module.mjs' 22 | import pgkJson from '../package.json' assert { type: 'json' } 23 | import pgkJsonWith from '../package.json' with { type: 'json' } 24 | 25 | const { foo: fooWithQuery } = await import(`./foo.mjs?q=${Date.now()}`) 26 | 27 | await test('file-type should work', () => { 28 | assert.ok(supportedExtensions.has('jpg')) 29 | }) 30 | 31 | await test('resolve adjacent file path', () => { 32 | assert.equal(foo(), 'foo') 33 | }) 34 | 35 | await test('resolve nested file path', () => { 36 | assert.equal(bar(), 'bar') 37 | }) 38 | 39 | await test('resolve nested entry point', () => { 40 | assert.equal(baz(), 'baz') 41 | }) 42 | 43 | await test('resolve paths', () => { 44 | assert.equal(subBar(), 'bar') 45 | }) 46 | 47 | await test('resolve with query', () => { 48 | assert.equal(fooWithQuery(), 'foo') 49 | }) 50 | 51 | await test('compiled js file with .d.ts', () => { 52 | const instance = new CompiledClass() 53 | assert.equal(instance.name, 'CompiledClass') 54 | }) 55 | 56 | await test('jsx should work', () => { 57 | assert.equal(renderToString(Component()), '
Component
') 58 | }) 59 | 60 | await test('resolve @napi-rs projects', () => { 61 | assert.equal(RepositoryState.ApplyMailbox, 10) 62 | }) 63 | 64 | await test('resolve simple-git', () => { 65 | assert.ok(simpleGit) 66 | }) 67 | 68 | await test('resolve local cjs module', () => { 69 | assert.equal(cjs.default(), 'default.default') 70 | }) 71 | 72 | await test('resolve commonjs module', () => { 73 | assert.equal(common, 'common') 74 | }) 75 | 76 | await test('resolve json file', () => { 77 | assert.equal(pgkJson.name, 'integrate-module') 78 | assert.equal(pgkJsonWith.name, 'integrate-module') 79 | }) 80 | 81 | await test('resolve ipaddr.js', () => { 82 | assert.ok(ipaddr.isValid('192.168.1.1')) 83 | }) 84 | 85 | await test('esmock should work', async () => { 86 | const main = await esmock('./mocked.ts', { 87 | path: { 88 | basename: () => 'hello', 89 | }, 90 | }) 91 | 92 | assert.strictEqual(main.pathbasenamewrap(), 'hello') 93 | }) 94 | 95 | await test('postgres should work', async () => { 96 | postgres({ 97 | host: 'postgres://localhost', 98 | }) 99 | }) 100 | -------------------------------------------------------------------------------- /packages/integrate-module/src/js-module.mjs: -------------------------------------------------------------------------------- 1 | /* eslint import/order: off */ 2 | import assert from 'node:assert' 3 | import test from 'node:test' 4 | 5 | import { supportedExtensions } from 'file-type' 6 | 7 | import { foo } from './foo.mts' 8 | import { bar } from './subdirectory/bar.mts' 9 | import { baz } from './subdirectory/index.mts' 10 | import { bar as subBar } from '@subdirectory/bar.mts' 11 | 12 | await test('js:file-type should work', () => { 13 | assert.ok(supportedExtensions.has('jpg')) 14 | }) 15 | 16 | await test('js:resolve adjacent file path', () => { 17 | assert.equal(foo(), 'foo') 18 | }) 19 | 20 | await test('js:resolve nested file path', () => { 21 | assert.equal(bar(), 'bar') 22 | }) 23 | 24 | await test('js:resolve nested entry point', () => { 25 | assert.equal(baz(), 'baz') 26 | }) 27 | 28 | await test('js:resolve paths', () => { 29 | assert.equal(subBar(), 'bar') 30 | }) 31 | -------------------------------------------------------------------------------- /packages/integrate-module/src/mocked.ts: -------------------------------------------------------------------------------- 1 | import path from 'path' 2 | 3 | export default { 4 | pathbasenamewrap: (n: string) => path.basename(n), 5 | } 6 | -------------------------------------------------------------------------------- /packages/integrate-module/src/subdirectory/bar.mts: -------------------------------------------------------------------------------- 1 | export function bar() { 2 | return 'bar' 3 | } 4 | -------------------------------------------------------------------------------- /packages/integrate-module/src/subdirectory/baz.mts: -------------------------------------------------------------------------------- 1 | export function baz() { 2 | return 'baz' 3 | } 4 | -------------------------------------------------------------------------------- /packages/integrate-module/src/subdirectory/index.mts: -------------------------------------------------------------------------------- 1 | export { baz } from './baz.mjs' 2 | export function module() { 3 | return 'module' 4 | } 5 | -------------------------------------------------------------------------------- /packages/integrate-module/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "target": "ESNext", 5 | "module": "ESNext", 6 | "composite": true, 7 | "jsx": "react-jsx", 8 | "outDir": "dist", 9 | "baseUrl": "./", 10 | "paths": { 11 | "@subdirectory/*": ["./src/subdirectory/*"] 12 | } 13 | }, 14 | "include": ["src", "package.json"] 15 | } 16 | -------------------------------------------------------------------------------- /packages/integrate/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | ## [0.1.1](https://github.com/swc-project/swc-node/compare/@swc-node/integrate@0.1.0...@swc-node/integrate@0.1.1) (2021-06-07) 7 | 8 | **Note:** Version bump only for package @swc-node/integrate 9 | 10 | # 0.1.0 (2021-05-21) 11 | 12 | ### Bug Fixes 13 | 14 | - **core:** wrong this in function call expr in optional chains ([5d9966f](https://github.com/swc-project/swc-node/commit/5d9966f9eb4b1026356a3f824568f9804faa05ff)) 15 | - **register:** add fastpath to handle .d.ts file ([afe4ca9](https://github.com/swc-project/swc-node/commit/afe4ca921985ecce907cb752fb674c8278041bc0)) 16 | - **register:** integrate tests ([b012869](https://github.com/swc-project/swc-node/commit/b0128698e7274d37856e2593849d58f67e9e302b)) 17 | 18 | ### Features 19 | 20 | - **core:** support react config in transformOptions ([313e021](https://github.com/swc-project/swc-node/commit/313e02128f833b09f4bf6dd9200b82819cb734cc)) 21 | - **jest:** jest integration ([2b1f837](https://github.com/swc-project/swc-node/commit/2b1f83773683d5f9a1c7d7fab27d8fe35338b6b9)) 22 | - **loader:** implement tsconfig compatible loader ([8c1cd85](https://github.com/swc-project/swc-node/commit/8c1cd858a64a6b6ec6ff23811bafab7dfe30554d)) 23 | - **register:** support read include/exclude config from tsconfig ([0dec2cd](https://github.com/swc-project/swc-node/commit/0dec2cdcf002c361abef068cf227a8bfe6cdea2a)) 24 | - add name-shadowed fixture ([f0ea5e7](https://github.com/swc-project/swc-node/commit/f0ea5e7331a99b7c62123cd64382613c7fe84221)) 25 | - init toolchains ([9585294](https://github.com/swc-project/swc-node/commit/9585294b79765c0cf15d376133d5a593a50fbe86)) 26 | -------------------------------------------------------------------------------- /packages/integrate/README.md: -------------------------------------------------------------------------------- 1 | # `@swc-node/integrate` 2 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/class-property-order/class-property-order.spec.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | import Sinon from 'sinon' 3 | 4 | import { Base } from './class-property-order' 5 | 6 | test('hygiene constructor variable', (t) => { 7 | const spy = Sinon.spy() 8 | const base = new Base({ logger: { child: (data) => () => spy(data) } }) 9 | base.log() 10 | t.is(spy.callCount, 2) 11 | const [[arg]] = spy.args 12 | t.is(arg.component, 'Base') 13 | }) 14 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/class-property-order/class-property-order.ts: -------------------------------------------------------------------------------- 1 | interface Container { 2 | logger: { child: (data: { component: string }) => () => void } 3 | } 4 | 5 | export class Base { 6 | readonly log = this.container.logger.child({ component: this.constructor.name }) 7 | 8 | constructor(protected readonly container: C) { 9 | this.log() 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/declare-file/declare.d.ts: -------------------------------------------------------------------------------- 1 | export declare function Foo(): number 2 | 3 | declare module '*.svg' 4 | 5 | Undeclared 6 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/declare-file/declare.spec.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | 3 | import { Foo } from './declare' 4 | 5 | test('should handle declare file', (t) => { 6 | const fixture = 42 7 | const foo: typeof Foo = () => fixture 8 | 9 | t.is(foo(), fixture) 10 | }) 11 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/hygiene/class.ts: -------------------------------------------------------------------------------- 1 | const DURATION = 1000 2 | 3 | export class HygieneTest { 4 | private readonly duration: number = DURATION 5 | 6 | constructor(duration?: number) { 7 | this.duration = duration ?? DURATION 8 | } 9 | 10 | getDuration() { 11 | return this.duration 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/hygiene/hygiene.spec.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | 3 | import { HygieneTest } from './class' 4 | 5 | test('hygiene constructor variable', (t) => { 6 | const DURATION = 1000 7 | const a = new HygieneTest() 8 | t.is(a.getDuration(), DURATION) 9 | }) 10 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/name-shadowed/name-shadowed.spec.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | 3 | import { setup } from './shadowed' 4 | 5 | test('shadowed name spec', (t) => { 6 | const url = '/a/b/c' 7 | const qs = { 8 | foo: '1', 9 | bar: '2', 10 | } 11 | t.is(setup(url, qs), url + '?' + 'foo=1&bar=2') 12 | }) 13 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/name-shadowed/shadowed.ts: -------------------------------------------------------------------------------- 1 | import { queryString } from './url' 2 | 3 | export function setup(url: string, obj: any) { 4 | const _queryString = queryString(obj) 5 | const _url = url + '?' + _queryString 6 | return _url 7 | } 8 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/name-shadowed/url.ts: -------------------------------------------------------------------------------- 1 | export function queryString(object: any) { 2 | return Object.entries(object) 3 | .reduce((acc, [key, value]) => { 4 | acc.push(`${key}=${value}`) 5 | return acc 6 | }, [] as string[]) 7 | .join('&') 8 | } 9 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/optional-call/optional-chain-function-call.spec.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | 3 | test('optional chain function call', (t) => { 4 | const obj = { 5 | a: { 6 | b: { 7 | c: function () { 8 | return this.foo 9 | }, 10 | foo: 2, 11 | }, 12 | foo: 1, 13 | }, 14 | } 15 | 16 | t.is(obj?.a?.b?.c(), 2) 17 | }) 18 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/paths/js-paths.spec.js: -------------------------------------------------------------------------------- 1 | import { FIXTURE } from '@integrate/fixture' 2 | import test from 'ava' 3 | 4 | test('js should transpile paths', (t) => { 5 | t.is(FIXTURE, 'fixture') 6 | }) 7 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/paths/paths.spec.ts: -------------------------------------------------------------------------------- 1 | import { FIXTURE } from '@integrate/fixture' 2 | import test from 'ava' 3 | 4 | test('should transpile paths', (t) => { 5 | t.is(FIXTURE, 'fixture') 6 | }) 7 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/react-pragma/__snapshots__/react-pragma.spec.ts.md: -------------------------------------------------------------------------------- 1 | # Snapshot report for `packages/integrate/__tests__/react-pragma/react-pragma.spec.ts` 2 | 3 | The actual snapshot is saved in `react-pragma.spec.ts.snap`. 4 | 5 | Generated by [AVA](https://avajs.dev). 6 | 7 | ## should transform jsx factory use React.pragma 8 | 9 | > Snapshot 1 10 | 11 | `"use strict";␊ 12 | const Button = ({ text })=>/*#__PURE__*/ h("div", null, text);␊ 13 | ␊ 14 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sIm1hcHBpbmdzIjoiO0FBQ0UsTUFBTUEsU0FBUyxDQUFDLEVBQUVDLElBQUksRUFBRSxpQkFDdEIsRUFBQ0MsYUFDRUQifQ==` 15 | 16 | ## should transform jsx into new jsx runtime 17 | 18 | > Snapshot 1 19 | 20 | `"use strict";␊ 21 | Object.defineProperty(exports, "__esModule", {␊ 22 | value: true␊ 23 | });␊ 24 | const _jsxruntime = require("react/jsx-runtime");␊ 25 | const Button = ({ text })=>/*#__PURE__*/ (0, _jsxruntime.jsx)("div", {␊ 26 | children: text␊ 27 | });␊ 28 | ␊ 29 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sIm1hcHBpbmdzIjoiOzs7OztBQUNFLE1BQU1BLFNBQVMsQ0FBQyxFQUFFQyxJQUFJLEVBQUUsaUJBQ3RCLHFCQUFDQztrQkFDRUQifQ==` 30 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/react-pragma/__snapshots__/react-pragma.spec.ts.snap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swc-project/swc-node/1c896356dc5573bd4c6103a94bb7cbc7376a6f05/packages/integrate/__tests__/react-pragma/__snapshots__/react-pragma.spec.ts.snap -------------------------------------------------------------------------------- /packages/integrate/__tests__/react-pragma/react-pragma.spec.ts: -------------------------------------------------------------------------------- 1 | import { transform } from '@swc-node/core' 2 | import test from 'ava' 3 | 4 | const fixture = ` 5 | const Button = ({ text }) => ( 6 |
7 | {text} 8 |
9 | ) 10 | ` 11 | 12 | test('should transform jsx factory use React.pragma', async (t) => { 13 | t.snapshot( 14 | ( 15 | await transform(fixture, 'test.tsx', { 16 | react: { 17 | pragma: 'h', 18 | }, 19 | }) 20 | ).code, 21 | ) 22 | }) 23 | 24 | test('should transform jsx into new jsx runtime', async (t) => { 25 | t.snapshot( 26 | ( 27 | await transform(fixture, 'test.tsx', { 28 | react: { 29 | runtime: 'automatic', 30 | }, 31 | }) 32 | ).code, 33 | ) 34 | }) 35 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.md: -------------------------------------------------------------------------------- 1 | # Snapshot report for `packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts` 2 | 3 | The actual snapshot is saved in `sourcemaps.spec.ts.snap`. 4 | 5 | Generated by [AVA](https://avajs.dev). 6 | 7 | ## should work with sourcemaps 8 | 9 | > Snapshot 1 10 | 11 | `Error: ␊ 12 | at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:18:26␊ 13 | at Test.callFn (file:///node_modules/.pnpm/ava@6.2.0_encoding@0.1.13/node_modules/ava/lib/test.js:525:26)␊ 14 | at Test.run (file:///node_modules/.pnpm/ava@6.2.0_encoding@0.1.13/node_modules/ava/lib/test.js:534:33)␊ 15 | at Runner.runSingle (file:///node_modules/.pnpm/ava@6.2.0_encoding@0.1.13/node_modules/ava/lib/runner.js:281:33)␊ 16 | at Runner.runTest (file:///node_modules/.pnpm/ava@6.2.0_encoding@0.1.13/node_modules/ava/lib/runner.js:363:30)␊ 17 | at async Promise.all (index 0)␊ 18 | at file:///node_modules/.pnpm/ava@6.2.0_encoding@0.1.13/node_modules/ava/lib/runner.js:528:21␊ 19 | at Runner.start (file:///node_modules/.pnpm/ava@6.2.0_encoding@0.1.13/node_modules/ava/lib/runner.js:536:15)` 20 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.snap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swc-project/swc-node/1c896356dc5573bd4c6103a94bb7cbc7376a6f05/packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.snap -------------------------------------------------------------------------------- /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts: -------------------------------------------------------------------------------- 1 | import { join } from 'path' 2 | 3 | import test from 'ava' 4 | 5 | // @ts-expect-error 6 | interface _Unused1 {} 7 | // @ts-expect-error 8 | interface _Unused2 {} 9 | // @ts-expect-error 10 | interface _Unused3 {} 11 | // @ts-expect-error 12 | interface _Unused4 {} 13 | // @ts-expect-error 14 | interface _Unused5 {} 15 | // @ts-expect-error 16 | interface _Unused6 {} 17 | // @ts-expect-error 18 | interface _Unused7 {} 19 | // @ts-expect-error 20 | interface _Unused8 {} 21 | // @ts-expect-error 22 | interface _Unused9 {} 23 | 24 | test('should work with sourcemaps', (t) => { 25 | if (process.platform === 'win32') { 26 | return t.pass('Skip on Windows') 27 | } 28 | const projectRoot = join(__dirname, '..', '..', '..', '..') 29 | t.snapshot( 30 | new Error().stack 31 | ?.split('\n') 32 | .map((l) => l.replace(projectRoot, '')) 33 | .filter((n) => !n.includes('node:internal')) 34 | .join('\n'), 35 | ) 36 | }) 37 | -------------------------------------------------------------------------------- /packages/integrate/__tests__/super/class-super.spec.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | 3 | class BadRequestError extends Error { 4 | constructor(public readonly message: string) { 5 | super(message) 6 | } 7 | } 8 | 9 | test('should handler class super', (t) => { 10 | const message = 'Bad request' 11 | const e = new BadRequestError(message) 12 | t.is(e.message, message) 13 | }) 14 | -------------------------------------------------------------------------------- /packages/integrate/jest/inline-snapshot/inline-snapshot.test.ts: -------------------------------------------------------------------------------- 1 | test('should support inlineSnapshot', () => { 2 | const fixture = Buffer.from('hello') 3 | expect(fixture.toString('base64')).toMatchInlineSnapshot(`"aGVsbG8="`) 4 | }) 5 | -------------------------------------------------------------------------------- /packages/integrate/jest/inline-snapshot/jsx-runtime.test.tsx: -------------------------------------------------------------------------------- 1 | import { renderToString } from 'react-dom/server' 2 | 3 | function Component() { 4 | return
Hello
5 | } 6 | 7 | test('should read jsx runtime options from tsconfig', () => { 8 | expect(renderToString()).toMatchInlineSnapshot(`"
Hello
"`) 9 | }) 10 | -------------------------------------------------------------------------------- /packages/integrate/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@swc-node/integrate", 3 | "version": "0.1.1", 4 | "description": "Integrate testing", 5 | "keywords": [ 6 | "ava", 7 | "swc", 8 | "ts-node" 9 | ], 10 | "author": "LongYinan ", 11 | "homepage": "https://github.com/swc-project/swc-node", 12 | "license": "MIT", 13 | "private": true, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/swc-project/swc-node.git" 17 | }, 18 | "bugs": { 19 | "url": "https://github.com/swc-project/swc-node/issues" 20 | }, 21 | "devDependencies": { 22 | "@swc/helpers": "^0.5.11", 23 | "@swc-node/core": "^1.13.1", 24 | "@swc-node/register": "workspace:*", 25 | "@types/jest": "^29.5.12", 26 | "@types/react": "^19.0.0", 27 | "@types/react-dom": "^19.0.0", 28 | "jest": "^29.7.0", 29 | "react": "^19.0.0", 30 | "react-dom": "^19.0.0", 31 | "sinon": "^20.0.0", 32 | "typescript": "^5.5.3" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /packages/integrate/src/fixture.ts: -------------------------------------------------------------------------------- 1 | export const FIXTURE = 'fixture' 2 | -------------------------------------------------------------------------------- /packages/integrate/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "composite": true, 5 | "rootDir": ".", 6 | "outDir": "./lib", 7 | "jsx": "react-jsx", 8 | "types": ["node", "jest"], 9 | "allowImportingTsExtensions": true 10 | }, 11 | "references": [ 12 | { 13 | "path": "../core" 14 | }, 15 | { 16 | "path": "../register" 17 | } 18 | ], 19 | "include": ["."], 20 | "files": ["./package.json"], 21 | "exclude": ["lib"] 22 | } 23 | -------------------------------------------------------------------------------- /packages/jest/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | ## [1.8.13](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.12...@swc-node/jest@1.8.13) (2025-03-13) 7 | 8 | **Note:** Version bump only for package @swc-node/jest 9 | 10 | ## [1.8.12](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.11...@swc-node/jest@1.8.12) (2024-07-17) 11 | 12 | **Note:** Version bump only for package @swc-node/jest 13 | 14 | ## [1.8.11](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.10...@swc-node/jest@1.8.11) (2024-07-16) 15 | 16 | **Note:** Version bump only for package @swc-node/jest 17 | 18 | ## [1.8.10](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.9...@swc-node/jest@1.8.10) (2024-07-14) 19 | 20 | **Note:** Version bump only for package @swc-node/jest 21 | 22 | ## [1.8.9](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.8...@swc-node/jest@1.8.9) (2024-07-13) 23 | 24 | **Note:** Version bump only for package @swc-node/jest 25 | 26 | ## [1.8.8](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.7...@swc-node/jest@1.8.8) (2024-07-13) 27 | 28 | **Note:** Version bump only for package @swc-node/jest 29 | 30 | ## [1.8.7](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.6...@swc-node/jest@1.8.7) (2024-07-12) 31 | 32 | **Note:** Version bump only for package @swc-node/jest 33 | 34 | ## [1.8.6](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.5...@swc-node/jest@1.8.6) (2024-07-08) 35 | 36 | **Note:** Version bump only for package @swc-node/jest 37 | 38 | ## [1.8.5](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.4...@swc-node/jest@1.8.5) (2024-07-05) 39 | 40 | **Note:** Version bump only for package @swc-node/jest 41 | 42 | ## [1.8.4](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.3...@swc-node/jest@1.8.4) (2024-07-05) 43 | 44 | **Note:** Version bump only for package @swc-node/jest 45 | 46 | ## [1.8.3](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.2...@swc-node/jest@1.8.3) (2024-06-28) 47 | 48 | **Note:** Version bump only for package @swc-node/jest 49 | 50 | ## [1.8.2](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.1...@swc-node/jest@1.8.2) (2024-06-08) 51 | 52 | **Note:** Version bump only for package @swc-node/jest 53 | 54 | ## [1.8.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.8.0...@swc-node/jest@1.8.1) (2024-05-01) 55 | 56 | ### Bug Fixes 57 | 58 | - support compile js files. close [#761](https://github.com/swc-project/swc-node/issues/761) ([#767](https://github.com/swc-project/swc-node/issues/767)) ([016f1aa](https://github.com/swc-project/swc-node/commit/016f1aab2a17d2512d30b5a12848ed1941b59e49)) 59 | 60 | # [1.8.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.9...@swc-node/jest@1.8.0) (2024-03-05) 61 | 62 | ### Features 63 | 64 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 65 | 66 | # [1.7.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.9...@swc-node/jest@1.7.0) (2024-02-01) 67 | 68 | ### Features 69 | 70 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 71 | 72 | ## [1.6.9](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.8...@swc-node/jest@1.6.9) (2024-02-01) 73 | 74 | **Note:** Version bump only for package @swc-node/jest 75 | 76 | ## [1.6.8](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.7...@swc-node/jest@1.6.8) (2023-09-26) 77 | 78 | **Note:** Version bump only for package @swc-node/jest 79 | 80 | ## [1.6.7](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.6...@swc-node/jest@1.6.7) (2023-08-18) 81 | 82 | **Note:** Version bump only for package @swc-node/jest 83 | 84 | ## [1.6.6](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.5...@swc-node/jest@1.6.6) (2023-06-21) 85 | 86 | ### Bug Fixes 87 | 88 | - tsCompilerOptionsToSwcConfig should not override default swc config for jest ([#714](https://github.com/swc-project/swc-node/issues/714)) ([60ea642](https://github.com/swc-project/swc-node/commit/60ea64284582ce3164ca3705976b4dc4215c2504)) 89 | 90 | ## [1.6.5](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.4...@swc-node/jest@1.6.5) (2023-04-24) 91 | 92 | **Note:** Version bump only for package @swc-node/jest 93 | 94 | ## [1.6.4](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.3...@swc-node/jest@1.6.4) (2023-04-10) 95 | 96 | **Note:** Version bump only for package @swc-node/jest 97 | 98 | ## [1.6.3](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.2...@swc-node/jest@1.6.3) (2023-03-31) 99 | 100 | **Note:** Version bump only for package @swc-node/jest 101 | 102 | ## [1.6.2](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.1...@swc-node/jest@1.6.2) (2023-02-15) 103 | 104 | ### Bug Fixes 105 | 106 | - **core:** enforce sourcemap: 'inline' for jest ([#695](https://github.com/swc-project/swc-node/issues/695)) ([2439b7e](https://github.com/swc-project/swc-node/commit/2439b7e2cd28fee7d0d1038b91baeb042a32c146)) 107 | 108 | ## [1.6.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.6.0...@swc-node/jest@1.6.1) (2023-02-11) 109 | 110 | **Note:** Version bump only for package @swc-node/jest 111 | 112 | # [1.6.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.9...@swc-node/jest@1.6.0) (2023-02-10) 113 | 114 | ### Features 115 | 116 | - **register:** experimental esm loader ([#643](https://github.com/swc-project/swc-node/issues/643)) ([0b4d305](https://github.com/swc-project/swc-node/commit/0b4d30505408f6f07c1ff8ea5c1953e1d22bb4e1)) 117 | 118 | ## [1.5.9](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.8...@swc-node/jest@1.5.9) (2023-02-10) 119 | 120 | **Note:** Version bump only for package @swc-node/jest 121 | 122 | ## [1.5.8](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.7...@swc-node/jest@1.5.8) (2023-02-07) 123 | 124 | **Note:** Version bump only for package @swc-node/jest 125 | 126 | ## [1.5.7](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.6...@swc-node/jest@1.5.7) (2023-02-07) 127 | 128 | ### Bug Fixes 129 | 130 | - **register:** always inline swc helpers ([1d557ec](https://github.com/swc-project/swc-node/commit/1d557ece0d9ccbba027ff9f2d262c03d4b918bcb)) 131 | 132 | ## [1.5.6](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.5...@swc-node/jest@1.5.6) (2023-01-05) 133 | 134 | **Note:** Version bump only for package @swc-node/jest 135 | 136 | ## [1.5.5](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.4...@swc-node/jest@1.5.5) (2022-10-06) 137 | 138 | **Note:** Version bump only for package @swc-node/jest 139 | 140 | ## [1.5.4](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.3...@swc-node/jest@1.5.4) (2022-10-06) 141 | 142 | **Note:** Version bump only for package @swc-node/jest 143 | 144 | ## [1.5.3](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.2...@swc-node/jest@1.5.3) (2022-09-22) 145 | 146 | **Note:** Version bump only for package @swc-node/jest 147 | 148 | ## [1.5.2](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.1...@swc-node/jest@1.5.2) (2022-05-05) 149 | 150 | ### Performance Improvements 151 | 152 | - **jest:** readDefaultTsConfig should just execute once ([7223356](https://github.com/swc-project/swc-node/commit/7223356b820502627d4ab4a6e24a6ec698b9d031)) 153 | 154 | ## [1.5.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.5.0...@swc-node/jest@1.5.1) (2022-04-28) 155 | 156 | **Note:** Version bump only for package @swc-node/jest 157 | 158 | # [1.5.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.4.3...@swc-node/jest@1.5.0) (2022-04-27) 159 | 160 | ### Features 161 | 162 | - **jest:** read tsconfig for default jest transform options ([8c180e6](https://github.com/swc-project/swc-node/commit/8c180e68abbc66aa68f83b401d985a6c8617baa9)) 163 | 164 | ## [1.4.3](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.4.2...@swc-node/jest@1.4.3) (2021-12-15) 165 | 166 | **Note:** Version bump only for package @swc-node/jest 167 | 168 | ## [1.4.2](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.4.1...@swc-node/jest@1.4.2) (2021-12-13) 169 | 170 | **Note:** Version bump only for package @swc-node/jest 171 | 172 | ## [1.4.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.4.0...@swc-node/jest@1.4.1) (2021-11-14) 173 | 174 | ### Bug Fixes 175 | 176 | - **jest:** internal cache logic duplicated with jest cache ([43a36b9](https://github.com/swc-project/swc-node/commit/43a36b9be9aec0295a4a8d549fa189bef095d44f)) 177 | 178 | # [1.4.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.3.5...@swc-node/jest@1.4.0) (2021-11-08) 179 | 180 | ### Features 181 | 182 | - accept to transpile files with .mjs extension ([c1df0c8](https://github.com/swc-project/swc-node/commit/c1df0c827b8f8abaa7c5b04d837884378438385a)) 183 | 184 | ## [1.3.5](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.3.4...@swc-node/jest@1.3.5) (2021-10-29) 185 | 186 | ### Bug Fixes 187 | 188 | - **jest:** xxhash should be dependency ([5559136](https://github.com/swc-project/swc-node/commit/555913638c07d17470d80a9944cba9e7a18bd94c)) 189 | 190 | ## [1.3.4](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.3.3...@swc-node/jest@1.3.4) (2021-10-29) 191 | 192 | ### Performance Improvements 193 | 194 | - **jest:** replace sha1 with xxhash for better performance ([8d436dd](https://github.com/swc-project/swc-node/commit/8d436dd7000b9fee21c278bd294c017216e02b69)) 195 | 196 | ## [1.3.3](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.3.2...@swc-node/jest@1.3.3) (2021-10-16) 197 | 198 | **Note:** Version bump only for package @swc-node/jest 199 | 200 | ## [1.3.2](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.3.1...@swc-node/jest@1.3.2) (2021-09-11) 201 | 202 | **Note:** Version bump only for package @swc-node/jest 203 | 204 | ## [1.3.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.3.0...@swc-node/jest@1.3.1) (2021-06-07) 205 | 206 | **Note:** Version bump only for package @swc-node/jest 207 | 208 | # [1.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.2.1...@swc-node/jest@1.3.0) (2021-05-21) 209 | 210 | ### Features 211 | 212 | - **loader:** implement tsconfig compatible loader ([8c1cd85](https://github.com/swc-project/swc-node/commit/8c1cd858a64a6b6ec6ff23811bafab7dfe30554d)) 213 | 214 | ## [1.2.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.2.0...@swc-node/jest@1.2.1) (2021-04-28) 215 | 216 | **Note:** Version bump only for package @swc-node/jest 217 | 218 | # [1.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.1.1...@swc-node/jest@1.2.0) (2021-04-23) 219 | 220 | ### Features 221 | 222 | - **jest:** support jest 27 transformerConfig ([e2a51b2](https://github.com/swc-project/swc-node/commit/e2a51b2873898dcfc0a1b03a49f48ce70ea70f0a)) 223 | 224 | ## [1.1.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.1.0...@swc-node/jest@1.1.1) (2021-03-04) 225 | 226 | **Note:** Version bump only for package @swc-node/jest 227 | 228 | # [1.1.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.0.3...@swc-node/jest@1.1.0) (2021-01-25) 229 | 230 | ### Features 231 | 232 | - **jest:** cache transpile result to avoid source code to be compiled mult times ([77cc3d8](https://github.com/swc-project/swc-node/commit/77cc3d8ea82728b9b486e1b5fd898f82180c3f37)) 233 | 234 | ## [1.0.3](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.0.2...@swc-node/jest@1.0.3) (2021-01-04) 235 | 236 | **Note:** Version bump only for package @swc-node/jest 237 | 238 | ## [1.0.2](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.0.1...@swc-node/jest@1.0.2) (2020-11-18) 239 | 240 | **Note:** Version bump only for package @swc-node/jest 241 | 242 | ## [1.0.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@1.0.0...@swc-node/jest@1.0.1) (2020-10-21) 243 | 244 | **Note:** Version bump only for package @swc-node/jest 245 | 246 | ## [0.3.8](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.6...@swc-node/jest@0.3.8) (2020-09-16) 247 | 248 | **Note:** Version bump only for package @swc-node/jest 249 | 250 | ## [0.3.7](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.6...@swc-node/jest@0.3.7) (2020-09-13) 251 | 252 | **Note:** Version bump only for package @swc-node/jest 253 | 254 | ## [0.3.6](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.5...@swc-node/jest@0.3.6) (2020-09-11) 255 | 256 | **Note:** Version bump only for package @swc-node/jest 257 | 258 | ## [0.3.5](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.4...@swc-node/jest@0.3.5) (2020-09-09) 259 | 260 | **Note:** Version bump only for package @swc-node/jest 261 | 262 | ## [0.3.4](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.3...@swc-node/jest@0.3.4) (2020-09-07) 263 | 264 | **Note:** Version bump only for package @swc-node/jest 265 | 266 | ## [0.3.3](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.2...@swc-node/jest@0.3.3) (2020-09-06) 267 | 268 | **Note:** Version bump only for package @swc-node/jest 269 | 270 | ## [0.3.2](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.1...@swc-node/jest@0.3.2) (2020-09-04) 271 | 272 | **Note:** Version bump only for package @swc-node/jest 273 | 274 | ## [0.3.1](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.3.0...@swc-node/jest@0.3.1) (2020-09-01) 275 | 276 | **Note:** Version bump only for package @swc-node/jest 277 | 278 | # [0.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.27...@swc-node/jest@0.3.0) (2020-08-31) 279 | 280 | ### Features 281 | 282 | - **core:** provide jest hoist plugin ([f3638f2](https://github.com/swc-project/swc-node/commit/f3638f2004b9fb323261a301b6fe354255846965)) 283 | - **jest:** provide jest preset config ([7750ef5](https://github.com/swc-project/swc-node/commit/7750ef5d7cff3977a2b96f4d76810043e5f0988d)) 284 | 285 | # [0.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.27...@swc-node/jest@0.2.0) (2020-08-28) 286 | 287 | ### Features 288 | 289 | - **core:** provide jest hoist plugin ([f3638f2](https://github.com/swc-project/swc-node/commit/f3638f2004b9fb323261a301b6fe354255846965)) 290 | 291 | ## [0.1.27](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.26...@swc-node/jest@0.1.27) (2020-08-24) 292 | 293 | **Note:** Version bump only for package @swc-node/jest 294 | 295 | ## [0.1.26](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.25...@swc-node/jest@0.1.26) (2020-08-23) 296 | 297 | **Note:** Version bump only for package @swc-node/jest 298 | 299 | ## [0.1.25](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.24...@swc-node/jest@0.1.25) (2020-08-23) 300 | 301 | **Note:** Version bump only for package @swc-node/jest 302 | 303 | ## [0.1.24](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.23...@swc-node/jest@0.1.24) (2020-08-21) 304 | 305 | **Note:** Version bump only for package @swc-node/jest 306 | 307 | ## [0.1.23](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.22...@swc-node/jest@0.1.23) (2020-08-20) 308 | 309 | **Note:** Version bump only for package @swc-node/jest 310 | 311 | ## [0.1.22](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.21...@swc-node/jest@0.1.22) (2020-08-20) 312 | 313 | **Note:** Version bump only for package @swc-node/jest 314 | 315 | ## [0.1.20](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.19...@swc-node/jest@0.1.20) (2020-08-18) 316 | 317 | **Note:** Version bump only for package @swc-node/jest 318 | 319 | ## [0.1.19](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.18...@swc-node/jest@0.1.19) (2020-08-14) 320 | 321 | **Note:** Version bump only for package @swc-node/jest 322 | 323 | ## [0.1.18](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.17...@swc-node/jest@0.1.18) (2020-08-13) 324 | 325 | **Note:** Version bump only for package @swc-node/jest 326 | 327 | ## [0.1.17](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.16...@swc-node/jest@0.1.17) (2020-08-10) 328 | 329 | **Note:** Version bump only for package @swc-node/jest 330 | 331 | ## [0.1.16](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.15...@swc-node/jest@0.1.16) (2020-08-10) 332 | 333 | **Note:** Version bump only for package @swc-node/jest 334 | 335 | ## [0.1.15](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.14...@swc-node/jest@0.1.15) (2020-08-09) 336 | 337 | **Note:** Version bump only for package @swc-node/jest 338 | 339 | ## [0.1.14](https://github.com/swc-project/swc-node/compare/@swc-node/jest@0.1.13...@swc-node/jest@0.1.14) (2020-08-09) 340 | 341 | **Note:** Version bump only for package @swc-node/jest 342 | -------------------------------------------------------------------------------- /packages/jest/README.md: -------------------------------------------------------------------------------- 1 | # `@swc-node/jest` 2 | 3 | Downloads 4 | 5 | > 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn) 6 | 7 | ## Usage 8 | 9 | ```ts 10 | // jest.config.js 11 | 12 | module.exports = { 13 | transform: { 14 | '^.+\\.(t|j)sx?$': ['@swc-node/jest'], 15 | }, 16 | } 17 | ``` 18 | 19 | ## Configuration 20 | 21 | Configuration can be passed as a second argument to `transform`: 22 | 23 | ```ts 24 | // jest.config.js 25 | 26 | module.exports = { 27 | transform: { 28 | '^.+\\.(t|j)sx?$': [ 29 | '@swc-node/jest', 30 | 31 | // configuration 32 | { 33 | dynamicImport: true, 34 | react: { 35 | pragma: 'h', 36 | }, 37 | }, 38 | ], 39 | }, 40 | } 41 | ``` 42 | 43 | [List of all of the available configuration options](https://github.com/swc-project/swc-node/blob/master/packages/core/index.ts#L6). 44 | -------------------------------------------------------------------------------- /packages/jest/__test__/hoist-top-level.spec.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | 3 | const { process } = require('../index') 4 | 5 | const BrorrwFromTsJest = ` 6 | const foo = 'foo' 7 | console.log(foo) 8 | jest.enableAutomock() 9 | jest.disableAutomock() 10 | jest.mock('./foo') 11 | jest.mock('./foo/bar', () => 'bar') 12 | jest.unmock('./bar/foo').dontMock('./bar/bar') 13 | jest.deepUnmock('./foo') 14 | jest.mock('./foo').mock('./bar') 15 | const func = () => { 16 | const bar = 'bar' 17 | console.log(bar) 18 | jest.unmock('./foo') 19 | jest.mock('./bar') 20 | jest.mock('./bar/foo', () => 'foo') 21 | jest.unmock('./foo/bar') 22 | jest.unmock('./bar/foo').dontMock('./bar/bar') 23 | jest.deepUnmock('./bar') 24 | jest.mock('./foo').mock('./bar') 25 | } 26 | const func2 = () => { 27 | const bar = 'bar' 28 | console.log(bar) 29 | jest.mock('./bar') 30 | jest.unmock('./foo/bar') 31 | jest.mock('./bar/foo', () => 'foo') 32 | jest.unmock('./foo') 33 | jest.unmock('./bar/foo').dontMock('./bar/bar') 34 | jest.deepUnmock('./bar') 35 | jest.mock('./foo').mock('./bar') 36 | } 37 | ` 38 | 39 | test('should hoist top level jest mock call', (t) => { 40 | const { code } = process(BrorrwFromTsJest, 'jest.spec.ts', { 41 | transformerConfig: { target: 'es2018', sourcemap: false }, 42 | }) 43 | t.snapshot(code) 44 | }) 45 | -------------------------------------------------------------------------------- /packages/jest/__test__/hoist-top-level.spec.ts.md: -------------------------------------------------------------------------------- 1 | # Snapshot report for `packages/jest/__test__/hoist-top-level.spec.ts` 2 | 3 | The actual snapshot is saved in `hoist-top-level.spec.ts.snap`. 4 | 5 | Generated by [AVA](https://avajs.dev). 6 | 7 | ## should hoist top level jest mock call 8 | 9 | > Snapshot 1 10 | 11 | `"use strict";␊ 12 | jest.enableAutomock();␊ 13 | jest.disableAutomock();␊ 14 | jest.mock('./foo');␊ 15 | jest.mock('./foo/bar', ()=>'bar');␊ 16 | jest.deepUnmock('./foo');␊ 17 | jest.mock('./foo').mock('./bar');␊ 18 | const foo = 'foo';␊ 19 | console.log(foo);␊ 20 | jest.unmock('./bar/foo').dontMock('./bar/bar');␊ 21 | const func = ()=>{␊ 22 | jest.unmock('./foo');␊ 23 | jest.mock('./bar');␊ 24 | jest.mock('./bar/foo', ()=>'foo');␊ 25 | jest.unmock('./foo/bar');␊ 26 | jest.deepUnmock('./bar');␊ 27 | jest.mock('./foo').mock('./bar');␊ 28 | const bar = 'bar';␊ 29 | console.log(bar);␊ 30 | jest.unmock('./bar/foo').dontMock('./bar/bar');␊ 31 | };␊ 32 | const func2 = ()=>{␊ 33 | jest.mock('./bar');␊ 34 | jest.unmock('./foo/bar');␊ 35 | jest.mock('./bar/foo', ()=>'foo');␊ 36 | jest.unmock('./foo');␊ 37 | jest.deepUnmock('./bar');␊ 38 | jest.mock('./foo').mock('./bar');␊ 39 | const bar = 'bar';␊ 40 | console.log(bar);␊ 41 | jest.unmock('./bar/foo').dontMock('./bar/bar');␊ 42 | };␊ 43 | ␊ 44 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImplc3Quc3BlYy50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJcbmNvbnN0IGZvbyA9ICdmb28nXG5jb25zb2xlLmxvZyhmb28pXG5qZXN0LmVuYWJsZUF1dG9tb2NrKClcbmplc3QuZGlzYWJsZUF1dG9tb2NrKClcbmplc3QubW9jaygnLi9mb28nKVxuamVzdC5tb2NrKCcuL2Zvby9iYXInLCAoKSA9PiAnYmFyJylcbmplc3QudW5tb2NrKCcuL2Jhci9mb28nKS5kb250TW9jaygnLi9iYXIvYmFyJylcbmplc3QuZGVlcFVubW9jaygnLi9mb28nKVxuamVzdC5tb2NrKCcuL2ZvbycpLm1vY2soJy4vYmFyJylcbmNvbnN0IGZ1bmMgPSAoKSA9PiB7XG4gIGNvbnN0IGJhciA9ICdiYXInXG4gIGNvbnNvbGUubG9nKGJhcilcbiAgamVzdC51bm1vY2soJy4vZm9vJylcbiAgamVzdC5tb2NrKCcuL2JhcicpXG4gIGplc3QubW9jaygnLi9iYXIvZm9vJywgKCkgPT4gJ2ZvbycpXG4gIGplc3QudW5tb2NrKCcuL2Zvby9iYXInKVxuICBqZXN0LnVubW9jaygnLi9iYXIvZm9vJykuZG9udE1vY2soJy4vYmFyL2JhcicpXG4gIGplc3QuZGVlcFVubW9jaygnLi9iYXInKVxuICBqZXN0Lm1vY2soJy4vZm9vJykubW9jaygnLi9iYXInKVxufVxuY29uc3QgZnVuYzIgPSAoKSA9PiB7XG4gIGNvbnN0IGJhciA9ICdiYXInXG4gIGNvbnNvbGUubG9nKGJhcilcbiAgamVzdC5tb2NrKCcuL2JhcicpXG4gIGplc3QudW5tb2NrKCcuL2Zvby9iYXInKVxuICBqZXN0Lm1vY2soJy4vYmFyL2ZvbycsICgpID0+ICdmb28nKVxuICBqZXN0LnVubW9jaygnLi9mb28nKVxuICBqZXN0LnVubW9jaygnLi9iYXIvZm9vJykuZG9udE1vY2soJy4vYmFyL2JhcicpXG4gIGplc3QuZGVlcFVubW9jaygnLi9iYXInKVxuICBqZXN0Lm1vY2soJy4vZm9vJykubW9jaygnLi9iYXInKVxufVxuIl0sIm5hbWVzIjpbImplc3QiLCJlbmFibGVBdXRvbW9jayIsImRpc2FibGVBdXRvbW9jayIsIm1vY2siLCJkZWVwVW5tb2NrIiwiZm9vIiwiY29uc29sZSIsImxvZyIsInVubW9jayIsImRvbnRNb2NrIiwiZnVuYyIsImJhciIsImZ1bmMyIl0sIm1hcHBpbmdzIjoiO0FBR0FBLEtBQUtDLGNBQWM7QUFDbkJELEtBQUtFLGVBQWU7QUFDcEJGLEtBQUtHLElBQUksQ0FBQztBQUNWSCxLQUFLRyxJQUFJLENBQUMsYUFBYSxJQUFNO0FBRTdCSCxLQUFLSSxVQUFVLENBQUM7QUFDaEJKLEtBQUtHLElBQUksQ0FBQyxTQUFTQSxJQUFJLENBQUM7QUFSeEIsTUFBTUUsTUFBTTtBQUNaQyxRQUFRQyxHQUFHLENBQUNGO0FBS1pMLEtBQUtRLE1BQU0sQ0FBQyxhQUFhQyxRQUFRLENBQUM7QUFHbEMsTUFBTUMsT0FBTztJQUdYVixLQUFLUSxNQUFNLENBQUM7SUFDWlIsS0FBS0csSUFBSSxDQUFDO0lBQ1ZILEtBQUtHLElBQUksQ0FBQyxhQUFhLElBQU07SUFDN0JILEtBQUtRLE1BQU0sQ0FBQztJQUVaUixLQUFLSSxVQUFVLENBQUM7SUFDaEJKLEtBQUtHLElBQUksQ0FBQyxTQUFTQSxJQUFJLENBQUM7SUFSeEIsTUFBTVEsTUFBTTtJQUNaTCxRQUFRQyxHQUFHLENBQUNJO0lBS1pYLEtBQUtRLE1BQU0sQ0FBQyxhQUFhQyxRQUFRLENBQUM7QUFHcEM7QUFDQSxNQUFNRyxRQUFRO0lBR1paLEtBQUtHLElBQUksQ0FBQztJQUNWSCxLQUFLUSxNQUFNLENBQUM7SUFDWlIsS0FBS0csSUFBSSxDQUFDLGFBQWEsSUFBTTtJQUM3QkgsS0FBS1EsTUFBTSxDQUFDO0lBRVpSLEtBQUtJLFVBQVUsQ0FBQztJQUNoQkosS0FBS0csSUFBSSxDQUFDLFNBQVNBLElBQUksQ0FBQztJQVJ4QixNQUFNUSxNQUFNO0lBQ1pMLFFBQVFDLEdBQUcsQ0FBQ0k7SUFLWlgsS0FBS1EsTUFBTSxDQUFDLGFBQWFDLFFBQVEsQ0FBQztBQUdwQyJ9` 45 | -------------------------------------------------------------------------------- /packages/jest/__test__/hoist-top-level.spec.ts.snap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swc-project/swc-node/1c896356dc5573bd4c6103a94bb7cbc7376a6f05/packages/jest/__test__/hoist-top-level.spec.ts.snap -------------------------------------------------------------------------------- /packages/jest/index.ts: -------------------------------------------------------------------------------- 1 | import { xxh64 } from '@node-rs/xxhash' 2 | import type { Output } from '@swc/core' 3 | import { Options, transformJest } from '@swc-node/core' 4 | import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from '@swc-node/register/read-default-tsconfig' 5 | 6 | interface JestConfig26 { 7 | transform: [match: string, transformerPath: string, options: Options][] 8 | } 9 | 10 | interface JestConfig27 { 11 | transformerConfig: Options 12 | } 13 | 14 | function getJestTransformConfig(jestConfig: JestConfig26 | JestConfig27): Options { 15 | if ('transformerConfig' in jestConfig) { 16 | // jest 27 17 | return jestConfig.transformerConfig 18 | } 19 | 20 | if ('transform' in jestConfig) { 21 | // jest 26 22 | return jestConfig.transform.find(([, transformerPath]) => transformerPath === __filename)?.[2] ?? {} 23 | } 24 | 25 | return {} 26 | } 27 | 28 | const defaultTsConfig = readDefaultTsConfig() 29 | 30 | export = { 31 | process(src: string, path: string, jestConfig: JestConfig26 | JestConfig27): Output | string { 32 | if (/\.(tsx?|jsx?|mjs)$/.test(path)) { 33 | return transformJest(src, path, { 34 | ...tsCompilerOptionsToSwcConfig(defaultTsConfig, path), 35 | ...getJestTransformConfig(jestConfig), 36 | }) 37 | } 38 | return src 39 | }, 40 | getCacheKey(src: string, _filepath: string, config: Options) { 41 | return xxh64(src + JSON.stringify(config)).toString(16) 42 | }, 43 | } 44 | -------------------------------------------------------------------------------- /packages/jest/jest-preset.js: -------------------------------------------------------------------------------- 1 | module.exports = { transform: { '^.+\\.((m|c)?t|(m|c)?j)sx?$': '@swc-node/jest' } } 2 | -------------------------------------------------------------------------------- /packages/jest/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@swc-node/jest", 3 | "version": "1.8.13", 4 | "description": "swc preprocessor for jest with source map support", 5 | "keywords": [ 6 | "swc", 7 | "jest", 8 | "ts-jest", 9 | "napi", 10 | "N-API", 11 | "typescript", 12 | "node-rs", 13 | "napi-rs", 14 | "ts-node" 15 | ], 16 | "author": "LongYinan ", 17 | "homepage": "https://github.com/swc-project/swc-node", 18 | "license": "MIT", 19 | "main": "./lib/index.js", 20 | "files": [ 21 | "lib", 22 | "jest-preset.js", 23 | "LICENSE" 24 | ], 25 | "publishConfig": { 26 | "registry": "https://registry.npmjs.org/", 27 | "access": "public" 28 | }, 29 | "dependencies": { 30 | "@node-rs/xxhash": "^1.7.3", 31 | "@swc-node/core": "^1.13.3", 32 | "@swc-node/register": "^1.10.10" 33 | }, 34 | "peerDependencies": { 35 | "@swc/core": ">= 1.4.13", 36 | "@swc/types": ">= 0.1", 37 | "typescript": ">= 5.0" 38 | }, 39 | "devDependencies": { 40 | "@swc/core": "^1.6.6", 41 | "@swc/types": "^0.1.9" 42 | }, 43 | "repository": { 44 | "type": "git", 45 | "url": "git+https://github.com/swc-project/swc-node.git" 46 | }, 47 | "bugs": { 48 | "url": "https://github.com/swc-project/swc-node/issues" 49 | }, 50 | "funding": { 51 | "type": "github", 52 | "url": "https://github.com/sponsors/Brooooooklyn" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /packages/jest/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "composite": true, 5 | "rootDir": ".", 6 | "outDir": "./lib", 7 | }, 8 | "references": [{ "path": "../register" }], 9 | "files": ["index.ts"], 10 | "exclude": ["lib"], 11 | } 12 | -------------------------------------------------------------------------------- /packages/loader/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | ## [1.5.13](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.12...@swc-node/loader@1.5.13) (2025-03-13) 7 | 8 | **Note:** Version bump only for package @swc-node/loader 9 | 10 | ## [1.5.12](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.11...@swc-node/loader@1.5.12) (2024-07-17) 11 | 12 | **Note:** Version bump only for package @swc-node/loader 13 | 14 | ## [1.5.11](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.10...@swc-node/loader@1.5.11) (2024-07-16) 15 | 16 | **Note:** Version bump only for package @swc-node/loader 17 | 18 | ## [1.5.10](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.9...@swc-node/loader@1.5.10) (2024-07-14) 19 | 20 | **Note:** Version bump only for package @swc-node/loader 21 | 22 | ## [1.5.9](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.8...@swc-node/loader@1.5.9) (2024-07-13) 23 | 24 | **Note:** Version bump only for package @swc-node/loader 25 | 26 | ## [1.5.8](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.7...@swc-node/loader@1.5.8) (2024-07-13) 27 | 28 | **Note:** Version bump only for package @swc-node/loader 29 | 30 | ## [1.5.7](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.6...@swc-node/loader@1.5.7) (2024-07-12) 31 | 32 | **Note:** Version bump only for package @swc-node/loader 33 | 34 | ## [1.5.6](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.5...@swc-node/loader@1.5.6) (2024-07-08) 35 | 36 | **Note:** Version bump only for package @swc-node/loader 37 | 38 | ## [1.5.5](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.4...@swc-node/loader@1.5.5) (2024-07-05) 39 | 40 | **Note:** Version bump only for package @swc-node/loader 41 | 42 | ## [1.5.4](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.3...@swc-node/loader@1.5.4) (2024-07-05) 43 | 44 | **Note:** Version bump only for package @swc-node/loader 45 | 46 | ## [1.5.3](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.2...@swc-node/loader@1.5.3) (2024-06-28) 47 | 48 | **Note:** Version bump only for package @swc-node/loader 49 | 50 | ## [1.5.2](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.1...@swc-node/loader@1.5.2) (2024-06-08) 51 | 52 | **Note:** Version bump only for package @swc-node/loader 53 | 54 | ## [1.5.1](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.5.0...@swc-node/loader@1.5.1) (2024-05-01) 55 | 56 | **Note:** Version bump only for package @swc-node/loader 57 | 58 | # [1.5.0](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.9...@swc-node/loader@1.5.0) (2024-03-05) 59 | 60 | ### Features 61 | 62 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 63 | 64 | # [1.4.0](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.9...@swc-node/loader@1.4.0) (2024-02-01) 65 | 66 | ### Features 67 | 68 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 69 | 70 | ## [1.3.9](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.8...@swc-node/loader@1.3.9) (2024-02-01) 71 | 72 | **Note:** Version bump only for package @swc-node/loader 73 | 74 | ## [1.3.8](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.7...@swc-node/loader@1.3.8) (2023-09-26) 75 | 76 | **Note:** Version bump only for package @swc-node/loader 77 | 78 | ## [1.3.7](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.6...@swc-node/loader@1.3.7) (2023-08-18) 79 | 80 | **Note:** Version bump only for package @swc-node/loader 81 | 82 | ## [1.3.6](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.5...@swc-node/loader@1.3.6) (2023-06-21) 83 | 84 | **Note:** Version bump only for package @swc-node/loader 85 | 86 | ## [1.3.5](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.4...@swc-node/loader@1.3.5) (2023-04-24) 87 | 88 | **Note:** Version bump only for package @swc-node/loader 89 | 90 | ## [1.3.4](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.3...@swc-node/loader@1.3.4) (2023-04-10) 91 | 92 | **Note:** Version bump only for package @swc-node/loader 93 | 94 | ## [1.3.3](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.2...@swc-node/loader@1.3.3) (2023-03-31) 95 | 96 | **Note:** Version bump only for package @swc-node/loader 97 | 98 | ## [1.3.2](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.1...@swc-node/loader@1.3.2) (2023-02-15) 99 | 100 | **Note:** Version bump only for package @swc-node/loader 101 | 102 | ## [1.3.1](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.3.0...@swc-node/loader@1.3.1) (2023-02-11) 103 | 104 | **Note:** Version bump only for package @swc-node/loader 105 | 106 | # [1.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.8...@swc-node/loader@1.3.0) (2023-02-10) 107 | 108 | ### Features 109 | 110 | - **register:** experimental esm loader ([#643](https://github.com/swc-project/swc-node/issues/643)) ([0b4d305](https://github.com/swc-project/swc-node/commit/0b4d30505408f6f07c1ff8ea5c1953e1d22bb4e1)) 111 | 112 | ## [1.2.8](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.7...@swc-node/loader@1.2.8) (2023-02-10) 113 | 114 | **Note:** Version bump only for package @swc-node/loader 115 | 116 | ## [1.2.7](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.6...@swc-node/loader@1.2.7) (2023-02-07) 117 | 118 | **Note:** Version bump only for package @swc-node/loader 119 | 120 | ## [1.2.6](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.5...@swc-node/loader@1.2.6) (2023-02-07) 121 | 122 | **Note:** Version bump only for package @swc-node/loader 123 | 124 | ## [1.2.5](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.4...@swc-node/loader@1.2.5) (2023-01-05) 125 | 126 | **Note:** Version bump only for package @swc-node/loader 127 | 128 | ## [1.2.4](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.3...@swc-node/loader@1.2.4) (2022-10-06) 129 | 130 | ### Bug Fixes 131 | 132 | - **register:** @swc/core should be in peerDependencies ([cb05cae](https://github.com/swc-project/swc-node/commit/cb05cae69dd92d13593c210f8c0044b6aff8ff1c)) 133 | 134 | ## [1.2.3](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.2...@swc-node/loader@1.2.3) (2022-10-06) 135 | 136 | **Note:** Version bump only for package @swc-node/loader 137 | 138 | ## [1.2.2](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.1...@swc-node/loader@1.2.2) (2022-09-22) 139 | 140 | ### Bug Fixes 141 | 142 | - **loader:** fix logic when no compilerOptions passed by webpack ([1606f3c](https://github.com/swc-project/swc-node/commit/1606f3cf20e1a5d1d6ea3144b22366a59849822c)) 143 | 144 | ## [1.2.1](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.2.0...@swc-node/loader@1.2.1) (2022-04-28) 145 | 146 | **Note:** Version bump only for package @swc-node/loader 147 | 148 | # [1.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.12...@swc-node/loader@1.2.0) (2022-04-27) 149 | 150 | ### Features 151 | 152 | - **jest:** read tsconfig for default jest transform options ([8c180e6](https://github.com/swc-project/swc-node/commit/8c180e68abbc66aa68f83b401d985a6c8617baa9)) 153 | 154 | ## [1.1.12](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.11...@swc-node/loader@1.1.12) (2021-12-15) 155 | 156 | **Note:** Version bump only for package @swc-node/loader 157 | 158 | ## [1.1.11](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.10...@swc-node/loader@1.1.11) (2021-12-13) 159 | 160 | **Note:** Version bump only for package @swc-node/loader 161 | 162 | ## [1.1.10](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.9...@swc-node/loader@1.1.10) (2021-11-08) 163 | 164 | **Note:** Version bump only for package @swc-node/loader 165 | 166 | ## [1.1.9](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.8...@swc-node/loader@1.1.9) (2021-11-08) 167 | 168 | **Note:** Version bump only for package @swc-node/loader 169 | 170 | ## [1.1.8](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.7...@swc-node/loader@1.1.8) (2021-10-29) 171 | 172 | **Note:** Version bump only for package @swc-node/loader 173 | 174 | ## [1.1.7](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.6...@swc-node/loader@1.1.7) (2021-10-16) 175 | 176 | **Note:** Version bump only for package @swc-node/loader 177 | 178 | ## [1.1.6](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.5...@swc-node/loader@1.1.6) (2021-09-11) 179 | 180 | **Note:** Version bump only for package @swc-node/loader 181 | 182 | ## [1.1.5](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.4...@swc-node/loader@1.1.5) (2021-08-16) 183 | 184 | **Note:** Version bump only for package @swc-node/loader 185 | 186 | ## [1.1.4](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.3...@swc-node/loader@1.1.4) (2021-08-01) 187 | 188 | **Note:** Version bump only for package @swc-node/loader 189 | 190 | ## [1.1.3](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.2...@swc-node/loader@1.1.3) (2021-06-07) 191 | 192 | ### Bug Fixes 193 | 194 | - **register:** always enable dynamicImport ([0eb1bf2](https://github.com/swc-project/swc-node/commit/0eb1bf2e0bce97ca70d72dc13c51c8eac221029d)) 195 | 196 | ## [1.1.2](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.1...@swc-node/loader@1.1.2) (2021-05-23) 197 | 198 | **Note:** Version bump only for package @swc-node/loader 199 | 200 | ## [1.1.1](https://github.com/swc-project/swc-node/compare/@swc-node/loader@1.1.0...@swc-node/loader@1.1.1) (2021-05-21) 201 | 202 | ### Bug Fixes 203 | 204 | - **loader:** missing entry file ([8efbcd7](https://github.com/swc-project/swc-node/commit/8efbcd743688ccd6b24b2c021fa52d0eca037cd1)) 205 | 206 | # 1.1.0 (2021-05-21) 207 | 208 | ### Features 209 | 210 | - **loader:** implement tsconfig compatible loader ([8c1cd85](https://github.com/swc-project/swc-node/commit/8c1cd858a64a6b6ec6ff23811bafab7dfe30554d)) 211 | -------------------------------------------------------------------------------- /packages/loader/README.md: -------------------------------------------------------------------------------- 1 | # `@swc-node/loader` 2 | 3 | > 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn) 4 | 5 | Downloads 6 | 7 | ## Usage 8 | 9 | ```js 10 | { 11 | test: /\.tsx?$/, 12 | use: [ 13 | { 14 | loader: '@swc-node/loader', 15 | // If options not passed 16 | // `@swc-node/loader` will read the project `tsconfig.json` as compile options 17 | // If the default `tsconfig.json` parse failed or not existed 18 | // The default options will be used 19 | // `compilerOptions` is the same with `compilerOptions in tsconfig` 20 | options: { 21 | // if `compilerOptions` provided, `configFile` will be ignored 22 | compilerOptions: { 23 | target: 'ES5', 24 | module: 'esnext', 25 | sourceMap: true, 26 | jsx: true, 27 | }, 28 | // absolute path for tsconfig.json 29 | configFile: path.join(process.cwd(), 'tsconfig.build.json'), 30 | // enable react fast refresh 31 | fastRefresh: true 32 | } 33 | } 34 | ], 35 | exclude: /node_modules/, 36 | } 37 | ``` 38 | 39 | ## Differences between [swc-loader](https://github.com/swc-project/swc-loader) 40 | 41 | This `loader` is compatible with `tsconfig.json` and `compilerOptions` in `tsconfig.json`. 42 | -------------------------------------------------------------------------------- /packages/loader/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib').loader 2 | -------------------------------------------------------------------------------- /packages/loader/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@swc-node/loader", 3 | "version": "1.5.13", 4 | "description": "Webpack loader powered by swc", 5 | "keywords": [ 6 | "swc", 7 | "babel", 8 | "loader", 9 | "ts-loader", 10 | "babel-loader", 11 | "swc-loader", 12 | "napi-rs", 13 | "napi", 14 | "node-api", 15 | "tsc" 16 | ], 17 | "author": "LongYinan ", 18 | "homepage": "https://github.com/swc-project/swc-node", 19 | "license": "MIT", 20 | "main": "./index.js", 21 | "files": [ 22 | "lib", 23 | "LICENSE", 24 | "index.js" 25 | ], 26 | "publishConfig": { 27 | "registry": "https://registry.npmjs.org/", 28 | "access": "public" 29 | }, 30 | "dependencies": { 31 | "@swc-node/core": "^1.13.3", 32 | "@swc-node/register": "^1.10.10" 33 | }, 34 | "peerDependencies": { 35 | "typescript": ">= 4.3", 36 | "webpack": ">= 5.0.0" 37 | }, 38 | "repository": { 39 | "type": "git", 40 | "url": "git+https://github.com/swc-project/swc-node.git" 41 | }, 42 | "bugs": { 43 | "url": "https://github.com/swc-project/swc-node/issues" 44 | }, 45 | "funding": { 46 | "type": "github", 47 | "url": "https://github.com/sponsors/Brooooooklyn" 48 | }, 49 | "devDependencies": { 50 | "typescript": "^5.5.3", 51 | "webpack": "^5.92.1" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /packages/loader/src/index.ts: -------------------------------------------------------------------------------- 1 | import { transform } from '@swc-node/core' 2 | import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from '@swc-node/register/read-default-tsconfig' 3 | import { CompilerOptions, convertCompilerOptionsFromJson } from 'typescript' 4 | import type { LoaderContext } from 'webpack' 5 | 6 | export function loader( 7 | this: LoaderContext<{ 8 | compilerOptions?: CompilerOptions 9 | configFile?: string 10 | fastRefresh?: boolean 11 | }>, 12 | source: string, 13 | ) { 14 | const callback = this.async() 15 | const { compilerOptions, configFile, fastRefresh } = this.getOptions() ?? {} 16 | const { options: assignedOptions } = convertCompilerOptionsFromJson(compilerOptions, '') 17 | const options = 18 | !assignedOptions || Object.keys(assignedOptions).length === 0 ? readDefaultTsConfig(configFile) : assignedOptions 19 | const swcOptions = tsCompilerOptionsToSwcConfig(options, this.resourcePath) 20 | if (fastRefresh) { 21 | if (swcOptions.react) { 22 | swcOptions.react.refresh = true 23 | } else { 24 | swcOptions.react = { 25 | refresh: true, 26 | } 27 | } 28 | } 29 | transform(source, this.resourcePath, swcOptions) 30 | .then(({ code, map }) => callback(null, code, map)) 31 | .catch((err) => callback(err)) 32 | } 33 | -------------------------------------------------------------------------------- /packages/loader/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "composite": true, 5 | "rootDir": "./src", 6 | "outDir": "./lib" 7 | }, 8 | "include": ["."], 9 | "exclude": ["lib"], 10 | "references": [{ "path": "../core" }, { "path": "../register" }] 11 | } 12 | -------------------------------------------------------------------------------- /packages/register/.gitignore: -------------------------------------------------------------------------------- 1 | esm/ 2 | -------------------------------------------------------------------------------- /packages/register/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | ## [1.10.10](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.9...@swc-node/register@1.10.10) (2025-03-13) 7 | 8 | ### Bug Fixes 9 | 10 | - **deps:** update dependency oxc-resolver to v2 ([#858](https://github.com/swc-project/swc-node/issues/858)) ([fca85bd](https://github.com/swc-project/swc-node/commit/fca85bd1ba1705455ca8c7adb345690cb029b4d3)) 11 | - **deps:** update dependency oxc-resolver to v3 ([#887](https://github.com/swc-project/swc-node/issues/887)) ([ff409a7](https://github.com/swc-project/swc-node/commit/ff409a787a68fd2dc2a81cd8703ee5a452dbe445)) 12 | - **deps:** update dependency oxc-resolver to v5 ([#910](https://github.com/swc-project/swc-node/issues/910)) ([9bf404a](https://github.com/swc-project/swc-node/commit/9bf404a8a245e67cb57dc2cad9b40c05bea6f96e)) 13 | - **register/esm:** pass file urls as paths ([#840](https://github.com/swc-project/swc-node/issues/840)) ([27d922d](https://github.com/swc-project/swc-node/commit/27d922d64c046d48dda94733c5277171b0a358bd)) 14 | - **register:** Windows path ([#906](https://github.com/swc-project/swc-node/issues/906)) ([406a3d8](https://github.com/swc-project/swc-node/commit/406a3d886cb86dffe6626c752233012c6f853375)) 15 | 16 | ## [1.10.9](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.8...@swc-node/register@1.10.9) (2024-07-17) 17 | 18 | ### Bug Fixes 19 | 20 | - **register:** skip load files in node_modules ([#828](https://github.com/swc-project/swc-node/issues/828)) ([f6816c1](https://github.com/swc-project/swc-node/commit/f6816c160191052454aae232ce75b9a8f8b3d9a5)) 21 | 22 | ## [1.10.8](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.7...@swc-node/register@1.10.8) (2024-07-16) 23 | 24 | ### Bug Fixes 25 | 26 | - **register:** bump oxc-resolver ([#824](https://github.com/swc-project/swc-node/issues/824)) ([2792552](https://github.com/swc-project/swc-node/commit/2792552302baa854c1efd8dc77b435fa86b84cf1)) 27 | - **register:** default TSCONFIG_PATH on windows. ([#818](https://github.com/swc-project/swc-node/issues/818)) ([92a3bbf](https://github.com/swc-project/swc-node/commit/92a3bbf329269a1d123ea4513541d3a6a418dd2d)) 28 | - **register:** remove file extension tests in compile ([#825](https://github.com/swc-project/swc-node/issues/825)) ([7c686f1](https://github.com/swc-project/swc-node/commit/7c686f13aa644bc280143f399ab60fc7fd8a9da3)) 29 | 30 | ## [1.10.7](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.6...@swc-node/register@1.10.7) (2024-07-14) 31 | 32 | **Note:** Version bump only for package @swc-node/register 33 | 34 | ## [1.10.6](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.5...@swc-node/register@1.10.6) (2024-07-13) 35 | 36 | ### Bug Fixes 37 | 38 | - **register:** node18 compatible issues ([#814](https://github.com/swc-project/swc-node/issues/814)) ([6bbe5c2](https://github.com/swc-project/swc-node/commit/6bbe5c25f0348d9c8c47dc5c38d394784cf26214)) 39 | 40 | ## [1.10.5](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.4...@swc-node/register@1.10.5) (2024-07-13) 41 | 42 | ### Bug Fixes 43 | 44 | - **register:** allow running .tsx file ([#812](https://github.com/swc-project/swc-node/issues/812)) ([6a49076](https://github.com/swc-project/swc-node/commit/6a49076bd235ad95099887f1d89569ca89ac26bb)) 45 | - **register:** resolve .cjs/.cts file in esm package ([#813](https://github.com/swc-project/swc-node/issues/813)) ([154ee94](https://github.com/swc-project/swc-node/commit/154ee94224fd3c98e9e2fe22ad401c96e4dd2f5e)) 46 | 47 | ## [1.10.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.3...@swc-node/register@1.10.4) (2024-07-12) 48 | 49 | ### Bug Fixes 50 | 51 | - **register:** resolve internal cjs module ([#811](https://github.com/swc-project/swc-node/issues/811)) ([9aad5b0](https://github.com/swc-project/swc-node/commit/9aad5b0a86dbe58aed8cba9c57524ad8e553f21c)) 52 | 53 | ## [1.10.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.2...@swc-node/register@1.10.3) (2024-07-08) 54 | 55 | ### Bug Fixes 56 | 57 | - **register:** support moduleResolution Bundler ([#806](https://github.com/swc-project/swc-node/issues/806)) ([aab1e0f](https://github.com/swc-project/swc-node/commit/aab1e0f383f0ecd02d31916872f39de86d723467)) 58 | 59 | ## [1.10.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.1...@swc-node/register@1.10.2) (2024-07-05) 60 | 61 | ### Bug Fixes 62 | 63 | - **register:** resolve builtin modules without node: protocal ([#803](https://github.com/swc-project/swc-node/issues/803)) ([a4a6832](https://github.com/swc-project/swc-node/commit/a4a68320ef03385fab8d1f0b32203ad14c887590)) 64 | 65 | ## [1.10.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.10.0...@swc-node/register@1.10.1) (2024-07-05) 66 | 67 | ### Bug Fixes 68 | 69 | - **register:** do not send undefined source code for compilation ([#797](https://github.com/swc-project/swc-node/issues/797)) ([3499611](https://github.com/swc-project/swc-node/commit/3499611349c3bff2ab9d0eef7cb27b118f5305bf)) 70 | - **register:** ensure TS compiler option to SWC config transformer respects inline source map option ([#726](https://github.com/swc-project/swc-node/issues/726)) ([59f2d99](https://github.com/swc-project/swc-node/commit/59f2d99aa0f8d4ebe67d2b5b792e28c811b82420)) 71 | - **register:** file path with query ([#801](https://github.com/swc-project/swc-node/issues/801)) ([9e53df0](https://github.com/swc-project/swc-node/commit/9e53df0c40cc443820352098602a61f7a39e1330)) 72 | - **register:** tsx file ([#800](https://github.com/swc-project/swc-node/issues/800)) ([1071d8d](https://github.com/swc-project/swc-node/commit/1071d8d8f55e42822556ad0f16e14a7562242073)) 73 | 74 | # [1.10.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.9.2...@swc-node/register@1.10.0) (2024-06-28) 75 | 76 | ### Bug Fixes 77 | 78 | - **esm-resolver:** only return early if the specifier is an unsupported file ([#789](https://github.com/swc-project/swc-node/issues/789)) ([014cf6a](https://github.com/swc-project/swc-node/commit/014cf6a1df0f58568bad296a3eac3f76c31abdde)) 79 | 80 | ### Features 81 | 82 | - try to resolve format for absolute path import ([86fb5d2](https://github.com/swc-project/swc-node/commit/86fb5d2f660c8dc1f8a73ba54fbc242aba628768)) 83 | - update esm module resolver ([92f05d4](https://github.com/swc-project/swc-node/commit/92f05d4a305e83446ee4ff6e66d3f42b474490e6)) 84 | 85 | ## [1.9.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.9.1...@swc-node/register@1.9.2) (2024-06-08) 86 | 87 | ### Bug Fixes 88 | 89 | - fix ts extension detect regex, close [#775](https://github.com/swc-project/swc-node/issues/775) [#774](https://github.com/swc-project/swc-node/issues/774) [#772](https://github.com/swc-project/swc-node/issues/772) ([#777](https://github.com/swc-project/swc-node/issues/777)) ([fd85848](https://github.com/swc-project/swc-node/commit/fd8584802b2d08ceef9fa0012ac7e47e30c8b130)) 90 | 91 | ## [1.9.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.9.0...@swc-node/register@1.9.1) (2024-05-01) 92 | 93 | ### Bug Fixes 94 | 95 | - **register:** fix esm entry resolver for third-party executer, close [#762](https://github.com/swc-project/swc-node/issues/762) ([#766](https://github.com/swc-project/swc-node/issues/766)) ([9e6c02f](https://github.com/swc-project/swc-node/commit/9e6c02feb9a19d9782981f984084885e92dae031)) 96 | - support compile js files. close [#761](https://github.com/swc-project/swc-node/issues/761) ([#767](https://github.com/swc-project/swc-node/issues/767)) ([016f1aa](https://github.com/swc-project/swc-node/commit/016f1aab2a17d2512d30b5a12848ed1941b59e49)) 97 | 98 | # [1.9.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.7.0...@swc-node/register@1.9.0) (2024-03-05) 99 | 100 | ### Bug Fixes 101 | 102 | - add default tsconfig.baseUrl to align with tsc behavior ([#759](https://github.com/swc-project/swc-node/issues/759)) ([96139f7](https://github.com/swc-project/swc-node/commit/96139f70f2e2415477f14af03eb2bc45a472f33c)) 103 | - esm module resolve issues ([#754](https://github.com/swc-project/swc-node/issues/754)) ([d35ddf1](https://github.com/swc-project/swc-node/commit/d35ddf1a0f8b3fa1c498ec869ec4ce562b7fb3a4)) 104 | - import ts from node_modules ([#744](https://github.com/swc-project/swc-node/issues/744)) ([c4485ca](https://github.com/swc-project/swc-node/commit/c4485ca185cb31e36126e0da4e0b79a03acfc79b)) 105 | 106 | ### Features 107 | 108 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 109 | 110 | # [1.8.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.7.0...@swc-node/register@1.8.0) (2024-02-01) 111 | 112 | ### Bug Fixes 113 | 114 | - import ts from node_modules ([#744](https://github.com/swc-project/swc-node/issues/744)) ([c4485ca](https://github.com/swc-project/swc-node/commit/c4485ca185cb31e36126e0da4e0b79a03acfc79b)) 115 | 116 | ### Features 117 | 118 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 119 | 120 | # [1.7.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.8...@swc-node/register@1.7.0) (2024-02-01) 121 | 122 | ### Bug Fixes 123 | 124 | - **core:** respect useDefineForClassFields tsconfig value ([#740](https://github.com/swc-project/swc-node/issues/740)) ([9330c1a](https://github.com/swc-project/swc-node/commit/9330c1a1183723638b3c83cff63ec6f18a09294c)) 125 | 126 | ### Features 127 | 128 | - add esm-register for node>20.6 ([#748](https://github.com/swc-project/swc-node/issues/748)) ([23e511c](https://github.com/swc-project/swc-node/commit/23e511c47938d14a0c3d6fc542692469e6039433)) 129 | - **register:** add `SWC_NODE_IGNORE_DYNAMIC` env option ([0862975](https://github.com/swc-project/swc-node/commit/08629752249dc27e9f5b72a9af606dfd6fd7b48a)) 130 | 131 | ## [1.6.8](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.7...@swc-node/register@1.6.8) (2023-09-26) 132 | 133 | ### Bug Fixes 134 | 135 | - **register:** extension-aware module resolution and format handling ([e34f006](https://github.com/swc-project/swc-node/commit/e34f006484d89c53dd4cac7cface3c4d70841e34)) 136 | 137 | ## [1.6.7](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.6...@swc-node/register@1.6.7) (2023-08-18) 138 | 139 | ### Bug Fixes 140 | 141 | - **core:** Pass jsc.baseUrl ([#721](https://github.com/swc-project/swc-node/issues/721)) ([877bd58](https://github.com/swc-project/swc-node/commit/877bd58f44072d2f44f6164960271dc9e3fda873)) 142 | - fix import absolute path support ([ffecee5](https://github.com/swc-project/swc-node/commit/ffecee519075cd8311101e1e2327d7402cd3ba1f)) 143 | 144 | ## [1.6.6](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.5...@swc-node/register@1.6.6) (2023-06-21) 145 | 146 | ### Bug Fixes 147 | 148 | - tsCompilerOptionsToSwcConfig should not override default swc config for jest ([#714](https://github.com/swc-project/swc-node/issues/714)) ([60ea642](https://github.com/swc-project/swc-node/commit/60ea64284582ce3164ca3705976b4dc4215c2504)) 149 | 150 | ## [1.6.5](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.4...@swc-node/register@1.6.5) (2023-04-24) 151 | 152 | ### Bug Fixes 153 | 154 | - **register:** Fix to resolve adjacent file path ([#711](https://github.com/swc-project/swc-node/issues/711)) ([8dbea4f](https://github.com/swc-project/swc-node/commit/8dbea4fd0ac154cbc72088202a8ddf707e40fa13)) 155 | - **register:** inline sourcemap ([#708](https://github.com/swc-project/swc-node/issues/708)) ([07eec33](https://github.com/swc-project/swc-node/commit/07eec334de984c32eca8551e1d9a647075b6b035)) 156 | 157 | ## [1.6.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.3...@swc-node/register@1.6.4) (2023-04-10) 158 | 159 | ### Bug Fixes 160 | 161 | - **register:** make sourcemaps work with both error stack & debugger ([#707](https://github.com/swc-project/swc-node/issues/707)) ([334ada1](https://github.com/swc-project/swc-node/commit/334ada16617011f00b5cb6b240e7f62f08296761)) 162 | - **register:** url now returns the href of a url object ([#698](https://github.com/swc-project/swc-node/issues/698)) ([fd63aa1](https://github.com/swc-project/swc-node/commit/fd63aa1660140bc922fd810b51f27f756a719acb)) 163 | 164 | ## [1.6.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.2...@swc-node/register@1.6.3) (2023-03-31) 165 | 166 | ### Bug Fixes 167 | 168 | - **core,register:** ts Compiler to Swc Config: respects decorators config and SWCRC=true ([#702](https://github.com/swc-project/swc-node/issues/702)) ([d421ca8](https://github.com/swc-project/swc-node/commit/d421ca8aa02a07ea01b1f97e2f38d696d84e4531)) 169 | 170 | ## [1.6.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.1...@swc-node/register@1.6.2) (2023-02-15) 171 | 172 | ### Bug Fixes 173 | 174 | - **register:** enforece module option in register/esm ([#694](https://github.com/swc-project/swc-node/issues/694)) ([860d1f6](https://github.com/swc-project/swc-node/commit/860d1f6f5f7ece197e92a822470a093ae7a7a68a)) 175 | 176 | ## [1.6.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.6.0...@swc-node/register@1.6.1) (2023-02-11) 177 | 178 | ### Bug Fixes 179 | 180 | - **register:** include esm files ([8d6b0b7](https://github.com/swc-project/swc-node/commit/8d6b0b77d5ec725ff75989455f8163d88c306878)) 181 | 182 | # [1.6.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.8...@swc-node/register@1.6.0) (2023-02-10) 183 | 184 | ### Features 185 | 186 | - **register:** experimental esm loader ([#643](https://github.com/swc-project/swc-node/issues/643)) ([0b4d305](https://github.com/swc-project/swc-node/commit/0b4d30505408f6f07c1ff8ea5c1953e1d22bb4e1)) 187 | 188 | ## [1.5.8](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.7...@swc-node/register@1.5.8) (2023-02-10) 189 | 190 | ### Bug Fixes 191 | 192 | - **register:** paths option ([c51be25](https://github.com/swc-project/swc-node/commit/c51be25d28da06d29620caee2505bff609cba445)) 193 | 194 | ## [1.5.7](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.6...@swc-node/register@1.5.7) (2023-02-07) 195 | 196 | ### Bug Fixes 197 | 198 | - **register:** align externalHelpers with tsconfig.importHelpers ([2f3e155](https://github.com/swc-project/swc-node/commit/2f3e155b400605130ada4ef7dc6cfa5dfedd0c0c)) 199 | 200 | ## [1.5.6](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.5...@swc-node/register@1.5.6) (2023-02-07) 201 | 202 | ### Bug Fixes 203 | 204 | - **register:** always inline swc helpers ([1d557ec](https://github.com/swc-project/swc-node/commit/1d557ece0d9ccbba027ff9f2d262c03d4b918bcb)) 205 | - **register:** react configuration ([af643b8](https://github.com/swc-project/swc-node/commit/af643b849c32abb58bd1c0fdf98eeeac08548e25)) 206 | 207 | ## [1.5.5](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.4...@swc-node/register@1.5.5) (2023-01-05) 208 | 209 | **Note:** Version bump only for package @swc-node/register 210 | 211 | ## [1.5.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.3...@swc-node/register@1.5.4) (2022-10-06) 212 | 213 | ### Bug Fixes 214 | 215 | - **register:** @swc/core should be in peerDependencies ([cb05cae](https://github.com/swc-project/swc-node/commit/cb05cae69dd92d13593c210f8c0044b6aff8ff1c)) 216 | 217 | ## [1.5.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.2...@swc-node/register@1.5.3) (2022-10-06) 218 | 219 | **Note:** Version bump only for package @swc-node/register 220 | 221 | ## [1.5.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.1...@swc-node/register@1.5.2) (2022-09-22) 222 | 223 | ### Bug Fixes 224 | 225 | - **register:** support paths alias with baseUrl ([2a6848a](https://github.com/swc-project/swc-node/commit/2a6848a00b8931f41b62f6b5a519bdbc548bfec3)) 226 | 227 | ## [1.5.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.5.0...@swc-node/register@1.5.1) (2022-04-28) 228 | 229 | ### Bug Fixes 230 | 231 | - **register:** move typescript to devDependencies ([c0011ca](https://github.com/swc-project/swc-node/commit/c0011ca0eb535f7eacf184ec116c775121c64905)) 232 | 233 | # [1.5.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.4.2...@swc-node/register@1.5.0) (2022-04-27) 234 | 235 | ### Features 236 | 237 | - **jest:** read tsconfig for default jest transform options ([8c180e6](https://github.com/swc-project/swc-node/commit/8c180e68abbc66aa68f83b401d985a6c8617baa9)) 238 | - **register:** always resolve full file path for tsconfig and add paths if available ([#646](https://github.com/swc-project/swc-node/issues/646)) ([3062413](https://github.com/swc-project/swc-node/commit/3062413e464a5b5706c5ac4912f43ef7451fe73b)) 239 | - **register:** return addHook result to it can be reverted ([222e205](https://github.com/swc-project/swc-node/commit/222e2056351e3a2ba2a4764537c9410b0112051e)), closes [/github.com/facebook/jest/blob/199f9811ae68b15879cbe18b7ef7ebd61eefcf23/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L83-101](https://github.com//github.com/facebook/jest/blob/199f9811ae68b15879cbe18b7ef7ebd61eefcf23/packages/jest-config/src/readConfigFileAndSetRootDir.ts/issues/L83-101) 240 | - **register:** support hook options ([4c6dad7](https://github.com/swc-project/swc-node/commit/4c6dad7bfbf4563c44bd25476a6ab5d78cff55dc)) 241 | 242 | ## [1.4.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.4.1...@swc-node/register@1.4.2) (2021-12-15) 243 | 244 | **Note:** Version bump only for package @swc-node/register 245 | 246 | ## [1.4.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.4.0...@swc-node/register@1.4.1) (2021-12-13) 247 | 248 | **Note:** Version bump only for package @swc-node/register 249 | 250 | # [1.4.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.8...@swc-node/register@1.4.0) (2021-11-08) 251 | 252 | ### Features 253 | 254 | - **register:** support tsconfig in subdirectory ([634d766](https://github.com/swc-project/swc-node/commit/634d766aa22013ec725c0d30317eb38963410db3)) 255 | 256 | ## [1.3.8](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.7...@swc-node/register@1.3.8) (2021-11-08) 257 | 258 | **Note:** Version bump only for package @swc-node/register 259 | 260 | ## [1.3.7](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.6...@swc-node/register@1.3.7) (2021-10-29) 261 | 262 | **Note:** Version bump only for package @swc-node/register 263 | 264 | ## [1.3.6](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.5...@swc-node/register@1.3.6) (2021-10-16) 265 | 266 | ### Bug Fixes 267 | 268 | - **register:** basePath arg to ts should be cwd ([e5d4c31](https://github.com/swc-project/swc-node/commit/e5d4c3118b73bb38d4710daa84befff0e16d8d81)) 269 | 270 | ## [1.3.5](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.4...@swc-node/register@1.3.5) (2021-09-11) 271 | 272 | **Note:** Version bump only for package @swc-node/register 273 | 274 | ## [1.3.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.3...@swc-node/register@1.3.4) (2021-08-16) 275 | 276 | ### Bug Fixes 277 | 278 | - **register:** outdated ts->swc config ([cff217b](https://github.com/swc-project/swc-node/commit/cff217b9b45199c580e9ed308f3826b577776bb3)) 279 | 280 | ## [1.3.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.2...@swc-node/register@1.3.3) (2021-08-01) 281 | 282 | ### Bug Fixes 283 | 284 | - **register:** make typescript a dependency ([#563](https://github.com/swc-project/swc-node/issues/563)) ([9152f74](https://github.com/swc-project/swc-node/commit/9152f74c8494a603315a0bcfd6f05e9a691879d2)) 285 | 286 | ## [1.3.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.1...@swc-node/register@1.3.2) (2021-06-07) 287 | 288 | ### Bug Fixes 289 | 290 | - **register:** always enable dynamicImport ([0eb1bf2](https://github.com/swc-project/swc-node/commit/0eb1bf2e0bce97ca70d72dc13c51c8eac221029d)) 291 | 292 | ## [1.3.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.3.0...@swc-node/register@1.3.1) (2021-05-23) 293 | 294 | ### Bug Fixes 295 | 296 | - **register:** exports field in package.json ([bd0459d](https://github.com/swc-project/swc-node/commit/bd0459da56930bf0334bcb5cc5059edfec9fa99c)) 297 | 298 | # [1.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.2.1...@swc-node/register@1.3.0) (2021-05-21) 299 | 300 | ### Features 301 | 302 | - **loader:** implement tsconfig compatible loader ([8c1cd85](https://github.com/swc-project/swc-node/commit/8c1cd858a64a6b6ec6ff23811bafab7dfe30554d)) 303 | 304 | ## [1.2.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.2.0...@swc-node/register@1.2.1) (2021-04-29) 305 | 306 | ### Bug Fixes 307 | 308 | - **register:** 'both' value is not valid sourcemaps config ([38207d5](https://github.com/swc-project/swc-node/commit/38207d52fd55c0157319370e3857d5372cb697ba)) 309 | 310 | # [1.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.1.0...@swc-node/register@1.2.0) (2021-04-28) 311 | 312 | ### Features 313 | 314 | - **core:** expose inline and both config for sourcemap ([780f2bb](https://github.com/swc-project/swc-node/commit/780f2bb81053af6fc6af865a979059ffff470eac)) 315 | 316 | # [1.1.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.0.5...@swc-node/register@1.1.0) (2021-04-21) 317 | 318 | ### Features 319 | 320 | - **register:** use keepClassNames: true by default ([5936616](https://github.com/swc-project/swc-node/commit/59366169c700544bf287b7a68e883efaaad6806b)) 321 | 322 | ## [1.0.5](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.0.4...@swc-node/register@1.0.5) (2021-03-04) 323 | 324 | **Note:** Version bump only for package @swc-node/register 325 | 326 | ## [1.0.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.0.3...@swc-node/register@1.0.4) (2021-01-25) 327 | 328 | ### Bug Fixes 329 | 330 | - adhere to ReactConfig type ([8d44f6a](https://github.com/swc-project/swc-node/commit/8d44f6ad067a833dfc461f5eb8a2c5491d968810)) 331 | - support custom jsx pragma ([3b98312](https://github.com/swc-project/swc-node/commit/3b983121fecd666eef0ae6ff8c4f1df19563fca8)) 332 | 333 | ## [1.0.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.0.2...@swc-node/register@1.0.3) (2021-01-04) 334 | 335 | **Note:** Version bump only for package @swc-node/register 336 | 337 | ## [1.0.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.0.1...@swc-node/register@1.0.2) (2020-11-18) 338 | 339 | ### Bug Fixes 340 | 341 | - **register:** handle absolute file paths ([f21a48a](https://github.com/swc-project/swc-node/commit/f21a48a5a3150ce388a695bf8e36d5d8a64895db)) 342 | 343 | ## [1.0.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@1.0.0...@swc-node/register@1.0.1) (2020-10-21) 344 | 345 | **Note:** Version bump only for package @swc-node/register 346 | 347 | ## [0.5.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.5.2...@swc-node/register@0.5.3) (2020-09-18) 348 | 349 | ### Bug Fixes 350 | 351 | - **register:** integrate tests ([b012869](https://github.com/swc-project/swc-node/commit/b0128698e7274d37856e2593849d58f67e9e302b)) 352 | 353 | ## [0.5.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.5.1...@swc-node/register@0.5.2) (2020-09-18) 354 | 355 | ### Bug Fixes 356 | 357 | - **register:** remove .d.ts from extensions ([62b6a69](https://github.com/swc-project/swc-node/commit/62b6a69704d9a2f908489728bfbd41b24672612e)) 358 | 359 | ## [0.5.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.5.0...@swc-node/register@0.5.1) (2020-09-17) 360 | 361 | ### Bug Fixes 362 | 363 | - **register:** add fastpath to handle .d.ts file ([afe4ca9](https://github.com/swc-project/swc-node/commit/afe4ca921985ecce907cb752fb674c8278041bc0)) 364 | 365 | # [0.5.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.10...@swc-node/register@0.5.0) (2020-09-16) 366 | 367 | ### Features 368 | 369 | - **register:** support read include/exclude config from tsconfig ([0dec2cd](https://github.com/swc-project/swc-node/commit/0dec2cdcf002c361abef068cf227a8bfe6cdea2a)) 370 | 371 | ## [0.4.11](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.10...@swc-node/register@0.4.11) (2020-09-13) 372 | 373 | **Note:** Version bump only for package @swc-node/register 374 | 375 | ## [0.4.10](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.9...@swc-node/register@0.4.10) (2020-09-11) 376 | 377 | ### Bug Fixes 378 | 379 | - **core:** add support for esModuleInterop ([a7f3331](https://github.com/swc-project/swc-node/commit/a7f3331f06d597e39cb44be8a8d73f264a417a71)) 380 | 381 | ## [0.4.9](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.8...@swc-node/register@0.4.9) (2020-09-09) 382 | 383 | **Note:** Version bump only for package @swc-node/register 384 | 385 | ## [0.4.8](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.7...@swc-node/register@0.4.8) (2020-09-07) 386 | 387 | **Note:** Version bump only for package @swc-node/register 388 | 389 | ## [0.4.7](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.6...@swc-node/register@0.4.7) (2020-09-06) 390 | 391 | **Note:** Version bump only for package @swc-node/register 392 | 393 | ## [0.4.6](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.5...@swc-node/register@0.4.6) (2020-09-04) 394 | 395 | **Note:** Version bump only for package @swc-node/register 396 | 397 | ## [0.4.5](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.4...@swc-node/register@0.4.5) (2020-09-01) 398 | 399 | **Note:** Version bump only for package @swc-node/register 400 | 401 | ## [0.4.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.2...@swc-node/register@0.4.4) (2020-08-31) 402 | 403 | **Note:** Version bump only for package @swc-node/register 404 | 405 | ## [0.4.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.2...@swc-node/register@0.4.3) (2020-08-28) 406 | 407 | **Note:** Version bump only for package @swc-node/register 408 | 409 | ## [0.4.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.1...@swc-node/register@0.4.2) (2020-08-24) 410 | 411 | **Note:** Version bump only for package @swc-node/register 412 | 413 | ## [0.4.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.4.0...@swc-node/register@0.4.1) (2020-08-23) 414 | 415 | ### Bug Fixes 416 | 417 | - **register:** should not install sourcemap if sourcemap is undefined ([517f927](https://github.com/swc-project/swc-node/commit/517f927d20f79c8bbcd81bb6d445c52b0602f203)) 418 | 419 | # [0.4.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.3.4...@swc-node/register@0.4.0) (2020-08-23) 420 | 421 | ### Bug Fixes 422 | 423 | - **register:** missing register.js file in package.json ([366d370](https://github.com/swc-project/swc-node/commit/366d3706af778e47f0427f92fe1a171cd43e2a62)) 424 | 425 | ### BREAKING CHANGES 426 | 427 | - **register:** missing files 428 | 429 | ## [0.3.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.3.3...@swc-node/register@0.3.4) (2020-08-23) 430 | 431 | **Note:** Version bump only for package @swc-node/register 432 | 433 | ## [0.3.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.3.2...@swc-node/register@0.3.3) (2020-08-21) 434 | 435 | **Note:** Version bump only for package @swc-node/register 436 | 437 | ## [0.3.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.3.1...@swc-node/register@0.3.2) (2020-08-20) 438 | 439 | **Note:** Version bump only for package @swc-node/register 440 | 441 | ## [0.3.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.3.0...@swc-node/register@0.3.1) (2020-08-20) 442 | 443 | **Note:** Version bump only for package @swc-node/register 444 | 445 | ## [0.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.2.6...@swc-node/register@0.3.0) (2020-08-20) 446 | 447 | ### BREAKING CHANGE 448 | 449 | - **register:** respect tsconfig.json in SWC_NODE_PROJECT 450 | 451 | ## [0.2.6](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.2.5...@swc-node/register@0.2.6) (2020-08-18) 452 | 453 | **Note:** Version bump only for package @swc-node/register 454 | 455 | ## [0.2.5](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.2.4...@swc-node/register@0.2.5) (2020-08-14) 456 | 457 | **Note:** Version bump only for package @swc-node/register 458 | 459 | ## [0.2.4](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.2.3...@swc-node/register@0.2.4) (2020-08-13) 460 | 461 | **Note:** Version bump only for package @swc-node/register 462 | 463 | ## [0.2.3](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.2.2...@swc-node/register@0.2.3) (2020-08-10) 464 | 465 | **Note:** Version bump only for package @swc-node/register 466 | 467 | ## [0.2.2](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.2.1...@swc-node/register@0.2.2) (2020-08-10) 468 | 469 | **Note:** Version bump only for package @swc-node/register 470 | 471 | ## [0.2.1](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.2.0...@swc-node/register@0.2.1) (2020-08-09) 472 | 473 | **Note:** Version bump only for package @swc-node/register 474 | 475 | # [0.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/register@0.1.12...@swc-node/register@0.2.0) (2020-08-09) 476 | 477 | ### Features 478 | 479 | - **core:** add support for emitDecoratorMetadata ([edd2fd5](https://github.com/swc-project/swc-node/commit/edd2fd575bf43bf4206a49b5b078945de5eae95a)) 480 | -------------------------------------------------------------------------------- /packages/register/README.md: -------------------------------------------------------------------------------- 1 | # `@swc-node/register` 2 | 3 | Downloads 4 | 5 | > 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn) 6 | 7 | ## Usage 8 | 9 | ```ts 10 | const { register } = require('@swc-node/register/register') 11 | 12 | register({ 13 | ... 14 | }) 15 | ``` 16 | 17 | ### CLI 18 | 19 | ``` 20 | node -r @swc-node/register index.ts 21 | ``` 22 | 23 | ### Mocha 24 | 25 | ``` 26 | mocha --require @swc-node/register --watch-extensions ts,tsx "test/**/*.{ts,tsx}" [...args] 27 | ``` 28 | 29 | ### ava 30 | 31 | ```json 32 | // package.json 33 | 34 | { 35 | "ava": { 36 | "extensions": ["ts", "tsx"], 37 | "require": ["@swc-node/register"], 38 | "files": ["packages/**/*.spec.{ts,tsx}"] 39 | } 40 | } 41 | ``` 42 | 43 | ## Read `tsconfig.json` 44 | 45 | set `SWC_NODE_PROJECT` or `TS_NODE_PROJECT` env: 46 | 47 | ```bash 48 | SWC_NODE_PROJECT=./tsconfig.test.json mocha --require @swc-node/register --watch-extensions ts,tsx "test/**/*.{ts,tsx}" [...args] 49 | ``` 50 | 51 | `@swc-node/register` respect the following option in `tsconfig`: 52 | 53 | ### `extends` 54 | 55 | `@swc-node/register` respect the extends key in `tsconfig.json`, and use the **merged** values. 56 | 57 | ### `compilerOptions.target` 58 | 59 | ```ts 60 | switch (target) { 61 | case ts.ScriptTarget.ES3: 62 | return 'es3' 63 | case ts.ScriptTarget.ES5: 64 | return 'es5' 65 | case ts.ScriptTarget.ES2015: 66 | return 'es2015' 67 | case ts.ScriptTarget.ES2016: 68 | return 'es2016' 69 | case ts.ScriptTarget.ES2017: 70 | return 'es2017' 71 | case ts.ScriptTarget.ES2018: 72 | return 'es2018' 73 | case ts.ScriptTarget.ES2019: 74 | return 'es2019' 75 | case ts.ScriptTarget.ES2020: 76 | case ts.ScriptTarget.ES2021: 77 | case ts.ScriptTarget.ES2022: 78 | case ts.ScriptTarget.ESNext: 79 | case ts.ScriptTarget.Latest: 80 | return 'es2020' 81 | case ts.ScriptTarget.JSON: 82 | return 'es5' 83 | } 84 | ``` 85 | 86 | ### `compilerOptions.jsx` 87 | 88 | If `filename` endsWith `.jsx` or `.tsx`, always set the `jsx: true` in `swc config` regards the `jsx` option in `tsconfig`. 89 | If `filename` not endsWith `.jsx` or `.tsx`, set the `jsx: Boolean(tsconfig.compilerOptions.jsx)` in `swc config`. 90 | 91 | ### compilerOptions.module 92 | 93 | > notes, if `compilerOptions.module` higher than `es2020`, the `dynamicImport` in `swc config` will be set to `true`. 94 | 95 | ```ts 96 | switch (moduleKind) { 97 | case ts.ModuleKind.CommonJS: 98 | return 'commonjs' 99 | case ts.ModuleKind.UMD: 100 | return 'umd' 101 | case ts.ModuleKind.AMD: 102 | return 'amd' 103 | case ts.ModuleKind.ES2015: 104 | case ts.ModuleKind.ES2020: 105 | case ts.ModuleKind.ESNext: 106 | case ts.ModuleKind.None: 107 | return 'es6' 108 | case ts.ModuleKind.System: 109 | throw new TypeError('Do not support system kind module') 110 | } 111 | ``` 112 | 113 | ### compilerOptions.experimentalDecorators 114 | 115 | Respect the boolean value in `tsconfig`. 116 | 117 | ### compilerOptions.emitDecoratorMetadata 118 | 119 | Respect the boolean value in `tsconfig`. 120 | 121 | ### compilerOptions.esModuleInterop 122 | 123 | Respect the boolean value in `tsconfig`. 124 | 125 | ### include/exclude 126 | 127 | `TypeScript` gives files list to `@swc-node/register`, if parse `tsconfig.json` failed or files list empty, `@swc-node/register` will transform all files which were required. 128 | 129 | And if failed to parse `tsconfig.json`, `@swc-node/register` will print warning which contains failed reason. 130 | -------------------------------------------------------------------------------- /packages/register/__test__/read-default-config.spec.ts: -------------------------------------------------------------------------------- 1 | import { join, dirname } from 'path' 2 | 3 | import test from 'ava' 4 | import { omit } from 'lodash' 5 | import sinon from 'sinon' 6 | import * as ts from 'typescript' 7 | 8 | import { readDefaultTsConfig } from '../read-default-tsconfig' 9 | 10 | test('should read tsconfig from cwd if without any config', (t) => { 11 | delete process.env.SWC_NODE_PROJECT 12 | const defaultOptions = readDefaultTsConfig() 13 | const { config } = ts.readConfigFile(join(process.cwd(), 'tsconfig.json'), ts.sys.readFile) 14 | const { options } = ts.parseJsonConfigFileContent(config, ts.sys, process.cwd()) 15 | t.deepEqual(omit(defaultOptions, 'files'), options) 16 | }) 17 | 18 | test('should RESPECT SWC_NODE_PROJECT env', (t) => { 19 | const configPath = join(__dirname, 'tsconfig.spec.json') 20 | delete process.env.SWC_NODE_PROJECT 21 | delete process.env.TS_NODE_PROJECT 22 | process.env.SWC_NODE_PROJECT = configPath 23 | const defaultOptions = readDefaultTsConfig() 24 | const { config } = ts.readConfigFile(configPath, ts.sys.readFile) 25 | const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(configPath)) 26 | t.deepEqual(omit(defaultOptions, 'files'), options) 27 | }) 28 | 29 | test('should RESPECT TS_NODE_PROJECT env', (t) => { 30 | const configPath = join(__dirname, 'tsconfig.spec.json') 31 | delete process.env.SWC_NODE_PROJECT 32 | delete process.env.TS_NODE_PROJECT 33 | process.env.TS_NODE_PROJECT = configPath 34 | const defaultOptions = readDefaultTsConfig() 35 | const { config } = ts.readConfigFile(configPath, ts.sys.readFile) 36 | const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(configPath)) 37 | t.deepEqual(omit(defaultOptions, 'files'), options) 38 | }) 39 | 40 | test('should RESPECT tsconfig path in subdirectory', (t) => { 41 | const configPath = join(__dirname, 'subdirectory/tsconfig.extend.json') 42 | delete process.env.SWC_NODE_PROJECT 43 | delete process.env.TS_NODE_PROJECT 44 | process.env.TS_NODE_PROJECT = configPath 45 | const defaultOptions = readDefaultTsConfig() 46 | const { config } = ts.readConfigFile(configPath, ts.sys.readFile) 47 | const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(configPath)) 48 | t.deepEqual(omit(defaultOptions, 'files'), options) 49 | }) 50 | 51 | test('should return default compiler options when the tsConfigPath is invalid', (t) => { 52 | const configPath = join(__dirname, 'invalid', 'tsconfig.json') 53 | 54 | delete process.env.SWC_NODE_PROJECT 55 | delete process.env.TS_NODE_PROJECT 56 | process.env.TS_NODE_PROJECT = configPath 57 | 58 | const defaultOptions = readDefaultTsConfig() 59 | t.deepEqual(defaultOptions, { 60 | target: ts.ScriptTarget.ES2018, 61 | module: ts.ModuleKind.CommonJS, 62 | moduleResolution: ts.ModuleResolutionKind.NodeJs, 63 | sourceMap: true, 64 | esModuleInterop: true, 65 | }) 66 | }) 67 | 68 | test('should RESPECT tsconfig path in subdirectory with a relative path', (t) => { 69 | const configPath = join('..', '__test__', 'tsconfig.spec.json') 70 | const fullConfigPath = join(__dirname, 'tsconfig.spec.json') 71 | 72 | delete process.env.SWC_NODE_PROJECT 73 | delete process.env.TS_NODE_PROJECT 74 | process.env.TS_NODE_PROJECT = configPath 75 | 76 | sinon.replace(process, 'cwd', () => __dirname) 77 | 78 | const defaultOptions = readDefaultTsConfig() 79 | 80 | sinon.restore() 81 | 82 | const { config } = ts.readConfigFile(fullConfigPath, ts.sys.readFile) 83 | const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(fullConfigPath)) 84 | t.deepEqual(omit(defaultOptions, 'files'), options) 85 | }) 86 | -------------------------------------------------------------------------------- /packages/register/__test__/subdirectory/tsconfig.extend.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.spec.json", 3 | "compilerOptions": { 4 | "sourceMap": false 5 | }, 6 | "include": ["../*.ts"] 7 | } 8 | -------------------------------------------------------------------------------- /packages/register/__test__/ts-compiler-options-to-swc-config.spec.ts: -------------------------------------------------------------------------------- 1 | import { join } from 'path' 2 | 3 | import test from 'ava' 4 | import * as ts from 'typescript' 5 | 6 | import { tsCompilerOptionsToSwcConfig } from '../read-default-tsconfig' 7 | 8 | test('default values', (t) => { 9 | const options: ts.CompilerOptions = {} 10 | const filename = 'some-file.tsx' 11 | const swcConfig = tsCompilerOptionsToSwcConfig(options, filename) 12 | const expected = { 13 | baseUrl: undefined, 14 | module: 'es6', 15 | sourcemap: false, 16 | experimentalDecorators: false, 17 | emitDecoratorMetadata: false, 18 | useDefineForClassFields: false, 19 | esModuleInterop: false, 20 | dynamicImport: true, 21 | externalHelpers: false, 22 | ignoreDynamic: false, 23 | jsx: true, 24 | paths: {}, 25 | keepClassNames: true, 26 | target: 'es2018', 27 | react: undefined, 28 | swc: { 29 | inputSourceMap: undefined, 30 | sourceRoot: undefined, 31 | }, 32 | } 33 | t.deepEqual(swcConfig, expected) 34 | }) 35 | 36 | test('should set the decorator config', (t) => { 37 | const options: ts.CompilerOptions = { 38 | experimentalDecorators: true, 39 | emitDecoratorMetadata: true, 40 | } 41 | const filename = 'some-file.ts' 42 | const swcConfig = tsCompilerOptionsToSwcConfig(options, filename) 43 | const expected = { 44 | experimentalDecorators: true, 45 | emitDecoratorMetadata: true, 46 | } 47 | t.like(swcConfig, expected) 48 | }) 49 | 50 | test('should force the jsx config', (t) => { 51 | const options: ts.CompilerOptions = { 52 | jsx: ts.JsxEmit.ReactJSX, 53 | } 54 | const filename = 'some-file.ts' 55 | const swcConfig = tsCompilerOptionsToSwcConfig(options, filename) 56 | const expected = { 57 | module: 'es6', 58 | jsx: true, 59 | react: { 60 | pragma: options.jsxFactory, 61 | pragmaFrag: options.jsxFragmentFactory, 62 | importSource: 'react', 63 | runtime: 'automatic', 64 | useBuiltins: true, 65 | }, 66 | swc: { 67 | inputSourceMap: undefined, 68 | sourceRoot: undefined, 69 | }, 70 | } 71 | t.like(swcConfig, expected) 72 | }) 73 | 74 | test('should set all values', (t) => { 75 | const options: ts.CompilerOptions = { 76 | module: ts.ModuleKind.CommonJS, 77 | target: ts.ScriptTarget.ES5, 78 | sourceMap: true, 79 | esModuleInterop: true, 80 | inlineSourceMap: true, 81 | sourceRoot: 'source-root', 82 | importHelpers: true, 83 | jsx: ts.JsxEmit.None, 84 | experimentalDecorators: true, 85 | emitDecoratorMetadata: true, 86 | paths: { 87 | '@test': ['./specific-path-1/test'], 88 | '@another': ['./specific-path-2/another'], 89 | }, 90 | jsxFactory: 'jsx-factory', 91 | jsxFragmentFactory: 'jsx-fragment-factory', 92 | jsxImportSource: 'jsx-import-source', 93 | baseUrl: './packages/register/__test__', 94 | } 95 | const filename = 'some-file.tsx' 96 | const swcConfig = tsCompilerOptionsToSwcConfig(options, filename) 97 | const expected = { 98 | baseUrl: join(process.cwd(), options.baseUrl!), 99 | module: 'commonjs', 100 | sourcemap: 'inline', 101 | target: 'es5', 102 | experimentalDecorators: options.experimentalDecorators, 103 | emitDecoratorMetadata: options.emitDecoratorMetadata, 104 | useDefineForClassFields: false, 105 | esModuleInterop: options.esModuleInterop, 106 | externalHelpers: true, 107 | ignoreDynamic: false, 108 | dynamicImport: true, 109 | keepClassNames: true, 110 | jsx: true, 111 | react: { 112 | pragma: options.jsxFactory, 113 | pragmaFrag: options.jsxFragmentFactory, 114 | importSource: options.jsxImportSource, 115 | runtime: 'classic', 116 | useBuiltins: true, 117 | }, 118 | paths: { 119 | '@test': [join(__dirname, './specific-path-1/test')], 120 | '@another': [join(__dirname, './specific-path-2/another')], 121 | }, 122 | swc: { 123 | inputSourceMap: options.inlineSourceMap, 124 | sourceRoot: options.sourceRoot, 125 | }, 126 | } 127 | t.deepEqual(swcConfig, expected) 128 | }) 129 | -------------------------------------------------------------------------------- /packages/register/__test__/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES3", 4 | "jsx": "react-native", 5 | "module": "System", 6 | "sourceMap": true, 7 | "experimentalDecorators": true, 8 | "emitDecoratorMetadata": true 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/register/esm-register.mts: -------------------------------------------------------------------------------- 1 | import { register } from 'node:module' 2 | import { pathToFileURL } from 'node:url' 3 | 4 | register('@swc-node/register/esm', pathToFileURL('./').toString()) 5 | -------------------------------------------------------------------------------- /packages/register/esm.mts: -------------------------------------------------------------------------------- 1 | import { readFile } from 'node:fs/promises' 2 | import { 3 | createRequire, 4 | type LoadFnOutput, 5 | type LoadHook, 6 | type ResolveFnOutput, 7 | type ResolveHook, 8 | builtinModules, 9 | } from 'node:module' 10 | import { extname, isAbsolute, join } from 'node:path' 11 | import { fileURLToPath, parse as parseUrl, pathToFileURL } from 'node:url' 12 | 13 | import debugFactory from 'debug' 14 | import { EnforceExtension, ResolverFactory } from 'oxc-resolver' 15 | import ts from 'typescript' 16 | 17 | // @ts-expect-error 18 | import { readDefaultTsConfig } from '../lib/read-default-tsconfig.js' 19 | // @ts-expect-error 20 | import { compile, DEFAULT_EXTENSIONS } from '../lib/register.js' 21 | 22 | const debug = debugFactory('@swc-node') 23 | 24 | const builtin = new Set(builtinModules) 25 | 26 | const tsconfig: ts.CompilerOptions = readDefaultTsConfig() 27 | tsconfig.module = ts.ModuleKind.ESNext 28 | 29 | const TSCONFIG_PATH = (function () { 30 | const pathFromEnv = 31 | process.env.SWC_NODE_PROJECT ?? process.env.TS_NODE_PROJECT ?? join(process.cwd(), 'tsconfig.json') 32 | if (!isAbsolute(pathFromEnv)) { 33 | return join(process.cwd(), pathFromEnv) 34 | } 35 | return pathFromEnv 36 | })() 37 | 38 | const resolver = new ResolverFactory({ 39 | tsconfig: { 40 | configFile: TSCONFIG_PATH, 41 | references: 'auto', 42 | }, 43 | conditionNames: ['node', 'import'], 44 | enforceExtension: EnforceExtension.Auto, 45 | extensions: ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts', '.json', '.wasm', '.node'], 46 | extensionAlias: { 47 | '.js': ['.ts', '.tsx', '.js'], 48 | '.mjs': ['.mts', '.mjs'], 49 | '.cjs': ['.cts', '.cjs'], 50 | }, 51 | }) 52 | 53 | const addShortCircuitSignal = (input: T): T => { 54 | return { 55 | ...input, 56 | shortCircuit: true, 57 | } 58 | } 59 | 60 | interface PackageJson { 61 | name: string 62 | version: string 63 | type?: 'module' | 'commonjs' 64 | main?: string 65 | } 66 | 67 | const packageJSONCache = new Map() 68 | 69 | const readFileIfExists = async (path: string) => { 70 | try { 71 | const content = await readFile(path, 'utf-8') 72 | 73 | return JSON.parse(content) 74 | } catch (e) { 75 | // eslint-disable-next-line no-undef 76 | if ((e as NodeJS.ErrnoException).code === 'ENOENT') { 77 | return undefined 78 | } 79 | 80 | throw e 81 | } 82 | } 83 | 84 | const readPackageJSON = async (path: string) => { 85 | if (packageJSONCache.has(path)) { 86 | return packageJSONCache.get(path) 87 | } 88 | 89 | const res = (await readFileIfExists(path)) as PackageJson 90 | packageJSONCache.set(path, res) 91 | return res 92 | } 93 | 94 | const getPackageForFile = async (url: string) => { 95 | // use URL instead path.resolve to handle relative path 96 | let packageJsonURL = new URL('./package.json', url) 97 | 98 | // eslint-disable-next-line no-constant-condition 99 | while (true) { 100 | const path = fileURLToPath(packageJsonURL) 101 | 102 | // for special case by some package manager 103 | if (path.endsWith('node_modules/package.json')) { 104 | break 105 | } 106 | 107 | const packageJson = await readPackageJSON(path) 108 | 109 | if (!packageJson) { 110 | const lastPath = packageJsonURL.pathname 111 | packageJsonURL = new URL('../package.json', packageJsonURL) 112 | 113 | // root level /package.json 114 | if (packageJsonURL.pathname === lastPath) { 115 | break 116 | } 117 | 118 | continue 119 | } 120 | 121 | if (packageJson.type && packageJson.type !== 'module' && packageJson.type !== 'commonjs') { 122 | packageJson.type = undefined 123 | } 124 | 125 | return packageJson 126 | } 127 | 128 | return undefined 129 | } 130 | 131 | export const getPackageType = async (url: string) => { 132 | const packageJson = await getPackageForFile(url) 133 | 134 | return packageJson?.type ?? undefined 135 | } 136 | 137 | const EXTENSION_MODULE_MAP = { 138 | '.mjs': 'module', 139 | '.cjs': 'commonjs', 140 | '.ts': 'module', 141 | '.tsx': 'module', 142 | '.mts': 'module', 143 | '.cts': 'commonjs', 144 | '.json': 'json', 145 | '.wasm': 'wasm', 146 | '.node': 'commonjs', 147 | } as const 148 | 149 | export const resolve: ResolveHook = async (specifier, context, nextResolve) => { 150 | debug('resolve', specifier, JSON.stringify(context)) 151 | 152 | if (specifier.startsWith('node:') || specifier.startsWith('nodejs:')) { 153 | debug('skip resolve: internal format', specifier) 154 | 155 | return addShortCircuitSignal({ 156 | url: specifier, 157 | format: 'builtin', 158 | }) 159 | } 160 | 161 | if (builtin.has(specifier)) { 162 | debug('skip resolve: internal format', specifier) 163 | 164 | return addShortCircuitSignal({ 165 | url: `node:${specifier}`, 166 | format: 'builtin', 167 | }) 168 | } 169 | 170 | if (specifier.startsWith('data:')) { 171 | debug('skip resolve: data url', specifier) 172 | 173 | return addShortCircuitSignal({ 174 | url: specifier, 175 | }) 176 | } 177 | 178 | const parsedUrl = parseUrl(specifier) 179 | 180 | // as entrypoint, just return specifier 181 | if (!context.parentURL || parsedUrl.protocol === 'file:') { 182 | debug('skip resolve: absolute path or entrypoint', specifier) 183 | 184 | let format: ResolveFnOutput['format'] = null 185 | 186 | const specifierPath = fileURLToPath(specifier) 187 | const ext = extname(specifierPath) 188 | 189 | if (ext === '.js') { 190 | format = (await getPackageType(specifier)) === 'module' ? 'module' : 'commonjs' 191 | } else { 192 | format = EXTENSION_MODULE_MAP[ext as keyof typeof EXTENSION_MODULE_MAP] 193 | } 194 | 195 | return addShortCircuitSignal({ 196 | url: specifier, 197 | format, 198 | }) 199 | } 200 | 201 | // import attributes, support json currently 202 | if (context.importAttributes?.type) { 203 | debug('skip resolve: import attributes', specifier) 204 | 205 | return addShortCircuitSignal(await nextResolve(specifier)) 206 | } 207 | 208 | const { error, path, moduleType } = await resolver.async( 209 | join(fileURLToPath(context.parentURL), '..'), 210 | specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier, 211 | ) 212 | 213 | if (error) { 214 | throw new Error(`${error}: ${specifier} cannot be resolved in ${context.parentURL}`) 215 | } 216 | 217 | // local project file 218 | if (path && isPathNotInNodeModules(path)) { 219 | debug('resolved: typescript', specifier, path) 220 | const url = new URL('file://' + join(path)) 221 | return addShortCircuitSignal({ 222 | ...context, 223 | url: url.href, 224 | format: 225 | path.endsWith('cjs') || path.endsWith('cts') || moduleType === 'commonjs' || !moduleType 226 | ? 'commonjs' 227 | : moduleType === 'module' 228 | ? 'module' 229 | : 'commonjs', 230 | }) 231 | } 232 | 233 | try { 234 | // files could not resolved by typescript or resolved as dts, fallback to use node resolver 235 | const res = await nextResolve(specifier) 236 | debug('resolved: fallback node', specifier, res.url, res.format) 237 | return addShortCircuitSignal(res) 238 | } catch (resolveError) { 239 | // fallback to cjs resolve as may import non-esm files 240 | try { 241 | const resolution = pathToFileURL(createRequire(process.cwd()).resolve(specifier)).toString() 242 | 243 | debug('resolved: fallback commonjs', specifier, resolution) 244 | 245 | return addShortCircuitSignal({ 246 | format: 'commonjs', 247 | url: resolution, 248 | }) 249 | } catch (error) { 250 | debug('resolved by cjs error', specifier, error) 251 | throw resolveError 252 | } 253 | } 254 | } 255 | 256 | const tsconfigForSWCNode = { 257 | ...tsconfig, 258 | paths: undefined, 259 | baseUrl: undefined, 260 | } 261 | 262 | export const load: LoadHook = async (url, context, nextLoad) => { 263 | debug('load', url, JSON.stringify(context)) 264 | 265 | if (url.startsWith('data:')) { 266 | debug('skip load: data url', url) 267 | 268 | return nextLoad(url, context) 269 | } 270 | 271 | if (url.includes('/node_modules/')) { 272 | debug('skip load: node_modules', url) 273 | 274 | return nextLoad(url, context) 275 | } 276 | 277 | if (['builtin', 'json', 'wasm'].includes(context.format)) { 278 | debug('loaded: internal format', url) 279 | return nextLoad(url, context) 280 | } 281 | 282 | const { source, format: resolvedFormat } = await nextLoad(url, context) 283 | 284 | if (!source) { 285 | debug('No source', url, resolvedFormat) 286 | return { 287 | source, 288 | format: resolvedFormat, 289 | } 290 | } 291 | 292 | debug('loaded', url, resolvedFormat) 293 | 294 | const code = !source || typeof source === 'string' ? source : Buffer.from(source).toString() 295 | 296 | // url may be essentially an arbitrary string, but fixing the binding module, which currently 297 | // expects a real file path, to correctly interpret this doesn't have an obvious solution, 298 | // and would likely be a breaking change anyway. Do a best effort to give a real path 299 | // like it expects, which at least fixes relative input sourcemap paths. 300 | const filename = url.startsWith('file:') ? fileURLToPath(url) : url 301 | const compiled = await compile(code, filename, tsconfigForSWCNode, true) 302 | 303 | debug('compiled', url, resolvedFormat) 304 | 305 | return addShortCircuitSignal({ 306 | // for lazy: ts-node think format would undefined, actually it should not, keep it as original temporarily 307 | format: resolvedFormat, 308 | source: compiled, 309 | }) 310 | } 311 | 312 | function isPathNotInNodeModules(path: string) { 313 | return ( 314 | (process.platform !== 'win32' && !path.includes('/node_modules/')) || 315 | (process.platform === 'win32' && !path.includes('\\node_modules\\')) 316 | ) 317 | } 318 | -------------------------------------------------------------------------------- /packages/register/index.js: -------------------------------------------------------------------------------- 1 | const { register } = require('./lib/register') 2 | 3 | register() 4 | -------------------------------------------------------------------------------- /packages/register/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@swc-node/register", 3 | "version": "1.10.10", 4 | "description": "SWC node register", 5 | "keywords": [ 6 | "swc", 7 | "babel", 8 | "ts-node", 9 | "napi-rs", 10 | "uglify", 11 | "node-rs", 12 | "napi-rs", 13 | "napi", 14 | "n-api", 15 | "esbuild", 16 | "tsc", 17 | "webpack" 18 | ], 19 | "author": "LongYinan ", 20 | "homepage": "https://github.com/swc-project/swc-node", 21 | "license": "MIT", 22 | "main": "index.js", 23 | "files": [ 24 | "index.js", 25 | "lib", 26 | "esm", 27 | "README.md", 28 | "LICENSE", 29 | "*.d.ts" 30 | ], 31 | "publishConfig": { 32 | "registry": "https://registry.npmjs.org/", 33 | "access": "public" 34 | }, 35 | "repository": { 36 | "type": "git", 37 | "url": "git+https://github.com/swc-project/swc-node.git" 38 | }, 39 | "bugs": { 40 | "url": "https://github.com/swc-project/swc-node/issues" 41 | }, 42 | "dependencies": { 43 | "@swc-node/core": "^1.13.3", 44 | "@swc-node/sourcemap-support": "^0.5.1", 45 | "colorette": "^2.0.20", 46 | "debug": "^4.3.5", 47 | "oxc-resolver": "^8.0.0", 48 | "pirates": "^4.0.6", 49 | "tslib": "^2.6.3" 50 | }, 51 | "peerDependencies": { 52 | "@swc/core": ">= 1.4.13", 53 | "typescript": ">= 4.3" 54 | }, 55 | "devDependencies": { 56 | "@swc/core": "^1.6.6", 57 | "@swc/helpers": "^0.5.11", 58 | "@types/debug": "^4.1.12", 59 | "lodash": "^4.17.21", 60 | "sinon": "^20.0.0", 61 | "typescript": "^5.5.3" 62 | }, 63 | "funding": { 64 | "type": "github", 65 | "url": "https://github.com/sponsors/Brooooooklyn" 66 | }, 67 | "exports": { 68 | ".": { 69 | "node": "./index.js" 70 | }, 71 | "./read-default-tsconfig": { 72 | "node": "./lib/read-default-tsconfig.js" 73 | }, 74 | "./register": { 75 | "require": "./lib/register.js" 76 | }, 77 | "./esm": { 78 | "import": "./esm/esm.mjs" 79 | }, 80 | "./esm-register": { 81 | "import": "./esm/esm-register.mjs" 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /packages/register/read-default-tsconfig.d.ts: -------------------------------------------------------------------------------- 1 | export * from './lib/read-default-tsconfig' 2 | -------------------------------------------------------------------------------- /packages/register/read-default-tsconfig.ts: -------------------------------------------------------------------------------- 1 | import { existsSync } from 'fs' 2 | import { join, dirname, resolve } from 'path' 3 | 4 | import type { Options } from '@swc-node/core' 5 | import { yellow } from 'colorette' 6 | import debugFactory from 'debug' 7 | import * as ts from 'typescript' 8 | 9 | const debug = debugFactory('@swc-node') 10 | 11 | const configCache: Record boolean }>> = {} 12 | 13 | export function readDefaultTsConfig( 14 | tsConfigPath = process.env.SWC_NODE_PROJECT ?? process.env.TS_NODE_PROJECT ?? join(process.cwd(), 'tsconfig.json'), 15 | ) { 16 | let compilerOptions: Partial boolean }> = { 17 | target: ts.ScriptTarget.ES2018, 18 | module: ts.ModuleKind.CommonJS, 19 | moduleResolution: ts.ModuleResolutionKind.NodeJs, 20 | sourceMap: true, 21 | esModuleInterop: true, 22 | } 23 | 24 | if (!tsConfigPath) { 25 | return compilerOptions 26 | } 27 | 28 | const fullTsConfigPath = resolve(tsConfigPath) 29 | 30 | if (fullTsConfigPath in configCache) { 31 | return configCache[fullTsConfigPath] 32 | } 33 | 34 | if (!existsSync(fullTsConfigPath)) { 35 | return compilerOptions 36 | } 37 | 38 | try { 39 | debug(`Read config file from ${fullTsConfigPath}`) 40 | const { config } = ts.readConfigFile(fullTsConfigPath, ts.sys.readFile) 41 | 42 | const { options, errors, fileNames } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(fullTsConfigPath)) 43 | 44 | // if baseUrl not set, use dirname of tsconfig.json. align with ts https://www.typescriptlang.org/tsconfig#paths 45 | if (options.paths && !options.baseUrl) { 46 | options.baseUrl = dirname(fullTsConfigPath) 47 | } 48 | 49 | if (!errors.length) { 50 | compilerOptions = options 51 | compilerOptions.files = fileNames 52 | } else { 53 | console.info(yellow(`Convert compiler options from json failed, ${errors.map((d) => d.messageText).join('\n')}`)) 54 | } 55 | } catch (e) { 56 | console.info(yellow(`Read ${tsConfigPath} failed: ${(e as Error).message}`)) 57 | } 58 | 59 | configCache[fullTsConfigPath] = compilerOptions 60 | 61 | return compilerOptions 62 | } 63 | 64 | function toTsTarget(target: ts.ScriptTarget): Options['target'] { 65 | switch (target) { 66 | case ts.ScriptTarget.ES3: 67 | return 'es3' 68 | case ts.ScriptTarget.ES5: 69 | return 'es5' 70 | case ts.ScriptTarget.ES2015: 71 | return 'es2015' 72 | case ts.ScriptTarget.ES2016: 73 | return 'es2016' 74 | case ts.ScriptTarget.ES2017: 75 | return 'es2017' 76 | case ts.ScriptTarget.ES2018: 77 | return 'es2018' 78 | case ts.ScriptTarget.ES2019: 79 | return 'es2019' 80 | case ts.ScriptTarget.ES2020: 81 | return 'es2020' 82 | case ts.ScriptTarget.ES2021: 83 | return 'es2021' 84 | case ts.ScriptTarget.ES2022: 85 | case ts.ScriptTarget.ESNext: 86 | case ts.ScriptTarget.Latest: 87 | return 'es2022' 88 | case ts.ScriptTarget.JSON: 89 | return 'es5' 90 | } 91 | } 92 | 93 | function toModule(moduleKind: ts.ModuleKind) { 94 | switch (moduleKind) { 95 | case ts.ModuleKind.CommonJS: 96 | return 'commonjs' 97 | case ts.ModuleKind.UMD: 98 | return 'umd' 99 | case ts.ModuleKind.AMD: 100 | return 'amd' 101 | case ts.ModuleKind.ES2015: 102 | case ts.ModuleKind.ES2020: 103 | case ts.ModuleKind.ES2022: 104 | case ts.ModuleKind.ESNext: 105 | case ts.ModuleKind.Node16: 106 | case ts.ModuleKind.NodeNext: 107 | case ts.ModuleKind.None: 108 | return 'es6' 109 | case ts.ModuleKind.System: 110 | throw new TypeError('Do not support system kind module') 111 | } 112 | } 113 | 114 | /** 115 | * The default value for useDefineForClassFields depends on the emit target 116 | * @see https://www.typescriptlang.org/tsconfig#useDefineForClassFields 117 | */ 118 | function getUseDefineForClassFields(compilerOptions: ts.CompilerOptions, target: ts.ScriptTarget): boolean { 119 | return compilerOptions.useDefineForClassFields ?? target >= ts.ScriptTarget.ES2022 120 | } 121 | 122 | export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filename: string): Options { 123 | const isJsx = filename.endsWith('.tsx') || filename.endsWith('.jsx') || Boolean(options.jsx) 124 | const target = options.target ?? ts.ScriptTarget.ES2018 125 | return { 126 | module: toModule(options.module ?? ts.ModuleKind.ES2015), 127 | target: toTsTarget(target), 128 | jsx: isJsx, 129 | // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing 130 | sourcemap: options.sourceMap || options.inlineSourceMap ? 'inline' : Boolean(options.sourceMap), 131 | experimentalDecorators: options.experimentalDecorators ?? false, 132 | emitDecoratorMetadata: options.emitDecoratorMetadata ?? false, 133 | useDefineForClassFields: getUseDefineForClassFields(options, target), 134 | esModuleInterop: options.esModuleInterop ?? false, 135 | dynamicImport: true, 136 | keepClassNames: true, 137 | externalHelpers: Boolean(options.importHelpers), 138 | react: 139 | (options.jsxFactory ?? options.jsxFragmentFactory ?? options.jsx ?? options.jsxImportSource) 140 | ? { 141 | pragma: options.jsxFactory, 142 | pragmaFrag: options.jsxFragmentFactory, 143 | importSource: options.jsxImportSource ?? 'react', 144 | runtime: (options.jsx ?? 0) >= ts.JsxEmit.ReactJSX ? 'automatic' : 'classic', 145 | useBuiltins: true, 146 | } 147 | : undefined, 148 | baseUrl: options.baseUrl ? resolve(options.baseUrl) : undefined, 149 | paths: Object.fromEntries( 150 | Object.entries(options.paths ?? {}).map(([aliasKey, aliasPaths]) => [ 151 | aliasKey, 152 | ((aliasPaths as string[]) ?? []).map((path) => resolve(options.baseUrl ?? './', path)), 153 | ]), 154 | ) as Options['paths'], 155 | ignoreDynamic: Boolean(process.env.SWC_NODE_IGNORE_DYNAMIC), 156 | swc: { 157 | sourceRoot: options.sourceRoot, 158 | inputSourceMap: options.inlineSourceMap, 159 | }, 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /packages/register/register.d.ts: -------------------------------------------------------------------------------- 1 | export * from './lib/register' 2 | -------------------------------------------------------------------------------- /packages/register/register.ts: -------------------------------------------------------------------------------- 1 | import { Options, transform, transformSync } from '@swc-node/core' 2 | import { installSourceMapSupport, SourcemapMap } from '@swc-node/sourcemap-support' 3 | import { addHook } from 'pirates' 4 | import * as ts from 'typescript' 5 | 6 | import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from './read-default-tsconfig' 7 | 8 | const DEFAULT_EXTENSIONS = new Set([ 9 | ts.Extension.Js, 10 | ts.Extension.Ts, 11 | ts.Extension.Jsx, 12 | ts.Extension.Tsx, 13 | ts.Extension.Mjs, 14 | ts.Extension.Mts, 15 | ts.Extension.Cjs, 16 | ts.Extension.Cts, 17 | '.es6', 18 | '.es', 19 | ]) 20 | 21 | const injectInlineSourceMap = ({ 22 | filename, 23 | code, 24 | map, 25 | }: { 26 | filename: string 27 | code: string 28 | map: string | undefined 29 | }): string => { 30 | if (map) { 31 | SourcemapMap.set(filename, map) 32 | const base64Map = Buffer.from(map, 'utf8').toString('base64') 33 | const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}` 34 | return `${code}\n${sourceMapContent}` 35 | } 36 | return code 37 | } 38 | 39 | export function compile( 40 | sourcecode: string | undefined, 41 | filename: string, 42 | options: ts.CompilerOptions & { 43 | fallbackToTs?: (filename: string) => boolean 44 | }, 45 | ): string 46 | 47 | export function compile( 48 | sourcecode: string | undefined, 49 | filename: string, 50 | options: ts.CompilerOptions & { 51 | fallbackToTs?: (filename: string) => boolean 52 | }, 53 | async: false, 54 | ): string 55 | 56 | export function compile( 57 | sourcecode: string | undefined, 58 | filename: string, 59 | options: ts.CompilerOptions & { 60 | fallbackToTs?: (filename: string) => boolean 61 | }, 62 | async: true, 63 | ): Promise 64 | 65 | export function compile( 66 | sourcecode: string | undefined, 67 | filename: string, 68 | options: ts.CompilerOptions & { 69 | fallbackToTs?: (filename: string) => boolean 70 | }, 71 | async: boolean, 72 | ): string | Promise 73 | 74 | export function compile( 75 | sourcecode: string | undefined, 76 | filename: string, 77 | options: ts.CompilerOptions & { 78 | fallbackToTs?: (filename: string) => boolean 79 | }, 80 | async = false, 81 | ) { 82 | if (sourcecode == null) { 83 | return 84 | } 85 | if (options && typeof options.fallbackToTs === 'function' && options.fallbackToTs(filename)) { 86 | delete options.fallbackToTs 87 | const { outputText, sourceMapText } = ts.transpileModule(sourcecode, { 88 | fileName: filename, 89 | compilerOptions: options, 90 | }) 91 | return injectInlineSourceMap({ filename, code: outputText, map: sourceMapText }) 92 | } 93 | 94 | let swcRegisterConfig: Options 95 | if (process.env.SWCRC) { 96 | // when SWCRC environment variable is set to true it will use swcrc file 97 | swcRegisterConfig = { 98 | swc: { 99 | swcrc: true, 100 | configFile: process.env.SWC_CONFIG_FILE 101 | }, 102 | } 103 | } else { 104 | swcRegisterConfig = tsCompilerOptionsToSwcConfig(options, filename) 105 | } 106 | 107 | if (async) { 108 | return transform(sourcecode, filename, swcRegisterConfig).then(({ code, map }) => { 109 | return injectInlineSourceMap({ filename, code, map }) 110 | }) 111 | } else { 112 | const { code, map } = transformSync(sourcecode, filename, swcRegisterConfig) 113 | return injectInlineSourceMap({ filename, code, map }) 114 | } 115 | } 116 | 117 | export function register(options: Partial = {}, hookOpts = {}) { 118 | if (!process.env.SWCRC) { 119 | options = Object.keys(options).length ? options : readDefaultTsConfig() 120 | } 121 | options.module = ts.ModuleKind.CommonJS 122 | installSourceMapSupport() 123 | return addHook((code, filename) => compile(code, filename, options), { 124 | exts: Array.from(DEFAULT_EXTENSIONS), 125 | ...hookOpts, 126 | }) 127 | } 128 | -------------------------------------------------------------------------------- /packages/register/tsconfig.esm.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "esnext", 5 | "outDir": "esm" 6 | }, 7 | "include": [], 8 | "files": ["./esm.mts", "./esm-register.mts", "register.d.ts", "./read-default-tsconfig.d.ts"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/register/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "composite": true, 5 | "rootDir": ".", 6 | "outDir": "./lib", 7 | }, 8 | "references": [ 9 | { 10 | "path": "../core", 11 | }, 12 | ], 13 | "include": ["."], 14 | "exclude": ["lib", "__test__"], 15 | } 16 | -------------------------------------------------------------------------------- /packages/sourcemap-support/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | ## [0.5.1](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.5.0...@swc-node/sourcemap-support@0.5.1) (2024-07-05) 7 | 8 | **Note:** Version bump only for package @swc-node/sourcemap-support 9 | 10 | # [0.5.0](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.3.0...@swc-node/sourcemap-support@0.5.0) (2024-03-05) 11 | 12 | ### Features 13 | 14 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 15 | 16 | # [0.4.0](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.3.0...@swc-node/sourcemap-support@0.4.0) (2024-02-01) 17 | 18 | ### Features 19 | 20 | - upgrade dependencies ([#751](https://github.com/swc-project/swc-node/issues/751)) ([653bd13](https://github.com/swc-project/swc-node/commit/653bd13c4ac84bd4bd28b886dc0f4e77362d0734)) 21 | 22 | # [0.3.0](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.2.4...@swc-node/sourcemap-support@0.3.0) (2023-02-10) 23 | 24 | ### Features 25 | 26 | - **register:** experimental esm loader ([#643](https://github.com/swc-project/swc-node/issues/643)) ([0b4d305](https://github.com/swc-project/swc-node/commit/0b4d30505408f6f07c1ff8ea5c1953e1d22bb4e1)) 27 | 28 | ## [0.2.4](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.2.3...@swc-node/sourcemap-support@0.2.4) (2023-02-07) 29 | 30 | **Note:** Version bump only for package @swc-node/sourcemap-support 31 | 32 | ## [0.2.3](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.2.2...@swc-node/sourcemap-support@0.2.3) (2023-01-05) 33 | 34 | **Note:** Version bump only for package @swc-node/sourcemap-support 35 | 36 | ## [0.2.2](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.2.1...@swc-node/sourcemap-support@0.2.2) (2022-10-06) 37 | 38 | ### Bug Fixes 39 | 40 | - **sourcemap-support:** add "tslib" as a dependnecy for "sourcemap-suport" ([#670](https://github.com/swc-project/swc-node/issues/670)) ([ec9ffeb](https://github.com/swc-project/swc-node/commit/ec9ffeb8691ac6c8d6b08c85f0f6890fc99004e9)) 41 | 42 | ## [0.2.1](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.2.0...@swc-node/sourcemap-support@0.2.1) (2022-09-22) 43 | 44 | **Note:** Version bump only for package @swc-node/sourcemap-support 45 | 46 | # [0.2.0](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.1.11...@swc-node/sourcemap-support@0.2.0) (2022-04-27) 47 | 48 | ### Features 49 | 50 | - **jest:** read tsconfig for default jest transform options ([8c180e6](https://github.com/swc-project/swc-node/commit/8c180e68abbc66aa68f83b401d985a6c8617baa9)) 51 | 52 | ## [0.1.11](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.1.10...@swc-node/sourcemap-support@0.1.11) (2021-12-13) 53 | 54 | **Note:** Version bump only for package @swc-node/sourcemap-support 55 | 56 | ## [0.1.10](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.1.9...@swc-node/sourcemap-support@0.1.10) (2021-10-29) 57 | 58 | **Note:** Version bump only for package @swc-node/sourcemap-support 59 | 60 | ## [0.1.9](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.1.8...@swc-node/sourcemap-support@0.1.9) (2021-10-16) 61 | 62 | **Note:** Version bump only for package @swc-node/sourcemap-support 63 | 64 | ## [0.1.8](https://github.com/swc-project/swc-node/compare/@swc-node/sourcemap-support@0.1.7...@swc-node/sourcemap-support@0.1.8) (2020-08-31) 65 | 66 | **Note:** Version bump only for package @swc-node/sourcemap-support 67 | -------------------------------------------------------------------------------- /packages/sourcemap-support/README.md: -------------------------------------------------------------------------------- 1 | # `@swc-node/sourcemap-support` 2 | 3 | ```ts 4 | import { SourcemapMap, installSourceMapSupport } from '@swc-node/sourcemap-support' 5 | 6 | installSourceMapSupport() 7 | 8 | function transform(sourcecode, filename, options) { 9 | const { code, map } = transformSync(sourcecode, filename, options) 10 | SourcemapMap.set(filename, map) 11 | return code 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /packages/sourcemap-support/index.ts: -------------------------------------------------------------------------------- 1 | import sourceMapSupport from 'source-map-support' 2 | 3 | export const SourcemapMap = new Map() 4 | 5 | export function installSourceMapSupport() { 6 | sourceMapSupport.install({ 7 | handleUncaughtExceptions: false, 8 | environment: 'node', 9 | retrieveSourceMap(file) { 10 | if (SourcemapMap.has(file)) { 11 | return { 12 | url: file, 13 | map: SourcemapMap.get(file), 14 | } 15 | } 16 | return null 17 | }, 18 | }) 19 | } 20 | -------------------------------------------------------------------------------- /packages/sourcemap-support/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@swc-node/sourcemap-support", 3 | "version": "0.5.1", 4 | "description": "Runtime sourcemap support in NodeJS", 5 | "keywords": [ 6 | "swc", 7 | "ts-node", 8 | "N-API", 9 | "NAPI", 10 | "napi-rs", 11 | "node-rs", 12 | "rust", 13 | "esbuild", 14 | "typescript" 15 | ], 16 | "author": "LongYinan ", 17 | "homepage": "https://github.com/swc-project/swc-node", 18 | "license": "MIT", 19 | "main": "lib/index.js", 20 | "types": "lib/index.d.ts", 21 | "files": [ 22 | "lib", 23 | "README.md", 24 | "LICENSE" 25 | ], 26 | "publishConfig": { 27 | "registry": "https://registry.npmjs.org/", 28 | "access": "public" 29 | }, 30 | "repository": { 31 | "type": "git", 32 | "url": "git+https://github.com/swc-project/swc-node.git" 33 | }, 34 | "dependencies": { 35 | "source-map-support": "^0.5.21", 36 | "tslib": "^2.6.3" 37 | }, 38 | "bugs": { 39 | "url": "https://github.com/swc-project/swc-node/issues" 40 | }, 41 | "devDependencies": { 42 | "@types/source-map-support": "^0.5.10" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /packages/sourcemap-support/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "rootDir": ".", 5 | "outDir": "./lib", 6 | "composite": true 7 | }, 8 | "include": [], 9 | "files": ["./index.ts"] 10 | } 11 | -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | packages: ['packages/**'] 2 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["config:base", ":preserveSemverRanges"], 3 | "packageRules": [ 4 | { 5 | "automerge": true, 6 | "matchUpdateTypes": ["minor", "patch", "pin", "digest"] 7 | } 8 | ], 9 | "lockFileMaintenance": { 10 | "enabled": true, 11 | "extends": ["schedule:weekly"] 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "jsx": "react-jsx", 4 | "allowSyntheticDefaultImports": true, 5 | "declaration": true, 6 | "downlevelIteration": true, 7 | "emitDecoratorMetadata": true, 8 | "experimentalDecorators": true, 9 | "importHelpers": false, 10 | "module": "CommonJS", 11 | "moduleResolution": "node", 12 | "newLine": "LF", 13 | "noEmitHelpers": false, 14 | "noUnusedLocals": true, 15 | "noUnusedParameters": true, 16 | "strict": true, 17 | "skipLibCheck": true, 18 | "forceConsistentCasingInFileNames": true, 19 | "preserveSymlinks": true, 20 | "target": "ES2018", 21 | "sourceMap": true, 22 | "esModuleInterop": true, 23 | "stripInternal": true, 24 | "resolveJsonModule": true, 25 | "importsNotUsedAsValues": "remove", 26 | "lib": ["dom", "DOM.Iterable", "ES5", "ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "esnext"], 27 | "baseUrl": "./packages", 28 | "paths": { 29 | "@swc-node/core": ["./core/index.ts"], 30 | "@swc-node/register/*": ["./register/*"], 31 | "@integrate/*": ["./integrate/src/*"], 32 | }, 33 | }, 34 | "references": [ 35 | { "path": "./packages/core" }, 36 | { "path": "./packages/register" }, 37 | { "path": "./packages/register/tsconfig.esm.json" }, 38 | { "path": "./packages/jest" }, 39 | { "path": "./packages/sourcemap-support" }, 40 | { "path": "./packages/loader" }, 41 | ], 42 | "include": [], 43 | "files": [], 44 | } 45 | -------------------------------------------------------------------------------- /tsconfig.lint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "include": ["."] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "CommonJS" 5 | }, 6 | "include": ["packages"] 7 | } 8 | --------------------------------------------------------------------------------