dmsnell / diff-match-patch

Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.
Apache License 2.0
29 stars 6 forks source link

Tracking Issue: Generate and publish `npm` package of JavaScript library. #3

Closed dmsnell closed 4 months ago

dmsnell commented 9 months ago

There is no official npm package for this library. There are some which package it separately. It should build an artifact that be published directly to npm.

TheSpyder commented 9 months ago

I'm not sure how exactly google publishes from this repo to NPM, but here are the imports I use across two files import { Diff, DIFF_DELETE, DIFF_INSERT } from 'diff-match-patch'; import { Diff, DIFF_EQUAL, diff_match_patch } from 'diff-match-patch';

Which seems to be all of the exports from index.js in the NPM package. We also use @types/diff-match-patch, but once the NPM publishing is sorted I can probably PR shipping types within the module.

dmsnell commented 9 months ago

@TheSpyder are you using a fork's npm package? I believe one does exist but it's unlinked from diff-match-patch itself. Or have you created your own package?

TheSpyder commented 9 months ago

Oh wow, I don't think my team realised the diff-match-patch NPM package was itself a fork 🫠

I hadn't quite figured out how I wanted to deploy the fork internally yet, it makes sense now why it seemed so difficult 😂

TheSpyder commented 9 months ago

This might be a more interesting migration than I expected. The NPM package made some slight tweaks to the API, such as https://github.com/JackuB/diff-match-patch/pull/18

And given that I use it as ESM, perhaps I'll take the ideas in https://github.com/JackuB/diff-match-patch/pull/20 and PR those too at some point.

TheSpyder commented 9 months ago

I'm tracking through the history and it looks like the JackuB fork was inherited from a previous owner. But in the end the sum of it's changes are not very much. Comparing to before your surrogate pairs fix: diff.patch

It replaces the global export with module exports:

module.exports = diff_match_patch;
module.exports['diff_match_patch'] = diff_match_patch;
module.exports['DIFF_DELETE'] = DIFF_DELETE;
module.exports['DIFF_INSERT'] = DIFF_INSERT;
module.exports['DIFF_EQUAL'] = DIFF_EQUAL;

And replaces the entire diff_match_patch.Diff array-like class with a simple array to support JavaScript destructuring:

diff_match_patch.Diff = function(op, text) {
  return [op, text];
};

I think these are very sensible changes, I'll include them in a PR to add the other NPM package boilerplate unless you have strong opposition.