Skip to content

Commit

Permalink
feat: remove experimental.generateMissingPreprocessorSourcemaps (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikg committed Dec 2, 2022
1 parent 1c780ce commit 0ef1625
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 180 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-dryers-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

remove experimental.generateMissingPreprocessorSourcemaps
7 changes: 0 additions & 7 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
8 changes: 0 additions & 8 deletions packages/vite-plugin-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 0 additions & 25 deletions packages/vite-plugin-svelte/src/utils/__tests__/sourcemap.spec.ts

This file was deleted.

12 changes: 3 additions & 9 deletions packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
66 changes: 1 addition & 65 deletions packages/vite-plugin-svelte/src/utils/preprocess.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
58 changes: 0 additions & 58 deletions packages/vite-plugin-svelte/src/utils/sourcemap.ts

This file was deleted.

8 changes: 0 additions & 8 deletions pnpm-lock.yaml

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

0 comments on commit 0ef1625

Please sign in to comment.