Skip to content

Commit b80ca90

Browse files
committed
feat: 🎸 support prependData for almost every preprocessor
BREAKING CHANGE: 🧨 This is a general evolution of the specific `scss.data` property that was used to prepend data to components written in scss. `{preprocessorOptions}.prependData` is now the way to prepend some string to any preprocessor.
1 parent 493bf1a commit b80ca90

27 files changed

+317
-412
lines changed

Diff for: ‎README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
- [Standalone processors](#standalone-processors)
2727
- [Options](#options)
2828
- [Specifics and limitations](#specifics-and-limitations)
29+
- [General](#general)
2930
- [`scss`/`sass`](#scsssass)
3031
- [`typescript`](#typescript)
3132
- [`pug`](#pug)
3233
- [`coffeescript`](#coffeescript)
3334
- [FAQ](#faq)
3435
- [My VS Code is displaying a lot of errors on my templates when I try to use `x`...](#my-vs-code-is-displaying-a-lot-of-errors-on-my-templates-when-i-try-to-use-x)
35-
- [My `typescript` compilation is sloooooooow](#my-typescript-compilation-is-sloooooooow)
3636

3737
<!-- /code_chunk_output -->
3838

@@ -544,11 +544,33 @@ svelte.preprocess(input, sveltePreprocess(options));
544544

545545
## Specifics and limitations
546546

547+
### General
548+
549+
#### Prepending data
550+
551+
For almost language, `svelte-preprocess` accepts a `prependData` property that allows to prepend any string content to your component's `style` or `script` sections.
552+
553+
```js
554+
import preprocess from 'svelte-preprocess'
555+
556+
expor default {
557+
...,
558+
preprocess: preprocess({
559+
scss: {
560+
prependData: `@use theme`
561+
},
562+
javascript: {
563+
prependData: `import { _ } from 'svelte-i18n'`
564+
}
565+
}),
566+
...
567+
}
568+
```
569+
547570
### `scss`/`sass`
548571

549572
The SCSS/SASS processor accepts the default sass options alongside two other props:
550573

551-
- `data: string` - a string prepended to every scss file processed.
552574
- `renderSync: boolean` - if `true`, use the sync render method which is faster for dart sass.
553575
- `implementation: object` - pass the module to use to compile sass, if unspecified, `svelte-preprocess` will first look for `node-sass` and then for `sass`.
554576

Diff for: ‎src/autoProcess.ts

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import stripIndent from 'strip-indent';
2-
31
import {
42
PreprocessorGroup,
53
Preprocessor,
@@ -11,9 +9,10 @@ import {
119
} from './types';
1210
import { hasDepInstalled } from './modules/hasDepInstalled';
1311
import { concat } from './modules/concat';
14-
import { parseFile } from './modules/parseFile';
12+
import { getTagInfo } from './modules/tagInfo';
1513
import { addLanguageAlias, getLanguageFromAlias } from './modules/language';
1614
import { throwError } from './modules/errors';
15+
import { prepareContent } from './modules/prepareContent';
1716

1817
type AutoPreprocessOptions = {
1918
markupTagName?: string;
@@ -61,9 +60,6 @@ export const runTransformer = async (
6160
return { code: content };
6261
}
6362

64-
// remove any unnecessary indentation (useful for coffee, pug and sugarss)
65-
content = stripIndent(content);
66-
6763
if (typeof options === 'function') {
6864
return options({ content, map, filename, attributes });
6965
}
@@ -149,26 +145,33 @@ export function autoPreprocess(
149145
alias,
150146
dependencies,
151147
attributes,
152-
} = await parseFile(svelteFile);
148+
} = await getTagInfo(svelteFile);
153149

154150
if (lang == null || alias == null) {
155151
alias = defaultLanguages[type];
156152
lang = getLanguageFromAlias(alias);
157153
}
158154

159155
if (preserve.includes(lang) || preserve.includes(alias)) {
160-
return;
156+
return { code: content };
161157
}
162158

159+
const transformerOptions = getTransformerOptions(lang, alias);
160+
161+
content = prepareContent({
162+
options: transformerOptions,
163+
content,
164+
});
165+
163166
if (lang === targetLanguage) {
164167
return { code: content, dependencies };
165168
}
166169

167-
const transformed = await runTransformer(
168-
lang,
169-
getTransformerOptions(lang, alias),
170-
{ content: stripIndent(content), filename, attributes },
171-
);
170+
const transformed = await runTransformer(lang, transformerOptions, {
171+
content,
172+
filename,
173+
attributes,
174+
});
172175

173176
return {
174177
...transformed,
@@ -235,8 +238,6 @@ export function autoPreprocess(
235238
filename,
236239
});
237240

238-
if (transformResult == null) return;
239-
240241
let { code, map, dependencies, diagnostics } = transformResult;
241242

242243
if (transformers.babel) {
@@ -262,8 +263,6 @@ export function autoPreprocess(
262263
filename,
263264
});
264265

265-
if (transformResult == null) return;
266-
267266
let { code, map, dependencies } = transformResult;
268267

269268
if (await hasDepInstalled('postcss')) {

Diff for: ‎src/modules/prepareContent.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import stripIndent from 'strip-indent';
2+
3+
export function prepareContent({
4+
options,
5+
content,
6+
}: {
7+
options: any;
8+
content: string;
9+
}) {
10+
if (typeof options !== 'object') {
11+
return content;
12+
}
13+
14+
content = stripIndent(content);
15+
16+
if (options?.prependData) {
17+
content = `${options.prependData}\n${content}`;
18+
}
19+
20+
return content;
21+
}

Diff for: ‎src/modules/parseFile.ts renamed to ‎src/modules/tagInfo.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { resolve, dirname } from 'path';
44
import { PreprocessorArgs } from '../types';
55
import { getLanguage } from './language';
66

7-
export const resolveSrc = (importerFile: string, srcPath: string) =>
7+
const resolveSrc = (importerFile: string, srcPath: string) =>
88
resolve(dirname(importerFile), srcPath);
99

10-
export const getSrcContent = (file: string): Promise<string> => {
10+
const getSrcContent = (file: string): Promise<string> => {
1111
return new Promise((resolve, reject) => {
1212
readFile(file, (error: Error, data: unknown) => {
1313
// istanbul ignore if
@@ -21,7 +21,7 @@ async function doesFileExist(file: string) {
2121
return new Promise((resolve) => access(file, 0, (err) => resolve(!err)));
2222
}
2323

24-
export const parseFile = async ({
24+
export const getTagInfo = async ({
2525
attributes,
2626
filename,
2727
content,

0 commit comments

Comments
 (0)