From f223bc1c532b0ed9b7a0a5f4f66d3e1abd90ee05 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 23 May 2023 17:39:54 +0200 Subject: [PATCH] feat: change preprocessor ordering, allow attributes modification (#8618) - change mapping order - add support to modify attributes of script/style tags - add source mapping tests to preprocessor tests --- CHANGELOG.md | 2 + site/content/docs/05-compile-time.md | 74 +++++----- src/compiler/preprocess/index.js | 137 ++++++++++++++---- src/compiler/preprocess/public.d.ts | 42 ++++++ test/preprocess/preprocess.test.js | 12 +- .../samples/multiple-preprocessors/_config.js | 8 +- .../multiple-preprocessors/output.svelte | 4 +- test/preprocess/samples/script/_config.js | 13 +- .../samples/script/expected_map.json | 8 + .../_config.js | 12 ++ .../expected_map.json | 8 + .../input.svelte | 5 + .../output.svelte | 5 + .../style-attributes-modified/_config.js | 14 ++ .../expected_map.json | 8 + .../style-attributes-modified/input.svelte | 5 + .../style-attributes-modified/output.svelte | 5 + .../style-attributes/expected_map.json | 8 + .../samples/style-attributes/input.svelte | 2 +- 19 files changed, 297 insertions(+), 75 deletions(-) create mode 100644 test/preprocess/samples/script/expected_map.json create mode 100644 test/preprocess/samples/style-attributes-modified-longer/_config.js create mode 100644 test/preprocess/samples/style-attributes-modified-longer/expected_map.json create mode 100644 test/preprocess/samples/style-attributes-modified-longer/input.svelte create mode 100644 test/preprocess/samples/style-attributes-modified-longer/output.svelte create mode 100644 test/preprocess/samples/style-attributes-modified/_config.js create mode 100644 test/preprocess/samples/style-attributes-modified/expected_map.json create mode 100644 test/preprocess/samples/style-attributes-modified/input.svelte create mode 100644 test/preprocess/samples/style-attributes-modified/output.svelte create mode 100644 test/preprocess/samples/style-attributes/expected_map.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 7492e1d035..3607060b43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ * **breaking** Deprecate `SvelteComponentTyped`, use `SvelteComponent` instead ([#8512](https://github.com/sveltejs/svelte/pull/8512)) * **breaking** Error on falsy values instead of stores passed to `derived` ([#7947](https://github.com/sveltejs/svelte/pull/7947)) * **breaking** Custom store implementers now need to pass an `update` function additionally to the `set` function ([#6750](https://github.com/sveltejs/svelte/pull/6750)) +* **breaking** Change order in which preprocessors are applied ([#8618](https://github.com/sveltejs/svelte/pull/8618)) +* Add a way to modify attributes for script/style preprocessors ([#8618](https://github.com/sveltejs/svelte/pull/8618)) * Improve hydration speed by adding `data-svelte-h` attribute to detect unchanged HTML elements ([#7426](https://github.com/sveltejs/svelte/pull/7426)) * Add `a11y no-noninteractive-element-interactions` rule ([#8391](https://github.com/sveltejs/svelte/pull/8391)) * Add `a11y-no-static-element-interactions`rule ([#8251](https://github.com/sveltejs/svelte/pull/8251)) diff --git a/site/content/docs/05-compile-time.md b/site/content/docs/05-compile-time.md index 45dd454ea0..bdd1de243e 100644 --- a/site/content/docs/05-compile-time.md +++ b/site/content/docs/05-compile-time.md @@ -186,7 +186,7 @@ const ast = svelte.parse(source, { filename: 'App.svelte' }); ### `svelte.preprocess` -A number of [community-maintained preprocessing plugins](https://sveltesociety.dev/tools#preprocessors) are available to allow you to use Svelte with tools like TypeScript, PostCSS, SCSS, and Less. +A number of [official and community-maintained preprocessing plugins](https://sveltesociety.dev/tools#preprocessors) are available to allow you to use Svelte with tools like TypeScript, PostCSS, SCSS, and Less. You can write your own preprocessor using the `svelte.preprocess` API. @@ -197,6 +197,7 @@ result: { } = await svelte.preprocess( source: string, preprocessors: Array<{ + name: string, markup?: (input: { content: string, filename: string }) => Promise<{ code: string, dependencies?: Array @@ -220,48 +221,41 @@ result: { The `preprocess` function provides convenient hooks for arbitrarily transforming component source code. For example, it can be used to convert a ` \ No newline at end of file diff --git a/test/preprocess/samples/script/_config.js b/test/preprocess/samples/script/_config.js index 1a21045465..535917ade6 100644 --- a/test/preprocess/samples/script/_config.js +++ b/test/preprocess/samples/script/_config.js @@ -1,8 +1,17 @@ +import MagicString from 'magic-string'; + export default { preprocess: { - script: ({ content }) => { + script: ({ content, filename }) => { + const s = new MagicString(content); + s.overwrite( + content.indexOf('__THE_ANSWER__'), + content.indexOf('__THE_ANSWER__') + '__THE_ANSWER__'.length, + '42' + ); return { - code: content.replace('__THE_ANSWER__', '42') + code: s.toString(), + map: s.generateMap({ hires: true, file: filename }) }; } } diff --git a/test/preprocess/samples/script/expected_map.json b/test/preprocess/samples/script/expected_map.json new file mode 100644 index 0000000000..d5bf98483f --- /dev/null +++ b/test/preprocess/samples/script/expected_map.json @@ -0,0 +1,8 @@ +{ + "version": 3, + "mappings": "AAAA,CAAC,MAAM,CAAC;AACR,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAc,CAAC,CAAC;AAC7B,CAAC,CAAC,MAAM", + "names": [], + "sources": [ + "input.svelte" + ] +} \ No newline at end of file diff --git a/test/preprocess/samples/style-attributes-modified-longer/_config.js b/test/preprocess/samples/style-attributes-modified-longer/_config.js new file mode 100644 index 0000000000..1fbb8b3d23 --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified-longer/_config.js @@ -0,0 +1,12 @@ +import * as assert from 'node:assert'; + +export default { + preprocess: { + style: ({ attributes }) => { + assert.deepEqual(attributes, { + lang: 'scss' + }); + return { code: 'PROCESSED', attributes: { sth: 'wayyyyyyyyyyyyy looooooonger' } }; + } + } +}; diff --git a/test/preprocess/samples/style-attributes-modified-longer/expected_map.json b/test/preprocess/samples/style-attributes-modified-longer/expected_map.json new file mode 100644 index 0000000000..28397f95d9 --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified-longer/expected_map.json @@ -0,0 +1,8 @@ +{ + "version": 3, + "mappings": "AAAA;;AAEA,MAAM,mCAAa,UAAM,CAAC,CAAC,KAAK;;AAEhC", + "names": [], + "sources": [ + "input.svelte" + ] +} \ No newline at end of file diff --git a/test/preprocess/samples/style-attributes-modified-longer/input.svelte b/test/preprocess/samples/style-attributes-modified-longer/input.svelte new file mode 100644 index 0000000000..8b2713472a --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified-longer/input.svelte @@ -0,0 +1,5 @@ +foo + + + +bar diff --git a/test/preprocess/samples/style-attributes-modified-longer/output.svelte b/test/preprocess/samples/style-attributes-modified-longer/output.svelte new file mode 100644 index 0000000000..e9971f7f18 --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified-longer/output.svelte @@ -0,0 +1,5 @@ +foo + + + +bar diff --git a/test/preprocess/samples/style-attributes-modified/_config.js b/test/preprocess/samples/style-attributes-modified/_config.js new file mode 100644 index 0000000000..cfeae0c027 --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified/_config.js @@ -0,0 +1,14 @@ +import * as assert from 'node:assert'; + +export default { + preprocess: { + style: ({ attributes }) => { + assert.deepEqual(attributes, { + lang: 'scss', + 'data-foo': 'bar', + bool: true + }); + return { code: 'PROCESSED', attributes: { sth: 'else' } }; + } + } +}; diff --git a/test/preprocess/samples/style-attributes-modified/expected_map.json b/test/preprocess/samples/style-attributes-modified/expected_map.json new file mode 100644 index 0000000000..65f340c1c0 --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified/expected_map.json @@ -0,0 +1,8 @@ +{ + "version": 3, + "mappings": "AAAA;;AAEA,MAAM,WAAiC,UAAM,CAAC,CAAC,KAAK;;AAEpD", + "names": [], + "sources": [ + "input.svelte" + ] +} \ No newline at end of file diff --git a/test/preprocess/samples/style-attributes-modified/input.svelte b/test/preprocess/samples/style-attributes-modified/input.svelte new file mode 100644 index 0000000000..2b2b0e4423 --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified/input.svelte @@ -0,0 +1,5 @@ +foo + + + +bar diff --git a/test/preprocess/samples/style-attributes-modified/output.svelte b/test/preprocess/samples/style-attributes-modified/output.svelte new file mode 100644 index 0000000000..ff8ca98bf9 --- /dev/null +++ b/test/preprocess/samples/style-attributes-modified/output.svelte @@ -0,0 +1,5 @@ +foo + + + +bar diff --git a/test/preprocess/samples/style-attributes/expected_map.json b/test/preprocess/samples/style-attributes/expected_map.json new file mode 100644 index 0000000000..2eca3ec763 --- /dev/null +++ b/test/preprocess/samples/style-attributes/expected_map.json @@ -0,0 +1,8 @@ +{ + "version": 3, + "mappings": "AAAA,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,UAAO,CAAC,CAAC,KAAK", + "names": [], + "sources": [ + "input.svelte" + ] +} \ No newline at end of file diff --git a/test/preprocess/samples/style-attributes/input.svelte b/test/preprocess/samples/style-attributes/input.svelte index 3a55a5a3f6..c513b8bf21 100644 --- a/test/preprocess/samples/style-attributes/input.svelte +++ b/test/preprocess/samples/style-attributes/input.svelte @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file