Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support ESNext target via cli option for kernel #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ def main(argv=None):


if __name__ == '__main__':
main()
main()
16 changes: 9 additions & 7 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface CompletionInfo {
candidates: string[];
/**
* The original completion from TS compiler.
* It's exposed for debugging purpuse.
* It's exposed for debugging purpose.
*/
original?: ts.CompletionInfo;
}
Expand All @@ -78,12 +78,14 @@ export interface IsCompleteResult {
}

export interface ConverterOptions {
/** If true, JavaScript mode. TypeSceript mode otherwise */
/** If true, JavaScript mode. TypeScript mode otherwise */
isJS?: boolean;
/** If true, creates a converter for browser mode. Otherwise, Node.js */
isBrowser?: boolean;
/** Only for testing. File changes are forwarded to this handler. */
_fileWatcher?: ts.FileWatcherCallback;
/** If specified target will be used */
target?: string;
}

export interface Converter {
Expand Down Expand Up @@ -117,17 +119,17 @@ export function createConverter(options?: ConverterOptions): Converter {
const dstFilename = normalizeJoin(outDir, "__tslab__.js");
const dstDeclFilename = normalizeJoin(outDir, "__tslab__.d.ts");

const transpileTargetOption = ts.ScriptTarget[options?.target];
// c.f.
// https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping
// https://github.com/microsoft/TypeScript/issues/22306#issuecomment-412266626
const transpileTarget =
semver.major(process.version) >= 12
const transpileTarget = transpileTargetOption ??
(semver.major(process.version) >= 12
? ts.ScriptTarget.ES2019
: ts.ScriptTarget.ES2018;
: ts.ScriptTarget.ES2018);
// References:
// https://github.com/microsoft/TypeScript/blob/master/src/lib/es2019.full.d.ts
const transpileLib =
transpileTarget === ts.ScriptTarget.ES2019 ? ["es2019"] : ["es2018"];
const transpileLib = [ ts.ScriptTarget[transpileTarget] ];
if (options?.isBrowser) {
transpileLib.push("dom");
transpileLib.push("dom.iterable");
Expand Down
42 changes: 14 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ class ConverterSetImpl implements ConverterSet {
private jsKernel: boolean;
private _node: Converter;
private _browser: Converter;
private target?: string;

constructor(jsKernel: boolean) {
constructor(jsKernel: boolean, target?: string) {
this.jsKernel = jsKernel;
this.target = target;
}
get node(): Converter {
if (!this._node) {
this._node = createConverter({ isJS: this.jsKernel, isBrowser: false });
this._node = createConverter({ isJS: this.jsKernel, isBrowser: false, target: this.target });
}
return this._node;
}
get browser(): Converter {
if (!this._browser) {
this._browser = createConverter({ isJS: this.jsKernel, isBrowser: true });
this._browser = createConverter({ isJS: this.jsKernel, isBrowser: true, target: this.target });
}
return this._browser;
}
Expand Down Expand Up @@ -83,15 +85,16 @@ export function startKernel({
enableFindLocal = true,
jsKernel = false,
globalVersion = "",
target,
}): void {
if (enableFindLocal) {
const local = findLocalStartKernel();
if (local) {
local({ configPath, enableFindLocal: false, jsKernel, globalVersion });
local({ configPath, enableFindLocal: false, jsKernel, globalVersion, target });
return;
}
}
const convs = new ConverterSetImpl(jsKernel);
const convs = new ConverterSetImpl(jsKernel, target);
convs.node; // Warm the converter for Node.js
const executor = createExecutor(process.cwd(), convs, {
log: console.log,
Expand Down Expand Up @@ -145,17 +148,8 @@ export function main() {
"--prefix [prefix]",
"Kernelspec will be installed in {PREFIX}/share/jupyter/kernels/"
)
.action(function () {
if (arguments.length != 1) {
console.error(
"Unused args:",
Array.from(arguments).filter((arg) => {
return typeof arg === "string";
})
);
process.exit(1);
}
let { binary, python, user, sysPrefix, prefix } = arguments[0];
.action(function (options) {
const { binary, python, user, sysPrefix, prefix } = options;
const args = [path.join(path.dirname(__dirname), "python", "install.py")];
args.push(`--tslab=${binary}`);
if (user) {
Expand Down Expand Up @@ -184,18 +178,10 @@ export function main() {
.description("Start Jupyter kernel. Used from Jupyter internally")
.option("--config-path <path>", "Path of config file")
.option("--js", "If set, start JavaScript kernel. Otherwise, TypeScript.")
.action(function () {
if (arguments.length != 1) {
console.error(
"Unused args:",
Array.from(arguments).filter((arg) => {
return typeof arg === "string";
})
);
process.exit(1);
}
let { configPath, js: jsKernel } = arguments[0];
startKernel({ configPath, jsKernel, globalVersion: getVersion() });
.option("-t, --target [target]", "If set, target will be used by transpiler.")
.action(function (options) {
const { configPath, js: jsKernel, target } = options;
startKernel({ configPath, jsKernel, globalVersion: getVersion(), target });
});

program.parse(process.argv);
Expand Down