Skip to content

Commit

Permalink
Fix diff package import (#12462)
Browse files Browse the repository at this point in the history
Co-authored-by: Sosuke Suzuki <aosukeke@gmail.com>
  • Loading branch information
fisker and sosukesuzuki committed Mar 15, 2022
1 parent 935d767 commit cae1951
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
17 changes: 17 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,7 @@ const coreBundles = [
replacement: "const utilInspect = require('util').inspect",
},
],
replaceModule: replaceDiffPackageEntry("lib/diff/array.js"),
},
{
input: "src/document/index.js",
Expand All @@ -248,6 +263,7 @@ const coreBundles = [
),
[createRequire(require.resolve("vnopts")).resolve("chalk")]:
require.resolve("./shims/chalk.cjs"),
...replaceDiffPackageEntry("lib/diff/array.js"),
},
},
{
Expand All @@ -260,6 +276,7 @@ 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

0 comments on commit cae1951

Please sign in to comment.