Skip to content

Commit af7b881

Browse files
authored
fix: turn diff-match-patch into an optional peer dependency (#110)
1 parent b65efa2 commit af7b881

File tree

9 files changed

+56
-18
lines changed

9 files changed

+56
-18
lines changed

.changeset/nasty-poems-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix: turn diff-match-patch into an optional peer dependency to reduce footprint

packages/playground/windicss/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@sveltejs/vite-plugin-svelte": "workspace:*",
1515
"svelte": "^3.38.3",
1616
"vite": "^2.4.2",
17-
"vite-plugin-windicss": "^1.2.4"
17+
"vite-plugin-windicss": "^1.2.4",
18+
"diff-match-patch": "^1.0.5"
1819
}
1920
}

packages/playground/windicss/vite.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ const vitePluginWindicss = require('vite-plugin-windicss').default;
55
module.exports = defineConfig(({ command, mode }) => {
66
const isProduction = mode === 'production';
77
return {
8-
plugins: [svelte(), vitePluginWindicss()],
8+
plugins: [
9+
svelte({ experimental: { generateMissingPreprocessorSourcemaps: true } }),
10+
vitePluginWindicss()
11+
],
912
build: {
1013
minify: isProduction
1114
}

packages/vite-plugin-svelte/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,26 @@
4646
"dependencies": {
4747
"@rollup/pluginutils": "^4.1.1",
4848
"debug": "^4.3.2",
49-
"diff-match-patch": "^1.0.5",
5049
"kleur": "^4.1.4",
5150
"magic-string": "^0.25.7",
5251
"require-relative": "^0.8.7",
5352
"svelte-hmr": "^0.14.6"
5453
},
54+
5555
"peerDependencies": {
56+
"diff-match-patch": "^1.0.5",
5657
"svelte": "^3.34.0",
5758
"vite": "^2.3.7"
5859
},
60+
"peerDependenciesMeta": {
61+
"diff-match-patch": {
62+
"optional": true
63+
}
64+
},
5965
"devDependencies": {
6066
"@types/debug": "^4.1.6",
6167
"@types/diff-match-patch": "^1.0.32",
68+
"diff-match-patch": "^1.0.5",
6269
"esbuild": "^0.12.15",
6370
"rollup": "^2.53.2",
6471
"svelte": "^3.38.3",

packages/vite-plugin-svelte/src/utils/__tests__/sourcemap.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { buildMagicString, buildSourceMap } from '../sourcemap';
22

33
describe('sourcemap', () => {
44
describe('buildMagicString', () => {
5-
it('should return a valid magic string', () => {
5+
it('should return a valid magic string', async () => {
66
const from = 'h1{color: blue}\nh2{color: green}\nh3{color: red}\n';
77
const to = 'h1{color: blue}\ndiv{color: white}\nh3{color: red}\nh2{color: green}\n';
8-
const m = buildMagicString(from, to);
8+
const m = await buildMagicString(from, to);
99
expect(m).toBeDefined();
1010
expect(m.original).toBe(from);
1111
expect(m.toString()).toBe(to);
1212
});
1313
});
1414
describe('buildSourceMap', () => {
15-
it('should return a map with mappings and filename', () => {
16-
const map = buildSourceMap('foo', 'bar', 'foo.txt');
15+
it('should return a map with mappings and filename', async () => {
16+
const map = await buildSourceMap('foo', 'bar', 'foo.txt');
1717
expect(map).toBeDefined();
1818
expect(map.mappings).toBeDefined();
1919
expect(map.mappings[0]).toBeDefined();

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ export interface ExperimentalOptions {
295295

296296
/**
297297
* wrap all preprocessors in with a function that adds a sourcemap to the output if missing
298+
*
299+
* to use this option you have to install "diff-match-patch"
298300
*/
299301
generateMissingPreprocessorSourcemaps?: boolean;
300302
}

packages/vite-plugin-svelte/src/utils/preprocess.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,14 @@ function validateSourceMapOutputWrapper(group: PreprocessorGroup, i: number): Pr
204204
}
205205
if (invalidMap) {
206206
try {
207-
const map = buildSourceMap(options.content, result.code, options.filename);
208-
log.warn.once('adding generated sourcemap to preprocesor result');
209-
result.map = map;
207+
const map = await buildSourceMap(options.content, result.code, options.filename);
208+
if (map) {
209+
log.debug.enabled &&
210+
log.debug(
211+
`adding generated sourcemap to preprocesor result for ${options.filename}`
212+
);
213+
result.map = map;
214+
}
210215
} catch (e) {
211216
log.error(`failed to build sourcemap`, e);
212217
}

packages/vite-plugin-svelte/src/utils/sourcemap.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import MagicString, { MagicStringOptions } from 'magic-string';
2-
import { diff_match_patch, DIFF_DELETE, DIFF_INSERT } from 'diff-match-patch';
2+
import { log } from './log';
33

4-
export function buildMagicString(
4+
export async function buildMagicString(
55
from: string,
66
to: string,
77
options?: MagicStringOptions
8-
): MagicString {
8+
): Promise<MagicString | null> {
9+
let diff_match_patch, DIFF_DELETE: number, DIFF_INSERT: number;
10+
try {
11+
const dmpPkg = await import('diff-match-patch');
12+
diff_match_patch = dmpPkg.diff_match_patch;
13+
DIFF_INSERT = dmpPkg.DIFF_INSERT;
14+
DIFF_DELETE = dmpPkg.DIFF_DELETE;
15+
} catch (e) {
16+
log.error.once(
17+
'Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.'
18+
);
19+
return null;
20+
}
21+
922
const dmp = new diff_match_patch();
1023
const diffs = dmp.diff_main(from, to);
1124
dmp.diff_cleanupSemantic(diffs);
@@ -38,8 +51,8 @@ export function buildMagicString(
3851
return m;
3952
}
4053

41-
export function buildSourceMap(from: string, to: string, filename?: string) {
54+
export async function buildSourceMap(from: string, to: string, filename?: string) {
4255
// @ts-ignore
43-
const m = buildMagicString(from, to, { filename });
44-
return m.generateDecodedMap({ source: filename, hires: true, includeContent: false });
56+
const m = await buildMagicString(from, to, { filename });
57+
return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
4558
}

pnpm-lock.yaml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)