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

Fix diff package import #12462

Merged
merged 4 commits into from Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions scripts/build/config.mjs
Expand Up @@ -23,6 +23,20 @@ const { require } = createEsmUtils(import.meta);
* @property {string[]} ignore - paths of CJS modules to ignore
*/

/*
`diff` use deprecated folder mapping "./" in the "exports" field,
so we can't `require("diff/lib/diff/array.js")` directory.
To reduce the bundle size

We can switch to deep require once https://github.com/kpdecker/jsdiff/pull/351 get merged
*/
const replaceDiffPackageEntry = (file) => ({
[require.resolve("diff")]: path.join(
path.dirname(require.resolve("diff/package.json")),
file
),
});

/** @type {Bundle[]} */
const parsers = [
{
Expand Down Expand Up @@ -229,6 +243,9 @@ const coreBundles = [
replacement: "const utilInspect = require('util').inspect",
},
],
replaceModule: {
...replaceDiffPackageEntry("lib/diff/array.js"),
},
fisker marked this conversation as resolved.
Show resolved Hide resolved
},
{
input: "src/document/index.js",
Expand All @@ -248,6 +265,7 @@ const coreBundles = [
),
[createRequire(require.resolve("vnopts")).resolve("chalk")]:
require.resolve("./shims/chalk.cjs"),
...replaceDiffPackageEntry("lib/diff/array.js"),
},
},
{
Expand All @@ -260,6 +278,9 @@ const coreBundles = [
input: "src/cli/index.js",
output: "cli.js",
external: ["benchmark"],
replaceModule: {
...replaceDiffPackageEntry("lib/patch/create.js"),
},
},
{
input: "src/common/third-party.js",
Expand Down
15 changes: 3 additions & 12 deletions src/cli/format.js
Expand Up @@ -17,18 +17,9 @@ const getOptionsForFile = require("./options/get-options-for-file.js");
const isTTY = require("./is-tty.js");

function diff(a, b) {
// Use `diff/lib/patch/create.js` instead of `diff` to reduce bundle size
return require("diff/lib/patch/create.js").createTwoFilesPatch(
"",
"",
a,
b,
"",
"",
{
context: 2,
}
);
return require("diff").createTwoFilesPatch("", "", a, b, "", "", {
context: 2,
});
}

function handleError(context, filename, error, printedFilename) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/core.js
@@ -1,7 +1,6 @@
"use strict";

// Use `diff/lib/diff/array.js` instead of `diff` to reduce bundle size
const { diffArrays } = require("diff/lib/diff/array.js");
const { diffArrays } = require("diff");

const {
printer: { printDocToString },
Expand Down