Skip to content

Commit d89241e

Browse files
dummdidummcurran
andauthored
feat: semi-standalone browser build (#430)
Adds a semi-standalone browser build under prettier-plugin-svelte/browser. part of #69 (full fix would mean import maps work, which they don't because you need svelte/compiler.cjs which has the wrong mime type on package CDNs - this isn't fixable within this package, rather needs a different file type upstream) closes #239 closes #257 closes #417 --------- Co-authored-by: Curran <[email protected]>
1 parent 288d915 commit d89241e

File tree

7 files changed

+77
-10
lines changed

7 files changed

+77
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
.history
44
plugin.js
55
plugin.js.map
6+
browser.js

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 3.2.0 (Unreleased)
44

55
- (feat) format JSON script tags
6+
- (feat) introduce separate entry point using `prettier/standalone`
67
- (fix) don't duplicate comments of nested script/style tags
78
- (fix) handle updated `Snippet` block AST shape
89

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ Since we are using configuration overrides to handle svelte files, you might als
220220
}
221221
```
222222

223+
## Usage in the browser
224+
225+
Usage in the browser is semi-supported. You can import the plugin from `prettier-plugin-svelte/browser` to get a version that depends on `prettier/standalone` and therefore doesn't use any node APIs. What isn't supported in a good way yet is using this without a build step - you still need a bundler like Vite to build everything together as one self-contained package in advance.
226+
223227
## Migration
224228

225229
```diff

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"files": [
77
"plugin.js",
88
"plugin.js.map",
9+
"browser.js",
910
"index.d.ts"
1011
],
1112
"types": "./index.d.ts",
@@ -14,6 +15,7 @@
1415
"types": "./index.d.ts",
1516
"default": "./plugin.js"
1617
},
18+
"./browser": "./browser.js",
1719
"./package.json": "./package.json"
1820
},
1921
"scripts": {
@@ -37,6 +39,7 @@
3739
},
3840
"homepage": "https://github.com/sveltejs/prettier-plugin-svelte#readme",
3941
"devDependencies": {
42+
"@rollup/plugin-alias": "^5.1.0",
4043
"@rollup/plugin-commonjs": "14.0.0",
4144
"@rollup/plugin-node-resolve": "11.0.1",
4245
"@types/node": "^14.0.0",

rollup.config.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1+
import alias from '@rollup/plugin-alias';
12
import resolve from '@rollup/plugin-node-resolve';
23
import commonjs from '@rollup/plugin-commonjs';
34
import typescript from 'rollup-plugin-typescript';
45

5-
export default {
6-
input: 'src/index.ts',
7-
plugins: [resolve(), commonjs(), typescript()],
8-
external: ['prettier', 'svelte'],
9-
output: {
10-
file: 'plugin.js',
11-
format: 'cjs',
12-
sourcemap: true,
6+
export default [
7+
// CommonJS build
8+
{
9+
input: 'src/index.ts',
10+
plugins: [resolve(), commonjs(), typescript()],
11+
external: ['prettier', 'svelte/compiler'],
12+
output: {
13+
file: 'plugin.js',
14+
format: 'cjs',
15+
sourcemap: true,
16+
},
1317
},
14-
};
18+
// Browser build
19+
// Supported use case: importing the plugin from a bundler like Vite or Webpack
20+
// Semi-supported use case: importing the plugin directly in the browser through using import maps.
21+
// (semi-supported because it requires a svelte/compiler.cjs import map and the .cjs ending has the wrong mime type on CDNs)
22+
{
23+
input: 'src/index.ts',
24+
plugins: [
25+
alias({
26+
entries: [{ find: 'prettier', replacement: 'prettier/standalone' }],
27+
}),
28+
resolve(),
29+
commonjs(),
30+
typescript(),
31+
],
32+
external: ['prettier/standalone', 'prettier/plugins/babel', 'svelte/compiler'],
33+
output: {
34+
file: 'browser.js',
35+
format: 'esm',
36+
},
37+
},
38+
];

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { hasPragma, print } from './print';
44
import { ASTNode } from './print/nodes';
55
import { embed, getVisitorKeys } from './embed';
66
import { snipScriptAndStyleTagContent } from './lib/snipTagContent';
7+
import { parse } from 'svelte/compiler';
78

89
const babelParser = prettierPluginBabel.parsers.babel;
910

@@ -29,7 +30,7 @@ export const parsers: Record<string, Parser> = {
2930
hasPragma,
3031
parse: (text) => {
3132
try {
32-
return <ASTNode>{ ...require(`svelte/compiler`).parse(text), __isRoot: true };
33+
return <ASTNode>{ ...parse(text), __isRoot: true };
3334
} catch (err: any) {
3435
if (err.start != null && err.end != null) {
3536
// Prettier expects error objects to have loc.start and loc.end fields.

0 commit comments

Comments
 (0)