Skip to content

Commit

Permalink
fix: diff-sequences export not playing nice with Vite
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Mar 7, 2022
1 parent b51f39e commit f87a7b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"lint": "prettier --check --plugin-search-dir=. .",
"__lint": "prettier --check --plugin-search-dir=. . && eslint --cache --ignore-path .gitignore .",
"format": "prettier --write --plugin-search-dir=. .",
"prepare": "husky install && cd tools && node generateEmbeddedAceJsonWorker.js && cd ..",
"prepare": "husky install && cd tools && node generateEmbeddedAceJsonWorker.js && node generateDiffSequenceEsm.js && cd ..",
"release": "npm run lint && npm test && standard-version && npm run package && git push && git push --tag && npm publish package/"
},
"dependencies": {
Expand Down
6 changes: 2 additions & 4 deletions src/lib/logic/sort.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import diffSequencesExport from 'diff-sequences'
import diffSequence from '../generated/diffSequence.js'
import { compileJSONPointer, getIn, setIn } from 'immutable-json-patch'
import { first, initial, isEmpty, isEqual, last } from 'lodash-es'
import naturalCompare from 'natural-compare-lite'
import { parseJSONPointerWithArrayIndices } from '../utils/jsonPointer.js'

const diffSequences = diffSequencesExport.default || diffSequencesExport

export function caseInsensitiveNaturalCompare(a, b) {
const aLower = typeof a === 'string' ? a.toLowerCase() : a
const bLower = typeof b === 'string' ? b.toLowerCase() : b
Expand Down Expand Up @@ -166,7 +164,7 @@ export function sortOperationsMoveAdvanced(array, comparator) {
return aIndex === sortedIndices[bIndex]
}

diffSequences(size, size, isCommon, foundSubsequence)
diffSequence(size, size, isCommon, foundSubsequence)
foundSubsequence(0, size, size)

// every move will change the actual indices, so we've to adjust for that
Expand Down
30 changes: 30 additions & 0 deletions tools/generateDiffSequenceEsm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readFileSync, writeFileSync } from 'fs'

// The original file contains an export that doesn't play nice with Vite
// This script copies the source code and turns it into regular a ES module
// The original export looks like:
//
// Object.defineProperty(exports, '__esModule', {
// value: true
// });
// exports.default = diffSequence;
//
// The ESM export looks like:
//
// export default diffSequence;

const commonjsFile = '../node_modules/diff-sequences/build/index.js'
const esmFile = '../src/lib/generated/diffSequence.js'
const commonjsCode = String(readFileSync(commonjsFile))

const commentsStart = commonjsCode.indexOf('/**')

const esmHeader = `// esm version of diff-sequences
// generated by ./tools/generateDiffSequenceEsm.js
export default diffSequence;
`

const esmCode = esmHeader + commonjsCode.slice(commentsStart)

writeFileSync(esmFile, esmCode)

0 comments on commit f87a7b3

Please sign in to comment.