Skip to content

Commit

Permalink
Get webpack to work properly
Browse files Browse the repository at this point in the history
Webpack was generating __dirname code that threw runtime errors :(
  • Loading branch information
petervdonovan committed May 22, 2024
1 parent 3cb4710 commit fae3c6d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.vsix
.vscode-test
node_modules/*
out/*
out/
.gradle/*
__pycache__/*
lfw-pkg/
Expand Down
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tslint.json
uf.py
package-lock.json
developer-notes.md
temp*
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@
"scripts": {
"clean": "rimraf out && rimraf lib && rimraf vscode-lingua-franca-*.vsix",
"transpile": "webpack",
"vscode:prepublish": "npm run transpile -- --minify",
"compile": "./write-version-to-file.sh src/extension_version.ts && npm run transpile -- --sourcemap",
"watch": "npm run transpile -- --sourcemap --watch",
"vscode:prepublish": "npm run transpile",
"compile": "./write-version-to-file.sh src/extension_version.ts && npm run transpile",
"watch": "npm run transpile --watch",
"build": "npx ts-node src/build_lds.ts",
"compile-tests": "npx tsc --lib \"dom\" --outDir out/test --inlineSourceMap",
"package": "move-cli lingua-franca ../temp-lingua-franca89539275 && vsce package && move-cli ../temp-lingua-franca89539275 lingua-franca",
"package-pre-release": "move-cli lingua-franca ../temp-lingua-franca89539275 && vsce package --pre-release && move-cli ../temp-lingua-franca89539275 lingua-franca",
"package": "move-cli lingua-franca ../temp-lingua-franca89539275 && (vsce package; move-cli ../temp-lingua-franca89539275 lingua-franca)",
"package-pre-release": "move-cli lingua-franca ../temp-lingua-franca89539275 && (vsce package --pre-release; move-cli ../temp-lingua-franca89539275 lingua-franca)",
"deploy": "echo \"Y\r\n\" | code --install-extension vscode-lingua-franca-*.vsix --force",
"install": "npm run clean && npm run build && npm run package && npm run deploy",
"test": "npm run compile-tests && node ./out/test/test/test_runner.js --dependencies=present",
Expand Down
39 changes: 23 additions & 16 deletions src/build_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,41 @@ const getWorkspace =
return ret;
};

const fsReadCapability = (p: string) => {
try {
return require('node:fs').readFileSync(p, {encoding: "utf-8"});
} catch {
return undefined;
};
};

async function getJson(uri: string): Promise<string> {
let json: string | undefined = (await import('lfwasm')).lfc_json(vscode.Uri.parse(uri).fsPath, fsReadCapability);
if (!json) {
json = "";
}
return json;
}

/**
* Return the action that should be taken in case of a request to build.
* @param withLogs A messageShowerTransformer that lets the user request to view logs.
* @param client The language client.
* @returns The action that should be taken in case of a request to build.
*/
const build = (withLogs: MessageShowerTransformer, client: LanguageClient) =>
(textEditor: vscode.TextEditor) => {
async (textEditor: vscode.TextEditor) => {
const uri = getLfUri(textEditor.document);
if (!uri) return;
vscode.workspace.saveAll().then((successful: boolean) => {
if (!successful) return;
client.sendRequest('generator/build', [uri, getJson(uri)]).then((messageAny: any) => {
const message: string = messageAny;
if (message) withLogs(vscode.window.showInformationMessage)(message);
else withLogs(vscode.window.showErrorMessage)('Build failed.');
});
const successful = vscode.workspace.saveAll();
if (!successful) return;
client.sendRequest('generator/build', [uri, await getJson(uri)]).then((messageAny: any) => {
const message: string = messageAny;
if (message) withLogs(vscode.window.showInformationMessage)(message);
else withLogs(vscode.window.showErrorMessage)('Build failed.');
});
};

async function getJson(uri: string): Promise<string> {
let json: string | undefined = lfw.lfc_json(vscode.Uri.parse(uri).fsPath, (p: string) => vscode.workspace.fs.readFile(vscode.Uri.file(p)));
if (!json) {
json = "";
}
return json;
}

/**
* Return the action that should be taken in case of a request to build and run.
* @param withLogs A messageShowerTransformer that lets the user request to view logs.
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const libDirName = 'lib';
export const repoURL = 'https://github.com/lf-lang/lingua-franca.git';

/** Absolute path to the root directory of the vscode-lingua-franca repo. */
export const baseDirPath = path.resolve(path.dirname(require.main!.filename), '..');
export const baseDirPath = path.resolve(__dirname, '..');

/** Absolute path to the directory in which to put the jar files. */
export const libDirPath = path.resolve(baseDirPath, libDirName);
Expand Down
2 changes: 1 addition & 1 deletion src/extension_version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';
// This is a generated file. Do not edit.
export const version = "fe7bb171ee79706b762d795af62034fdf117d013";
export const version = "807cd213f6cfa3c434fa62ba030af7ac409075f9";
7 changes: 5 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const extensionConfig = {

entry: './src/extension.ts', // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
output: {
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, 'dist'),
// the bundle is stored in the 'out' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, 'out'),
filename: 'extension.js',
libraryTarget: 'commonjs2'
},
Expand Down Expand Up @@ -46,6 +46,9 @@ const extensionConfig = {
},
experiments: {
asyncWebAssembly: true
},
node: {
__dirname: true
}
};
module.exports = [ extensionConfig ];

0 comments on commit fae3c6d

Please sign in to comment.