Skip to content

Commit 732fe81

Browse files
committed
fix typescript compilation #40
1 parent c9d395a commit 732fe81

File tree

11 files changed

+113
-121
lines changed

11 files changed

+113
-121
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.0.4 (26 Sept 2021)
44
* Support for a REPL (Interactive Window) experience (https://github.com/DonJayamanne/typescript-notebook/issues/37)
5+
* Fix typescript copmilation (https://github.com/DonJayamanne/typescript-notebook/issues/40)
56

67
## 2.0.3 (12 Sept 2021)
78
* Support for user input in notebooks using [readline](https://nodejs.org/api/readline.html#readline_readline_createinterface_options)

build/webpack/webpack.client.config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const FixDefaultImportPlugin = require('webpack-fix-default-import-plugin');
33
const path = require('path');
44
const constants = require('../constants');
55
const configFileName = 'tsconfig.client.json';
6+
const CopyPlugin = require("copy-webpack-plugin");
67
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
78
// Any build on the CI is considered production mode.
89
const isProdBuild = constants.isCI || process.argv.some((argv) => argv.includes('mode') && argv.includes('production'));
@@ -40,8 +41,14 @@ module.exports = {
4041
}
4142

4243
// reportFiles: ['src/client/**/*.{ts,tsx}']
43-
})
44+
}),
4445
// ...common.getDefaultPlugins('extension')
46+
new CopyPlugin({
47+
patterns: [
48+
{ from: path.join(constants.ExtensionRootDir, "node_modules/typescript/lib/typescript.js"), to: path.join(constants.ExtensionRootDir, "resources/scripts/node_modules/typescript/index.js") },
49+
{ from: path.join(constants.ExtensionRootDir, "node_modules/typescript/LICENSE.txt"), to: path.join(constants.ExtensionRootDir, "resources/scripts/node_modules/typescript/LICENSE.txt") }
50+
],
51+
}),
4552
],
4653
stats: {
4754
performance: false

build/webpack/webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module.exports = [
2-
require('./webpack.typescript.config'),
32
require('./webpack.extension.config'),
43
require('./webpack.client.config')
54
];

build/webpack/webpack.typescript.config.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

package-lock.json

Lines changed: 19 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Iterative & iteractive programming for Node.js in JavaScript/TypeScript (REPL), with great support for Tensorflow.js, debugging, & more..",
55
"version": "2.0.4",
66
"engines": {
7-
"vscode": "^1.60.0"
7+
"vscode": "^1.61.0"
88
},
99
"publisher": "donjayamanne",
1010
"author": {
@@ -343,13 +343,12 @@
343343
},
344344
"scripts": {
345345
"vscode:prepublish": "npm run build",
346-
"dev": "concurrently -r npm:compile:typescript npm:compile:extension:watch npm:compile:client:watch",
346+
"dev": "concurrently -r npm:compile:extension:watch npm:compile:client:watch",
347347
"installtsnode": "cd resources/scripts && npm i -S ts-node",
348348
"build": "webpack --config=build/webpack/webpack.config.js --mode=production",
349349
"compile:extension:watch": "tsc --watch -p ./",
350350
"compile:client:watch": "webpack --config=build/webpack/webpack.client.config.js --watch",
351351
"compile:extension:webpack": "webpack --config=build/webpack/webpack.extension.config.js",
352-
"compile:typescript": "webpack --config=build/webpack/webpack.typescript.config.js",
353352
"lint": "eslint src/client --ext ts && eslint src/extension --ext ts && eslint build --ext js",
354353
"lint-format": "npx prettier 'src/**/*.ts*' --check && npx prettier 'build/**/*.js' --check",
355354
"package": "vsce package --yarn -o node-notebooks.vsix",
@@ -377,7 +376,7 @@
377376
"shell-quote": "^1.7.2",
378377
"source-map": "^0.6.1",
379378
"tmp": "^0.2.1",
380-
"typescript": "^4.4.3",
379+
"typescript": "^4.4.4",
381380
"uuid": "^8.3.2",
382381
"ws": "^7.5.3",
383382
"xterm": "^4.13.0",
@@ -400,7 +399,7 @@
400399
"arquero": "^4.8.4",
401400
"cache-loader": "^4.1.0",
402401
"concurrently": "^5.2.0",
403-
"copy-webpack-plugin": "^9.0.0",
402+
"copy-webpack-plugin": "^9.0.1",
404403
"css-loader": "^3.5.3",
405404
"danfojs-node": "^0.2.7",
406405
"eslint": "^7.32.0",

resources/scripts/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/scripts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"license": "ISC",
1212
"dependencies": {
1313
"profoundjs-node-pty": "^2.0.3",
14-
"ts-node": "^10.2.1"
14+
"ts-node": "^10.3.0"
1515
}
1616
}

src/extension/kernel/compiler.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export namespace Compiler {
208208
const matchingItem =
209209
typeof location.column === 'number'
210210
? // Find the next closes column we have, we if cannot find an exact match.
211-
map.get(location.column) || map.get(location.column - 1) || map.get(location.column + 1)
211+
map.get(location.column) || map.get(location.column - 1) || map.get(location.column + 1)
212212
: map.get(0)!;
213213
if (matchingItem) {
214214
mappedLocation.line = matchingItem.generatedLine;
@@ -260,11 +260,12 @@ export namespace Compiler {
260260
{
261261
sourceMap: true,
262262
inlineSourceMap: true,
263-
sourceRoot: path.dirname(details.sourceFilename),
263+
jsx: ts.JsxEmit.None,
264+
sourceRoot: path.dirname(details.sourceTsFilename),
264265
noImplicitUseStrict: true,
265266
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Remove,
266267
strict: false, // No way.
267-
fileName: details.sourceFilename,
268+
fileName: details.sourceTsFilename,
268269
resolveJsonModule: true,
269270
removeComments: true,
270271
target: ts.ScriptTarget.ESNext, // Minimum Node12 (but let users use what ever they want). Lets look into user defined tsconfig.json.
@@ -282,7 +283,7 @@ export namespace Compiler {
282283
allowSyntheticDefaultImports: true,
283284
skipLibCheck: true // We expect users to rely on VS Code to let them know if they have issues in their code.
284285
},
285-
details.sourceFilename
286+
details.sourceTsFilename
286287
);
287288
// let transpiledCode = result;
288289
// let transpiledCode = result.replace(
@@ -522,6 +523,7 @@ function createCodeObject(cell: NotebookCell) {
522523
const codeObject: CodeObject = {
523524
code: '',
524525
sourceFilename: '',
526+
sourceTsFilename: '',
525527
sourceMapFilename: '',
526528
friendlyName: cwd
527529
? `${path.relative(cwd, cell.notebook.uri.fsPath)}?cell=${cell.index + 1}`
@@ -535,10 +537,16 @@ function createCodeObject(cell: NotebookCell) {
535537
// codeObject.sourceFilename = codeObject.sourceFilename || cell.document.uri.toString();
536538
codeObject.sourceFilename =
537539
codeObject.sourceFilename || path.join(tmpDirectory, `notebook_cell_${cell.document.uri.fragment}.js`);
540+
codeObject.sourceTsFilename =
541+
codeObject.sourceTsFilename || path.join(tmpDirectory, `notebook_cell_${cell.document.uri.fragment}.ts`);
538542
codeObject.sourceMapFilename = codeObject.sourceMapFilename || `${codeObject.sourceFilename}.map`;
539543
mapOfSourceFilesToNotebookUri.set(codeObject.sourceFilename, cell.document.uri);
540544
mapOfSourceFilesToNotebookUri.set(cell.document.uri.toString(), cell.document.uri);
541545
mapFromCellToPath.set(cell, codeObject);
546+
// For some reason if the file is empty, then generics don't work
547+
// To fix this issue (https://github.com/DonJayamanne/typescript-notebook/issues/40) we must have code in the file.
548+
// Even tried locally by manullay importing typescript and using compiler, same problem.
549+
fs.writeFileSync(codeObject.sourceTsFilename, cell.document.getText());
542550
return codeObject;
543551
}
544552
function updateCodeObject(codeObject: CodeObject, cell: NotebookCell, sourceCode: string, sourceMapText: string) {
@@ -549,6 +557,7 @@ function updateCodeObject(codeObject: CodeObject, cell: NotebookCell, sourceCode
549557

550558
if (codeObject.textDocumentVersion !== cell.document.version) {
551559
fs.writeFileSync(codeObject.sourceFilename, sourceCode);
560+
fs.writeFileSync(codeObject.sourceTsFilename, sourceCode);
552561
// if (sourceMapText) {
553562
// fs.writeFileSync(codeObject.sourceMapFilename, sourceMapText);
554563
// }
@@ -655,15 +664,15 @@ export type VariableDeclaration = BaseNode<'VariableDeclaration'> & {
655664
};
656665
type VariableDeclarator = BaseNode<'VariableDeclarator'> & {
657666
id:
658-
| (BaseNode<string> & { name: string; loc: BodyLocation; type: 'Identifier' | '<other>' })
659-
| (BaseNode<'ObjectPattern'> & {
660-
name: string;
661-
properties: { type: 'Property'; key: { name: string }; value: { name: string } }[];
662-
})
663-
| (BaseNode<'ArrayPattern'> & {
664-
name: string;
665-
elements: { name: string; type: 'Identifier' }[];
666-
});
667+
| (BaseNode<string> & { name: string; loc: BodyLocation; type: 'Identifier' | '<other>' })
668+
| (BaseNode<'ObjectPattern'> & {
669+
name: string;
670+
properties: { type: 'Property'; key: { name: string }; value: { name: string } }[];
671+
})
672+
| (BaseNode<'ArrayPattern'> & {
673+
name: string;
674+
elements: { name: string; type: 'Identifier' }[];
675+
});
667676
init?: { loc: BodyLocation };
668677
loc: BodyLocation;
669678
};
@@ -677,14 +686,14 @@ type BlockStatement = {
677686
};
678687
type ExpressionStatement = BaseNode<'ExpressionStatement'> & {
679688
expression:
689+
| (BaseNode<'CallExpression'> & {
690+
callee:
691+
| (BaseNode<'ArrowFunctionExpression'> & { body: BlockStatement })
680692
| (BaseNode<'CallExpression'> & {
681-
callee:
682-
| (BaseNode<'ArrowFunctionExpression'> & { body: BlockStatement })
683-
| (BaseNode<'CallExpression'> & {
684-
body: BlockStatement;
685-
callee: { name: string; loc: BodyLocation };
686-
});
687-
})
688-
| BaseNode<'other'>;
693+
body: BlockStatement;
694+
callee: { name: string; loc: BodyLocation };
695+
});
696+
})
697+
| BaseNode<'other'>;
689698
loc: BodyLocation;
690699
};

src/extension/server/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type CodeObject = {
3535
code: string;
3636
textDocumentVersion: number;
3737
sourceFilename: string;
38+
sourceTsFilename: string;
3839
friendlyName: string;
3940
sourceMapFilename: string;
4041
};

0 commit comments

Comments
 (0)