Skip to content

Commit

Permalink
make javascript package support both esm & commonjs
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Harris <harris.jb@gmail.com>
  • Loading branch information
jharris4 committed Mar 20, 2023
1 parent adbe946 commit c926ca3
Show file tree
Hide file tree
Showing 16 changed files with 1,238 additions and 5,200 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import antlr4 from 'file://<runtimePath>/src/antlr4/index.node.js'
import antlr4 from 'antlr4';
import <lexerName> from './<lexerName>.js';
<if(parserName)>
import <parserName> from './<parserName>.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"type": "module",
"devDependencies": {
"devDependencies": {
"@types/node": "^18.0.5"
},
"dependencies": {
"antlr4": "^4.12.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@
import org.antlr.v4.test.runtime.states.GeneratedState;
import org.stringtemplate.v4.ST;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

import static org.antlr.v4.test.runtime.FileUtils.writeFile;
import static org.antlr.v4.test.runtime.RuntimeTestUtils.isWindows;

public class NodeRunner extends RuntimeRunner {
private final static String NPM_EXEC = "npm" + (isWindows() ? ".cmd" : "");

@Override
public String getLanguage() {
return "JavaScript";
}

@Override
protected void initRuntime(RunOptions runOptions) throws Exception {
npmLinkRuntime();
}

private void npmLinkRuntime() throws Exception {
Processor.run(new String[] {NPM_EXEC, "--silent", "install"}, normalizedRuntimePath);
Processor.run(new String[] {NPM_EXEC, "--silent", "run", "build"}, normalizedRuntimePath);
Processor.run(new String[] {NPM_EXEC, "--silent", "link"}, normalizedRuntimePath);
}

@Override
public String getExtension() { return "js"; }

Expand All @@ -35,25 +45,24 @@ public String getLanguage() {
public String getRuntimeToolName() { return "node"; }

private final static String normalizedRuntimePath = getRuntimePath("JavaScript").replace('\\', '/');
private final static String newImportAntlrString =
"import antlr4 from 'file://" + normalizedRuntimePath + "/src/antlr4/index.node.js'";

@Override
protected CompiledState compile(RunOptions runOptions, GeneratedState generatedState) {
List<GeneratedFile> generatedFiles = generatedState.generatedFiles;
for (GeneratedFile generatedFile : generatedFiles) {
try {
FileUtils.replaceInFile(Paths.get(getTempDirPath(), generatedFile.name),
"import antlr4 from 'antlr4';",
newImportAntlrString);
} catch (IOException e) {
return new CompiledState(generatedState, e);
}
try {
writeFile(getTempDirPath(), "package.json",
RuntimeTestUtils.getTextFromResource("org/antlr/v4/test/runtime/helpers/package_js.json"));

npmLinkAntlr4();

return new CompiledState(generatedState, null);

} catch (Exception e) {
return new CompiledState(generatedState, e);
}
}

writeFile(getTempDirPath(), "package.json",
RuntimeTestUtils.getTextFromResource("org/antlr/v4/test/runtime/helpers/package_js.json"));
return new CompiledState(generatedState, null);
private void npmLinkAntlr4() throws Exception {
Processor.run(new String[] {NPM_EXEC, "--silent", "link", "antlr4"}, getTempDirPath());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class TsNodeRunner extends RuntimeRunner {
/* TypeScript runtime is the same as JavaScript runtime */
private final static String NORMALIZED_JAVASCRIPT_RUNTIME_PATH = getRuntimePath("JavaScript").replace('\\', '/');
private final static String NPM_EXEC = "npm" + (isWindows() ? ".cmd" : "");
private final static String WEBPACK_EXEC = "webpack" + (isWindows() ? ".cmd" : "");

@Override
public String getLanguage() {
Expand All @@ -30,17 +29,17 @@ public String getLanguage() {

@Override
protected void initRuntime(RunOptions runOptions) throws Exception {
npmInstallTsNodeAndWebpack();
npmInstallTsNode();
npmLinkRuntime();
}

private void npmInstallTsNodeAndWebpack() throws Exception {
Processor.run(new String[] {NPM_EXEC, "--silent", "install", "-g", "typescript", "ts-node", "webpack", "webpack-cli"}, null);
private void npmInstallTsNode() throws Exception {
Processor.run(new String[] {NPM_EXEC, "--silent", "install", "-g", "typescript", "ts-node"}, null);
}

private void npmLinkRuntime() throws Exception {
Processor.run(new String[] {NPM_EXEC, "--silent", "install"}, NORMALIZED_JAVASCRIPT_RUNTIME_PATH);
Processor.run(new String[] {WEBPACK_EXEC, "--no-stats"}, NORMALIZED_JAVASCRIPT_RUNTIME_PATH);
Processor.run(new String[] {NPM_EXEC, "--silent", "run", "build"}, NORMALIZED_JAVASCRIPT_RUNTIME_PATH);
Processor.run(new String[] {NPM_EXEC, "--silent", "link"}, NORMALIZED_JAVASCRIPT_RUNTIME_PATH);
}

Expand Down
3 changes: 0 additions & 3 deletions runtime/JavaScript/.babelrc

This file was deleted.

5 changes: 4 additions & 1 deletion runtime/JavaScript/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/dist/
/umd/*
/lib/*
/es/*
!/es/package.json
/node_modules
21 changes: 21 additions & 0 deletions runtime/JavaScript/babel.commonjs.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
presets: [
[
"@babel/preset-env",
{
modules: "commonjs",
targets: {
browsers: [
// 'last 2 versions',
// 'not ie > 0',
"chrome > 60"
]
}
// "targets": "> 0.25%, not dead"
// "targets": "last 2 versions, > 5%"
}
]
],
plugins: [
]
};
21 changes: 21 additions & 0 deletions runtime/JavaScript/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
presets: [
[
"@babel/preset-env",
{
modules: false,
targets: {
browsers: [
// 'last 2 versions',
// 'not ie > 0',
"chrome > 60"
]
}
// "targets": "> 0.25%, not dead"
// "targets": "last 2 versions, > 5%"
}
]
],
plugins: [
]
};
3 changes: 3 additions & 0 deletions runtime/JavaScript/es/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Loading

0 comments on commit c926ca3

Please sign in to comment.