Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: semi-standalone browser build #430

Merged
merged 7 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
.history
plugin.js
plugin.js.map
browser.js
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.2.0 (Unreleased)

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

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ Since we are using configuration overrides to handle svelte files, you might als
}
```

## Usage in the browser

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.

## Migration

```diff
Expand Down
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"files": [
"plugin.js",
"plugin.js.map",
"browser.js",
"index.d.ts"
],
"types": "./index.d.ts",
Expand All @@ -14,6 +15,7 @@
"types": "./index.d.ts",
"default": "./plugin.js"
},
"./browser": "./browser.js",
"./package.json": "./package.json"
},
"scripts": {
Expand All @@ -37,6 +39,7 @@
},
"homepage": "https://github.com/sveltejs/prettier-plugin-svelte#readme",
"devDependencies": {
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-commonjs": "14.0.0",
"@rollup/plugin-node-resolve": "11.0.1",
"@types/node": "^14.0.0",
Expand Down
42 changes: 33 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import alias from '@rollup/plugin-alias';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript';

export default {
input: 'src/index.ts',
plugins: [resolve(), commonjs(), typescript()],
external: ['prettier', 'svelte'],
output: {
file: 'plugin.js',
format: 'cjs',
sourcemap: true,
export default [
// CommonJS build
{
input: 'src/index.ts',
plugins: [resolve(), commonjs(), typescript()],
external: ['prettier', 'svelte/compiler'],
output: {
file: 'plugin.js',
format: 'cjs',
sourcemap: true,
},
},
};
// Browser build
// Supported use case: importing the plugin from a bundler like Vite or Webpack
// Semi-supported use case: importing the plugin directly in the browser through using import maps.
// (semi-supported because it requires a svelte/compiler.cjs import map and the .cjs ending has the wrong mime type on CDNs)
{
input: 'src/index.ts',
plugins: [
alias({
entries: [{ find: 'prettier', replacement: 'prettier/standalone' }],
}),
resolve(),
commonjs(),
typescript(),
],
external: ['prettier/standalone', 'prettier/plugins/babel', 'svelte/compiler'],
output: {
file: 'browser.js',
format: 'esm',
},
},
];
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { hasPragma, print } from './print';
import { ASTNode } from './print/nodes';
import { embed, getVisitorKeys } from './embed';
import { snipScriptAndStyleTagContent } from './lib/snipTagContent';
import { parse } from 'svelte/compiler';

const babelParser = prettierPluginBabel.parsers.babel;

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