Skip to content

fix: turn diff-match-patch into an optional peer dependency #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-poems-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

fix: turn diff-match-patch into an optional peer dependency to reduce footprint
3 changes: 2 additions & 1 deletion packages/playground/windicss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.38.3",
"vite": "^2.4.2",
"vite-plugin-windicss": "^1.2.4"
"vite-plugin-windicss": "^1.2.4",
"diff-match-patch": "^1.0.5"
}
}
5 changes: 4 additions & 1 deletion packages/playground/windicss/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const vitePluginWindicss = require('vite-plugin-windicss').default;
module.exports = defineConfig(({ command, mode }) => {
const isProduction = mode === 'production';
return {
plugins: [svelte(), vitePluginWindicss()],
plugins: [
svelte({ experimental: { generateMissingPreprocessorSourcemaps: true } }),
vitePluginWindicss()
],
build: {
minify: isProduction
}
Expand Down
9 changes: 8 additions & 1 deletion packages/vite-plugin-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,26 @@
"dependencies": {
"@rollup/pluginutils": "^4.1.1",
"debug": "^4.3.2",
"diff-match-patch": "^1.0.5",
"kleur": "^4.1.4",
"magic-string": "^0.25.7",
"require-relative": "^0.8.7",
"svelte-hmr": "^0.14.6"
},

"peerDependencies": {
"diff-match-patch": "^1.0.5",
"svelte": "^3.34.0",
"vite": "^2.3.7"
},
"peerDependenciesMeta": {
"diff-match-patch": {
"optional": true
}
},
"devDependencies": {
"@types/debug": "^4.1.6",
"@types/diff-match-patch": "^1.0.32",
"diff-match-patch": "^1.0.5",
"esbuild": "^0.12.15",
"rollup": "^2.53.2",
"svelte": "^3.38.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { buildMagicString, buildSourceMap } from '../sourcemap';

describe('sourcemap', () => {
describe('buildMagicString', () => {
it('should return a valid magic string', () => {
it('should return a valid magic string', async () => {
const from = 'h1{color: blue}\nh2{color: green}\nh3{color: red}\n';
const to = 'h1{color: blue}\ndiv{color: white}\nh3{color: red}\nh2{color: green}\n';
const m = buildMagicString(from, to);
const m = await buildMagicString(from, to);
expect(m).toBeDefined();
expect(m.original).toBe(from);
expect(m.toString()).toBe(to);
});
});
describe('buildSourceMap', () => {
it('should return a map with mappings and filename', () => {
const map = buildSourceMap('foo', 'bar', 'foo.txt');
it('should return a map with mappings and filename', async () => {
const map = await buildSourceMap('foo', 'bar', 'foo.txt');
expect(map).toBeDefined();
expect(map.mappings).toBeDefined();
expect(map.mappings[0]).toBeDefined();
Expand Down
2 changes: 2 additions & 0 deletions packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ export interface ExperimentalOptions {

/**
* wrap all preprocessors in with a function that adds a sourcemap to the output if missing
*
* to use this option you have to install "diff-match-patch"
*/
generateMissingPreprocessorSourcemaps?: boolean;
}
Expand Down
11 changes: 8 additions & 3 deletions packages/vite-plugin-svelte/src/utils/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,14 @@ function validateSourceMapOutputWrapper(group: PreprocessorGroup, i: number): Pr
}
if (invalidMap) {
try {
const map = buildSourceMap(options.content, result.code, options.filename);
log.warn.once('adding generated sourcemap to preprocesor result');
result.map = map;
const map = await buildSourceMap(options.content, result.code, options.filename);
if (map) {
log.debug.enabled &&
log.debug(
`adding generated sourcemap to preprocesor result for ${options.filename}`
);
result.map = map;
}
} catch (e) {
log.error(`failed to build sourcemap`, e);
}
Expand Down
25 changes: 19 additions & 6 deletions packages/vite-plugin-svelte/src/utils/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import MagicString, { MagicStringOptions } from 'magic-string';
import { diff_match_patch, DIFF_DELETE, DIFF_INSERT } from 'diff-match-patch';
import { log } from './log';

export function buildMagicString(
export async function buildMagicString(
from: string,
to: string,
options?: MagicStringOptions
): MagicString {
): Promise<MagicString | null> {
let diff_match_patch, DIFF_DELETE: number, DIFF_INSERT: number;
try {
const dmpPkg = await import('diff-match-patch');
diff_match_patch = dmpPkg.diff_match_patch;
DIFF_INSERT = dmpPkg.DIFF_INSERT;
DIFF_DELETE = dmpPkg.DIFF_DELETE;
} catch (e) {
log.error.once(
'Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.'
);
return null;
}

const dmp = new diff_match_patch();
const diffs = dmp.diff_main(from, to);
dmp.diff_cleanupSemantic(diffs);
Expand Down Expand Up @@ -38,8 +51,8 @@ export function buildMagicString(
return m;
}

export function buildSourceMap(from: string, to: string, filename?: string) {
export async function buildSourceMap(from: string, to: string, filename?: string) {
// @ts-ignore
const m = buildMagicString(from, to, { filename });
return m.generateDecodedMap({ source: filename, hires: true, includeContent: false });
const m = await buildMagicString(from, to, { filename });
return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
}
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.