From 129194ddd8a7132422622cb1f90ebd21b160941e Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Mon, 23 Nov 2026 08:05:20 +0000 Subject: [PATCH] fix(parser): declare an explicit ESM module scope --- parser/package.json | 44 +++++++++++++++--------------- parser/scripts/prepare.js | 27 +++++++++--------- parser/templates/index.js.template | 2 +- parser/test/esm.test.mjs | 16 +++++++++++ 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/parser/package.json b/parser/package.json index 3fbb53c2..349a1ba0 100644 --- a/parser/package.json +++ b/parser/package.json @@ -4,43 +4,43 @@ "author": "Constructive ", "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": [ diff --git a/parser/scripts/prepare.js b/parser/scripts/prepare.js index 9c1ac240..24a75ecf 100644 --- a/parser/scripts/prepare.js +++ b/parser/scripts/prepare.js @@ -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) { @@ -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'); @@ -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 @@ -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' } ]; @@ -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'); diff --git a/parser/templates/index.js.template b/parser/templates/index.js.template index 6716d1eb..4e3c348c 100644 --- a/parser/templates/index.js.template +++ b/parser/templates/index.js.template @@ -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) { diff --git a/parser/test/esm.test.mjs b/parser/test/esm.test.mjs index c5241dfe..3ed35b13 100644 --- a/parser/test/esm.test.mjs +++ b/parser/test/esm.test.mjs @@ -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')); @@ -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' }); +});