Skip to content

Commit

Permalink
Reduce bundled size of diff package (prettier#12160)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 27, 2022
1 parent 7e73f1c commit 7d7f7e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/cli/format.js
Expand Up @@ -16,9 +16,18 @@ const getOptionsForFile = require("./options/get-options-for-file.js");
const isTTY = require("./is-tty.js");

function diff(a, b) {
return require("diff").createTwoFilesPatch("", "", a, b, "", "", {
context: 2,
});
// 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,
}
);
}

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

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

const {
printer: { printDocToString },
Expand Down Expand Up @@ -118,7 +119,7 @@ function coreFormat(originalText, opts, addAlignmentSize = 0) {

const newCursorNodeCharArray = [...newCursorNodeText];

const cursorNodeDiff = diff.diffArrays(
const cursorNodeDiff = diffArrays(
oldCursorNodeCharArray,
newCursorNodeCharArray
);
Expand Down

0 comments on commit 7d7f7e9

Please sign in to comment.