diff --git a/.changeset/plenty-chicken-sparkle.md b/.changeset/plenty-chicken-sparkle.md new file mode 100644 index 0000000..9e42b7d --- /dev/null +++ b/.changeset/plenty-chicken-sparkle.md @@ -0,0 +1,5 @@ +--- +'@melt-ui/pp': patch +--- + +fix: Fixed source maps diff --git a/.changeset/rude-games-think.md b/.changeset/rude-games-think.md new file mode 100644 index 0000000..b73f1b7 --- /dev/null +++ b/.changeset/rude-games-think.md @@ -0,0 +1,5 @@ +--- +'@melt-ui/pp': minor +--- + +feat: Added a built-in sequential preprocessor diff --git a/src/index.ts b/src/index.ts index aa55d5a..a261be6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,8 @@ import { traverse } from './traverse/index.js'; import type { TemplateNode } from 'svelte/types/compiler/interfaces'; import type { Config, Node } from './types.js'; +export * from './sequence.js'; + export type PreprocessOptions = { /** * For aliasing the name of the `melt` action. @@ -119,7 +121,6 @@ export function preprocessMeltUI(options?: PreprocessOptions): PreprocessorGroup return { code: config.markup.toString(), - map: config.markup.generateMap(), }; }, }; diff --git a/src/sequence.ts b/src/sequence.ts new file mode 100644 index 0000000..73ed266 --- /dev/null +++ b/src/sequence.ts @@ -0,0 +1,53 @@ +// Originally sourced and modified from https://github.com/pchynoweth/svelte-sequential-preprocessor + +import { preprocess } from 'svelte/compiler'; +import { PreprocessorGroup, Processed } from 'svelte/types/compiler/preprocess'; + +/** + * A Svelte preprocessor that wraps other preprocessors and forces them to run sequentially. + * + * @example + * ```js + * // svelte.config.js + * import { preprocessMeltUI, sequence } from '@melt-ui/pp'; + * + * const config = { + * // ... other svelte config options + * preprocess: sequence([ + * // ... other preprocessors (e.g. `vitePreprocess()`) + * preprocessMeltUI() + * ]) + * // ... + * }; + * ``` + */ +export function sequence(preprocessors: PreprocessorGroup[]): PreprocessorGroup { + return { + async markup({ content, filename }): Promise { + let code = content; + let map: Processed['map']; + let attributes: Processed['attributes']; + let toString: Processed['toString']; + const dependencies: Processed['dependencies'] = []; + + for (const pp of preprocessors) { + const processed = await preprocess(code, pp, { filename }); + if (processed && processed.dependencies) { + dependencies.push(...processed.dependencies); + } + code = processed ? processed.code : code; + map = processed.map ?? map; + attributes = processed.attributes ?? attributes; + toString = processed.toString ?? toString; + } + + return { + code, + dependencies, + map, + attributes, + toString, + }; + }, + }; +}