Skip to content

Commit 8173512

Browse files
committed
Provide recommended lib files to the playground
1 parent 784768f commit 8173512

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"worker-loader": "^2.0.0"
4242
},
4343
"dependencies": {
44+
"@types/webpack-env": "^1.15.1",
4445
"fengari-web": "^0.1.4",
4546
"highlight.js": "^9.18.0",
4647
"monaco-editor": "^0.19.3",

src/empty.ts

Whitespace-only changes.

src/playground/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async function onCodeChanged() {
6868
const getWorker = await monaco.languages.typescript.getTypeScriptWorker();
6969
const client: CustomTypeScriptWorker = await getWorker(model.uri);
7070

71-
const { code, ast } = await client.getTranspileOutput();
71+
const { code, ast } = await client.getTranspileOutput(model.uri.toString());
7272
luaEditor.setValue(code);
7373
setLuaAST(ast);
7474
fengariWorker.postMessage({ code });
@@ -80,6 +80,14 @@ fengariWorker.onmessage = event => {
8080
outputTerminalContent.innerText = messages.map(m => m.text).join("\n");
8181
};
8282

83+
function addLibsFromContext(context: __WebpackModuleApi.RequireContext) {
84+
for (const request of context.keys()) {
85+
monaco.languages.typescript.typescriptDefaults.addExtraLib(context(request).default);
86+
}
87+
}
88+
89+
addLibsFromContext(require.context("!!raw-loader!typescript/lib/", false, /lib(\.es(.+))?\.d\.ts$/));
90+
8391
const tsEditor = monaco.editor.create(tsEditorContainer, {
8492
value: getInitialCode(),
8593
language: "typescript",

src/playground/ts.worker.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,44 @@ import { TypeScriptWorker } from "monaco-editor/esm/vs/language/typescript/tsWor
44
import * as ts from "typescript";
55
import * as tstl from "typescript-to-lua";
66

7+
const libContext = require.context(`raw-loader!typescript-to-lua/dist/lualib`, true, /(.+)(?<!lualib_bundle)\.lua$/);
78
const emitHost: tstl.EmitHost = {
8-
getCurrentDirectory: () => ".",
9+
getCurrentDirectory: () => "",
910
readFile: (fileName: string) => {
10-
const featureName = fileName.replace("/dist/lualib/", "").replace(".lua", "");
11-
return require(`raw-loader!typescript-to-lua/dist/lualib/${featureName}.lua`).default;
11+
const [, featureName] = fileName.match(/\/dist\/lualib\/(.+)\.lua$/) ?? [];
12+
if (featureName === undefined) {
13+
throw new Error(`Unexpected file to read: ${fileName}`);
14+
}
15+
16+
return libContext(`./${featureName}.lua`).default;
1217
},
1318
};
1419

1520
// TODO: In latest monaco-typescript it returns `ts.Diagnostic[]`
1621
const clearDiagnostics = (TypeScriptWorker as any).clearFiles;
1722

1823
export class CustomTypeScriptWorker extends TypeScriptWorker {
19-
public async getTranspileOutput() {
20-
const { transpiledFiles } = this.transpileLua();
24+
public async getTranspileOutput(fileName: string) {
25+
const { transpiledFiles } = this.transpileLua(fileName);
2126
const [transpiledFile] = transpiledFiles;
2227
return { code: transpiledFile.lua!, ast: transpiledFile.luaAst! };
2328
}
2429

2530
public async getSemanticDiagnostics(fileName: string) {
2631
const diagnostics = await super.getSemanticDiagnostics(fileName);
27-
const { diagnostics: transpileDiagnostics } = this.transpileLua();
32+
const { diagnostics: transpileDiagnostics } = this.transpileLua(fileName);
2833
clearDiagnostics(transpileDiagnostics);
2934
return [...diagnostics, ...transpileDiagnostics];
3035
}
3136

32-
private transpileLua() {
37+
private transpileLua(fileName: string) {
3338
const program = ((this as any)._languageService as ts.LanguageService).getProgram()!;
3439

3540
const compilerOptions = program.getCompilerOptions();
3641
compilerOptions.luaLibImport = tstl.LuaLibImportKind.Inline;
3742
compilerOptions.luaTarget = tstl.LuaTarget.Lua53;
3843

39-
const sourceFiles = program.getRootFileNames().map(n => program.getSourceFile(n)!);
40-
return tstl.transpile({ program, emitHost, sourceFiles });
44+
return tstl.transpile({ program, emitHost, sourceFiles: [program.getSourceFile(fileName)!] });
4145
}
4246
}
4347

webpack.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ module.exports = {
2020
extensions: [".tsx", ".ts", ".js"],
2121
plugins: [PnpWebpackPlugin],
2222
alias: {
23-
// Replace vendored `monaco-typescript` services build with `typescript`, already used by `typescript-to-lua`
23+
// Replace vendored monaco-typescript services build with typescript, already used by typescript-to-lua
2424
[require.resolve("monaco-editor/esm/vs/language/typescript/lib/typescriptServices.js")]: require.resolve(
2525
"typescript",
2626
),
27+
28+
// Exclude builtin monaco-typescript libs
29+
[require.resolve("monaco-editor/esm/vs/language/typescript/lib/lib.js")]: resolve("src/empty.ts"),
2730
},
2831
},
2932
resolveLoader: {
@@ -62,7 +65,7 @@ module.exports = {
6265

6366
new webpack.DefinePlugin({ __LUA_SYNTAX_KIND__: JSON.stringify(LuaSyntaxKind) }),
6467

65-
// Ignore `pnpapi` reference in patched `typescript` source
68+
// Ignore "pnpapi" reference in patched typescript source
6669
new webpack.IgnorePlugin(/pnpapi/),
6770
],
6871
};

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ __metadata:
4343
languageName: node
4444
linkType: hard
4545

46+
"@types/webpack-env@npm:^1.15.1":
47+
version: 1.15.1
48+
resolution: "@types/webpack-env@npm:1.15.1"
49+
checksum: 481065aa7573f836e4e62dad60f81f6b51d0dfbe050ca9dcf624dd9dc4b95f10fe23ea61dc9c730fba9e4a3fdffc5231f5670f8d874f063b6bd326b5f9766612
50+
languageName: node
51+
linkType: hard
52+
4653
"@webassemblyjs/ast@npm:1.8.5":
4754
version: 1.8.5
4855
resolution: "@webassemblyjs/ast@npm:1.8.5"
@@ -6332,6 +6339,7 @@ resolve@^1.13.1:
63326339
dependencies:
63336340
"@types/highlight.js": ^9.12.3
63346341
"@types/node": ^13.5.0
6342+
"@types/webpack-env": ^1.15.1
63356343
css-loader: ^3.4.2
63366344
fengari-web: ^0.1.4
63376345
file-loader: ^5.0.2

0 commit comments

Comments
 (0)