Skip to content

Commit

Permalink
fix: Source maps and having sequence built-in (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonz97 authored Dec 19, 2023
1 parent 2db8a20 commit 978fd50
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/plenty-chicken-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@melt-ui/pp': patch
---

fix: Fixed source maps
5 changes: 5 additions & 0 deletions .changeset/rude-games-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@melt-ui/pp': minor
---

feat: Added a built-in sequential preprocessor
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -119,7 +121,6 @@ export function preprocessMeltUI(options?: PreprocessOptions): PreprocessorGroup

return {
code: config.markup.toString(),
map: config.markup.generateMap(),
};
},
};
Expand Down
53 changes: 53 additions & 0 deletions src/sequence.ts
Original file line number Diff line number Diff line change
@@ -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<Processed> {
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,
};
},
};
}

0 comments on commit 978fd50

Please sign in to comment.