Skip to content

Commit

Permalink
Added tests for commonjs and esm imports, made webpack config more co…
Browse files Browse the repository at this point in the history
…mpacted
  • Loading branch information
Tarjei400 committed Apr 11, 2023
1 parent ba6cb7a commit 3a86783
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 83 deletions.
16 changes: 14 additions & 2 deletions runtime/JavaScript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@
"node": ">=16"
},
"exports": {
"import": "./dist/antlr4.node.mjs",
"require": "./dist/antlr4.node.cjs"
".": {
"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"
}
}
}
}
31 changes: 31 additions & 0 deletions runtime/JavaScript/spec/imports/NodeCommonJSImportSpec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const antlr4 = __importStar(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
128 changes: 47 additions & 81 deletions runtime/JavaScript/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,63 @@ import {fileURLToPath} from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const nodeConfig = {

const buildConfig = ( platform, extentions ) => ({
mode: "production",
entry: './src/antlr4/index.node.js',
entry: `./src/antlr4/index.${platform}.js`,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'antlr4.node.mjs',
chunkFormat: "module",
filename: `antlr4.${platform}.${extentions}`,
chunkFormat: extentions === "mjs" ? "module" : "commonjs",
library: {
type: "module"
type: extentions === "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 nodeConfigCommonJs = {
mode: "production",
entry: './src/antlr4/index.node.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'antlr4.node.cjs',
chunkFormat: "commonjs",
library: {
type: "commonjs"
}
},
resolve: {
extensions: [ '.js']
},
target: "node",
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: [ 'babel-loader' ]
}]
},
...(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: false
outputModule: extentions === "mjs"
},
devtool: "source-map"
};
})

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
},
plugins: [ new ESLintPlugin() ],
experiments: {
outputModule: true
},
devtool: "source-map"
};

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

0 comments on commit 3a86783

Please sign in to comment.