Skip to content

Commit

Permalink
Fix yet another issue with TypeScript exports
Browse files Browse the repository at this point in the history
See [#420](#420).
  • Loading branch information
EvanHahn committed Apr 11, 2023
1 parent 68202a8 commit f8ae480
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
63 changes: 34 additions & 29 deletions build/build-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -148,30 +151,41 @@ function rollupForJs(entry: string): Promise<RollupBuild> {
}

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,
external: ["http"],
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({
Expand Down Expand Up @@ -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,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion test/project-setups/typescript-commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion test/project-setups/typescript-esnext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion test/project-setups/typescript-nodenext-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit f8ae480

Please sign in to comment.