Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Javascript] Added support for commonjs in nodejs package #4217

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion runtime/JavaScript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"description": "JavaScript runtime for ANTLR4",
"browser": "dist/antlr4.web.js",
"main": "dist/antlr4.node.js",
"main": "dist/antlr4.node.mjs",
"types": "src/antlr4/index.d.ts",
"repository": "antlr/antlr4.git",
"keywords": [
Expand Down Expand Up @@ -47,5 +47,21 @@
},
"engines": {
"node": ">=16"
},
"exports": {
".": {
"node": {
"types": "src/index.node.d.ts",
"import": "./dist/antlr4.node.mjs",
"require": "./dist/antlr4.node.cjs",
"default": "./dist/antlr4.node.mjs"
},
"browser": {
"types": "src/index.web.d.ts",
"import": "./dist/antlr4.web.mjs",
"require": "./dist/antlr4.web.cjs",
"default": "./dist/antlr4.web.mjs"
}
}
}
}
8 changes: 8 additions & 0 deletions runtime/JavaScript/spec/imports/NodeCommonJSImportSpec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

const antlr4 = require("antlr4");
describe('Antlr4 Node CommonJs', () => {
it('should use the CommonJS module on Node.js', () => {
expect(antlr4).toBeDefined();
});
});
8 changes: 8 additions & 0 deletions runtime/JavaScript/spec/imports/NodeEsmImportSpec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as antlr4 from 'antlr4'

describe('Antlr4 Node Esm', () => {
it('should use the Esm module on Node.js', () => {
expect(antlr4).toBeDefined();
});
});
export {};
1 change: 1 addition & 0 deletions runtime/JavaScript/spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*Spec.[c|m]*js",
"**/*Spec.js"
],
"helpers": [
Expand Down
101 changes: 48 additions & 53 deletions runtime/JavaScript/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,63 @@ import {fileURLToPath} from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const nodeConfig = {

const buildConfig = ( platform, extensions ) => ({
mode: "production",
entry: './src/antlr4/index.node.js',
entry: `./src/antlr4/index.${platform}.js`,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'antlr4.node.js',
chunkFormat: "module",
filename: `antlr4.${platform}.${extensions}`,
chunkFormat: extensions === "mjs" ? "module" : "commonjs",
library: {
type: "module"
type: extensions === "mjs" ? "module" : "commonjs"
}
},
resolve: {
extensions: [ '.js']
},
target: "node",

...(platform === 'web' && {
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: [ 'babel-loader' ]
}]
},
plugins: [ new ESLintPlugin() ],
experiments: {
outputModule: true
},
devtool: "source-map"
};
rules: [{
test: /\.js$/,
exclude: [ /node_modules/, path.resolve(__dirname, "src/FileStream.js") ],
use: [ 'babel-loader' ]
}]
},
performance: {
maxAssetSize: 512000,
maxEntrypointSize: 512000
},
resolve: {
extensions: [ '.js'],
fallback: {
fs: false
}
},
}),

const webConfig = {
mode: "production",
entry: './src/antlr4/index.web.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'antlr4.web.js',
library: {
type: "module"
}
},
resolve: {
extensions: [ '.js'],
fallback: {
fs: false
}
},
target: "web",
module: {
rules: [{
test: /\.js$/,
exclude: [ /node_modules/, path.resolve(__dirname, "src/FileStream.js") ],
use: [ 'babel-loader' ]
}]
},
performance: {
maxAssetSize: 512000,
maxEntrypointSize: 512000
},
...(platform === 'node' && {
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: [ 'babel-loader' ]
}]
},
resolve: {
extensions: [ '.js'],
},
}),
target: platform,
plugins: [ new ESLintPlugin() ],
devtool: "source-map",
experiments: {
outputModule: true
outputModule: extensions === "mjs"
},
devtool: "source-map"
};
})


export default [ nodeConfig, webConfig ];
export default [
buildConfig("node", "cjs"),
buildConfig("node", "mjs"),
buildConfig("web", "cjs"),
buildConfig("web", "mjs"),
];