Skip to content
Closed
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
44 changes: 22 additions & 22 deletions parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@
"author": "Constructive <developers@constructive.io>",
"description": "Multi-version PostgreSQL parser with dynamic version selection",
"main": "./wasm/index.cjs",
"module": "./wasm/index.js",
"module": "./wasm/esm/index.js",
"types": "./wasm/index.d.ts",
"exports": {
".": {
"import": "./wasm/index.js",
"require": "./wasm/index.cjs",
"types": "./wasm/index.d.ts"
"types": "./wasm/index.d.ts",
"import": "./wasm/esm/index.js",
"require": "./wasm/index.cjs"
},
"./v13": {
"import": "./wasm/v13.js",
"require": "./wasm/v13.cjs",
"types": "./wasm/v13.d.ts"
"types": "./wasm/v13.d.ts",
"import": "./wasm/esm/v13.js",
"require": "./wasm/v13.cjs"
},
"./v14": {
"import": "./wasm/v14.js",
"require": "./wasm/v14.cjs",
"types": "./wasm/v14.d.ts"
"types": "./wasm/v14.d.ts",
"import": "./wasm/esm/v14.js",
"require": "./wasm/v14.cjs"
},
"./v15": {
"import": "./wasm/v15.js",
"require": "./wasm/v15.cjs",
"types": "./wasm/v15.d.ts"
"types": "./wasm/v15.d.ts",
"import": "./wasm/esm/v15.js",
"require": "./wasm/v15.cjs"
},
"./v16": {
"import": "./wasm/v16.js",
"require": "./wasm/v16.cjs",
"types": "./wasm/v16.d.ts"
"types": "./wasm/v16.d.ts",
"import": "./wasm/esm/v16.js",
"require": "./wasm/v16.cjs"
},
"./v17": {
"import": "./wasm/v17.js",
"require": "./wasm/v17.cjs",
"types": "./wasm/v17.d.ts"
"types": "./wasm/v17.d.ts",
"import": "./wasm/esm/v17.js",
"require": "./wasm/v17.cjs"
},
"./v18": {
"import": "./wasm/v18.js",
"require": "./wasm/v18.cjs",
"types": "./wasm/v18.d.ts"
"types": "./wasm/v18.d.ts",
"import": "./wasm/esm/v18.js",
"require": "./wasm/v18.cjs"
}
},
"files": [
Expand Down
27 changes: 14 additions & 13 deletions parser/scripts/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ console.log(`Description: ${config.description}`);
console.log(`Versions: ${config.versions.join(', ')}`);
console.log('');

// Ensure wasm directory exists
const wasmDir = path.join(__dirname, '../wasm');
if (!fs.existsSync(wasmDir)) {
fs.mkdirSync(wasmDir, { recursive: true });
}
const esmDir = path.join(wasmDir, 'esm');
fs.mkdirSync(esmDir, { recursive: true });
fs.writeFileSync(path.join(esmDir, 'package.json'), '{"type":"module"}\n');

// Helper function to generate template variables
function generateTemplateVars(versions) {
Expand All @@ -45,8 +44,8 @@ function generateTemplateVars(versions) {
// For TypeScript union: 13 | 14 | 15 | 16 | 17
const versionUnion = versions.join(' | ');

// For ESM exports: export * as v13 from './v13/index.js';
const versionExports = versions.map(v => `export * as v${v} from './v${v}/index.js';`).join('\n');
// For ESM exports: export * as v13 from './v13.js';
const versionExports = versions.map(v => `export * as v${v} from './v${v}.js';`).join('\n');

// For CommonJS requires: v13: require('./v13/index.cjs'),
const versionRequires = versions.map(v => ` v${v}: require('./v${v}/index.cjs')`).join(',\n');
Expand Down Expand Up @@ -117,6 +116,14 @@ config.versions.forEach(version => {

if (fs.statSync(sourcePath).isFile()) {
console.log(`Copying ${file} for v${version}...`);
if (file === 'index.js') {
const content = fs.readFileSync(sourcePath, 'utf8')
.replace(/\.\/libpg-query\.js/g, `../v${version}/libpg-query.js`)
.replace(/@pgsql\/types/g, `../v${version}/types/index.js`);
fs.writeFileSync(path.join(esmDir, `v${version}.js`), content);
return;
}

fs.copyFileSync(sourcePath, destPath);

// Update any references to @pgsql/types to use local types
Expand Down Expand Up @@ -162,7 +169,7 @@ const templateVars = generateTemplateVars(config.versions);

// Process each template
const templates = [
{ template: 'index.js.template', output: 'index.js' },
{ template: 'index.js.template', output: 'esm/index.js' },
{ template: 'index.cjs.template', output: 'index.cjs' },
{ template: 'index.d.ts.template', output: 'index.d.ts' }
];
Expand All @@ -180,12 +187,6 @@ templates.forEach(({ template, output }) => {

// Create version-specific export files
config.versions.forEach(version => {
// ESM version
const esmContent = `// Re-export everything from the v${version} parser
export * from './v${version}/index.js';
`;
fs.writeFileSync(path.join(wasmDir, `v${version}.js`), esmContent);

// CJS version
const cjsContent = `// Re-export everything from the v${version} parser
module.exports = require('./v${version}/index.cjs');
Expand Down
2 changes: 1 addition & 1 deletion parser/templates/index.js.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Parser {
}

async _doLoad() {
const module = await import(`./v${this.version}/index.js`);
const module = await import(`./v${this.version}.js`);
this.parser = module;

if (this.parser.loadModule) {
Expand Down
16 changes: 16 additions & 0 deletions parser/test/esm.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';

const { versions } = JSON.parse(readFileSync(new URL('../wasm/build-info.json', import.meta.url), 'utf8'));
Expand All @@ -21,3 +22,18 @@ describe('ESM entry points', () => {
});
}
});

it('imports the root and v18 without module syntax detection', () => {
const flags = process.allowedNodeEnvironmentFlags.has('--experimental-detect-module')
? ['--no-experimental-detect-module']
: [];
execFileSync(process.execPath, [...flags, '--input-type=module', '-e', `
import assert from 'node:assert/strict';
import { Parser } from '@pgsql/parser';
import { loadModule, parseSync } from '@pgsql/parser/v18';

assert.equal((await new Parser().parse('SELECT 1')).stmts.length, 1);
await loadModule();
assert.equal(parseSync('SELECT 1').stmts.length, 1);
`], { cwd: new URL('..', import.meta.url), stdio: 'inherit' });
});
Loading