Reduce size of standalone.js (#13543)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-10-04 18:09:24 +08:00
1 parent 933834984e
commit 572452acf8
2 files changed
+37 -3

No files matched your search

+28
View File
@@ -61,6 +61,25 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
return text.slice(0, index);
},
},
/*
`jest-docblock` try to detect new line in code, and it will fallback to `os.EOL`,
We already replaced line end to `\n` before calling it
*/
{
module: require.resolve("jest-docblock"),
process: (text) =>
text
.replace(
"const line = (0, _detectNewline().default)(docblock) || _os().EOL;",
'const line = "\\n"'
)
.replace(
"const line = (0, _detectNewline().default)(comments) || _os().EOL;",
'const line = "\\n"'
)
.replace(/\nfunction _os().*?\n}/s, "")
.replace(/\nfunction _detectNewline().*?\n}/s, ""),
},
];
const define = {
@@ -120,6 +139,15 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
find: 'import { createRequire } from "node:module";',
replacement: "",
},
// Prevent `esbuildPluginNodeModulePolyfills` shim `assert`, which will include a big `buffer` shim
// TODO[@fisker]: Find a better way
{
module: "*",
find: ' from "node:assert";',
replacement: ` from ${JSON.stringify(
path.join(dirname, "./shims/assert.js")
)};`,
},
// Prevent `esbuildPluginNodeModulePolyfills` include shim for this module
{
module: "assert",
+9 -3
View File
@@ -23,7 +23,7 @@ function insertPragma(originalText) {
const { shebang, text, pragmas, comments } = parseDocBlock(originalText);
const strippedText = strip(text);
const docBlock = print({
let docBlock = print({
pragmas: {
format: "",
...pragmas,
@@ -31,10 +31,16 @@ function insertPragma(originalText) {
comments: comments.trimStart(),
});
// normalise newlines (mitigate use of os.EOL by jest-docblock)
// Only needed in development version on Windows,
// bundler will hack `jest-docblock` enforce it to use `\n` in production
if (process.env.NODE_ENV !== "production") {
docBlock = normalizeEndOfLine(docBlock);
}
return (
(shebang ? `${shebang}\n` : "") +
// normalise newlines (mitigate use of os.EOL by jest-docblock)
normalizeEndOfLine(docBlock) +
docBlock +
(strippedText.startsWith("\n") ? "\n" : "\n\n") +
strippedText
);