├── .envrc
├── .gitignore
├── test-components
├── ScriptSetup.vue
├── EmptyTemplate.vue
├── NoScript.vue
├── NoTemplate.vue
├── NoStyle.vue
├── Vanilla.vue
├── DifferentOrderBlocks.vue
├── LeadingNewlines.vue
├── AttributesOnBlocks.vue
├── CustomIndents.vue
├── ExtraNewlinesBetweenBlocks.vue
├── MultipleStyles.vue
└── CustomBlocks.vue
├── CHANGELOG.md
├── package.json
├── flake.nix
├── LICENSE.mit
├── flake.lock
├── README.md
├── index.js
├── test.js
└── __snapshots__
└── test.js.snap
/.envrc:
--------------------------------------------------------------------------------
1 | use flake .
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
3 | # nix
4 | .direnv
5 |
--------------------------------------------------------------------------------
/test-components/ScriptSetup.vue:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/test-components/EmptyTemplate.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
--------------------------------------------------------------------------------
/test-components/NoScript.vue:
--------------------------------------------------------------------------------
1 |
2 | Hello {{name}}
3 |
4 |
5 |
10 |
--------------------------------------------------------------------------------
/test-components/NoTemplate.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
16 |
--------------------------------------------------------------------------------
/test-components/NoStyle.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 | Hello {{name}}
13 |
14 |
--------------------------------------------------------------------------------
/test-components/Vanilla.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 | Hello {{name}}
13 |
14 |
15 |
20 |
--------------------------------------------------------------------------------
/test-components/DifferentOrderBlocks.vue:
--------------------------------------------------------------------------------
1 |
2 | Hello {{name}}
3 |
4 |
5 |
14 |
15 |
20 |
--------------------------------------------------------------------------------
/test-components/LeadingNewlines.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
15 | Hello {{name}}
16 |
17 |
18 |
23 |
--------------------------------------------------------------------------------
/test-components/AttributesOnBlocks.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 | Hello {{name}}
13 |
14 |
15 |
20 |
--------------------------------------------------------------------------------
/test-components/CustomIndents.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 | Hello {{name}}
13 |
14 |
15 |
20 |
21 |
22 | hello 1
23 |
24 |
--------------------------------------------------------------------------------
/test-components/ExtraNewlinesBetweenBlocks.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
13 |
14 | Hello {{name}}
15 |
16 |
17 |
18 |
19 |
20 |
21 |
26 |
--------------------------------------------------------------------------------
/test-components/MultipleStyles.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 | Hello {{name}}
13 |
14 |
15 |
20 |
21 |
26 |
--------------------------------------------------------------------------------
/test-components/CustomBlocks.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 | Hello {{name}}
13 |
14 |
15 |
20 |
21 |
22 | hello 1
23 |
24 |
25 |
26 | hello 2
27 |
28 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 |
5 | ## [3.0.1] - 2024-04-11
6 |
7 | ### Fixed
8 |
9 | - Don't mutation options object.
10 | - Fix inaccuracy in docs ([#3](https://github.com/psalaets/vue-sfc-descriptor-to-string/issues/3)).
11 |
12 | ## [3.0.0] - 2024-04-11
13 |
14 | ### Breaking
15 |
16 | - Attributes are no longer sorted.
17 |
18 | ## [2.0.0] - 2023-06-02
19 |
20 | ### Added
21 |
22 | - Vue 3 and esm support. Thanks to [@dword-design](https://github.com/dword-design).
23 |
24 | ## [1.0.0] - 2018-02-18
25 |
26 | ### Added
27 |
28 | - Initial impl
29 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vue-sfc-descriptor-to-string",
3 | "version": "3.0.1",
4 | "description": "Convert SFCDescriptor to source",
5 | "main": "index.js",
6 | "type": "module",
7 | "scripts": {
8 | "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
9 | "ship": "np"
10 | },
11 | "homepage": "https://github.com/psalaets/vue-sfc-descriptor-to-string",
12 | "repository": {
13 | "type": "git",
14 | "url": "https://github.com/psalaets/vue-sfc-descriptor-to-string.git"
15 | },
16 | "keywords": [
17 | "vue",
18 | "sfc",
19 | "parse",
20 | "stringify"
21 | ],
22 | "author": "Paul Salaets ",
23 | "license": "MIT",
24 | "devDependencies": {
25 | "@vue/compiler-sfc": "^3.3.4",
26 | "jest": "^29.5.0",
27 | "np": "^8.0.2"
28 | },
29 | "dependencies": {
30 | "indent-string": "^5.0.0"
31 | },
32 | "engines": {
33 | "node": ">=12.17.0"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/flake.nix:
--------------------------------------------------------------------------------
1 | {
2 | inputs = {
3 | nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
4 | systems.url = "github:nix-systems/default";
5 | };
6 |
7 | outputs = {
8 | systems,
9 | nixpkgs,
10 | ...
11 | } @ inputs: let
12 | eachSystem = f:
13 | nixpkgs.lib.genAttrs (import systems) (
14 | system:
15 | f nixpkgs.legacyPackages.${system}
16 | );
17 | in {
18 | devShells = eachSystem (pkgs: {
19 | default = pkgs.mkShell {
20 | buildInputs = [
21 | pkgs.nodejs_20
22 | # You can set the major version of Node.js to a specific one instead
23 | # of the default version
24 | # pkgs.nodejs-19_x
25 |
26 | # You can choose pnpm, yarn, or none (npm).
27 | # pkgs.nodePackages.pnpm
28 | # pkgs.yarn
29 |
30 | # pkgs.nodePackages.typescript
31 | # pkgs.nodePackages.typescript-language-server
32 | ];
33 | };
34 | });
35 | };
36 | }
--------------------------------------------------------------------------------
/LICENSE.mit:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 Paul Salaets
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/flake.lock:
--------------------------------------------------------------------------------
1 | {
2 | "nodes": {
3 | "nixpkgs": {
4 | "locked": {
5 | "lastModified": 1712757991,
6 | "narHash": "sha256-kR7C7Fqt3JP40h0mzmSZeWI5pk1iwqj4CSeGjnUbVHc=",
7 | "owner": "NixOS",
8 | "repo": "nixpkgs",
9 | "rev": "d6b3ddd253c578a7ab98f8011e59990f21dc3932",
10 | "type": "github"
11 | },
12 | "original": {
13 | "owner": "NixOS",
14 | "ref": "nixpkgs-unstable",
15 | "repo": "nixpkgs",
16 | "type": "github"
17 | }
18 | },
19 | "root": {
20 | "inputs": {
21 | "nixpkgs": "nixpkgs",
22 | "systems": "systems"
23 | }
24 | },
25 | "systems": {
26 | "locked": {
27 | "lastModified": 1681028828,
28 | "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
29 | "owner": "nix-systems",
30 | "repo": "default",
31 | "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
32 | "type": "github"
33 | },
34 | "original": {
35 | "owner": "nix-systems",
36 | "repo": "default",
37 | "type": "github"
38 | }
39 | }
40 | },
41 | "root": "root",
42 | "version": 7
43 | }
44 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vue-sfc-descriptor-to-string
2 |
3 | Convert SFCDescriptor to source
4 |
5 | ## Install
6 |
7 | `npm install vue-sfc-descriptor-to-string`
8 |
9 | ## Usage
10 |
11 | ```js
12 | // parse an sfc
13 | const compiler = require('vue-template-compiler');
14 | const sfcDescriptor = compiler.parseComponent(sfcSource);
15 |
16 | // convert sfc descriptor back to source
17 | const toString = require('vue-sfc-descriptor-to-string');
18 | const result = toString(sfcDescriptor);
19 |
20 | result == sfcSource // => true, but see caveats below
21 | ```
22 |
23 | ## API
24 |
25 | ```js
26 | const toString = require('vue-sfc-descriptor-to-string');
27 | ```
28 |
29 | ### `toString(SFCDescriptor, options)`
30 |
31 | #### SFCDescriptor
32 |
33 | Object that looks like an [SFCDescriptor](https://github.com/vuejs/vue/blob/dev/flow/compiler.js#L177)
34 |
35 | #### options
36 |
37 | Optional object, defaults to `{}`
38 |
39 | ##### options.indents
40 |
41 | Optional object that can have a property for each sfc block name (e.g. `template`). Values are the number of spaces to indent that block's content.
42 |
43 | Defaults to
44 |
45 | ```js
46 | {
47 | template: 0,
48 | script: 0,
49 | style: 0
50 | }
51 | ```
52 |
53 | ## Caveats
54 |
55 | This module isn't a true inverse of `compiler.parseComponent()` because it doesn't always produce the exact same sfc compared to what was parsed. It assumes the parsed sfc
56 |
57 | - ends with a single newline
58 | - has a single space before each attribute on `
13 |
14 |
15 | Hello {{name}}
16 |
17 |
18 |
23 |
24 |
25 | hello 1
26 |
27 | "
28 | `;
29 |
30 | exports[`attributes on blocks 1`] = `
31 | "
40 |
41 |
42 | Hello {{name}}
43 |
44 |
45 |
50 | "
51 | `;
52 |
53 | exports[`custom blocks 1`] = `
54 | "
63 |
64 |
65 | Hello {{name}}
66 |
67 |
68 |
73 |
74 |
75 | hello 1
76 |
77 |
78 |
79 | hello 2
80 |
81 | "
82 | `;
83 |
84 | exports[`different order blocks 1`] = `
85 | "
86 | Hello {{name}}
87 |
88 |
89 |
98 |
99 |
104 | "
105 | `;
106 |
107 | exports[`empty template 1`] = `
108 | "
117 |
118 |
119 | "
120 | `;
121 |
122 | exports[`extra newlines between blocks 1`] = `
123 | "
132 |
133 |
134 |
135 |
136 | Hello {{name}}
137 |
138 |
139 |
140 |
141 |
142 |
143 |
148 | "
149 | `;
150 |
151 | exports[`leading newlines 1`] = `
152 | "
153 |
154 |
155 |
164 |
165 |
166 | Hello {{name}}
167 |
168 |
169 |
174 | "
175 | `;
176 |
177 | exports[`multiple styles 1`] = `
178 | "
187 |
188 |
189 | Hello {{name}}
190 |
191 |
192 |
197 |
198 |
203 | "
204 | `;
205 |
206 | exports[`no script 1`] = `
207 | "
208 | Hello {{name}}
209 |
210 |
211 |
216 | "
217 | `;
218 |
219 | exports[`no style 1`] = `
220 | "
229 |
230 |
231 | Hello {{name}}
232 |
233 | "
234 | `;
235 |
236 | exports[`no template 1`] = `
237 | "
246 |
247 |
252 | "
253 | `;
254 |
255 | exports[`script setup 1`] = `
256 | "
265 | "
266 | `;
267 |
268 | exports[`vanilla sfc 1`] = `
269 | "
278 |
279 |
280 | Hello {{name}}
281 |
282 |
283 |
288 | "
289 | `;
290 |
--------------------------------------------------------------------------------