Skip to content

Commit

Permalink
feat(plugin-sfc): extract all script blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Aug 24, 2022
1 parent cb702d1 commit 8c39df6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/plugin-sfc/src/sfc-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const sfcPlugin: PluginWithOptions<SfcPluginOptions> = (
template: null,
script: null,
scriptSetup: null,
scripts: [],
styles: [],
customBlocks: [],
};
Expand Down Expand Up @@ -74,6 +75,7 @@ export const sfcPlugin: PluginWithOptions<SfcPluginOptions> = (
// extract sfc blocks to `env.sfcBlocks`
const sfcBlock = match.groups;
if (sfcBlock.type === TAG_NAME_SCRIPT) {
env.sfcBlocks.scripts.push(sfcBlock);
if (SCRIPT_SETUP_TAG_OPEN_REGEXP.test(sfcBlock.tagOpen)) {
env.sfcBlocks.scriptSetup = sfcBlock;
} else {
Expand Down
27 changes: 27 additions & 0 deletions packages/plugin-sfc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,37 @@ export interface SfcBlock {
}

export interface MarkdownSfcBlocks {
/**
* The `<template>` block
*/
template: SfcBlock | null;

/**
* The common `<script>` block
*/
script: SfcBlock | null;

/**
* The `<script setup>` block
*/
scriptSetup: SfcBlock | null;

/**
* All `<script>` blocks.
*
* By default, SFC only allows one `<script>` block and one `<script setup>` block.
* However, some tools may support different types of `<script>`s, so we keep all of them here.
*/
scripts: SfcBlock[];

/**
* All `<style>` blocks.
*/
styles: SfcBlock[];

/**
* All custom blocks.
*/
customBlocks: SfcBlock[];
}

Expand Down
72 changes: 72 additions & 0 deletions packages/plugin-sfc/tests/__snapshots__/sfc-plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,42 @@ const foo = 'scriptSetup'
"tagOpen": "<script setup lang=\\"ts\\">",
"type": "script",
},
"scripts": [
{
"content": "<script setup lang=\\"ts\\">
const foo = 'scriptSetup'
</script>",
"contentStripped": "
const foo = 'scriptSetup'
",
"tagClose": "</script>",
"tagOpen": "<script setup lang=\\"ts\\">",
"type": "script",
},
{
"content": "<script>
export default {
setup() {
return {
msg: 'script'
}
}
}
</script>",
"contentStripped": "
export default {
setup() {
return {
msg: 'script'
}
}
}
",
"tagClose": "</script>",
"tagOpen": "<script>",
"type": "script",
},
],
"styles": [
{
"content": "<style lang=\\"stylus\\">
Expand Down Expand Up @@ -130,6 +166,42 @@ const foo = 'scriptSetup'
"tagOpen": "<script setup lang=\\"ts\\">",
"type": "script",
},
"scripts": [
{
"content": "<script setup lang=\\"ts\\">
const foo = 'scriptSetup'
</script>",
"contentStripped": "
const foo = 'scriptSetup'
",
"tagClose": "</script>",
"tagOpen": "<script setup lang=\\"ts\\">",
"type": "script",
},
{
"content": "<script>
export default {
setup() {
return {
msg: 'script'
}
}
}
</script>",
"contentStripped": "
export default {
setup() {
return {
msg: 'script'
}
}
}
",
"tagClose": "</script>",
"tagOpen": "<script>",
"type": "script",
},
],
"styles": [
{
"content": "<style lang=\\"stylus\\">
Expand Down

0 comments on commit 8c39df6

Please sign in to comment.