Skip to content

Commit

Permalink
basic type script support
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-demos-typescript-a.js,AUTO-COMMIT-demos-typescript-a.md,AUTO-COMMIT-demos-typescript-a.ts,AUTO-COMMIT-demos-typescript-app.ts,AUTO-COMMIT-demos-typescript-b.js,AUTO-COMMIT-demos-typescript-c.ts,AUTO-COMMIT-src-components-tools-lively-container.js,AUTO-COMMIT-src-components-widgets-lively-code-mirror.js,AUTO-COMMIT-src-external-babel-plugin-var-recorder.js,AUTO-COMMIT-src-plugin-babel.js,AUTO-COMMIT-src-systemjs-config.js,
  • Loading branch information
JensLincke committed Jun 6, 2024
1 parent 73d8861 commit 00633f9
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 9 deletions.
6 changes: 6 additions & 0 deletions demos/typescript/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


function foo() {

return "hello"
}
10 changes: 10 additions & 0 deletions demos/typescript/a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<script>


import foo from "./a.ts"


foo()

</script>
5 changes: 5 additions & 0 deletions demos/typescript/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.log("LOAD a.ts")

function foo() {
return "hello"
}
6 changes: 6 additions & 0 deletions demos/typescript/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as a from "./a.ts"

export default function foo() {

return a.foo()
}
5 changes: 5 additions & 0 deletions demos/typescript/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lively.notify("LOAD c.ts")

export default function foo(s : string) : string {
return "hello " + s
}
12 changes: 6 additions & 6 deletions src/components/tools/lively-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export default class Container extends Morph {

// #TODO #babel6refactoring
if (lively.modules) {
if (urlString.match(/\.js$/)) {
if (urlString.match(/\.((js)|(ts))$/)) {
var m = lively.modules.module(urlString);
}
}
Expand Down Expand Up @@ -1205,7 +1205,7 @@ export default class Container extends Morph {
lively.error("custom elements require a hyphen in their name!") // see https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname
}
this.openTemplateInstance(url);
} else if (url.match(/\.js$/)) {
} else if (url.match(/\.((js)|(ts))$/)) {
this.reloadModule(url);
} else {
lively.openBrowser(url);
Expand Down Expand Up @@ -1460,11 +1460,11 @@ export default class Container extends Morph {
}
this.updateOtherContainers();

var moduleName = this.getURL().pathname.match(/([^/]+)\.js$/);
var moduleName = this.getURL().pathname.match(/([^/]+)\.((js)|(ts))$/);
if (moduleName) {
moduleName = moduleName[1];

const testRegexp = /((test\/.*)|([.-]test)|([.-]spec))\.js/;
const testRegexp = /((test\/.*)|([.-]test)|([.-]spec))\.((js)|(ts))/;
if (this.lastLoadingFailed) {
console.log("last loading failed... reload")
await this.reloadModule(url); // use our own mechanism...
Expand Down Expand Up @@ -1529,7 +1529,7 @@ export default class Container extends Morph {


async onTextChanged() {
if (!this.getURL().pathname.match(/\.js$/)) {
if (!this.getURL().pathname.match(/\.((js)|(ts))$/)) {
return
}
}
Expand Down Expand Up @@ -2076,7 +2076,7 @@ export default class Container extends Morph {

if (codeMirror) {
const cmURL = "" + url;
if (cmURL.match(/\.((js)|(py))$/)) {
if (cmURL.match(/\.((js)|(ts)|(py))$/)) {
codeMirror.setTargetModule("" + url); // for editing
}

Expand Down
2 changes: 2 additions & 0 deletions src/components/widgets/lively-code-mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,8 @@ export default class LivelyCodeMirror extends HTMLElement {
mode = "text/jsx";
} else if (filename.match(/\.mjs$/)) {
mode = "text/jsx";
} else if (filename.match(/\.ts$/)) {
mode = "text/typescript";
} else if (filename.match(/\.py$/)) {
mode = "text/x-python";
} else if (filename.match(/\.c$/)) {
Expand Down
2 changes: 1 addition & 1 deletion src/external/babel-plugin-var-recorder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const moduleNameToVarRecorderName = new Map();

export function getScopeIdForModule(moduleName) {
console.log("[babel-plugin-var-recorder] getScopeIdForModule", moduleName);
// console.log("[babel-plugin-var-recorder] getScopeIdForModule", moduleName);
if (!moduleNameToVarRecorderName.has(moduleName)) {
var scopeId = (moduleName || "undefined").replace(lively4url, "").replace(/[^a-zA-Z0-9]/g, "_")
moduleNameToVarRecorderName.set(moduleName, scopeId);
Expand Down
10 changes: 8 additions & 2 deletions src/plugin-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,14 @@ async function transformSource(load, babelOptions, config) {
var stage3Syntax = []

// console.log(`transformSource ${config.filename} ${babelOptions.babel7level}`)

if (babelOptions.babel7level == "moduleOptionsNon") {
if (babelOptions.babel7level == "liveTS") {
allPlugins.push(...await basePlugins())
allPlugins.push(babel7.babelPluginTransformTypeScript)
allPlugins.push(babel7.babelPluginProposalDynamicImport)
allPlugins.push([babel7.babelPluginTransformModulesSystemJS, {
allowTopLevelThis: true
}])
} else if (babelOptions.babel7level == "moduleOptionsNon") {
allPlugins.push(babel7.babelPluginProposalDynamicImport)
allPlugins.push([babel7.babelPluginTransformModulesSystemJS, {
allowTopLevelThis: true
Expand Down
10 changes: 10 additions & 0 deletions src/systemjs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,15 @@ const liveES7 = {
}
};

const liveTS = {
babelOptions: {
plugins: [],
babel7: true,
babel7level: "liveTS"
}
};


const babel7base = {
babelOptions: {
babel7: true,
Expand All @@ -526,6 +535,7 @@ System.config({
meta: {
'*.js': liveES7,
'*.mjs': liveES7,
'*.ts': liveTS,
'https://unpkg.com/*.js': moduleOptionsNon,
/* FILE-BASED */
/* plugins are not transpiled with other plugins, except for SystemJS-internal plugins */
Expand Down

0 comments on commit 00633f9

Please sign in to comment.