Skip to content
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
4 changes: 3 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
"filebase",
"toplevel",
"commitlint",
"tapable"
"tapable",
"nocheck"
],
"ignorePaths": [
"CHANGELOG.md",
"package.json",
"src/serialize-javascript.js",
"dist/**",
"**/__snapshots__/**",
"package-lock.json",
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
src/serialize-javascript.js
/coverage
/dist
/node_modules
/test/fixtures
CHANGELOG.md
CHANGELOG.md
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import configs from "eslint-config-webpack/configs.js";

export default defineConfig([
{
ignores: ["./src/serialize-javascript.js"],
extends: [configs["recommended-dirty"]],
},
]);
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
testEnvironment: "node",
coveragePathIgnorePatterns: ["src/serialize-javascript.js"],
};
36 changes: 30 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
"scripts": {
"clean": "del-cli dist types",
"prebuild": "npm run clean",
"build:serialize-javascript": "node ./scripts/copy-serialize-javascript.js",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
"build": "npm-run-all -p \"build:**\"",
"commitlint": "commitlint --from=main",
"security": "npm audit --production",
"lint:serialize-javascript": "node ./scripts/copy-serialize-javascript.js --check",
"lint:prettier": "prettier --list-different .",
"lint:code": "eslint --cache .",
"lint:spelling": "cspell \"**/*.*\"",
Expand All @@ -62,7 +64,6 @@
"@jridgewell/trace-mapping": "^0.3.25",
"jest-worker": "^27.4.5",
"schema-utils": "^4.3.0",
"serialize-javascript": "^6.0.2",
"terser": "^5.31.1"
},
"devDependencies": {
Expand Down Expand Up @@ -91,6 +92,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^3.6.0",
"prettier-2": "npm:prettier@^2",
"serialize-javascript": "^7.0.4",
"standard-version": "^9.3.1",
"typescript": "^5.9.2",
"uglify-js": "^3.19.3",
Expand Down
72 changes: 72 additions & 0 deletions scripts/copy-serialize-javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const fs = require("fs").promises;
const path = require("path");

/* eslint-disable no-console */

const randomBytesFallback = `
var g = typeof globalThis !== 'undefined' ? globalThis : global;
var crypto = g.crypto || {};
if (typeof crypto.getRandomValues !== 'function') {
var nodeCrypto = require('crypto');
crypto.getRandomValues = function(typedArray) {
var bytes = nodeCrypto.randomBytes(typedArray.byteLength);
new Uint8Array(
typedArray.buffer,
typedArray.byteOffset,
typedArray.byteLength
).set(bytes);
return typedArray;
};
}
`;

/**
* @param {string} src source path
* @param {string} dest destination path
* @returns {Promise<void>}
*/
async function copyIfChanged(src, dest) {
let srcContent;

try {
srcContent = await fs.readFile(src, "utf8");
srcContent = `// @ts-nocheck\n${randomBytesFallback}${srcContent}`;
} catch (_err) {
srcContent = null;
}

let destContent;
try {
destContent = await fs.readFile(dest, "utf8");
} catch (_err) {
destContent = null;
}

if (
srcContent === null ||
destContent === null ||
srcContent !== destContent
) {
if (process.argv.includes("--check")) {
throw new Error(`Content mismatch between ${src} and ${dest}`);
}

await fs.writeFile(dest, srcContent);
console.log("File copied: content changed.");
} else {
console.log("No copying required: the content is identical.");
}
}

const src = path.resolve(
__dirname,
"../node_modules/serialize-javascript/index.js",
);
const dest = path.resolve(__dirname, "../src/serialize-javascript.js");

copyIfChanged(src, dest);
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const {
*/

const getTraceMapping = memoize(() => require("@jridgewell/trace-mapping"));
const getSerializeJavascript = memoize(() => require("serialize-javascript"));
const getSerializeJavascript = memoize(() => require("./serialize-javascript"));

/**
* @template [T=import("terser").MinifyOptions]
Expand Down
Loading
Loading