diff --git a/.changeset/funny-dryers-do.md b/.changeset/funny-dryers-do.md new file mode 100644 index 000000000..097d48247 --- /dev/null +++ b/.changeset/funny-dryers-do.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/vite-plugin-svelte': patch +--- + +remove experimental.generateMissingPreprocessorSourcemaps diff --git a/docs/config.md b/docs/config.md index dbadaf66e..f898d319d 100644 --- a/docs/config.md +++ b/docs/config.md @@ -251,13 +251,6 @@ export default { Use extra preprocessors that delegate style and TypeScript preprocessing to native Vite plugins. TypeScript will be transformed with esbuild. Styles will be transformed using [Vite's CSS plugin](https://vitejs.dev/guide/features.html#css), which handles `@imports`, `url()` references, PostCSS, CSS Modules, and `.scss`/`.sass`/`.less`/`.styl`/`.stylus` files. Do not use together with TypeScript or style preprocessors from `svelte-preprocess` as attempts to transform the content twice will fail! -### generateMissingPreprocessorSourcemaps - -- **Type:** `boolean` -- **Default:** `false` - - If a preprocessor does not provide a sourcemap, a best-effort fallback sourcemap will be provided. This option requires [diff-match-patch](https://github.com/google/diff-match-patch) to be installed as a peer dependency. - ### dynamicCompileOptions - **Type:** diff --git a/packages/vite-plugin-svelte/package.json b/packages/vite-plugin-svelte/package.json index 1ae66f6ca..b27fbd3a2 100644 --- a/packages/vite-plugin-svelte/package.json +++ b/packages/vite-plugin-svelte/package.json @@ -53,19 +53,11 @@ "vitefu": "^0.2.2" }, "peerDependencies": { - "diff-match-patch": "^1.0.5", "svelte": "^3.44.0", "vite": "^3.0.0" }, - "peerDependenciesMeta": { - "diff-match-patch": { - "optional": true - } - }, "devDependencies": { "@types/debug": "^4.1.7", - "@types/diff-match-patch": "^1.0.32", - "diff-match-patch": "^1.0.5", "esbuild": "^0.15.16", "rollup": "^2.79.1", "svelte": "^3.53.1", diff --git a/packages/vite-plugin-svelte/src/utils/__tests__/sourcemap.spec.ts b/packages/vite-plugin-svelte/src/utils/__tests__/sourcemap.spec.ts deleted file mode 100644 index e86baa2ad..000000000 --- a/packages/vite-plugin-svelte/src/utils/__tests__/sourcemap.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { describe, it, expect } from 'vitest'; -import { buildMagicString, buildSourceMap } from '../sourcemap'; - -describe('sourcemap', () => { - describe('buildMagicString', () => { - 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 = 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', async () => { - const map = await buildSourceMap('foo', 'bar', 'foo.txt'); - expect(map).toBeDefined(); - expect(map.mappings).toBeDefined(); - expect(map.mappings[0]).toBeDefined(); - expect(map.mappings[0][0]).toBeDefined(); - expect(map.sources[0]).toBe('foo.txt'); - }); - }); -}); diff --git a/packages/vite-plugin-svelte/src/utils/options.ts b/packages/vite-plugin-svelte/src/utils/options.ts index 539771b15..41d935936 100644 --- a/packages/vite-plugin-svelte/src/utils/options.ts +++ b/packages/vite-plugin-svelte/src/utils/options.ts @@ -315,6 +315,9 @@ function handleDeprecatedOptions(options: ResolvedOptions) { 'experimental.prebundleSvelteLibraries is no longer experimental and has moved to prebundleSvelteLibraries' ); } + if ((options.experimental as any)?.generateMissingPreprocessorSourcemaps) { + log.warn('experimental.generateMissingPreprocessorSourcemaps has been removed.'); + } } // vite passes unresolved `root`option to config hook but we need the resolved value, so do it here @@ -693,15 +696,6 @@ export interface ExperimentalOptions { */ useVitePreprocess?: boolean; - /** - * If a preprocessor does not provide a sourcemap, a best-effort fallback sourcemap will be provided. - * This option requires `diff-match-patch` to be installed as a peer dependency. - * - * @see https://github.com/google/diff-match-patch - * @default false - */ - generateMissingPreprocessorSourcemaps?: boolean; - /** * A function to update `compilerOptions` before compilation * diff --git a/packages/vite-plugin-svelte/src/utils/preprocess.ts b/packages/vite-plugin-svelte/src/utils/preprocess.ts index 7c0d4ce10..4d0c59f27 100644 --- a/packages/vite-plugin-svelte/src/utils/preprocess.ts +++ b/packages/vite-plugin-svelte/src/utils/preprocess.ts @@ -1,9 +1,8 @@ import type { ResolvedConfig, Plugin } from 'vite'; import MagicString from 'magic-string'; import { preprocess } from 'svelte/compiler'; -import { PreprocessorGroup, Processed, ResolvedOptions } from './options'; +import { PreprocessorGroup, ResolvedOptions } from './options'; import { log } from './log'; -import { buildSourceMap } from './sourcemap'; import path from 'path'; import { vitePreprocess } from '../preprocess'; @@ -117,67 +116,4 @@ export function addExtraPreprocessors(options: ResolvedOptions, config: Resolved options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors]; } } - const generateMissingSourceMaps = !!options.experimental?.generateMissingPreprocessorSourcemaps; - if (options.preprocess && generateMissingSourceMaps) { - options.preprocess = Array.isArray(options.preprocess) - ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) - : validateSourceMapOutputWrapper(options.preprocess, 0); - } -} - -function validateSourceMapOutputWrapper(group: PreprocessorGroup, i: number): PreprocessorGroup { - const wrapper: PreprocessorGroup = {}; - - for (const [processorType, processorFn] of Object.entries(group) as Array< - // eslint-disable-next-line no-unused-vars - [keyof PreprocessorGroup, (options: { filename?: string; content: string }) => Processed] - >) { - wrapper[processorType] = async (options) => { - const result = await processorFn(options); - - if (result && result.code !== options.content) { - let invalidMap = false; - if (!result.map) { - invalidMap = true; - log.warn.enabled && - log.warn.once( - `preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`, - { - filename: options.filename, - type: processorType, - processor: processorFn.toString() - } - ); - } else if ((result.map as any)?.mappings === '') { - invalidMap = true; - log.warn.enabled && - log.warn.once( - `preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`, - { - filename: options.filename, - type: processorType, - processor: processorFn.toString() - } - ); - } - if (invalidMap) { - try { - 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); - } - } - } - return result; - }; - } - - return wrapper; } diff --git a/packages/vite-plugin-svelte/src/utils/sourcemap.ts b/packages/vite-plugin-svelte/src/utils/sourcemap.ts deleted file mode 100644 index 5471af3ea..000000000 --- a/packages/vite-plugin-svelte/src/utils/sourcemap.ts +++ /dev/null @@ -1,58 +0,0 @@ -import MagicString, { MagicStringOptions } from 'magic-string'; -import { log } from './log'; - -export async function buildMagicString( - from: string, - to: string, - options?: MagicStringOptions -): Promise { - 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); - const m = new MagicString(from, options); - let pos = 0; - for (let i = 0; i < diffs.length; i++) { - const diff = diffs[i]; - const nextDiff = diffs[i + 1]; - if (diff[0] === DIFF_DELETE) { - if (nextDiff?.[0] === DIFF_INSERT) { - // delete followed by insert, use overwrite and skip ahead - m.overwrite(pos, pos + diff[1].length, nextDiff[1]); - i++; - } else { - m.remove(pos, pos + diff[1].length); - } - pos += diff[1].length; - } else if (diff[0] === DIFF_INSERT) { - if (nextDiff) { - m.appendRight(pos, diff[1]); - } else { - m.append(diff[1]); - } - } else { - // unchanged block, advance pos - pos += diff[1].length; - } - } - // at this point m.toString() === to - return m; -} - -export async function buildSourceMap(from: string, to: string, filename?: string) { - // @ts-ignore - const m = await buildMagicString(from, to, { filename }); - return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null; -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 292f1ce67..aeff6274f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -588,10 +588,8 @@ importers: packages/vite-plugin-svelte: specifiers: '@types/debug': ^4.1.7 - '@types/diff-match-patch': ^1.0.32 debug: ^4.3.4 deepmerge: ^4.2.2 - diff-match-patch: ^1.0.5 esbuild: ^0.15.16 kleur: ^4.1.5 magic-string: ^0.26.7 @@ -610,8 +608,6 @@ importers: vitefu: 0.2.2_vite@3.2.4 devDependencies: '@types/debug': 4.1.7 - '@types/diff-match-patch': 1.0.32 - diff-match-patch: 1.0.5 esbuild: 0.15.16 rollup: 2.79.1 svelte: 3.53.1 @@ -1141,10 +1137,6 @@ packages: '@types/ms': 0.7.31 dev: true - /@types/diff-match-patch/1.0.32: - resolution: {integrity: sha512-bPYT5ECFiblzsVzyURaNhljBH2Gh1t9LowgUwciMrNAhFewLkHT2H0Mto07Y4/3KCOGZHRQll3CTtQZ0X11D/A==} - dev: true - /@types/estree/1.0.0: resolution: {integrity: sha512-WulqXMDUTYAXCjZnk6JtIHPigp55cVtDgDrO2gHRwhyJto21+1zbVCtOYB2L1F9w4qCQ0rOGWBnBe0FNTiEJIQ==} dev: true