Skip to content

Commit 3956d25

Browse files
authored
fix: typescript compiler build (#72)
<!-- ps-id: 3ef8260a-000c-484f-86f4-f17987e25915 -->
1 parent d8ca15a commit 3956d25

File tree

6 files changed

+206
-3
lines changed

6 files changed

+206
-3
lines changed

.github/workflows/nodejs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ jobs:
4040
- name: ☢ Run tests with coverage
4141
run: npm run coverage
4242

43+
- name: 🧪 Test build artifacts
44+
run: npm test -- test/build-artifacts.spec.ts
45+
4346
- name: 📊 Upload coverage reports
4447
if: matrix.node-version == '20.x' # only upload coverage for one Node version
4548
uses: actions/upload-artifact@v4

.github/workflows/publish.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
with:
1919
node-version: 22
2020
- run: npm install
21+
- run: npm run build
2122
- run: npm test
2223

2324
publish-npm:
@@ -36,6 +37,16 @@ jobs:
3637
registry-url: https://registry.npmjs.org/
3738
- run: npm install
3839
- run: npm run build
40+
- name: 🔍 Validate build artifacts
41+
run: |
42+
test -f dist/jmespath.umd.js || (echo "Missing UMD bundle" && exit 1)
43+
test -f dist/jmespath.esm.js || (echo "Missing ESM bundle" && exit 1)
44+
test -f dist/types/index.d.ts || (echo "Missing TypeScript declarations" && exit 1)
45+
test -f dist/lib/bin/jp.js || (echo "Missing CLI binary" && exit 1)
46+
head -1 dist/lib/bin/jp.js | grep -q "^#!" || (echo "CLI binary missing shebang" && exit 1)
47+
echo "✅ All build artifacts validated"
48+
- name: 🧪 Test build artifacts
49+
run: npm test -- test/build-artifacts.spec.ts
3950
- name: ✉️ publish
4051
run: npm publish --verbose --access public --tag latest
4152
env:

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
"module": "dist/jmespath.esm.js",
1515
"typings": "dist/types/index.d.ts",
1616
"types": "dist/types/index.d.ts",
17+
"exports": {
18+
".": {
19+
"types": "./dist/types/index.d.ts",
20+
"import": "./dist/jmespath.esm.js",
21+
"require": "./dist/jmespath.umd.js"
22+
}
23+
},
1724
"files": [
1825
"dist/"
1926
],
@@ -44,12 +51,13 @@
4451
"lint:fix": "biome check --write",
4552
"lint:ci": "biome check --diagnostic-level=error",
4653
"prebuild": "npx rimraf dist",
47-
"build": "npx tsc --outDir dist/lib -d --module commonjs && npx rollup -c rollup.config.ts",
54+
"build": "npx tsc --outDir dist/lib -d --module commonjs && npx tsc --project tsconfig.bin.json && npx rollup -c rollup.config.ts",
4855
"perf": "node --expose-gc scripts/perf.js",
4956
"start": "npx rollup -c rollup.config.ts -w",
5057
"test": "vitest --run",
5158
"test:watch": "vitest",
52-
"test:prod": "npm run lint && npm run test",
59+
"test:prod": "npm run lint && npm run test && npm test -- test/build-artifacts.spec.ts",
60+
"test:build": "npm run build && npm test -- test/build-artifacts.spec.ts",
5361
"coverage": "vitest run --coverage",
5462
"deploy-docs": "ts-node scripts/gh-pages-publish",
5563
"report-coverage": "cat ./coverage/lcov.info | coveralls",

test/build-artifacts.spec.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { existsSync, readFileSync, statSync } from 'fs';
2+
import { join } from 'path';
3+
import { describe, expect, it } from 'vitest';
4+
5+
const DIST_DIR = join(process.cwd(), 'dist');
6+
const TYPES_DIR = join(DIST_DIR, 'types');
7+
const LIB_DIR = join(DIST_DIR, 'lib');
8+
9+
describe('Build Artifacts', () => {
10+
describe('TypeScript Declarations', () => {
11+
it('should generate main index.d.ts file', () => {
12+
const indexDtsPath = join(TYPES_DIR, 'index.d.ts');
13+
expect(existsSync(indexDtsPath), `Expected ${indexDtsPath} to exist`).toBe(true);
14+
15+
const content = readFileSync(indexDtsPath, 'utf-8');
16+
expect(content).toContain('export declare function search');
17+
expect(content).toContain('export declare function compile');
18+
expect(content).toContain('export declare const registerFunction');
19+
});
20+
21+
it('should generate all required type declaration files', () => {
22+
const requiredFiles = [
23+
'AST.type.d.ts',
24+
'JSON.type.d.ts',
25+
'Lexer.d.ts',
26+
'Lexer.type.d.ts',
27+
'Parser.d.ts',
28+
'Parser.type.d.ts',
29+
'Runtime.d.ts',
30+
'Scope.d.ts',
31+
'TreeInterpreter.d.ts',
32+
'utils/index.d.ts',
33+
'utils/strings.d.ts',
34+
'utils/text.d.ts',
35+
];
36+
37+
for (const file of requiredFiles) {
38+
const filePath = join(TYPES_DIR, file);
39+
expect(existsSync(filePath), `Expected ${filePath} to exist`).toBe(true);
40+
}
41+
});
42+
43+
it('should export all main types and functions', () => {
44+
const indexDtsPath = join(TYPES_DIR, 'index.d.ts');
45+
const content = readFileSync(indexDtsPath, 'utf-8');
46+
47+
// Check for main function exports
48+
expect(content).toContain('export declare function search');
49+
expect(content).toContain('export declare function compile');
50+
expect(content).toContain('export declare function tokenize');
51+
52+
// Check for type exports
53+
expect(content).toContain('export type { JSONArray, JSONObject, JSONPrimitive, JSONValue }');
54+
expect(content).toContain('export type { Options }');
55+
expect(content).toContain('export type { BuiltInFunctionNames');
56+
57+
// Check for constant exports
58+
expect(content).toContain('export declare const TYPE_ANY');
59+
expect(content).toContain('export declare const TYPE_STRING');
60+
expect(content).toContain('export declare const TYPE_NUMBER');
61+
});
62+
});
63+
64+
describe('UMD and ESM Bundles', () => {
65+
it('should generate UMD bundle', () => {
66+
const umdPath = join(DIST_DIR, 'jmespath.umd.js');
67+
expect(existsSync(umdPath), `Expected ${umdPath} to exist`).toBe(true);
68+
69+
const content = readFileSync(umdPath, 'utf-8');
70+
expect(content).toContain('(function (global, factory)');
71+
});
72+
73+
it('should generate minified UMD bundle', () => {
74+
const umdMinPath = join(DIST_DIR, 'jmespath.umd.min.js');
75+
expect(existsSync(umdMinPath), `Expected ${umdMinPath} to exist`).toBe(true);
76+
77+
const stat = statSync(umdMinPath);
78+
expect(stat.size).toBeGreaterThan(0);
79+
});
80+
81+
it('should generate ESM bundle', () => {
82+
const esmPath = join(DIST_DIR, 'jmespath.esm.js');
83+
expect(existsSync(esmPath), `Expected ${esmPath} to exist`).toBe(true);
84+
85+
const content = readFileSync(esmPath, 'utf-8');
86+
expect(content).toContain('export {');
87+
});
88+
89+
it('should generate minified ESM bundle', () => {
90+
const esmMinPath = join(DIST_DIR, 'jmespath.esm.min.js');
91+
expect(existsSync(esmMinPath), `Expected ${esmMinPath} to exist`).toBe(true);
92+
93+
const stat = statSync(esmMinPath);
94+
expect(stat.size).toBeGreaterThan(0);
95+
});
96+
});
97+
98+
describe('CLI Binary', () => {
99+
it('should generate CLI binary', () => {
100+
const binPath = join(LIB_DIR, 'bin', 'jp.js');
101+
expect(existsSync(binPath), `Expected ${binPath} to exist`).toBe(true);
102+
103+
const content = readFileSync(binPath, 'utf-8');
104+
expect(content).toContain('#! /usr/bin/env node');
105+
});
106+
107+
it('should generate source maps for CLI binary', () => {
108+
const mapPath = join(LIB_DIR, 'bin', 'jp.js.map');
109+
expect(existsSync(mapPath), `Expected ${mapPath} to exist`).toBe(true);
110+
});
111+
});
112+
113+
describe('Package.json Configuration', () => {
114+
it('should have correct main entry points', () => {
115+
const pkgPath = join(process.cwd(), 'package.json');
116+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
117+
118+
expect(pkg.main).toBe('dist/jmespath.umd.js');
119+
expect(pkg.module).toBe('dist/jmespath.esm.js');
120+
expect(pkg.types).toBe('dist/types/index.d.ts');
121+
expect(pkg.typings).toBe('dist/types/index.d.ts');
122+
});
123+
124+
it('should have correct bin configuration', () => {
125+
const pkgPath = join(process.cwd(), 'package.json');
126+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
127+
128+
expect(pkg.bin).toEqual({ jp: 'dist/lib/bin/jp.js' });
129+
});
130+
131+
it('should include dist directory in files', () => {
132+
const pkgPath = join(process.cwd(), 'package.json');
133+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
134+
135+
expect(pkg.files).toContain('dist/');
136+
});
137+
138+
it('should have modern exports field', () => {
139+
const pkgPath = join(process.cwd(), 'package.json');
140+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
141+
142+
expect(pkg.exports).toBeDefined();
143+
expect(pkg.exports['.']).toBeDefined();
144+
expect(pkg.exports['.'].types).toBe('./dist/types/index.d.ts');
145+
expect(pkg.exports['.'].import).toBe('./dist/jmespath.esm.js');
146+
expect(pkg.exports['.'].require).toBe('./dist/jmespath.umd.js');
147+
});
148+
});
149+
});

tsconfig.bin.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"allowSyntheticDefaultImports": true,
5+
"allowUnreachableCode": false,
6+
"declaration": false,
7+
"emitDecoratorMetadata": true,
8+
"experimentalDecorators": true,
9+
"lib": ["es2020", "dom"],
10+
"moduleResolution": "node",
11+
"module": "commonjs",
12+
"isolatedModules": true,
13+
"esModuleInterop": true,
14+
"target": "es6",
15+
"rootDir": ".",
16+
"outDir": "dist/lib",
17+
"noUnusedLocals": true,
18+
"noUnusedParameters": true,
19+
"noImplicitAny": true,
20+
"noImplicitReturns": true,
21+
"noImplicitThis": true,
22+
"resolveJsonModule": true,
23+
"skipLibCheck": true,
24+
"sourceMap": true,
25+
"strict": true,
26+
"strictNullChecks": true,
27+
"typeRoots": ["node_modules/@types"]
28+
},
29+
"include": ["bin/**/*"],
30+
"exclude": ["node_modules", "dist", "test", "src"]
31+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"isolatedModules": true,
1414
"esModuleInterop": true,
1515
"target": "es6",
16+
"rootDir": "src",
1617
"noUnusedLocals": true,
1718
"noUnusedParameters": true,
1819
"noImplicitAny": true,
@@ -26,5 +27,5 @@
2627
"typeRoots": ["node_modules/@types"]
2728
},
2829
"exclude": ["node_modules", "dist", "test"],
29-
"include": ["src", "bin"]
30+
"include": ["src"]
3031
}

0 commit comments

Comments
 (0)