diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c38740..debd2ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixed + +- Fixed yet another issue with TypeScript exports. See [#420](https://github.com/helmetjs/helmet/pull/418) + ## 6.1.4 - 2023-04-10 ### Fixed diff --git a/build/build-package.ts b/build/build-package.ts index 65f03f4..0a5bbe1 100644 --- a/build/build-package.ts +++ b/build/build-package.ts @@ -70,12 +70,15 @@ export async function buildAndPack( await Promise.all([ buildCjs({ entry, distDir }), ...(esm ? [buildMjs({ entry, distDir })] : []), - buildTypes({ entry, distDir }), + buildTypes({ esm, entry, distDir }), buildPackageJson({ esm, packageOverrides, distDir }), copyStaticFiles({ filesToCopy, distDir }), ]); - await prePackCrush(distDir); + // TODO: Restore this + if (Math.random() === 0) { + await prePackCrush(distDir); + } const npmPackedTarball = await pack(distDir); @@ -148,12 +151,11 @@ function rollupForJs(entry: string): Promise { } async function buildTypes({ + esm, entry, distDir, -}: Readonly<{ entry: string; distDir: string }>) { - const outputPath = path.join(distDir, "index.d.ts"); - - console.log(`Building ${outputPath}...`); +}: Readonly<{ esm: boolean; entry: string; distDir: string }>) { + console.log("Building types..."); const bundle = await rollup({ input: entry, @@ -161,17 +163,29 @@ async function buildTypes({ plugins: [rollupDts()], }); - await bundle.write({ - file: outputPath, - format: "esm", - // Despite this being an ES module, some TypeScript setups require this. - // (This doesn't remove the `export default` from the final file.) - outro: "export = helmet", - }); + await Promise.all([ + (async () => { + const cjsPath = path.join(distDir, "index.d.cts"); + await bundle.write({ + file: cjsPath, + format: "commonjs", + }); + console.log(`Built ${cjsPath}.`); + })(), + (async () => { + if (!esm) { + return; + } + const esmPath = path.join(distDir, "index.d.mts"); + await bundle.write({ + file: esmPath, + format: "esm", + }); + console.log(`Built ${esmPath}.`); + })(), + ]); await bundle.close(); - - console.log(`Built ${outputPath}.`); } async function buildPackageJson({ @@ -212,25 +226,16 @@ async function buildPackageJson({ }, exports: { - ".": { - ...(esm - ? { - import: { - types: "./index.d.ts", - default: "./index.mjs", - }, - } - : {}), - require: { - types: "./index.d.ts", - default: "./index.cjs", - }, - }, + ...(esm ? { import: "./index.mjs" } : {}), + require: "./index.cjs", }, // All supported versions of Node handle `exports`, but some build tools // still use `main`, so we keep it around. main: "./index.cjs", + // Support old TypeScript versions. + types: "./index.d.cts", + ...packageOverrides, }; diff --git a/test/project-setups/typescript-commonjs-nodenext-moduleResolution/package.json b/test/project-setups/typescript-commonjs-nodenext-moduleResolution/package.json index dcb891c..c7fcc17 100644 --- a/test/project-setups/typescript-commonjs-nodenext-moduleResolution/package.json +++ b/test/project-setups/typescript-commonjs-nodenext-moduleResolution/package.json @@ -2,6 +2,6 @@ "private": true, "type": "commonjs", "scripts": { - "helmet:test": "ts-node --compilerOptions '{\"module\": \"commonjs\", \"moduleResolution\": \"nodenext\", \"esModuleInterop\": true}' test.ts" + "helmet:test": "ts-node --compilerOptions '{\"module\": \"commonjs\", \"moduleResolution\": \"nodenext\", \"esModuleInterop\": true}' --skipProject test.ts" } } diff --git a/test/project-setups/typescript-commonjs/package.json b/test/project-setups/typescript-commonjs/package.json index 892dbaa..e90107a 100644 --- a/test/project-setups/typescript-commonjs/package.json +++ b/test/project-setups/typescript-commonjs/package.json @@ -2,6 +2,6 @@ "private": true, "type": "commonjs", "scripts": { - "helmet:test": "ts-node --compilerOptions '{\"module\": \"commonjs\", \"moduleResolution\": \"node\", \"esModuleInterop\": true}' test.ts" + "helmet:test": "ts-node --compilerOptions '{\"module\": \"commonjs\", \"moduleResolution\": \"node\", \"esModuleInterop\": true}' --skipProject test.ts" } } diff --git a/test/project-setups/typescript-esnext/package.json b/test/project-setups/typescript-esnext/package.json index e8a4c87..4466dbc 100644 --- a/test/project-setups/typescript-esnext/package.json +++ b/test/project-setups/typescript-esnext/package.json @@ -2,6 +2,6 @@ "private": true, "type": "module", "scripts": { - "helmet:test": "ts-node-esm --compilerOptions '{\"module\": \"esnext\", \"moduleResolution\": \"node\"}' test.ts" + "helmet:test": "ts-node-esm --compilerOptions '{\"module\": \"esnext\", \"moduleResolution\": \"node\"}' --skipProject test.ts" } } diff --git a/test/project-setups/typescript-nodenext-commonjs/package.json b/test/project-setups/typescript-nodenext-commonjs/package.json index 18c6d7b..4e8603d 100644 --- a/test/project-setups/typescript-nodenext-commonjs/package.json +++ b/test/project-setups/typescript-nodenext-commonjs/package.json @@ -2,6 +2,6 @@ "private": true, "type": "commonjs", "scripts": { - "helmet:test": "ts-node-esm --compilerOptions '{\"module\": \"nodenext\", \"moduleResolution\": \"nodenext\"}' test.ts" + "helmet:test": "ts-node-esm --compilerOptions '{\"module\": \"nodenext\", \"moduleResolution\": \"nodenext\"}' --skipProject test.ts" } } diff --git a/test/project-setups/typescript-nodenext-esm/package.json b/test/project-setups/typescript-nodenext-esm/package.json index e91c751..043ce77 100644 --- a/test/project-setups/typescript-nodenext-esm/package.json +++ b/test/project-setups/typescript-nodenext-esm/package.json @@ -2,6 +2,6 @@ "private": true, "type": "module", "scripts": { - "helmet:test": "ts-node-esm --compilerOptions '{\"module\": \"nodenext\", \"moduleResolution\": \"nodenext\"}' test.ts" + "helmet:test": "ts-node-esm --compilerOptions '{\"module\": \"nodenext\", \"moduleResolution\": \"nodenext\"}' --skipProject test.ts" } }