Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CJS/ESM interoperability support #345

Merged
merged 6 commits into from
Jan 22, 2022
Merged
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
8 changes: 4 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
!README.md
!SECURITY.md

!dist/index.cjs
!dist/index.d.ts
!dist/index.js
!dist/middlewares/**/*.d.ts
!dist/cjs/index.js
!dist/cjs/package.json
!dist/esm/index.js
!dist/types/**/*.d.ts
57 changes: 40 additions & 17 deletions bin/build-helmet.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,61 @@
#!/usr/bin/env node
import { promises as fs } from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
import rollupTypescript from "@rollup/plugin-typescript";
import { writeRollup, withCommonJsFile } from "./helpers.js";
import {
withCommonJsFile,
withEsmFile,
writeRollup
} from "./helpers.js";

const thisPath = fileURLToPath(import.meta.url);
const rootPath = path.join(path.dirname(thisPath), "..");
const distPath = path.join(rootPath, "dist");
const esmSourcePath = path.join(rootPath, "index.ts");
const esmDistPath = path.join(rootPath, "dist", "index.js");
const commonJsDistPath = path.join(rootPath, "dist", "index.cjs");
const esmDistDir = path.join(distPath, "esm");
const esmDistPath = path.join(esmDistDir, "index.js");
const commonJsDistDir = path.join(distPath, "cjs");
const commonJsDistPath = path.join(commonJsDistDir, "index.js");
const typesDistDir = path.join(distPath, "types");

const compileEsm = () =>
writeRollup(
{
input: esmSourcePath,
plugins: [rollupTypescript({ tsconfig: "./tsconfig-esm.json" })],
},
{ file: esmDistPath }
);
withEsmFile(esmSourcePath, async (esmTempPath) => {
await writeRollup(
{
input: esmTempPath,
plugins: [rollupTypescript({ tsconfig: "./tsconfig-esm.json" })],
},
{ file: path.join(distPath, "index.js") }
);

await fs.mkdir(esmDistDir);
await fs.rename(path.join(distPath, "index.js"), esmDistPath);
await fs.rename(path.join(typesDistDir, "tmp-esm-index.d.ts"), path.join(typesDistDir, "index.d.ts"));
});

const compileCommonjs = () =>
withCommonJsFile(esmSourcePath, (commonJsSourcePath) =>
writeRollup(
withCommonJsFile(esmSourcePath, async (commonJsTempPath) => {
await writeRollup(
{
input: commonJsSourcePath,
input: commonJsTempPath,
plugins: [rollupTypescript({ tsconfig: "./tsconfig-commonjs.json" })],
},
{
exports: "default",
exports: "named",
file: commonJsDistPath,
format: "cjs",
}
)
);
});

const cjsPackageJson = JSON.stringify({
type: "commonjs",
});

await fs.writeFile(
path.join(commonJsDistDir, "package.json"),
cjsPackageJson
);
});

async function main() {
await compileEsm();
Expand Down
53 changes: 46 additions & 7 deletions bin/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,62 @@ export async function writeRollup(inputOptions, outputOptions) {
await bundle.close();
}

export async function withEsmFile(esmSourcePath, fn) {
const esmTempPath = path.join(
path.dirname(esmSourcePath),
"tmp-esm-index.ts"
);

const lines = (await fs.readFile(esmSourcePath, "utf8")).split(/\r?\n/);
const startLineCjsFence = lines.findIndex((line) =>
line.includes("!helmet-start-of-commonjs-exports")
);
const endLineCjsFence = lines.findIndex((line) =>
line.includes("!helmet-end-of-commonjs-exports")
);
const lineCount = endLineCjsFence - startLineCjsFence + 2;
lines.splice(startLineCjsFence, lineCount);

const startLineEsmFence = lines.findIndex((line) =>
line.includes("!helmet-start-of-esm-exports")
);
lines.splice(startLineEsmFence, 1);

const endLineEsmFence = lines.findIndex((line) =>
line.includes("!helmet-end-of-esm-exports")
);
lines.splice(endLineEsmFence, 1);

try {
await fs.writeFile(esmTempPath, lines.join("\n"));

await fn(esmTempPath);
} finally {
await fs.unlink(esmTempPath);
}
}

export async function withCommonJsFile(esmSourcePath, fn) {
const commonJsSourcePath = path.join(
const commonJsTempPath = path.join(
path.dirname(esmSourcePath),
"tmp-commonjs-index.ts"
);

const lines = (await fs.readFile(esmSourcePath, "utf8")).split(/\r?\n/);
const resultLines = lines.slice(
0,
lines.findIndex((line) => line.includes("!helmet-end-of-commonjs"))
const startLine = lines.findIndex((line) =>
line.includes("!helmet-start-of-commonjs-exports")
);
const endLine = lines.findIndex((line) =>
line.includes("!helmet-end-of-commonjs-exports")
);
lines.splice(startLine, 1);
const resultLines = lines.slice(0, endLine - 1);

try {
await fs.writeFile(commonJsSourcePath, resultLines.join("\n"));
await fs.writeFile(commonJsTempPath, resultLines.join("\n"));

await fn(commonJsSourcePath);
await fn(commonJsTempPath);
} finally {
await fs.unlink(commonJsSourcePath);
await fs.unlink(commonJsTempPath);
}
}
7 changes: 6 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,12 @@ const helmet: Helmet = Object.assign(

export default helmet;

// !helmet-end-of-commonjs
// !helmet-start-of-commonjs-exports
exports = helmet;
module.exports = helmet;
// !helmet-end-of-commonjs-exports
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved

// !helmet-start-of-esm-exports
export {
contentSecurityPolicy,
crossOriginEmbedderPolicy,
Expand All @@ -292,3 +296,4 @@ export {
xPoweredBy as hidePoweredBy,
xXssProtection as xssFilter,
};
// !helmet-end-of-esm-exports
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@
},
"license": "MIT",
"type": "module",
"types": "./dist/index.d.ts",
"main": "./dist/index.cjs",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
MitchellCash marked this conversation as resolved.
Show resolved Hide resolved
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts"
}
}
}
7 changes: 3 additions & 4 deletions tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "./tsconfig",
"exclude": ["dist"],
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"declarationDir": "."
}
"declarationDir": "types"
},
"include": ["tmp-esm-index.ts"]
}
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"noUnusedParameters": true,
"noUncheckedIndexedAccess": true,
"strict": true,
"target": "es6"
}
"target": "es6",
"outDir": "."
},
"exclude": ["node_modules", "bin", "dist"]
}