├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitattributes ├── .github └── workflows │ ├── dev.yml │ └── release.yml ├── .gitignore ├── .npmrc ├── .prettierignore ├── CHANGELOG ├── CHANGELOG.md ├── LICENSE ├── README.md ├── cjs-stub.js ├── index.js ├── mjs-stub.js ├── package.json ├── scripts └── plugin.mjs └── test ├── .gitkeep ├── fixtures ├── babel.js │ └── 0 │ │ ├── package.json │ │ └── test.babel.js ├── babel.jsx │ └── 0 │ │ ├── package.json │ │ └── test.babel.jsx ├── babel.ts │ └── 0 │ │ ├── package.json │ │ └── test.babel.ts ├── babel.tsx │ └── 0 │ │ ├── data.babel.tsx │ │ ├── package.json │ │ └── test.babel.tsx ├── cjs │ ├── 0 │ │ ├── package.json │ │ ├── rechoir.js │ │ └── test.cjs │ └── 1 │ │ ├── package.json │ │ ├── rechoir.js │ │ └── test.cjs ├── coffee.md │ └── 0 │ │ ├── package.json │ │ └── test.coffee.md ├── coffee │ └── 0 │ │ ├── package.json │ │ └── test.coffee ├── cts │ └── 0 │ │ ├── package.json │ │ ├── test.cts │ │ └── tsconfig.json ├── esbuild.js │ └── 0 │ │ ├── package.json │ │ └── test.esbuild.js ├── esbuild.jsx │ └── 0 │ │ ├── package.json │ │ └── test.esbuild.jsx ├── esbuild.ts │ └── 0 │ │ ├── package.json │ │ └── test.esbuild.ts ├── esbuild.tsx │ └── 0 │ │ ├── package.json │ │ └── test.esbuild.tsx ├── esm.js │ └── 0 │ │ ├── package.json │ │ └── test.esm.js ├── js │ └── 0 │ │ ├── package.json │ │ └── test.js ├── json │ └── 0 │ │ ├── package.json │ │ └── test.json ├── json5 │ └── 0 │ │ ├── package.json │ │ └── test.json5 ├── jsx │ ├── 0 │ │ ├── package.json │ │ └── test.jsx │ └── 1 │ │ ├── package.json │ │ └── test.jsx ├── litcoffee │ └── 0 │ │ ├── package.json │ │ └── test.litcoffee ├── mdx │ └── 0 │ │ ├── package.json │ │ └── test.mdx ├── mjs │ ├── 0 │ │ ├── package.json │ │ └── test.mjs │ └── 1 │ │ ├── package.json │ │ └── test.mjs ├── sucrase.js │ └── 0 │ │ ├── package.json │ │ └── test.sucrase.js ├── sucrase.jsx │ └── 0 │ │ ├── package.json │ │ └── test.sucrase.jsx ├── sucrase.ts │ └── 0 │ │ ├── package.json │ │ └── test.sucrase.ts ├── sucrase.tsx │ └── 0 │ │ ├── package.json │ │ └── test.sucrase.tsx ├── swc.js │ └── 0 │ │ ├── package.json │ │ └── test.swc.js ├── swc.jsx │ └── 0 │ │ ├── package.json │ │ └── test.swc.jsx ├── swc.ts │ └── 0 │ │ ├── package.json │ │ └── test.swc.ts ├── swc.tsx │ └── 0 │ │ ├── package.json │ │ └── test.swc.tsx ├── toml │ └── 0 │ │ ├── package.json │ │ └── test.toml ├── ts │ ├── 0 │ │ ├── package.json │ │ ├── test.ts │ │ └── tsconfig.json │ ├── 1 │ │ ├── package.json │ │ ├── test.ts │ │ └── tsconfig.json │ ├── 2 │ │ ├── package.json │ │ └── test.ts │ ├── 3 │ │ ├── package.json │ │ └── test.ts │ └── 4 │ │ ├── package.json │ │ └── test.ts ├── tsx │ ├── 0 │ │ ├── package.json │ │ ├── test.tsx │ │ └── tsconfig.json │ ├── 1 │ │ ├── package.json │ │ ├── test.tsx │ │ └── tsconfig.json │ ├── 2 │ │ ├── data.tsx │ │ ├── package.json │ │ └── test.tsx │ ├── 3 │ │ ├── data.tsx │ │ ├── package.json │ │ └── test.tsx │ └── 4 │ │ ├── package.json │ │ └── test.tsx ├── yaml │ └── 0 │ │ ├── package.json │ │ └── test.yaml └── yml │ └── 0 │ ├── package.json │ └── test.yml └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | end_of_line = lf 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | test/fixtures 2 | coverage/ 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "gulp", 3 | "rules": { 4 | "max-len": ["error", { "code": 110, "ignoreComments": true }] 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/dev.yml: -------------------------------------------------------------------------------- 1 | name: dev 2 | on: 3 | pull_request: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | env: 9 | CI: true 10 | 11 | jobs: 12 | prettier: 13 | name: Format code 14 | runs-on: ubuntu-latest 15 | if: ${{ github.event_name == 'push' }} 16 | 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v2 20 | 21 | - name: Set Node.js version 22 | uses: actions/setup-node@v2 23 | with: 24 | node-version: 16 25 | 26 | - run: node --version 27 | - run: npm --version 28 | 29 | - name: Install npm dependencies 30 | run: npm install 31 | 32 | - name: Update README 33 | run: npm run readme 34 | 35 | - name: Prettier 36 | uses: gulpjs/prettier_action@v3.0 37 | with: 38 | commit_message: 'chore: Update readme & run prettier' 39 | prettier_options: '--write .' 40 | 41 | test: 42 | name: Tests for Node ${{ matrix.node }} on ${{ matrix.os }} 43 | runs-on: ${{ matrix.os }} 44 | 45 | strategy: 46 | fail-fast: false 47 | matrix: 48 | node: [10, 12, 14, 16] 49 | os: [ubuntu-latest, windows-latest, macos-latest] 50 | 51 | steps: 52 | - name: Clone repository 53 | uses: actions/checkout@v2 54 | 55 | - name: Set Node.js version 56 | uses: actions/setup-node@v2 57 | with: 58 | node-version: ${{ matrix.node }} 59 | 60 | - run: node --version 61 | - run: npm --version 62 | 63 | - name: Install npm dependencies 64 | run: npm install 65 | 66 | - name: Run lint 67 | run: npm run lint 68 | 69 | - name: Run tests 70 | run: npm test 71 | 72 | - name: Coveralls 73 | uses: coverallsapp/github-action@v1.1.2 74 | with: 75 | github-token: ${{ secrets.GITHUB_TOKEN }} 76 | flag-name: ${{matrix.os}}-node-${{ matrix.node }} 77 | parallel: true 78 | 79 | coveralls: 80 | needs: test 81 | name: Finish up 82 | 83 | runs-on: ubuntu-latest 84 | steps: 85 | - name: Coveralls Finished 86 | uses: coverallsapp/github-action@v1.1.2 87 | with: 88 | github-token: ${{ secrets.GITHUB_TOKEN }} 89 | parallel-finished: true 90 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | on: 3 | push: 4 | branches: 5 | - master 6 | - main 7 | 8 | jobs: 9 | release-please: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: GoogleCloudPlatform/release-please-action@v2 13 | with: 14 | token: ${{ secrets.GITHUB_TOKEN }} 15 | release-type: node 16 | package-name: release-please-action 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | # Garbage files 64 | .DS_Store 65 | tmp 66 | egcache 67 | 68 | # Test results 69 | test.xunit 70 | 71 | # Created in fixtures directories by test 72 | package-lock.json 73 | 74 | # Generated by our README generator 75 | scripts/extensions.yaml 76 | scripts/jsVariants.yaml 77 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | coverage/ 2 | .nyc_output/ 3 | CHANGELOG.md 4 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | v1.0.3: 2 | date: 2017-04-18 3 | changes: 4 | - fix buble support 5 | v1.0.2: 6 | date: 2017-03-29 7 | changes: 8 | - add support for coffeescript (now with no hyphen) 9 | v1.0.1: 10 | date: 2016-05-01 11 | changes: 12 | - add support for buble 13 | v1.0.0: 14 | date: 2015-11-18 15 | changes: 16 | - add support for babel-register 17 | - go stable! 18 | v0.6.6: 19 | date: 2015-09-21 20 | changes: 21 | - add support for ts-node (formerly typescript-node) 22 | v0.6.5: 23 | date: 2015-07-22 24 | changes: 25 | - add support for typescript 1.5 via typescript-node 26 | v0.6.4: 27 | date: 2015-07-07 28 | changes: 29 | - add support for earlgrey 30 | v0.6.3: 31 | date: 2015-07-03 32 | changes: 33 | - prefer babel/core to babel 34 | v0.6.2: 35 | date: 2015-05-20 36 | changes: 37 | - update module list for iced coffee-script 38 | v0.6.1: 39 | date: 2015-05-20 40 | changes: 41 | - Fix toml loader. 42 | v0.6.0: 43 | date: 2015-05-19 44 | changes: 45 | - Combine fallbacks and loaders into `extensions`. 46 | - Provide implementation guidance. 47 | v0.5.1: 48 | date: 2015-03-01 49 | changes: 50 | - Add support for CirruScript. 51 | v0.5.0: 52 | date: 2015-02-27 53 | changes: 54 | - Refactor es6 support via Babel (formerly 6to5) 55 | v0.4.3: 56 | date: 2015-02-09 57 | changes: 58 | - Switch support from typescript-require to typescript-register. 59 | v0.4.2: 60 | date: 2015-01-16 61 | changes: 62 | - Add support for wisp. 63 | v0.4.1: 64 | date: 2015-01-10 65 | changes: 66 | - Add support for 6to5 (es6) 67 | v0.4.0: 68 | date: 2014-01-09 69 | changes: 70 | - Add support for fallback (legacy) modules 71 | - Add support for module configurations 72 | v0.3.10: 73 | date: 2014-12-17 74 | changes: 75 | - Add support for json5. 76 | v0.3.9: 77 | date: 2014-12-08 78 | changes: 79 | - Add support for literate iced coffee. 80 | v0.3.8: 81 | date: 2014-11-20 82 | changes: 83 | - Add support for [cjsx](https://github.com/jsdf/coffee-react). 84 | v0.3.7: 85 | date: 2014-09-08 86 | changes: 87 | - Add support for [TypeScript](http://www.typescriptlang.org/). 88 | v0.3.6: 89 | date: 2014-08-25 90 | changes: 91 | - Add support for coffee.md. 92 | v0.3.5: 93 | date: 2014-07-03 94 | changes: 95 | - Add support for jsx. 96 | v0.3.4: 97 | date: 2014-06-27 98 | changes: 99 | - Make .js first jsVariant entry. 100 | v0.3.3: 101 | date: 2014-06-02 102 | changes: 103 | - Fix casing on livescript dependency. 104 | v0.3.0: 105 | date: 2014-04-20 106 | changes: 107 | - Simplify loading of coffee-script and iced-coffee-script. 108 | v0.2.0: 109 | date: 2014-04-20 110 | changes: 111 | - Move module loading into rechoir. 112 | v0.1.0: 113 | date: 2014-04-20 114 | changes: 115 | - Initial public release. 116 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ### [3.1.1](https://www.github.com/gulpjs/interpret/compare/v3.1.0...v3.1.1) (2022-06-29) 4 | 5 | 6 | ### Bug Fixes 7 | 8 | * Include cjs-stub in npm bundle ([4f7d798](https://www.github.com/gulpjs/interpret/commit/4f7d7981565a704af9ded99eb953faa1a838f8af)) 9 | 10 | ## [3.1.0](https://www.github.com/gulpjs/interpret/compare/v3.0.0...v3.1.0) (2022-06-29) 11 | 12 | 13 | ### Features 14 | 15 | * Add `.cts` to support typescript 4.7 ([#90](https://www.github.com/gulpjs/interpret/issues/90)) ([c1ffa36](https://www.github.com/gulpjs/interpret/commit/c1ffa36fbd1088f2dbdb00c9500eecfce70eafc0)) 16 | 17 | ## [3.0.0](https://www.github.com/gulpjs/interpret/compare/v2.2.0...v3.0.0) (2022-04-12) 18 | 19 | 20 | ### ⚠ BREAKING CHANGES 21 | 22 | * Provide default configuration in register functions (#83) 23 | * Only load sucrase tsx hook for `.tsx` extension 24 | * Normalize repository, dropping node <10.13 support (#80) 25 | * Remove legacy node-jsx hook 26 | * Remove deprecated typescript-node hook 27 | * Remove deprecated typescript-register hook 28 | * Remove unmaintained typescript-require hook 29 | * Replace legacy require-yaml with yaml-hook 30 | * Remove legacy require-xml support 31 | * Remove legacy/deprecated babel hooks 32 | * Remove legacy buble support 33 | * Remove legacy cirru-script support 34 | * Remove legacy node-cjsx support 35 | * Remove legacy coco support 36 | * Remove legacy/deprecated coffeescript hooks 37 | * Remove legacy require-csv support 38 | * Remove legacy earlgrey support 39 | * Remove legacy iced-coffee-script support 40 | * Remove legacy require-ini support 41 | * Remove legacy json5 hook 42 | * Remove legacy livescript support 43 | * Remove legacy wisp support 44 | * Drop legacy loaders & extensions (#79) 45 | * Ensure babel only transforms files that match the full extension 46 | 47 | ### Features 48 | 49 | * Add `.cjs` extension and stub hook ([#75](https://www.github.com/gulpjs/interpret/issues/75)) ([7989161](https://www.github.com/gulpjs/interpret/commit/7989161ba5c9ac4e99ec17043b73e525e8e07874)) 50 | * Add `@swc/register` as a loader for `.ts` and `.tsx` extensions ([#74](https://www.github.com/gulpjs/interpret/issues/74)) ([f160451](https://www.github.com/gulpjs/interpret/commit/f160451b720ea1b0db4a1f66a646fca9758e71ad)) 51 | * Add esbuild-register for typescript extensions ([#77](https://www.github.com/gulpjs/interpret/issues/77)) ([963f5fa](https://www.github.com/gulpjs/interpret/commit/963f5fadb0b01a0640c52b68f4d76480fdbf70eb)) 52 | * Add new extensions as JS variants ([8a8df59](https://www.github.com/gulpjs/interpret/commit/8a8df595afc2b813f48e5a87c0f6f88b17f746a7)) 53 | * Add sucrase hook as alternative for `.jsx` ([58f678e](https://www.github.com/gulpjs/interpret/commit/58f678e78c81fb953b50935cdefbc78bf6d3b77f)) 54 | * Add support for `.esbuild.(js|jsx|ts|tsx)` extensions ([fcb9672](https://www.github.com/gulpjs/interpret/commit/fcb967211180216f3ddd0358e8634e9ee5c955fd)) 55 | * Add support for `.sucrase.(js|jsx|ts|tsx)` extensions ([216ad12](https://www.github.com/gulpjs/interpret/commit/216ad128fdf0a774b81297729d1d17c2b2ff4893)) 56 | * Add support for `.swc.(js|jsx|ts|tsx)` extensions ([c054cf2](https://www.github.com/gulpjs/interpret/commit/c054cf2e5322e7306f8bbf6778956d97d65e37a1)) 57 | * Allow register function configuration to be overridden ([7856f7e](https://www.github.com/gulpjs/interpret/commit/7856f7ef812bae3cc609db15e3e98dff3a0cd536)) 58 | * Leverage endsWith instead of RegExp in matchers ([#82](https://www.github.com/gulpjs/interpret/issues/82)) ([6404724](https://www.github.com/gulpjs/interpret/commit/6404724bc9814540aaa46a680c165eac0c1f32d9)) 59 | * Provide default configuration in register functions ([#83](https://www.github.com/gulpjs/interpret/issues/83)) ([7856f7e](https://www.github.com/gulpjs/interpret/commit/7856f7ef812bae3cc609db15e3e98dff3a0cd536)) 60 | * Support `.babel.(jsx|tsx)` extensions ([1e3d0f8](https://www.github.com/gulpjs/interpret/commit/1e3d0f81c0aa96e6ea084a75a54af2bf3be53aef)) 61 | * Support `.mdx` extension hook ([#85](https://www.github.com/gulpjs/interpret/issues/85)) ([cd24c39](https://www.github.com/gulpjs/interpret/commit/cd24c39f38742adf8339183adf24d548479810f0)) 62 | 63 | 64 | ### Bug Fixes 65 | 66 | * Apply correct ordering to esbuild-register ([fcfbdb4](https://www.github.com/gulpjs/interpret/commit/fcfbdb44477c6ed063a22a9ce3b972698479da3b)) 67 | * Ensure babel only transforms files that match the full extension ([81ed502](https://www.github.com/gulpjs/interpret/commit/81ed5024b1d961029320a048036f7a559d11cdac)) 68 | * Ensure esbuild-register only applies to ts or tsx files ([5680b3f](https://www.github.com/gulpjs/interpret/commit/5680b3fbd5f08a23807f23d500d222b056cfe542)) 69 | * Only load sucrase tsx hook for `.tsx` extension ([e9376a1](https://www.github.com/gulpjs/interpret/commit/e9376a18587b5c73f5f1cb5fa60fa73b8fab3a96)) 70 | 71 | 72 | ### Miscellaneous Chores 73 | 74 | * Drop legacy loaders & extensions ([#79](https://www.github.com/gulpjs/interpret/issues/79)) ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 75 | * Normalize repository, dropping node <10.13 support ([#80](https://www.github.com/gulpjs/interpret/issues/80)) ([7b69c63](https://www.github.com/gulpjs/interpret/commit/7b69c632592dfe3e84332ee27eec28805c23a30e)) 76 | * Remove deprecated typescript-node hook ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 77 | * Remove deprecated typescript-register hook ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 78 | * Remove legacy buble support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 79 | * Remove legacy cirru-script support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 80 | * Remove legacy coco support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 81 | * Remove legacy earlgrey support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 82 | * Remove legacy iced-coffee-script support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 83 | * Remove legacy json5 hook ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 84 | * Remove legacy livescript support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 85 | * Remove legacy node-cjsx support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 86 | * Remove legacy node-jsx hook ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 87 | * Remove legacy require-csv support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 88 | * Remove legacy require-ini support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 89 | * Remove legacy require-xml support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 90 | * Remove legacy wisp support ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 91 | * Remove legacy/deprecated babel hooks ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 92 | * Remove legacy/deprecated coffeescript hooks ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 93 | * Remove unmaintained typescript-require hook ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 94 | * Replace legacy require-yaml with yaml-hook ([18a0319](https://www.github.com/gulpjs/interpret/commit/18a0319c0d53c4c33a8ce9badca4d6cfe98cb314)) 95 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2018, 2022 Tyler Kellen , Blaine Bublitz , and Eric Schoffstall 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 |

2 | 3 | 4 | 5 |

6 | 7 | # interpret 8 | 9 | [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url] 10 | 11 | A dictionary of file extensions and associated module loaders. 12 | 13 | ## What is it 14 | 15 | This is used by [Liftoff] to automatically require dependencies for configuration files, and by [rechoir] for registering module loaders. 16 | 17 | ## How to use it 18 | 19 | Consumers should use the exported `extensions` or `jsVariants` object to determine which module should be loaded for a given extension. If a matching extension is found, consumers should do the following: 20 | 21 | 1. If the value is null, do nothing. 22 | 2. If the value is a string, try to require it. 23 | 3. If the value is an object, try to require the `module` property. If successful, the `register` property (a function) should be called with the module passed as the first argument. **Advanced:** An optional second argument can be provided to replace the default configuration for a hook. 24 | 4. If the value is an array, iterate over it, attempting step #2 or #3 until one of the attempts does not throw. 25 | 26 | ## API 27 | 28 | This module provides two top-level properties: `extensions` and `jsVariants`. 29 | 30 | **Note:** This module does not depend on any of the loaders it recommends; instead, end-users are expected to install the hooks they want to use for the file types they want to use. See supported extensions and their hooks in the sections below. 31 | 32 | ### `extensions` 33 | 34 | A mapping of file extensions to modules which provide a [require.extensions] loader. 35 | 36 | File extension keys are all in the format of `'.foo'` or `'.foo.bar'` and module loader values are either `null` if the loader should fallthrough to node's loader, 37 | or a string representing the module to be required, an object of `{ module: 'foobar', register: function }`, or an array containing those strings and/or objects. 38 | 39 | A sample of an entry containing multiple hooks would look like: 40 | 41 | ```js 42 | { 43 | '.ts': [ 44 | 'ts-node/register', 45 | 'sucrase/register/ts', 46 | { 47 | module: '@babel/register', 48 | register: function(hook) { 49 | hook({ 50 | extensions: '.ts', 51 | rootMode: 'upward-optional', 52 | ignore: [ignoreNonBabelAndNodeModules], 53 | }); 54 | }, 55 | }, 56 | ], 57 | } 58 | ``` 59 | 60 | **Supported extensions and their hooks** 61 | 62 | ```yaml file=scripts/extensions.yaml 63 | .babel.js: 64 | - '@babel/register' 65 | .babel.jsx: 66 | - '@babel/register' 67 | .babel.ts: 68 | - '@babel/register' 69 | .babel.tsx: 70 | - '@babel/register' 71 | .cjs: 72 | - interpret/cjs-stub 73 | .coffee: 74 | - coffeescript/register 75 | .coffee.md: 76 | - coffeescript/register 77 | .cts: 78 | - ts-node/register 79 | .esbuild.js: 80 | - esbuild-register/dist/node 81 | .esbuild.jsx: 82 | - esbuild-register/dist/node 83 | .esbuild.ts: 84 | - esbuild-register/dist/node 85 | .esbuild.tsx: 86 | - esbuild-register/dist/node 87 | .esm.js: 88 | - esm 89 | .js: 90 | - built-in node.js loader 91 | .json: 92 | - built-in node.js loader 93 | .json5: 94 | - json5/lib/register 95 | .jsx: 96 | - '@babel/register' 97 | - sucrase/register/jsx 98 | .litcoffee: 99 | - coffeescript/register 100 | .mdx: 101 | - '@mdx-js/register' 102 | .mjs: 103 | - interpret/mjs-stub 104 | .node: 105 | - built-in node.js loader 106 | .sucrase.js: 107 | - sucrase/dist/register 108 | .sucrase.jsx: 109 | - sucrase/dist/register 110 | .sucrase.ts: 111 | - sucrase/dist/register 112 | .sucrase.tsx: 113 | - sucrase/dist/register 114 | .swc.js: 115 | - '@swc/register' 116 | .swc.jsx: 117 | - '@swc/register' 118 | .swc.ts: 119 | - '@swc/register' 120 | .swc.tsx: 121 | - '@swc/register' 122 | .toml: 123 | - toml-require 124 | .ts: 125 | - ts-node/register 126 | - sucrase/register/ts 127 | - '@babel/register' 128 | - esbuild-register/dist/node 129 | - '@swc/register' 130 | .tsx: 131 | - ts-node/register 132 | - sucrase/register/tsx 133 | - '@babel/register' 134 | - esbuild-register/dist/node 135 | - '@swc/register' 136 | .yaml: 137 | - yaml-hook/register 138 | .yml: 139 | - yaml-hook/register 140 | ``` 141 | 142 | ### `jsVariants` 143 | 144 | The `jsVariants` is the same mapping as above, but only include the extensions which are variants of JavaScript. 145 | 146 | **Supported extensions and their hooks** 147 | 148 | ```yaml file=scripts/jsVariants.yaml 149 | .babel.js: 150 | - '@babel/register' 151 | .babel.jsx: 152 | - '@babel/register' 153 | .babel.ts: 154 | - '@babel/register' 155 | .babel.tsx: 156 | - '@babel/register' 157 | .cjs: 158 | - interpret/cjs-stub 159 | .coffee: 160 | - coffeescript/register 161 | .coffee.md: 162 | - coffeescript/register 163 | .esbuild.js: 164 | - esbuild-register/dist/node 165 | .esbuild.jsx: 166 | - esbuild-register/dist/node 167 | .esbuild.ts: 168 | - esbuild-register/dist/node 169 | .esbuild.tsx: 170 | - esbuild-register/dist/node 171 | .esm.js: 172 | - esm 173 | .js: 174 | - built-in node.js loader 175 | .jsx: 176 | - '@babel/register' 177 | - sucrase/register/jsx 178 | .litcoffee: 179 | - coffeescript/register 180 | .mdx: 181 | - '@mdx-js/register' 182 | .mjs: 183 | - interpret/mjs-stub 184 | .sucrase.js: 185 | - sucrase/dist/register 186 | .sucrase.jsx: 187 | - sucrase/dist/register 188 | .sucrase.ts: 189 | - sucrase/dist/register 190 | .sucrase.tsx: 191 | - sucrase/dist/register 192 | .swc.js: 193 | - '@swc/register' 194 | .swc.jsx: 195 | - '@swc/register' 196 | .swc.ts: 197 | - '@swc/register' 198 | .swc.tsx: 199 | - '@swc/register' 200 | .ts: 201 | - ts-node/register 202 | - sucrase/register/ts 203 | - '@babel/register' 204 | - esbuild-register/dist/node 205 | - '@swc/register' 206 | .tsx: 207 | - ts-node/register 208 | - sucrase/register/tsx 209 | - '@babel/register' 210 | - esbuild-register/dist/node 211 | - '@swc/register' 212 | ``` 213 | 214 | ## License 215 | 216 | MIT 217 | 218 | 219 | 220 | [downloads-image]: https://img.shields.io/npm/dm/interpret.svg?style=flat-square 221 | 222 | [npm-url]: https://www.npmjs.com/package/interpret 223 | 224 | [npm-image]: https://img.shields.io/npm/v/interpret.svg?style=flat-square 225 | 226 | [ci-url]: https://github.com/gulpjs/interpret/actions?query=workflow:dev 227 | 228 | [ci-image]: https://img.shields.io/github/workflow/status/gulpjs/interpret/dev?style=flat-square 229 | 230 | [coveralls-url]: https://coveralls.io/r/gulpjs/interpret 231 | 232 | [coveralls-image]: https://img.shields.io/coveralls/gulpjs/interpret/master.svg?style=flat-square 233 | 234 | 235 | 236 | 237 | 238 | [Liftoff]: http://github.com/gulpjs/liftoff 239 | 240 | [rechoir]: http://github.com/gulpjs/rechoir 241 | 242 | [require.extensions]: https://nodejs.org/api/modules.html#requireextensions 243 | 244 | 245 | -------------------------------------------------------------------------------- /cjs-stub.js: -------------------------------------------------------------------------------- 1 | require.extensions['.cjs'] = null; 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | 3 | // We only register on the final extension (like `.js`) due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353 4 | // However, we use these matchers to apply the transform only if the full extension matches 5 | function endsInJsx(filename) { 6 | return filename.endsWith('.jsx'); 7 | } 8 | function endsInTs(filename) { 9 | return filename.endsWith('.ts'); 10 | } 11 | function endsInTsx(filename) { 12 | return filename.endsWith('.tsx'); 13 | } 14 | function endsInBabelJs(filename) { 15 | return filename.endsWith('.babel.js'); 16 | } 17 | function endsInBabelJsx(filename) { 18 | return filename.endsWith('.babel.jsx'); 19 | } 20 | function endsInBabelTs(filename) { 21 | return filename.endsWith('.babel.ts'); 22 | } 23 | function endsInBabelTsx(filename) { 24 | return filename.endsWith('.babel.tsx'); 25 | } 26 | function endsInEsbuildJs(filename) { 27 | return filename.endsWith('.esbuild.js'); 28 | } 29 | function endsInEsbuildJsx(filename) { 30 | return filename.endsWith('.esbuild.jsx'); 31 | } 32 | function endsInEsbuildTs(filename) { 33 | return filename.endsWith('.esbuild.ts'); 34 | } 35 | function endsInEsbuildTsx(filename) { 36 | return filename.endsWith('.esbuild.tsx'); 37 | } 38 | function endsInSucraseJs(filename) { 39 | return filename.endsWith('.sucrase.js'); 40 | } 41 | function endsInSucraseJsx(filename) { 42 | return filename.endsWith('.sucrase.jsx'); 43 | } 44 | function endsInSucraseTs(filename) { 45 | return filename.endsWith('.sucrase.ts'); 46 | } 47 | function endsInSucraseTsx(filename) { 48 | return filename.endsWith('.sucrase.tsx'); 49 | } 50 | function endsInSwcJs(filename) { 51 | return filename.endsWith('.swc.js'); 52 | } 53 | function endsInSwcJsx(filename) { 54 | return filename.endsWith('.swc.jsx'); 55 | } 56 | function endsInSwcTs(filename) { 57 | return filename.endsWith('.swc.ts'); 58 | } 59 | function endsInSwcTsx(filename) { 60 | return filename.endsWith('.swc.tsx'); 61 | } 62 | 63 | var cjsStub = path.join(__dirname, 'cjs-stub'); 64 | var mjsStub = path.join(__dirname, 'mjs-stub'); 65 | 66 | function isNodeModules(file) { 67 | return path.relative(process.cwd(), file).includes('node_modules'); 68 | } 69 | 70 | var extensions = { 71 | '.babel.js': { 72 | module: '@babel/register', 73 | register: function (hook, config) { 74 | config = config || { 75 | rootMode: 'upward-optional', 76 | overrides: [{ only: [endsInBabelJs], presets: ['@babel/preset-env'] }], 77 | }; 78 | 79 | hook(Object.assign({}, config, { extensions: '.js' })); 80 | }, 81 | }, 82 | '.babel.jsx': { 83 | module: '@babel/register', 84 | register: function (hook, config) { 85 | config = config || { 86 | rootMode: 'upward-optional', 87 | overrides: [ 88 | { 89 | only: [endsInBabelJsx], 90 | presets: ['@babel/preset-env', '@babel/preset-react'], 91 | }, 92 | ], 93 | }; 94 | 95 | hook(Object.assign({}, config, { extensions: '.jsx' })); 96 | }, 97 | }, 98 | '.babel.ts': [ 99 | { 100 | module: '@babel/register', 101 | register: function (hook, config) { 102 | config = config || { 103 | rootMode: 'upward-optional', 104 | overrides: [ 105 | { 106 | only: [endsInBabelTs], 107 | presets: ['@babel/preset-env', '@babel/preset-typescript'], 108 | }, 109 | ], 110 | }; 111 | 112 | hook(Object.assign({}, config, { extensions: '.ts' })); 113 | }, 114 | }, 115 | ], 116 | '.babel.tsx': { 117 | module: '@babel/register', 118 | register: function (hook, config) { 119 | config = config || { 120 | rootMode: 'upward-optional', 121 | overrides: [ 122 | { 123 | only: [endsInBabelTsx], 124 | presets: [ 125 | '@babel/preset-env', 126 | '@babel/preset-react', 127 | [ 128 | '@babel/preset-typescript', 129 | { 130 | isTSX: true, 131 | allExtensions: true, 132 | }, 133 | ], 134 | ], 135 | }, 136 | ], 137 | }; 138 | 139 | hook(Object.assign({}, config, { extensions: '.tsx' })); 140 | }, 141 | }, 142 | '.cjs': cjsStub, 143 | '.coffee': 'coffeescript/register', 144 | '.coffee.md': 'coffeescript/register', 145 | '.esbuild.js': { 146 | module: 'esbuild-register/dist/node', 147 | register: function (mod, config) { 148 | config = config || { 149 | target: 'node' + process.version.slice(1), 150 | hookMatcher: endsInEsbuildJs, 151 | }; 152 | 153 | mod.register(Object.assign({}, config, { extensions: ['.js'] })); 154 | }, 155 | }, 156 | '.esbuild.jsx': { 157 | module: 'esbuild-register/dist/node', 158 | register: function (mod, config) { 159 | config = config || { 160 | target: 'node' + process.version.slice(1), 161 | hookMatcher: endsInEsbuildJsx, 162 | }; 163 | 164 | mod.register(Object.assign({}, config, { extensions: ['.jsx'] })); 165 | }, 166 | }, 167 | '.esbuild.ts': { 168 | module: 'esbuild-register/dist/node', 169 | register: function (mod, config) { 170 | config = config || { 171 | target: 'node' + process.version.slice(1), 172 | hookMatcher: endsInEsbuildTs, 173 | }; 174 | 175 | mod.register(Object.assign({}, config, { extensions: ['.ts'] })); 176 | }, 177 | }, 178 | '.esbuild.tsx': { 179 | module: 'esbuild-register/dist/node', 180 | register: function (mod, config) { 181 | config = config || { 182 | target: 'node' + process.version.slice(1), 183 | hookMatcher: endsInEsbuildTsx, 184 | }; 185 | 186 | mod.register(Object.assign({}, config, { extensions: ['.tsx'] })); 187 | }, 188 | }, 189 | '.esm.js': { 190 | module: 'esm', 191 | register: function (hook) { 192 | // register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353 193 | // which only captures the final extension (.esm.js -> .js) 194 | var esmLoader = hook(module); 195 | require.extensions['.js'] = esmLoader('module')._extensions['.js']; 196 | }, 197 | }, 198 | '.js': null, 199 | '.json': null, 200 | '.json5': 'json5/lib/register', 201 | '.jsx': [ 202 | { 203 | module: '@babel/register', 204 | register: function (hook, config) { 205 | config = config || { 206 | rootMode: 'upward-optional', 207 | overrides: [ 208 | { 209 | only: [endsInJsx], 210 | presets: ['@babel/preset-env', '@babel/preset-react'], 211 | }, 212 | ], 213 | }; 214 | 215 | hook(Object.assign({}, config, { extensions: '.jsx' })); 216 | }, 217 | }, 218 | 'sucrase/register/jsx', 219 | ], 220 | '.litcoffee': 'coffeescript/register', 221 | // The mdx loader hooks both `.md` and `.mdx` when it is imported 222 | // but we only install the hook if `.mdx` is the first file 223 | '.mdx': '@mdx-js/register', 224 | '.mjs': mjsStub, 225 | '.node': null, 226 | '.sucrase.js': { 227 | module: 'sucrase/dist/register', 228 | register: function (hook, config) { 229 | config = config || { 230 | matcher: endsInSucraseJs, 231 | }; 232 | 233 | hook.registerJS(config); 234 | }, 235 | }, 236 | '.sucrase.jsx': { 237 | module: 'sucrase/dist/register', 238 | register: function (hook, config) { 239 | config = config || { 240 | matcher: endsInSucraseJsx, 241 | }; 242 | 243 | hook.registerJSX(config); 244 | }, 245 | }, 246 | '.sucrase.ts': { 247 | module: 'sucrase/dist/register', 248 | register: function (hook, config) { 249 | config = config || { 250 | matcher: endsInSucraseTs, 251 | }; 252 | 253 | hook.registerTS(config); 254 | }, 255 | }, 256 | '.sucrase.tsx': { 257 | module: 'sucrase/dist/register', 258 | register: function (hook, config) { 259 | config = config || { 260 | matcher: endsInSucraseTsx, 261 | }; 262 | 263 | hook.registerTSX(config); 264 | }, 265 | }, 266 | '.swc.js': { 267 | module: '@swc/register', 268 | register: function (hook, config) { 269 | config = config || { 270 | only: [endsInSwcJs], 271 | ignore: [isNodeModules], 272 | jsc: { 273 | parser: { 274 | syntax: 'ecmascript', 275 | }, 276 | }, 277 | module: { 278 | type: 'commonjs', 279 | }, 280 | }; 281 | 282 | hook( 283 | Object.assign({}, config, { 284 | extensions: '.js', 285 | }) 286 | ); 287 | }, 288 | }, 289 | '.swc.jsx': { 290 | module: '@swc/register', 291 | register: function (hook, config) { 292 | config = config || { 293 | only: [endsInSwcJsx], 294 | ignore: [isNodeModules], 295 | jsc: { 296 | parser: { 297 | syntax: 'ecmascript', 298 | jsx: true, 299 | }, 300 | }, 301 | module: { 302 | type: 'commonjs', 303 | }, 304 | }; 305 | 306 | hook( 307 | Object.assign({}, config, { 308 | extensions: '.jsx', 309 | }) 310 | ); 311 | }, 312 | }, 313 | '.swc.ts': { 314 | module: '@swc/register', 315 | register: function (hook, config) { 316 | config = config || { 317 | only: [endsInSwcTs], 318 | ignore: [isNodeModules], 319 | jsc: { 320 | parser: { 321 | syntax: 'typescript', 322 | }, 323 | }, 324 | module: { 325 | type: 'commonjs', 326 | }, 327 | }; 328 | 329 | hook( 330 | Object.assign({}, config, { 331 | extensions: '.ts', 332 | }) 333 | ); 334 | }, 335 | }, 336 | '.swc.tsx': { 337 | module: '@swc/register', 338 | register: function (hook, config) { 339 | config = config || { 340 | only: [endsInSwcTsx], 341 | ignore: [isNodeModules], 342 | jsc: { 343 | parser: { 344 | syntax: 'typescript', 345 | tsx: true, 346 | }, 347 | }, 348 | module: { 349 | type: 'commonjs', 350 | }, 351 | }; 352 | 353 | hook( 354 | Object.assign({}, config, { 355 | extensions: '.tsx', 356 | }) 357 | ); 358 | }, 359 | }, 360 | '.toml': { 361 | module: 'toml-require', 362 | register: function (hook, config) { 363 | hook.install(config); 364 | }, 365 | }, 366 | '.ts': [ 367 | 'ts-node/register', 368 | 'sucrase/register/ts', 369 | { 370 | module: '@babel/register', 371 | register: function (hook, config) { 372 | config = config || { 373 | rootMode: 'upward-optional', 374 | overrides: [ 375 | { 376 | only: [endsInTs], 377 | presets: ['@babel/preset-env', '@babel/preset-typescript'], 378 | }, 379 | ], 380 | }; 381 | 382 | hook( 383 | Object.assign({}, config, { 384 | extensions: '.ts', 385 | }) 386 | ); 387 | }, 388 | }, 389 | { 390 | module: 'esbuild-register/dist/node', 391 | register: function (mod, config) { 392 | config = config || { 393 | target: 'node' + process.version.slice(1), 394 | hookMatcher: endsInTs, 395 | }; 396 | 397 | mod.register( 398 | Object.assign({}, config, { 399 | extensions: ['.ts'], 400 | }) 401 | ); 402 | }, 403 | }, 404 | { 405 | module: '@swc/register', 406 | register: function (hook, config) { 407 | config = config || { 408 | only: [endsInTs], 409 | ignore: [isNodeModules], 410 | jsc: { 411 | parser: { 412 | syntax: 'typescript', 413 | }, 414 | }, 415 | module: { 416 | type: 'commonjs', 417 | }, 418 | }; 419 | 420 | hook( 421 | Object.assign({}, config, { 422 | extensions: '.ts', 423 | }) 424 | ); 425 | }, 426 | }, 427 | ], 428 | '.cts': ['ts-node/register'], 429 | '.tsx': [ 430 | 'ts-node/register', 431 | 'sucrase/register/tsx', 432 | { 433 | module: '@babel/register', 434 | register: function (hook, config) { 435 | config = config || { 436 | rootMode: 'upward-optional', 437 | overrides: [ 438 | { 439 | only: [endsInTsx], 440 | presets: [ 441 | '@babel/preset-env', 442 | '@babel/preset-react', 443 | [ 444 | '@babel/preset-typescript', 445 | { 446 | isTSX: true, 447 | allExtensions: true, 448 | }, 449 | ], 450 | ], 451 | }, 452 | ], 453 | }; 454 | 455 | hook( 456 | Object.assign({}, config, { 457 | extensions: '.tsx', 458 | }) 459 | ); 460 | }, 461 | }, 462 | { 463 | module: 'esbuild-register/dist/node', 464 | register: function (mod, config) { 465 | config = config || { 466 | target: 'node' + process.version.slice(1), 467 | hookMatcher: endsInTsx, 468 | }; 469 | 470 | mod.register( 471 | Object.assign({}, config, { 472 | extensions: ['.tsx'], 473 | }) 474 | ); 475 | }, 476 | }, 477 | { 478 | module: '@swc/register', 479 | register: function (hook, config) { 480 | config = config || { 481 | only: [endsInTsx], 482 | ignore: [isNodeModules], 483 | jsc: { 484 | parser: { 485 | syntax: 'typescript', 486 | tsx: true, 487 | }, 488 | }, 489 | module: { 490 | type: 'commonjs', 491 | }, 492 | }; 493 | 494 | hook( 495 | Object.assign({}, config, { 496 | extensions: '.tsx', 497 | }) 498 | ); 499 | }, 500 | }, 501 | ], 502 | '.yaml': 'yaml-hook/register', 503 | '.yml': 'yaml-hook/register', 504 | }; 505 | 506 | var jsVariantExtensions = [ 507 | '.js', 508 | '.babel.js', 509 | '.babel.jsx', 510 | '.babel.ts', 511 | '.babel.tsx', 512 | '.esbuild.js', 513 | '.esbuild.jsx', 514 | '.esbuild.ts', 515 | '.esbuild.tsx', 516 | '.cjs', 517 | '.coffee', 518 | '.coffee.md', 519 | '.esm.js', 520 | '.jsx', 521 | '.litcoffee', 522 | '.mdx', 523 | '.mjs', 524 | '.sucrase.js', 525 | '.sucrase.jsx', 526 | '.sucrase.ts', 527 | '.sucrase.tsx', 528 | '.swc.js', 529 | '.swc.jsx', 530 | '.swc.ts', 531 | '.swc.tsx', 532 | '.ts', 533 | '.tsx', 534 | ]; 535 | 536 | module.exports = { 537 | extensions: extensions, 538 | jsVariants: jsVariantExtensions.reduce(function (result, ext) { 539 | result[ext] = extensions[ext]; 540 | return result; 541 | }, {}), 542 | }; 543 | -------------------------------------------------------------------------------- /mjs-stub.js: -------------------------------------------------------------------------------- 1 | require.extensions['.mjs'] = null; 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "interpret", 3 | "version": "3.1.1", 4 | "description": "A dictionary of file extensions and associated module loaders.", 5 | "author": "Gulp Team (https://gulpjs.com/)", 6 | "contributors": [ 7 | "Blaine Bublitz ", 8 | "Tyler Kellen (http://goingslowly.com/)" 9 | ], 10 | "repository": "gulpjs/interpret", 11 | "license": "MIT", 12 | "engines": { 13 | "node": ">=10.13.0" 14 | }, 15 | "main": "index.js", 16 | "files": [ 17 | "LICENSE", 18 | "index.js", 19 | "cjs-stub.js", 20 | "mjs-stub.js" 21 | ], 22 | "scripts": { 23 | "readme": "remark README.md --use ./scripts/plugin.mjs --output", 24 | "lint": "eslint .", 25 | "pretest": "npm run lint", 26 | "test": "nyc mocha --async-only" 27 | }, 28 | "devDependencies": { 29 | "eslint": "^7.0.0", 30 | "eslint-config-gulp": "^5.0.0", 31 | "eslint-plugin-node": "^11.1.0", 32 | "expect": "^27.0.0", 33 | "js-yaml": "^4.1.0", 34 | "mocha": "^8.0.0", 35 | "nyc": "^15.0.0", 36 | "parse-node-version": "^2.0.0", 37 | "rechoir": "^0.8.0", 38 | "remark-cli": "^10.0.1", 39 | "remark-code-import": "^1.1.0", 40 | "shelljs": "0.8.5" 41 | }, 42 | "nyc": { 43 | "extension": [ 44 | ".js" 45 | ], 46 | "reporter": [ 47 | "lcov", 48 | "text-summary" 49 | ] 50 | }, 51 | "prettier": { 52 | "singleQuote": true 53 | }, 54 | "keywords": [ 55 | "coffee", 56 | "coffee.md", 57 | "coffeescript", 58 | "es", 59 | "es6", 60 | "js", 61 | "json", 62 | "json5", 63 | "jsx", 64 | "react", 65 | "litcoffee", 66 | "toml", 67 | "ts", 68 | "typescript", 69 | "xml", 70 | "yaml", 71 | "yml" 72 | ] 73 | } 74 | -------------------------------------------------------------------------------- /scripts/plugin.mjs: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import path from 'path'; 3 | import yaml from 'js-yaml'; 4 | import { codeImport } from 'remark-code-import'; 5 | 6 | import interpret from '../index.js'; 7 | 8 | // Exporting this so it can be used by remark-cli 9 | export default codeImport; 10 | 11 | // Also preparing the files we need for the README 12 | var config = { 13 | sortKeys: true, 14 | skipInvalid: true, 15 | replacer: function (key, value) { 16 | // Top-level object 17 | if (key === '') { 18 | return value; 19 | } 20 | 21 | if (key.startsWith('.')) { 22 | return Array.isArray(value) ? value : [value]; 23 | } 24 | 25 | if (value === null) { 26 | return 'built-in node.js loader'; 27 | } 28 | 29 | if (typeof value !== 'string') { 30 | return value.module; 31 | } 32 | 33 | if (value.includes('mjs-stub')) { 34 | return 'interpret/mjs-stub'; 35 | } 36 | 37 | if (value.includes('cjs-stub')) { 38 | return 'interpret/cjs-stub'; 39 | } 40 | 41 | return value; 42 | }, 43 | }; 44 | 45 | var extensions = yaml.dump(interpret.extensions, config); 46 | var jsVariants = yaml.dump(interpret.jsVariants, config); 47 | 48 | fs.writeFileSync( 49 | new URL('./extensions.yaml', import.meta.url), 50 | extensions, 51 | 'utf8' 52 | ); 53 | fs.writeFileSync( 54 | new URL('./jsVariants.yaml', import.meta.url), 55 | jsVariants, 56 | 'utf8' 57 | ); 58 | -------------------------------------------------------------------------------- /test/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gulpjs/interpret/c09bf70bc73d020b9d387223e7b74708687fdb47/test/.gitkeep -------------------------------------------------------------------------------- /test/fixtures/babel.js/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@babel/core": "^7.2.2", 4 | "@babel/preset-env": "^7.0.0", 5 | "@babel/register": "^7.0.0" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/babel.js/0/test.babel.js: -------------------------------------------------------------------------------- 1 | // Test ES6 arrow functions 2 | var fn = () => { 3 | var trueKey = true; 4 | var falseKey = false; 5 | var subKey = { subProp: 1 }; 6 | // Test harmony object short notation 7 | return { data: { trueKey, falseKey, subKey } }; 8 | }; 9 | 10 | module.exports = fn(); 11 | -------------------------------------------------------------------------------- /test/fixtures/babel.jsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@babel/core": "^7.2.2", 4 | "@babel/preset-env": "^7.0.0", 5 | "@babel/preset-react": "^7.0.0", 6 | "@babel/register": "^7.0.0" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/babel.jsx/0/test.babel.jsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement: function (Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | // Test harmony arrow functions 8 | const Component = () => { 9 | var trueKey = true; 10 | var falseKey = false; 11 | var subKey = { subProp: 1 }; 12 | // Test harmony object short notation 13 | return { data: { trueKey, falseKey, subKey } }; 14 | }; 15 | 16 | // Test JSX syntax 17 | module.exports = ; 18 | -------------------------------------------------------------------------------- /test/fixtures/babel.ts/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@babel/core": "^7.2.2", 4 | "@babel/preset-env": "^7.0.0", 5 | "@babel/preset-typescript": "^7.1.0", 6 | "@babel/register": "^7.0.0" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/babel.ts/0/test.babel.ts: -------------------------------------------------------------------------------- 1 | // Test ES6 arrow functions 2 | var fn = () => { 3 | var trueKey = true; 4 | var falseKey = false; 5 | var subKey = { subProp: 1 }; 6 | // Test harmony object short notation 7 | return { data: { trueKey, falseKey, subKey } }; 8 | }; 9 | 10 | module.exports = fn(); 11 | -------------------------------------------------------------------------------- /test/fixtures/babel.tsx/0/data.babel.tsx: -------------------------------------------------------------------------------- 1 | var test: { 2 | data: { 3 | trueKey: boolean; 4 | falseKey: boolean; 5 | subKey: { 6 | subProp: number; 7 | }; 8 | }; 9 | } = { 10 | data: { 11 | trueKey: true, 12 | falseKey: false, 13 | subKey: { 14 | subProp: 1, 15 | }, 16 | }, 17 | }; 18 | 19 | export default test; 20 | -------------------------------------------------------------------------------- /test/fixtures/babel.tsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@babel/core": "^7.2.2", 4 | "@babel/preset-env": "^7.2.3", 5 | "@babel/preset-react": "^7.0.0", 6 | "@babel/preset-typescript": "^7.1.0", 7 | "@babel/register": "^7.0.0" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/babel.tsx/0/test.babel.tsx: -------------------------------------------------------------------------------- 1 | import data from './data.babel'; 2 | 3 | const React = { 4 | createElement(Component: () => any) { 5 | return Component(); 6 | }, 7 | }; 8 | 9 | // Test harmony arrow functions. 10 | const Component = () => { 11 | var trueKey: boolean = true; 12 | var falseKey: boolean = false; 13 | var subKey = { subProp: 1 }; 14 | 15 | // Test harmony object short notation. 16 | return { data: { trueKey, falseKey, subKey } }; 17 | }; 18 | 19 | // Test TSX syntax. 20 | export default ; 21 | -------------------------------------------------------------------------------- /test/fixtures/cjs/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "module" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/cjs/0/rechoir.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert'; 2 | import path from 'path'; 3 | import url from 'url'; 4 | 5 | import rechoir from 'rechoir'; 6 | import { extensions } from '../../../../index.js'; 7 | 8 | var fixture = path.resolve('test.cjs'); 9 | 10 | rechoir.prepare(extensions, fixture); 11 | 12 | const { default: result } = await import(url.pathToFileURL(fixture).href); 13 | 14 | const expected = { 15 | data: { 16 | trueKey: true, 17 | falseKey: false, 18 | subKey: { 19 | subProp: 1, 20 | }, 21 | }, 22 | }; 23 | 24 | assert.deepEqual(result, expected); 25 | -------------------------------------------------------------------------------- /test/fixtures/cjs/0/test.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | }; 10 | -------------------------------------------------------------------------------- /test/fixtures/cjs/1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "commonjs" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/cjs/1/rechoir.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var path = require('path'); 3 | 4 | var rechoir = require('rechoir'); 5 | var { extensions } = require('../../../../index.js'); 6 | 7 | var fixture = path.resolve('test.cjs'); 8 | 9 | rechoir.prepare(extensions, fixture); 10 | 11 | const result = require(fixture); 12 | 13 | const expected = { 14 | data: { 15 | trueKey: true, 16 | falseKey: false, 17 | subKey: { 18 | subProp: 1, 19 | }, 20 | }, 21 | }; 22 | 23 | assert.deepEqual(result, expected); 24 | -------------------------------------------------------------------------------- /test/fixtures/cjs/1/test.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | }; 10 | -------------------------------------------------------------------------------- /test/fixtures/coffee.md/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "coffeescript": "^2.3.2" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/coffee.md/0/test.coffee.md: -------------------------------------------------------------------------------- 1 | # Test Fixture 2 | 3 | module.exports = 4 | data: 5 | trueKey: true 6 | falseKey: false 7 | subKey: 8 | subProp: 1 9 | -------------------------------------------------------------------------------- /test/fixtures/coffee/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "coffeescript": "^2.3.2" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/coffee/0/test.coffee: -------------------------------------------------------------------------------- 1 | module.exports = 2 | data: 3 | trueKey: true 4 | falseKey: false 5 | subKey: 6 | subProp: 1 7 | -------------------------------------------------------------------------------- /test/fixtures/cts/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "ts-node": "^10.8.0", 4 | "typescript": "^4.7.4" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/cts/0/test.cts: -------------------------------------------------------------------------------- 1 | var test: { 2 | data: { 3 | trueKey: boolean; 4 | falseKey: boolean; 5 | subKey: { 6 | subProp: number; 7 | }; 8 | }; 9 | } = { 10 | data: { 11 | trueKey: true, 12 | falseKey: false, 13 | subKey: { 14 | subProp: 1, 15 | }, 16 | }, 17 | }; 18 | 19 | export default test; 20 | -------------------------------------------------------------------------------- /test/fixtures/cts/0/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "declaration": false, 6 | "noImplicitAny": false, 7 | "removeComments": true, 8 | "sourceMap": true, 9 | "outDir": ".tmp" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.js/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "esbuild": "^0.14.29", 4 | "esbuild-register": "^3.3.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.js/0/test.esbuild.js: -------------------------------------------------------------------------------- 1 | class Foo { 2 | #x = 1; 3 | #y = 2; 4 | } 5 | 6 | export default { 7 | data: { 8 | trueKey: true, 9 | falseKey: false, 10 | subKey: { 11 | subProp: 1, 12 | }, 13 | }, 14 | }; 15 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.jsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "esbuild": "^0.14.29", 4 | "esbuild-register": "^3.3.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.jsx/0/test.esbuild.jsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | class Foo { 8 | #x = 1; 9 | #y = 2; 10 | } 11 | 12 | function Bar() { 13 | const foo = new Foo(); 14 | 15 | return foo; 16 | } 17 | 18 | const a = ; 19 | 20 | export default { 21 | data: { 22 | trueKey: true, 23 | falseKey: false, 24 | subKey: { 25 | subProp: 1, 26 | }, 27 | }, 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.ts/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "esbuild": "^0.14.29", 4 | "esbuild-register": "^3.3.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.ts/0/test.esbuild.ts: -------------------------------------------------------------------------------- 1 | class Foo { 2 | #x: number = 1; 3 | #y: number = 2; 4 | } 5 | 6 | export default { 7 | data: { 8 | trueKey: true as boolean, 9 | falseKey: false as boolean, 10 | subKey: { 11 | subProp: 1, 12 | }, 13 | }, 14 | } as const; 15 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.tsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "esbuild": "^0.14.29", 4 | "esbuild-register": "^3.3.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/esbuild.tsx/0/test.esbuild.tsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | class Foo { 8 | #x: number = 1; 9 | #y: number = 2; 10 | } 11 | 12 | function Bar() { 13 | const foo = new Foo(); 14 | 15 | return foo; 16 | } 17 | 18 | const a = ; 19 | 20 | export default { 21 | data: { 22 | trueKey: true as boolean, 23 | falseKey: false as boolean, 24 | subKey: { 25 | subProp: 1, 26 | }, 27 | }, 28 | } as const; 29 | -------------------------------------------------------------------------------- /test/fixtures/esm.js/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "esm": "^3.0.84" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/esm.js/0/test.esm.js: -------------------------------------------------------------------------------- 1 | var test = { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | }; 10 | 11 | export default test; 12 | -------------------------------------------------------------------------------- /test/fixtures/js/0/package.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /test/fixtures/js/0/test.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | }; 10 | -------------------------------------------------------------------------------- /test/fixtures/json/0/package.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /test/fixtures/json/0/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "data": { 3 | "trueKey": true, 4 | "falseKey": false, 5 | "subKey": { 6 | "subProp": 1 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/json5/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "json5": "^2.1.0" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/json5/0/test.json5: -------------------------------------------------------------------------------- 1 | { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | /* omg, a comment in json?? */ 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/jsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@babel/core": "^7.2.2", 4 | "@babel/preset-env": "^7.0.0", 5 | "@babel/preset-react": "^7.0.0", 6 | "@babel/register": "^7.0.0" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/jsx/0/test.jsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement: function (Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | // Test harmony arrow functions 8 | const Component = () => { 9 | var trueKey = true; 10 | var falseKey = false; 11 | var subKey = { subProp: 1 }; 12 | // Test harmony object short notation 13 | return { data: { trueKey, falseKey, subKey } }; 14 | }; 15 | 16 | // Test JSX syntax 17 | module.exports = ; 18 | -------------------------------------------------------------------------------- /test/fixtures/jsx/1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "sucrase": "^3.12.1" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/jsx/1/test.jsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement: function (Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | // Test harmony arrow functions 8 | const Component = () => { 9 | var trueKey = true; 10 | var falseKey = false; 11 | var subKey = { subProp: 1 }; 12 | // Test harmony object short notation 13 | return { data: { trueKey, falseKey, subKey } }; 14 | }; 15 | 16 | // Test JSX syntax 17 | module.exports = ; 18 | -------------------------------------------------------------------------------- /test/fixtures/litcoffee/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "coffeescript": "^2.3.2" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/litcoffee/0/test.litcoffee: -------------------------------------------------------------------------------- 1 | # comment 2 | 3 | module.exports = 4 | data: 5 | trueKey: true 6 | falseKey: false 7 | subKey: 8 | subProp: 1 9 | -------------------------------------------------------------------------------- /test/fixtures/mdx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@mdx-js/register": "^2.1.1", 4 | "react": "^18.0.0" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/mdx/0/test.mdx: -------------------------------------------------------------------------------- 1 | export const Thing = () => { 2 | var trueKey = true; 3 | var falseKey = false; 4 | var subKey = { subProp: 1 }; 5 | // Test harmony object short notation 6 | return { data: { trueKey, falseKey, subKey } }; 7 | }; 8 | 9 | 10 | -------------------------------------------------------------------------------- /test/fixtures/mjs/0/package.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /test/fixtures/mjs/0/test.mjs: -------------------------------------------------------------------------------- 1 | export default { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | }; 10 | -------------------------------------------------------------------------------- /test/fixtures/mjs/1/package.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /test/fixtures/mjs/1/test.mjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | }; 10 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.js/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "sucrase": "^3.12.1" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.js/0/test.sucrase.js: -------------------------------------------------------------------------------- 1 | const foo = {}; 2 | 3 | const bar = foo?.bar?.baz; 4 | 5 | export default { 6 | data: { 7 | trueKey: true, 8 | falseKey: false, 9 | subKey: { 10 | subProp: 1, 11 | }, 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.jsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "sucrase": "^3.12.1" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.jsx/0/test.sucrase.jsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | function Bar() { 8 | const foo = {}; 9 | 10 | const bar = foo?.bar?.baz; 11 | 12 | return bar; 13 | } 14 | 15 | const a = ; 16 | 17 | export default { 18 | data: { 19 | trueKey: true, 20 | falseKey: false, 21 | subKey: { 22 | subProp: 1, 23 | }, 24 | }, 25 | }; 26 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.ts/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "sucrase": "^3.12.1" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.ts/0/test.sucrase.ts: -------------------------------------------------------------------------------- 1 | const foo: { 2 | bar?: { 3 | baz?: boolean; 4 | }; 5 | } = {}; 6 | 7 | const bar = foo?.bar?.baz; 8 | 9 | export default { 10 | data: { 11 | trueKey: true as boolean, 12 | falseKey: false as boolean, 13 | subKey: { 14 | subProp: 1, 15 | }, 16 | }, 17 | } as const; 18 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.tsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "sucrase": "^3.12.1" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/sucrase.tsx/0/test.sucrase.tsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | function Bar() { 8 | const foo: { 9 | bar?: { 10 | baz?: boolean; 11 | }; 12 | } = {}; 13 | 14 | const bar = foo?.bar?.baz; 15 | 16 | return bar; 17 | } 18 | 19 | const a = ; 20 | 21 | export default { 22 | data: { 23 | trueKey: true as boolean, 24 | falseKey: false as boolean, 25 | subKey: { 26 | subProp: 1, 27 | }, 28 | }, 29 | } as const; 30 | -------------------------------------------------------------------------------- /test/fixtures/swc.js/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@swc/core": "^1.2.110", 4 | "@swc/register": "^0.1.7" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/swc.js/0/test.swc.js: -------------------------------------------------------------------------------- 1 | class Foo { 2 | #x = 1; 3 | #y = 2; 4 | } 5 | 6 | export default { 7 | data: { 8 | trueKey: true, 9 | falseKey: false, 10 | subKey: { 11 | subProp: 1, 12 | }, 13 | }, 14 | }; 15 | -------------------------------------------------------------------------------- /test/fixtures/swc.jsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@swc/core": "^1.2.110", 4 | "@swc/register": "^0.1.7" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/swc.jsx/0/test.swc.jsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | class Foo { 8 | #x = 1; 9 | #y = 2; 10 | } 11 | 12 | function Bar() { 13 | const foo = new Foo(); 14 | 15 | return foo; 16 | } 17 | 18 | const a = ; 19 | 20 | export default { 21 | data: { 22 | trueKey: true, 23 | falseKey: false, 24 | subKey: { 25 | subProp: 1, 26 | }, 27 | }, 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/swc.ts/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@swc/core": "^1.2.110", 4 | "@swc/register": "^0.1.7" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/swc.ts/0/test.swc.ts: -------------------------------------------------------------------------------- 1 | class Foo { 2 | #x: number = 1; 3 | #y: number = 2; 4 | } 5 | 6 | export default { 7 | data: { 8 | trueKey: true as boolean, 9 | falseKey: false as boolean, 10 | subKey: { 11 | subProp: 1, 12 | }, 13 | }, 14 | } as const; 15 | -------------------------------------------------------------------------------- /test/fixtures/swc.tsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@swc/core": "^1.2.110", 4 | "@swc/register": "^0.1.7" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/swc.tsx/0/test.swc.tsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component: () => any) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | // Test harmony arrow functions. 8 | const Component = () => { 9 | var trueKey: boolean = true; 10 | var falseKey: boolean = false; 11 | var subKey = { subProp: 1 }; 12 | 13 | // Test harmony object short notation. 14 | return { data: { trueKey, falseKey, subKey } }; 15 | }; 16 | 17 | // Test TSX syntax. 18 | export default ; 19 | -------------------------------------------------------------------------------- /test/fixtures/toml/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "toml-require": "^1.2.0" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/toml/0/test.toml: -------------------------------------------------------------------------------- 1 | [data] 2 | trueKey = true 3 | falseKey = false 4 | 5 | [data.subKey] 6 | subProp = 1 7 | -------------------------------------------------------------------------------- /test/fixtures/ts/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "ts-node": "^10.8.0", 4 | "typescript": "^4.7.4" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/ts/0/test.ts: -------------------------------------------------------------------------------- 1 | var test: { 2 | data: { 3 | trueKey: boolean; 4 | falseKey: boolean; 5 | subKey: { 6 | subProp: number; 7 | }; 8 | }; 9 | } = { 10 | data: { 11 | trueKey: true, 12 | falseKey: false, 13 | subKey: { 14 | subProp: 1, 15 | }, 16 | }, 17 | }; 18 | 19 | export default test; 20 | -------------------------------------------------------------------------------- /test/fixtures/ts/0/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "declaration": false, 6 | "noImplicitAny": false, 7 | "removeComments": true, 8 | "sourceMap": true, 9 | "outDir": ".tmp" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/ts/1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "sucrase": "^3.12.1" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/ts/1/test.ts: -------------------------------------------------------------------------------- 1 | var test: { 2 | data: { 3 | trueKey: boolean; 4 | falseKey: boolean; 5 | subKey: { 6 | subProp: number; 7 | }; 8 | }; 9 | } = { 10 | data: { 11 | trueKey: true, 12 | falseKey: false, 13 | subKey: { 14 | subProp: 1, 15 | }, 16 | }, 17 | }; 18 | 19 | var main: { 20 | default: typeof test; 21 | } = { 22 | default: test, 23 | }; 24 | 25 | export = main; 26 | -------------------------------------------------------------------------------- /test/fixtures/ts/1/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "declaration": false, 6 | "noImplicitAny": false, 7 | "removeComments": true, 8 | "sourceMap": true, 9 | "outDir": ".tmp" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/ts/2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@babel/core": "^7.2.2", 4 | "@babel/preset-env": "^7.2.3", 5 | "@babel/preset-typescript": "^7.1.0", 6 | "@babel/register": "^7.0.0" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/ts/2/test.ts: -------------------------------------------------------------------------------- 1 | var test: { 2 | data: { 3 | trueKey: boolean; 4 | falseKey: boolean; 5 | subKey: { 6 | subProp: number; 7 | }; 8 | }; 9 | } = { 10 | data: { 11 | trueKey: true, 12 | falseKey: false, 13 | subKey: { 14 | subProp: 1, 15 | }, 16 | }, 17 | }; 18 | 19 | export default test; 20 | -------------------------------------------------------------------------------- /test/fixtures/ts/3/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "esbuild": "^0.14.29", 4 | "esbuild-register": "^3.3.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/ts/3/test.ts: -------------------------------------------------------------------------------- 1 | function add(x: number, y: number): number { 2 | return x + y; 3 | } 4 | 5 | export default { 6 | data: { 7 | trueKey: true as boolean, 8 | falseKey: false as boolean, 9 | subKey: { 10 | subProp: add(0.5, 0.5), 11 | }, 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /test/fixtures/ts/4/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@swc/core": "^1.2.110", 4 | "@swc/register": "^0.1.7" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/ts/4/test.ts: -------------------------------------------------------------------------------- 1 | var test = { 2 | data: { 3 | trueKey: true, 4 | falseKey: false, 5 | subKey: { 6 | subProp: 1, 7 | }, 8 | }, 9 | }; 10 | 11 | var main = { 12 | default: test, 13 | }; 14 | 15 | export = main; 16 | -------------------------------------------------------------------------------- /test/fixtures/tsx/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "ts-node": "^10.8.0", 4 | "typescript": "4.7.4" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/tsx/0/test.tsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component: () => any) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | // Test harmony arrow functions. 8 | const Component = () => { 9 | var trueKey: boolean = true; 10 | var falseKey: boolean = false; 11 | var subKey = { subProp: 1 }; 12 | 13 | // Test harmony object short notation. 14 | return { data: { trueKey, falseKey, subKey } }; 15 | }; 16 | 17 | // Test TSX syntax. 18 | export default ; 19 | -------------------------------------------------------------------------------- /test/fixtures/tsx/0/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "declaration": false, 6 | "noImplicitAny": false, 7 | "removeComments": true, 8 | "sourceMap": true, 9 | "outDir": ".tmp", 10 | "jsx": "react" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/tsx/1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "sucrase": "^3.12.1" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/tsx/1/test.tsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component: () => any) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | // Test harmony arrow functions. 8 | const Component = () => { 9 | var trueKey: boolean = true; 10 | var falseKey: boolean = false; 11 | var subKey = { subProp: 1 }; 12 | 13 | // Test harmony object short notation. 14 | return { data: { trueKey, falseKey, subKey } }; 15 | }; 16 | 17 | // Test TSX syntax. 18 | export default ; 19 | -------------------------------------------------------------------------------- /test/fixtures/tsx/1/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "declaration": false, 6 | "noImplicitAny": false, 7 | "removeComments": true, 8 | "sourceMap": true, 9 | "outDir": ".tmp", 10 | "jsx": "react" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/tsx/2/data.tsx: -------------------------------------------------------------------------------- 1 | var test: { 2 | data: { 3 | trueKey: boolean; 4 | falseKey: boolean; 5 | subKey: { 6 | subProp: number; 7 | }; 8 | }; 9 | } = { 10 | data: { 11 | trueKey: true, 12 | falseKey: false, 13 | subKey: { 14 | subProp: 1, 15 | }, 16 | }, 17 | }; 18 | 19 | export default test; 20 | -------------------------------------------------------------------------------- /test/fixtures/tsx/2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@babel/core": "^7.2.2", 4 | "@babel/preset-env": "^7.2.3", 5 | "@babel/preset-react": "^7.0.0", 6 | "@babel/preset-typescript": "^7.1.0", 7 | "@babel/register": "^7.0.0" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/tsx/2/test.tsx: -------------------------------------------------------------------------------- 1 | import data from './data'; 2 | 3 | const React = { 4 | createElement(Component: () => any) { 5 | return Component(); 6 | }, 7 | }; 8 | 9 | // Test harmony arrow functions. 10 | const Component = () => { 11 | var trueKey: boolean = true; 12 | var falseKey: boolean = false; 13 | var subKey = { subProp: 1 }; 14 | 15 | // Test harmony object short notation. 16 | return { data: { trueKey, falseKey, subKey } }; 17 | }; 18 | 19 | // Test TSX syntax. 20 | export default ; 21 | -------------------------------------------------------------------------------- /test/fixtures/tsx/3/data.tsx: -------------------------------------------------------------------------------- 1 | export const data: { 2 | trueKey: boolean; 3 | falseKey: boolean; 4 | subKey: { 5 | subProp: number; 6 | }; 7 | } = { 8 | trueKey: true, 9 | falseKey: false, 10 | subKey: { 11 | subProp: 1, 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /test/fixtures/tsx/3/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "esbuild": "^0.14.29", 4 | "esbuild-register": "^3.3.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/tsx/3/test.tsx: -------------------------------------------------------------------------------- 1 | /** @jsx jsx */ 2 | 3 | import { data } from './data'; 4 | 5 | const Component = (props: object) => ({ data: props }); 6 | 7 | function jsx(element: typeof Component, props: object) { 8 | return element(props); 9 | } 10 | 11 | export default ; 12 | -------------------------------------------------------------------------------- /test/fixtures/tsx/4/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@swc/core": "^1.2.110", 4 | "@swc/register": "^0.1.7" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/tsx/4/test.tsx: -------------------------------------------------------------------------------- 1 | const React = { 2 | createElement(Component: () => any) { 3 | return Component(); 4 | }, 5 | }; 6 | 7 | // Test harmony arrow functions. 8 | const Component = () => { 9 | var trueKey: boolean = true; 10 | var falseKey: boolean = false; 11 | var subKey = { subProp: 1 }; 12 | 13 | // Test harmony object short notation. 14 | return { data: { trueKey, falseKey, subKey } }; 15 | }; 16 | 17 | // Test TSX syntax. 18 | export default ; 19 | -------------------------------------------------------------------------------- /test/fixtures/yaml/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "yaml-hook": "1.0.0" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/yaml/0/test.yaml: -------------------------------------------------------------------------------- 1 | data: 2 | trueKey: true 3 | falseKey: false 4 | subKey: 5 | subProp: 1 6 | -------------------------------------------------------------------------------- /test/fixtures/yml/0/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "yaml-hook": "1.0.0" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/yml/0/test.yml: -------------------------------------------------------------------------------- 1 | data: 2 | trueKey: true 3 | falseKey: false 4 | subKey: 5 | subProp: 1 6 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var expect = require('expect'); 4 | 5 | var path = require('path'); 6 | var Module = require('module'); 7 | var child = require('child_process'); 8 | var shell = require('shelljs'); 9 | var rechoir = require('rechoir'); 10 | 11 | var nodeVersion = require('parse-node-version')(process.version); 12 | var extensions = require('../').extensions; 13 | 14 | // Save the original Module._extensions 15 | var originalExtensions = Object.keys(Module._extensions); 16 | var original = originalExtensions.reduce(function (result, key) { 17 | result[key] = require.extensions[key]; 18 | return result; 19 | }, {}); 20 | // Save the original cache keys 21 | var originalCacheKeys = Object.keys(require.cache); 22 | // Save the original Module.prototype.load because coffee-script overwrites it 23 | var originalModuleLoad = Module.prototype.load; 24 | 25 | function cleanup() { 26 | function cleanupCache(key) { 27 | if (originalCacheKeys.indexOf(key) === -1) { 28 | delete require.cache[key]; 29 | } 30 | } 31 | 32 | function cleanupExtensions(ext) { 33 | if (originalExtensions.indexOf(ext) === -1) { 34 | delete Module._extensions[ext]; 35 | } else { 36 | Module._extensions[ext] = original[ext]; 37 | } 38 | } 39 | 40 | // Restore the require.cache to startup state 41 | Object.keys(require.cache).forEach(cleanupCache); 42 | // Restore the original Module.prototype.load 43 | Module.prototype.load = originalModuleLoad; 44 | // Restore the original Module._extensions 45 | Object.keys(Module._extensions).forEach(cleanupExtensions); 46 | 47 | // Cleanup babel global 48 | delete global._babelPolyfill; 49 | } 50 | 51 | // These modules need newer node features 52 | var minVersions = { 53 | '@mdx-js/register': { major: 12 }, 54 | }; 55 | 56 | var maxVersions = {}; 57 | 58 | describe('interpret.extensions', function () { 59 | beforeEach(cleanup); 60 | 61 | var exts = Object.keys(extensions); 62 | 63 | var attempts = exts.reduce(function (attempts, ext) { 64 | var modules = extensions[ext]; 65 | if (!Array.isArray(modules)) { 66 | modules = [modules]; 67 | } 68 | 69 | // Skip .node because those are binaries 70 | // TODO: maybe we could add a binary to the fixtures? 71 | if (ext === '.node') { 72 | return attempts; 73 | } 74 | 75 | // We will handle the .mjs & .cjs tests separately 76 | if (ext === '.mjs' || ext === '.cjs') { 77 | return attempts; 78 | } 79 | 80 | modules.forEach(function (mod, idx) { 81 | if (mod && typeof mod !== 'string') { 82 | mod = mod.module; 83 | } 84 | 85 | var name = mod || 'no loader'; 86 | var fixture = './fixtures/' + ext.slice(1) + '/' + idx + '/test' + ext; 87 | 88 | if (mod && mod[0] !== '@') { 89 | mod = mod.split('/')[0]; 90 | } 91 | 92 | attempts.push({ 93 | extension: ext, 94 | module: mod, 95 | name: name, 96 | index: idx, 97 | fixture: fixture, 98 | }); 99 | }); 100 | 101 | return attempts; 102 | }, []); 103 | 104 | attempts.forEach(function (attempt) { 105 | var extension = attempt.extension; 106 | var module = attempt.module; 107 | var name = attempt.name; 108 | var fixture = attempt.fixture; 109 | var fixtureDir = path.dirname(fixture); 110 | var idx = attempt.index; 111 | 112 | it( 113 | 'can require ' + extension + ' using ' + name + ' (' + idx + ')', 114 | function (done) { 115 | var minVersion = minVersions[module]; 116 | 117 | if (minVersion) { 118 | if (nodeVersion.major === 0 && nodeVersion.minor < minVersion.minor) { 119 | this.skip(); 120 | } else if (nodeVersion.major < minVersion.major) { 121 | this.skip(); 122 | } 123 | } 124 | 125 | var maxVersion = maxVersions[module]; 126 | 127 | if (maxVersion) { 128 | if (nodeVersion.major > maxVersion.major) { 129 | this.skip(); 130 | } 131 | } 132 | 133 | // Skip any swc test on linux due to https://github.com/swc-project/swc/issues/4107 134 | if (name === '@swc/register' && process.platform === 'linux') { 135 | this.skip(); 136 | } 137 | 138 | this.timeout(0); 139 | 140 | var expected; 141 | 142 | process.chdir(path.join(__dirname, fixtureDir)); 143 | 144 | shell.exec('rm -r node_modules', { silent: true }); 145 | shell.exec('rm package-lock.json', { silent: true }); 146 | shell.exec('npm install', { silent: true }); 147 | 148 | try { 149 | rechoir.prepare(extensions, fixture); 150 | } catch (err) { 151 | console.error(err.failures); 152 | throw err; 153 | } 154 | 155 | switch (extension) { 156 | case '.ts': 157 | case '.cts': 158 | case '.tsx': 159 | case '.esm.js': 160 | case '.babel.tsx': 161 | case '.esbuild.js': 162 | case '.esbuild.jsx': 163 | case '.esbuild.ts': 164 | case '.esbuild.tsx': 165 | case '.sucrase.js': 166 | case '.sucrase.jsx': 167 | case '.sucrase.ts': 168 | case '.sucrase.tsx': 169 | case '.swc.js': 170 | case '.swc.jsx': 171 | case '.swc.ts': 172 | case '.swc.tsx': 173 | expected = { 174 | default: { 175 | data: { 176 | trueKey: true, 177 | falseKey: false, 178 | subKey: { 179 | subProp: 1, 180 | }, 181 | }, 182 | }, 183 | }; 184 | expect(require(fixture)).toEqual(expected); 185 | break; 186 | 187 | case '.mdx': 188 | expected = { 189 | data: { 190 | trueKey: true, 191 | falseKey: false, 192 | subKey: { 193 | subProp: 1, 194 | }, 195 | }, 196 | }; 197 | var component = require(fixture); 198 | // React internals :shrug: 199 | expect(component().type()).toEqual(expected); 200 | break; 201 | 202 | case '.toml': 203 | expected = Object.create(null); 204 | expected.data = Object.create(null); 205 | expected.data.trueKey = true; 206 | expected.data.falseKey = false; 207 | expected.data.subKey = Object.create(null); 208 | expected.data.subKey.subProp = 1; 209 | expect(require(fixture)).toEqual(expected); 210 | break; 211 | 212 | default: 213 | expected = { 214 | data: { 215 | trueKey: true, 216 | falseKey: false, 217 | subKey: { 218 | subProp: 1, 219 | }, 220 | }, 221 | }; 222 | expect(require(fixture)).toEqual(expected); 223 | } 224 | done(); 225 | } 226 | ); 227 | }); 228 | 229 | it('does not error with the .mjs extension', function (done) { 230 | this.timeout(0); 231 | 232 | var ext = '.mjs'; 233 | var fixture = './fixtures/' + ext.slice(1) + '/0/test' + ext; 234 | 235 | var result = rechoir.prepare(extensions, fixture); 236 | 237 | expect(Array.isArray(result)).toEqual(true); 238 | expect(result[0].moduleName).toEqual(path.join(__dirname, '../mjs-stub')); 239 | done(); 240 | }); 241 | 242 | it('the module can be loaded with import(), ignoring the loader', function () { 243 | if (nodeVersion.major < 12) { 244 | this.skip(); 245 | } 246 | 247 | this.timeout(0); 248 | 249 | var ext = '.mjs'; 250 | var fixture = './fixtures/' + ext.slice(1) + '/0/test' + ext; 251 | 252 | rechoir.prepare(extensions, fixture); 253 | 254 | var expected = { 255 | data: { 256 | trueKey: true, 257 | falseKey: false, 258 | subKey: { 259 | subProp: 1, 260 | }, 261 | }, 262 | }; 263 | 264 | // This avoid SyntaxError when parsing on old node versions 265 | var imprt = new Function('a', 'return import(a)'); 266 | 267 | return imprt(fixture).then(function (result) { 268 | expect(result.default).toEqual(expected); 269 | }); 270 | }); 271 | 272 | it('stubs .mjs extension with null on old node that do not care about it', function (done) { 273 | if (nodeVersion.major > 10) { 274 | this.skip(); 275 | } 276 | 277 | this.timeout(0); 278 | 279 | var ext = '.mjs'; 280 | var fixture = './fixtures/' + ext.slice(1) + '/1/test' + ext; 281 | 282 | rechoir.prepare(extensions, fixture); 283 | 284 | var expected = { 285 | data: { 286 | trueKey: true, 287 | falseKey: false, 288 | subKey: { 289 | subProp: 1, 290 | }, 291 | }, 292 | }; 293 | expect(require(fixture)).toEqual(expected); 294 | done(); 295 | }); 296 | 297 | it('does not error with the .cjs extension when inside a type: module package', function (done) { 298 | if (nodeVersion.major < 14) { 299 | this.skip(); 300 | } 301 | 302 | this.timeout(0); 303 | 304 | process.chdir(path.join(__dirname, 'fixtures/cjs/0')); 305 | 306 | child.exec('node rechoir.js', done); 307 | }); 308 | 309 | it('does not error with the .cjs extension when inside a type: commonjs package', function (done) { 310 | this.timeout(0); 311 | 312 | process.chdir(path.join(__dirname, 'fixtures/cjs/1')); 313 | 314 | child.exec('node rechoir.js', done); 315 | }); 316 | }); 317 | --------------------------------------------------------------------------------