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 1 commit
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/.idea/
MitchellCash marked this conversation as resolved.
Show resolved Hide resolved
/node_modules/

/dist/
/package/
MitchellCash marked this conversation as resolved.
Show resolved Hide resolved
/tmp-commonjs-index.ts

/coverage/
41 changes: 28 additions & 13 deletions bin/build-helmet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,51 @@
import * as path from "path";
import { fileURLToPath } from "url";
import rollupTypescript from "@rollup/plugin-typescript";
import { writeRollup, withCommonJsFile } from "./helpers.js";
import {
writeRollup,
withCommonJsFile,
withEsmFile,
renameFile,
finalizeCommonJs,
} from "./helpers.js";

const thisPath = fileURLToPath(import.meta.url);
const rootPath = path.join(path.dirname(thisPath), "..");
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(rootPath, "dist", "esm");
const esmDistPath = path.join(esmDistDir, "index.js");
const commonJsDistDir = path.join(rootPath, "dist", "cjs");
const commonJsDistPath = path.join(commonJsDistDir, "index.js");

const compileEsm = () =>
writeRollup(
{
input: esmSourcePath,
plugins: [rollupTypescript({ tsconfig: "./tsconfig-esm.json" })],
},
{ file: esmDistPath }
withEsmFile(esmSourcePath, (esmTempPath) =>
writeRollup(
{
input: esmTempPath,
plugins: [rollupTypescript({ tsconfig: "./tsconfig-esm.json" })],
},
{ file: esmDistPath }
).then(() =>
renameFile(
path.join(esmDistDir, "tmp-esm-index.d.ts"),
path.join(esmDistDir, "index.d.ts")
)
)
);

const compileCommonjs = () =>
withCommonJsFile(esmSourcePath, (commonJsSourcePath) =>
withCommonJsFile(esmSourcePath, (commonJsTempPath) =>
writeRollup(
{
input: commonJsSourcePath,
input: commonJsTempPath,
plugins: [rollupTypescript({ tsconfig: "./tsconfig-commonjs.json" })],
},
{
exports: "default",
exports: "named",
file: commonJsDistPath,
format: "cjs",
}
)
).then(async () => await finalizeCommonJs(commonJsDistDir, esmDistDir))
MitchellCash marked this conversation as resolved.
Show resolved Hide resolved
);

async function main() {
Expand Down
65 changes: 58 additions & 7 deletions bin/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,74 @@ 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 startLine = lines.findIndex((line) =>
line.includes("!helmet-start-of-commonjs-exports")
);
const endLine = lines.findIndex((line) =>
line.includes("!helmet-end-of-commonjs-exports")
);
const lineCount = endLine - startLine + 1;
lines.splice(startLine, lineCount);

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);

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);
}
}

export function renameFile(oldPath, newPath) {
fs.rename(oldPath, newPath),
(error) => {
if (error) console.error(error);
};
}

export async function finalizeCommonJs(commonJsDistDir, esmDistDir) {
const cjsPackageJson = JSON.stringify({
type: "commonjs",
});

await fs.writeFile(
path.join(commonJsDistDir, "package.json"),
cjsPackageJson
);
await fs.copyFile(
path.join(esmDistDir, "index.d.ts"),
path.join(commonJsDistDir, "index.d.ts")
);
}
5 changes: 4 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ 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

export {
contentSecurityPolicy,
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
},
"license": "MIT",
"type": "module",
"types": "./dist/index.d.ts",
"main": "./dist/index.cjs",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"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"
}
}
},
"files": [
"dist"
]
MitchellCash marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 2 additions & 3 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": "."
}
},
"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"]
}