Skip to content

Commit 2cc8c00

Browse files
committed
Added tests for commonjs and esm imports, made webpack config more compacted
Signed-off-by: Adrian Jutrowski <[email protected]>
1 parent ba6cb7a commit 2cc8c00

File tree

5 files changed

+101
-83
lines changed

5 files changed

+101
-83
lines changed

runtime/JavaScript/package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@
4949
"node": ">=16"
5050
},
5151
"exports": {
52-
"import": "./dist/antlr4.node.mjs",
53-
"require": "./dist/antlr4.node.cjs"
52+
".": {
53+
"node": {
54+
"types": "src/index.node.d.ts",
55+
"import": "./dist/antlr4.node.mjs",
56+
"require": "./dist/antlr4.node.cjs",
57+
"default": "./dist/antlr4.node.mjs"
58+
},
59+
"browser": {
60+
"types": "src/index.web.d.ts",
61+
"import": "./dist/antlr4.web.mjs",
62+
"require": "./dist/antlr4.web.cjs",
63+
"default": "./dist/antlr4.web.mjs"
64+
}
65+
}
5466
}
5567
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
var desc = Object.getOwnPropertyDescriptor(m, k);
5+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6+
desc = { enumerable: true, get: function() { return m[k]; } };
7+
}
8+
Object.defineProperty(o, k2, desc);
9+
}) : (function(o, m, k, k2) {
10+
if (k2 === undefined) k2 = k;
11+
o[k2] = m[k];
12+
}));
13+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14+
Object.defineProperty(o, "default", { enumerable: true, value: v });
15+
}) : function(o, v) {
16+
o["default"] = v;
17+
});
18+
var __importStar = (this && this.__importStar) || function (mod) {
19+
if (mod && mod.__esModule) return mod;
20+
var result = {};
21+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22+
__setModuleDefault(result, mod);
23+
return result;
24+
};
25+
Object.defineProperty(exports, "__esModule", { value: true });
26+
const antlr4 = __importStar(require("antlr4"));
27+
describe('Antlr4 Node CommonJs', () => {
28+
it('should use the CommonJS module on Node.js', () => {
29+
expect(antlr4).toBeDefined();
30+
});
31+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as antlr4 from 'antlr4'
2+
3+
describe('Antlr4 Node Esm', () => {
4+
it('should use the Esm module on Node.js', () => {
5+
expect(antlr4).toBeDefined();
6+
});
7+
});
8+
export {};

runtime/JavaScript/spec/support/jasmine.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"spec_dir": "spec",
33
"spec_files": [
4+
"**/*Spec.[c|m]*js",
45
"**/*Spec.js"
56
],
67
"helpers": [

runtime/JavaScript/webpack.config.js

Lines changed: 47 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,97 +5,63 @@ import {fileURLToPath} from "url";
55
const __filename = fileURLToPath(import.meta.url);
66
const __dirname = path.dirname(__filename);
77

8-
const nodeConfig = {
8+
9+
const buildConfig = ( platform, extentions ) => ({
910
mode: "production",
10-
entry: './src/antlr4/index.node.js',
11+
entry: `./src/antlr4/index.${platform}.js`,
1112
output: {
1213
path: path.resolve(__dirname, 'dist'),
13-
filename: 'antlr4.node.mjs',
14-
chunkFormat: "module",
14+
filename: `antlr4.${platform}.${extentions}`,
15+
chunkFormat: extentions === "mjs" ? "module" : "commonjs",
1516
library: {
16-
type: "module"
17+
type: extentions === "mjs" ? "module" : "commonjs"
1718
}
1819
},
19-
resolve: {
20-
extensions: [ '.js']
21-
},
22-
target: "node",
20+
21+
...(platform === 'web' && {
2322
module: {
24-
rules: [{
25-
test: /\.js$/,
26-
exclude: /node_modules/,
27-
use: [ 'babel-loader' ]
28-
}]
29-
},
30-
plugins: [ new ESLintPlugin() ],
31-
experiments: {
32-
outputModule: true
33-
},
34-
devtool: "source-map"
35-
};
23+
rules: [{
24+
test: /\.js$/,
25+
exclude: [ /node_modules/, path.resolve(__dirname, "src/FileStream.js") ],
26+
use: [ 'babel-loader' ]
27+
}]
28+
},
29+
performance: {
30+
maxAssetSize: 512000,
31+
maxEntrypointSize: 512000
32+
},
33+
resolve: {
34+
extensions: [ '.js'],
35+
fallback: {
36+
fs: false
37+
}
38+
},
39+
}),
3640

37-
const nodeConfigCommonJs = {
38-
mode: "production",
39-
entry: './src/antlr4/index.node.js',
40-
output: {
41-
path: path.resolve(__dirname, 'dist'),
42-
filename: 'antlr4.node.cjs',
43-
chunkFormat: "commonjs",
44-
library: {
45-
type: "commonjs"
46-
}
47-
},
48-
resolve: {
49-
extensions: [ '.js']
50-
},
51-
target: "node",
52-
module: {
53-
rules: [{
54-
test: /\.js$/,
55-
exclude: /node_modules/,
56-
use: [ 'babel-loader' ]
57-
}]
58-
},
41+
...(platform === 'node' && {
42+
module: {
43+
rules: [{
44+
test: /\.js$/,
45+
exclude: /node_modules/,
46+
use: [ 'babel-loader' ]
47+
}]
48+
},
49+
resolve: {
50+
extensions: [ '.js'],
51+
},
52+
}),
53+
target: platform,
5954
plugins: [ new ESLintPlugin() ],
55+
devtool: "source-map",
6056
experiments: {
61-
outputModule: false
57+
outputModule: extentions === "mjs"
6258
},
63-
devtool: "source-map"
64-
};
59+
})
6560

66-
const webConfig = {
67-
mode: "production",
68-
entry: './src/antlr4/index.web.js',
69-
output: {
70-
path: path.resolve(__dirname, 'dist'),
71-
filename: 'antlr4.web.js',
72-
library: {
73-
type: "module"
74-
}
75-
},
76-
resolve: {
77-
extensions: [ '.js'],
78-
fallback: {
79-
fs: false
80-
}
81-
},
82-
target: "web",
83-
module: {
84-
rules: [{
85-
test: /\.js$/,
86-
exclude: [ /node_modules/, path.resolve(__dirname, "src/FileStream.js") ],
87-
use: [ 'babel-loader' ]
88-
}]
89-
},
90-
performance: {
91-
maxAssetSize: 512000,
92-
maxEntrypointSize: 512000
93-
},
94-
plugins: [ new ESLintPlugin() ],
95-
experiments: {
96-
outputModule: true
97-
},
98-
devtool: "source-map"
99-
};
10061

101-
export default [ nodeConfig, nodeConfigCommonJs, webConfig ];
62+
export default [
63+
buildConfig("node", "cjs"),
64+
buildConfig("node", "mjs"),
65+
buildConfig("web", "cjs"),
66+
buildConfig("web", "mjs"),
67+
];

0 commit comments

Comments
 (0)