Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into public-eval
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Nov 19, 2020
2 parents 58354c3 + f848983 commit 6d8a66f
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 109 deletions.
57 changes: 38 additions & 19 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,30 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu, windows]
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Don't forget to add all new flavors to this list!
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
include:
# Node 10
- flavor: 1
node: 10
nodeFlag: 10
typescript: latest
typescriptFlag: latest
# Node 12.15
- flavor: 2
node: 12.15
nodeFlag: 12_15
typescript: latest
typescriptFlag: latest
# Node 12.16
# TODO Add comments about why we test 2.15 and 2.16; I think git blame says it's because of an ESM behavioral change that happened at this version number
# TODO switch to '12' to get latest patch?
- flavor: 3
node: 12.16
nodeFlag: 12_16
typescript: latest
typescriptFlag: latest
# Node 13
- flavor: 4
node: 13
nodeFlag: 13
Expand All @@ -71,6 +78,7 @@ jobs:
nodeFlag: 13
typescript: next
typescriptFlag: next
# Node 14
- flavor: 7
node: 14
nodeFlag: 14
Expand All @@ -86,6 +94,32 @@ jobs:
nodeFlag: 14
typescript: next
typescriptFlag: next
# Node 14.13.0
# To test ESM builtin module resolution immediately before a node behavioral change: https://github.com/TypeStrong/ts-node/issues/1130
- flavor: 10
node: 14.13.0
nodeFlag: 14_13_0
typescript: latest
typescriptFlag: latest
# Node 15
- flavor: 11
node: 15
nodeFlag: 15
typescript: latest
typescriptFlag: latest
downgradeNpm: true
- flavor: 12
node: 15
nodeFlag: 15
typescript: 2.7
typescriptFlag: 2_7
downgradeNpm: true
- flavor: 13
node: 15
nodeFlag: 15
typescript: next
typescriptFlag: next
downgradeNpm: true
steps:
# checkout code
- uses: actions/checkout@v2
Expand All @@ -95,6 +129,9 @@ jobs:
with:
node-version: ${{ matrix.node }}
# lint, build, test
# Downgrade from npm 7 to 6 because 7 still seems buggy to me
- if: ${{ matrix.downgradeNpm }}
run: npm install -g npm@6
- run: npm install
- run: npm run build-nopack
- name: Download package artifact
Expand All @@ -111,21 +148,3 @@ jobs:
uses: codecov/codecov-action@v1
with:
flags: ${{ matrix.os }},node_${{ matrix.nodeFlag }},typescript_${{ matrix.typescriptFlag }}
- run: npm run coverage-fix-paths
- run: npm run coverage-report
- name: Coveralls
if: ${{ always() }}
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.github_token }}
parallel: true
finish:
needs: test
runs-on: ubuntu-latest
if: ${{ always() }}
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ _The name of the environment variable and the option's default value are denoted

### Programmatic-only Options

* `transformers` `_ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers)`: An object with transformers or a function that accepts a program and returns a transformers object to pass to TypeScript. Function isn't available with `transpileOnly` flag
* `transformers` `_ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers)`: An object with transformers or a factory function that accepts a program and returns a transformers object to pass to TypeScript. Factory function cannot be used with `transpileOnly` flag
* `readFile`: Custom TypeScript-compatible file reading function
* `fileExists`: Custom TypeScript-compatible file existence function

Expand Down Expand Up @@ -290,7 +290,7 @@ import UntypedJsLib from "untyped_js_lib"

**TypeScript Node** compiles source code via `require()`, watching files and code reloads are out of scope for the project. If you want to restart the `ts-node` process on file change, existing node.js tools such as [nodemon](https://github.com/remy/nodemon), [onchange](https://github.com/Qard/onchange) and [node-dev](https://github.com/fgnass/node-dev) work.

There's also [`ts-node-dev`](https://github.com/whitecolor/ts-node-dev), a modified version of [`node-dev`](https://github.com/fgnass/node-dev) using `ts-node` for compilation and won't restart the process on file change.
There's also [`ts-node-dev`](https://github.com/whitecolor/ts-node-dev), a modified version of [`node-dev`](https://github.com/fgnass/node-dev) using `ts-node` for compilation that will restart the process on file change.

## License

Expand Down
16 changes: 14 additions & 2 deletions dist-raw/node-esm-resolve-implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
// upstream changes and understand our modifications.
'use strict';

const [nodeMajor, nodeMinor, nodePatch] = process.versions.node.split('.').map(s => parseInt(s, 10))
// Test for 14.13.1 or higher
const builtinModuleProtocol = nodeMajor > 14 || (
nodeMajor === 14 && (
nodeMinor > 13 || (
nodeMinor === 13 && nodePatch > 0
)
)
)
? 'node:'
: 'nodejs:';

const {
ArrayIsArray,
JSONParse,
Expand Down Expand Up @@ -688,13 +700,13 @@ function defaultResolve(specifier, { parentURL } = {}, defaultResolveUnused) {
};
}
} catch {}
if (parsed && parsed.protocol === 'nodejs:')
if (parsed && parsed.protocol === builtinModuleProtocol)
return { url: specifier };
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
if (NativeModule.canBeRequiredByUsers(specifier)) {
return {
url: 'nodejs:' + specifier
url: builtinModuleProtocol + specifier
};
}
if (parentURL && StringPrototypeStartsWith(parentURL, 'data:')) {
Expand Down
4 changes: 3 additions & 1 deletion dist-raw/node-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function parseArgv(argv) {
'--preserve-symlinks': Boolean,
'--preserve-symlinks-main': Boolean,
'--input-type': String,
'--experimental-specifier-resolution': String
'--experimental-specifier-resolution': String,
// Legacy alias for node versions prior to 12.16
'--es-module-specifier-resolution': '--experimental-specifier-resolution',
}, {
argv,
permissive: true
Expand Down
2 changes: 1 addition & 1 deletion esm-usage-example/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"dependencies": {
"ts-node": "github:TypeStrong/ts-node#ab/esm-support",
"ts-node": "github:TypeStrong/ts-node#master",
"typescript": "^3.8.3"
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"test-spec": "mocha dist/**/*.spec.js -R spec --bail",
"test-cov": "nyc mocha -- \"dist/**/*.spec.js\" -R spec --bail",
"test": "npm run build && npm run lint && npm run test-cov",
"coverage-fix-paths": "node ./scripts/rewrite-coverage-paths.js",
"coverage-report": "nyc report --reporter=lcov",
"prepare": "npm run build-nopack"
},
Expand Down
26 changes: 0 additions & 26 deletions scripts/rewrite-coverage-paths.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/bin-script-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ console.warn(
'Please use ts-node-script instead'
)

main(['--script-mode', ...process.argv.slice(2)])
main(undefined, { '--script-mode': true })
2 changes: 1 addition & 1 deletion src/bin-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import { main } from './bin'

main(['--script-mode', ...process.argv.slice(2)])
main(undefined, { '--script-mode': true })
2 changes: 1 addition & 1 deletion src/bin-transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import { main } from './bin'

main(['--transpile-only', ...process.argv.slice(2)])
main(undefined, { '--transpile-only': true })
105 changes: 54 additions & 51 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,59 @@ import {
/**
* Main `bin` functionality.
*/
export function main (argv: string[]) {
const args = arg({
// Node.js-like options.
'--eval': String,
'--interactive': Boolean,
'--print': Boolean,
'--require': [String],

// CLI options.
'--help': Boolean,
'--script-mode': Boolean,
'--version': arg.COUNT,

// Project options.
'--dir': String,
'--files': Boolean,
'--compiler': String,
'--compiler-options': parse,
'--project': String,
'--ignore-diagnostics': [String],
'--ignore': [String],
'--transpile-only': Boolean,
'--type-check': Boolean,
'--compiler-host': Boolean,
'--pretty': Boolean,
'--skip-project': Boolean,
'--skip-ignore': Boolean,
'--prefer-ts-exts': Boolean,
'--log-error': Boolean,
'--emit': Boolean,

// Aliases.
'-e': '--eval',
'-i': '--interactive',
'-p': '--print',
'-r': '--require',
'-h': '--help',
'-s': '--script-mode',
'-v': '--version',
'-T': '--transpile-only',
'-H': '--compiler-host',
'-I': '--ignore',
'-P': '--project',
'-C': '--compiler',
'-D': '--ignore-diagnostics',
'-O': '--compiler-options'
}, {
argv,
stopAtPositional: true
})
export function main (argv: string[] = process.argv.slice(2), entrypointArgs: Record<string, any> = {}) {
const args = {
...entrypointArgs,
...arg({
// Node.js-like options.
'--eval': String,
'--interactive': Boolean,
'--print': Boolean,
'--require': [String],

// CLI options.
'--help': Boolean,
'--script-mode': Boolean,
'--version': arg.COUNT,

// Project options.
'--dir': String,
'--files': Boolean,
'--compiler': String,
'--compiler-options': parse,
'--project': String,
'--ignore-diagnostics': [String],
'--ignore': [String],
'--transpile-only': Boolean,
'--type-check': Boolean,
'--compiler-host': Boolean,
'--pretty': Boolean,
'--skip-project': Boolean,
'--skip-ignore': Boolean,
'--prefer-ts-exts': Boolean,
'--log-error': Boolean,
'--emit': Boolean,

// Aliases.
'-e': '--eval',
'-i': '--interactive',
'-p': '--print',
'-r': '--require',
'-h': '--help',
'-s': '--script-mode',
'-v': '--version',
'-T': '--transpile-only',
'-H': '--compiler-host',
'-I': '--ignore',
'-P': '--project',
'-C': '--compiler',
'-D': '--ignore-diagnostics',
'-O': '--compiler-options'
}, {
argv,
stopAtPositional: true
})
}

// Only setting defaults for CLI-specific flags
// Anything passed to `register()` can be `undefined`; `create()` will apply
Expand Down Expand Up @@ -303,5 +306,5 @@ function hasOwnProperty (object: any, property: string): boolean {
}

if (require.main === module) {
main(process.argv.slice(2))
main()
}
10 changes: 9 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,14 @@ describe('ts-node', function () {
return done()
})
})
it('via --es-module-specifier-resolution alias', (done) => {
exec(`${cmd} --experimental-modules --es-module-specifier-resolution=node index.ts`, { cwd: join(__dirname, '../tests/esm-node-resolver') }, function (err, stdout) {
expect(err).to.equal(null)
expect(stdout).to.equal('foo bar baz biff\n')

return done()
})
})
it('via NODE_OPTIONS', (done) => {
exec(`${cmd} index.ts`, {
cwd: join(__dirname, '../tests/esm-node-resolver'),
Expand Down Expand Up @@ -918,7 +926,7 @@ describe('ts-node', function () {
}, function (err, stdout, stderr) {
expect(err).to.not.equal(null)
// expect error from node's default resolver
expect(stderr).to.match(/Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*\n *at defaultResolve/)
expect(stderr).to.match(/Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*(?:\n.*){0,1}\n *at defaultResolve/)
return done()
})
})
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export const DEFAULTS: RegisterOptions = {
}

/**
* Default TypeScript compiler options required by `ts-node`.
* TypeScript compiler option values required by `ts-node` which cannot be overridden.
*/
const TS_NODE_COMPILER_OPTIONS = {
sourceMap: true,
Expand Down Expand Up @@ -447,7 +447,8 @@ export function create (rawOptions: CreateOptions = {}): Register {

const readFile = options.readFile || ts.sys.readFile
const fileExists = options.fileExists || ts.sys.fileExists
const transpileOnly = options.transpileOnly === true || options.typeCheck === false
// typeCheck can override transpileOnly, useful for CLI flag to override config file
const transpileOnly = options.transpileOnly === true && options.typeCheck !== true
const transformers = options.transformers || undefined
const ignoreDiagnostics = [
6059, // "'rootDir' is expected to contain all source files."
Expand Down

0 comments on commit 6d8a66f

Please sign in to comment.