Skip to content

Commit 0d12d1b

Browse files
committed
Inline lua.SyntaxKind as a build-time constant
1 parent 3f05a33 commit 0d12d1b

5 files changed

Lines changed: 17 additions & 23 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"fengari-web": "^0.1.4",
4545
"highlight.js": "^9.18.0",
4646
"monaco-editor": "^0.19.3",
47-
"node-libs-browser": "^2.2.1",
4847
"renderjson": "^1.4.0",
4948
"typescript-to-lua": "^0.31.0"
5049
}

src/playground/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "monaco-editor/esm/vs/basic-languages/typescript/typescript.contribution"
44
import "monaco-editor/esm/vs/editor/edcore.main";
55
import "monaco-editor/esm/vs/language/typescript/monaco.contribution";
66
import renderjson from "renderjson";
7-
import * as lua from "typescript-to-lua/dist/LuaAST";
87
import { version as tstlVersion } from "typescript-to-lua/package.json";
98
import EditorWorker from "worker-loader?name=editor.worker.js!monaco-editor/esm/vs/editor/editor.worker.js";
109
import FengariWorker from "worker-loader?name=fengari.worker.js!./fengari.worker";
@@ -14,6 +13,7 @@ import { getInitialCode, updateCodeHistory } from "./code";
1413

1514
// TODO: Use TypeScript 3.8 type imports
1615
type CustomTypeScriptWorker = import("./ts.worker").CustomTypeScriptWorker;
16+
type LuaBlock = import("typescript-to-lua/dist/LuaAST").Block;
1717

1818
(globalThis as any).MonacoEnvironment = {
1919
getWorker(_workerId: any, label: string) {
@@ -25,10 +25,11 @@ type CustomTypeScriptWorker = import("./ts.worker").CustomTypeScriptWorker;
2525
},
2626
};
2727

28+
const LuaSyntaxKind = __LUA_SYNTAX_KIND__;
2829
renderjson.set_show_to_level(1);
2930
renderjson.set_replacer((key: string, value: any) => {
3031
if (key === "kind") {
31-
return lua.SyntaxKind[value];
32+
return LuaSyntaxKind[value];
3233
}
3334

3435
return value;
@@ -56,7 +57,7 @@ const onTabClick = () => {
5657
luaTabText.onclick = onTabClick;
5758
luaTabAst.onclick = onTabClick;
5859

59-
function setLuaAST(ast: lua.Block) {
60+
function setLuaAST(ast: LuaBlock) {
6061
luaAstContainer.innerText = "";
6162
luaAstContainer.appendChild(renderjson(ast));
6263
}

src/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ declare module "worker-loader*" {
88

99
export = WebpackWorker;
1010
}
11+
12+
declare const __LUA_SYNTAX_KIND__: typeof import("typescript-to-lua").SyntaxKind;

webpack.config.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ const webpack = require("webpack");
22
const path = require("path");
33
const HtmlWebpackPlugin = require("html-webpack-plugin");
44
const PnpWebpackPlugin = require("pnp-webpack-plugin");
5+
// Not used directly in playground, because it imports typescript
6+
const { SyntaxKind: LuaSyntaxKind } = require("typescript-to-lua/dist/LuaAST");
57

68
const resolve = query => path.resolve(__dirname, query);
7-
const emptyModulePath = require.resolve("node-libs-browser/mock/empty.js");
89

910
/** @type {import("webpack").Configuration} */
1011
module.exports = {
@@ -18,6 +19,12 @@ module.exports = {
1819
resolve: {
1920
extensions: [".tsx", ".ts", ".js"],
2021
plugins: [PnpWebpackPlugin],
22+
alias: {
23+
// Replace vendored `monaco-typescript` services build with `typescript`, already used by `typescript-to-lua`
24+
[require.resolve("monaco-editor/esm/vs/language/typescript/lib/typescriptServices.js")]: require.resolve(
25+
"typescript",
26+
),
27+
},
2128
},
2229
resolveLoader: {
2330
plugins: [PnpWebpackPlugin.moduleLoader(module)],
@@ -53,23 +60,9 @@ module.exports = {
5360
contentFile: "play.html",
5461
}),
5562

56-
// Ignore pnpapi reference in patched typescript source
57-
new webpack.IgnorePlugin(/pnpapi/),
58-
59-
new webpack.NormalModuleReplacementPlugin(/typescript/, resource => {
60-
// Exclude `typescript` from `play_bundle` referenced from `typescript-to-lua/dist/LuaAST`
61-
const { issuer, compiler } = (resource.resourceResolveData && resource.resourceResolveData.context) || {};
62-
if (issuer === require.resolve("typescript-to-lua/dist/LuaAST") && compiler !== "worker") {
63-
resource.resource = emptyModulePath;
64-
}
63+
new webpack.DefinePlugin({ __LUA_SYNTAX_KIND__: JSON.stringify(LuaSyntaxKind) }),
6564

66-
// Replace vendored monaco-typescript's services build with full TypeScript API.
67-
if (
68-
resource.resource ===
69-
require.resolve("monaco-editor/esm/vs/language/typescript/lib/typescriptServices.js")
70-
) {
71-
resource.request = require.resolve("typescript");
72-
}
73-
}),
65+
// Ignore `pnpapi` reference in patched `typescript` source
66+
new webpack.IgnorePlugin(/pnpapi/),
7467
],
7568
};

yarn.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6340,7 +6340,6 @@ resolve@^1.13.1:
63406340
html-loader: ^0.5.5
63416341
html-webpack-plugin: ^3.2.0
63426342
monaco-editor: ^0.19.3
6343-
node-libs-browser: ^2.2.1
63446343
pnp-webpack-plugin: ^1.5.0
63456344
prettier: ^1.19.1
63466345
raw-loader: ^4.0.0

0 commit comments

Comments
 (0)