Skip to content

Add browser build #417

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
.history
plugin.js
plugin.js.map
browser.js
browser.js.map
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: check how big the .js.map of the browser build is, and if it's big, then decide if it's worth it to include it (I don't think so)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, could leave it out.

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.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"files": [
"plugin.js",
"plugin.js.map",
"browser.js",
"browser.js.map",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"browser.js.map",

"index.d.ts"
],
"types": "./index.d.ts",
Expand All @@ -14,6 +16,7 @@
"types": "./index.d.ts",
"default": "./plugin.js"
},
"./browser": "./browser.js",
"./package.json": "./package.json"
},
"scripts": {
Expand All @@ -37,6 +40,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
47 changes: 38 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
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
// Unsupported use case: importing the plugin directly in the browser
{
input: 'src/index.ts',
plugins: [
alias({
// Replace imports from 'prettier' with 'prettier/standalone'
entries: [
// But don't touch 'prettier/plugins/babel'
{ find: 'prettier/plugins/babel', replacement: 'prettier/plugins/babel' },
{ find: 'prettier', replacement: 'prettier/standalone' },
],
}),
resolve(),
commonjs(),
typescript(),
],
external: ['prettier/standalone', 'prettier/plugins/babel', 'svelte/compiler'],
output: {
file: 'browser.js',
format: 'esm',
sourcemap: true,
},
},
];
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