Skip to content

Commit 37c490c

Browse files
fix: update serialize-javascript
1 parent 207764f commit 37c490c

File tree

11 files changed

+437
-14
lines changed

11 files changed

+437
-14
lines changed

.cspell.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
"filebase",
3737
"toplevel",
3838
"commitlint",
39-
"tapable"
39+
"tapable",
40+
"nocheck"
4041
],
4142
"ignorePaths": [
4243
"CHANGELOG.md",
4344
"package.json",
45+
"src/serialize-javascript.js",
4446
"dist/**",
4547
"**/__snapshots__/**",
4648
"package-lock.json",

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
src/serialize-javascript.js
12
/coverage
23
/dist
34
/node_modules
45
/test/fixtures
5-
CHANGELOG.md
6+
CHANGELOG.md

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import configs from "eslint-config-webpack/configs.js";
33

44
export default defineConfig([
55
{
6+
ignores: ["./src/serialize-javascript.js"],
67
extends: [configs["recommended-dirty"]],
78
},
89
]);

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
testEnvironment: "node",
3+
coveragePathIgnorePatterns: ["src/serialize-javascript.js"],
34
};

package-lock.json

Lines changed: 30 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
"scripts": {
3838
"clean": "del-cli dist types",
3939
"prebuild": "npm run clean",
40+
"build:serialize-javascript": "node ./scripts/copy-serialize-javascript.js",
4041
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
4142
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
4243
"build": "npm-run-all -p \"build:**\"",
4344
"commitlint": "commitlint --from=main",
4445
"security": "npm audit --production",
46+
"lint:serialize-javascript": "node ./scripts/copy-serialize-javascript.js --check",
4547
"lint:prettier": "prettier --list-different .",
4648
"lint:code": "eslint --cache .",
4749
"lint:spelling": "cspell \"**/*.*\"",
@@ -62,7 +64,6 @@
6264
"@jridgewell/trace-mapping": "^0.3.25",
6365
"jest-worker": "^27.4.5",
6466
"schema-utils": "^4.3.0",
65-
"serialize-javascript": "^6.0.2",
6667
"terser": "^5.31.1"
6768
},
6869
"devDependencies": {
@@ -91,6 +92,7 @@
9192
"npm-run-all": "^4.1.5",
9293
"prettier": "^3.6.0",
9394
"prettier-2": "npm:prettier@^2",
95+
"serialize-javascript": "^7.0.4",
9496
"standard-version": "^9.3.1",
9597
"typescript": "^5.9.2",
9698
"uglify-js": "^3.19.3",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
2+
const fs = require("fs").promises;
3+
const path = require("path");
4+
5+
/* eslint-disable no-console */
6+
7+
const randomBytesFallback = `
8+
var g = typeof globalThis !== 'undefined' ? globalThis : global;
9+
var crypto = g.crypto || {};
10+
11+
if (typeof crypto.getRandomValues !== 'function') {
12+
var nodeCrypto = require('crypto');
13+
14+
crypto.getRandomValues = function(typedArray) {
15+
var bytes = nodeCrypto.randomBytes(typedArray.byteLength);
16+
17+
new Uint8Array(
18+
typedArray.buffer,
19+
typedArray.byteOffset,
20+
typedArray.byteLength
21+
).set(bytes);
22+
23+
return typedArray;
24+
};
25+
}
26+
`;
27+
28+
/**
29+
* @param {string} src source path
30+
* @param {string} dest destination path
31+
* @returns {Promise<void>}
32+
*/
33+
async function copyIfChanged(src, dest) {
34+
let srcContent;
35+
36+
try {
37+
srcContent = await fs.readFile(src, "utf8");
38+
srcContent = `// @ts-nocheck\n${randomBytesFallback}${srcContent}`;
39+
} catch (_err) {
40+
srcContent = null;
41+
}
42+
43+
let destContent;
44+
try {
45+
destContent = await fs.readFile(dest, "utf8");
46+
} catch (_err) {
47+
destContent = null;
48+
}
49+
50+
if (
51+
srcContent === null ||
52+
destContent === null ||
53+
srcContent !== destContent
54+
) {
55+
if (process.argv.includes("--check")) {
56+
throw new Error(`Content mismatch between ${src} and ${dest}`);
57+
}
58+
59+
await fs.writeFile(dest, srcContent);
60+
console.log("File copied: content changed.");
61+
} else {
62+
console.log("No copying required: the content is identical.");
63+
}
64+
}
65+
66+
const src = path.resolve(
67+
__dirname,
68+
"../node_modules/serialize-javascript/index.js",
69+
);
70+
const dest = path.resolve(__dirname, "../src/serialize-javascript.js");
71+
72+
copyIfChanged(src, dest);

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ const {
163163
*/
164164

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

168168
/**
169169
* @template [T=import("terser").MinifyOptions]

0 commit comments

Comments
 (0)