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

Add standalone build #239

Closed
wants to merge 4 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ node_modules
.history
plugin.js
plugin.js.map
standalone.js
standalone.js.map
esm
94 changes: 47 additions & 47 deletions package-lock.json

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

25 changes: 23 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,28 @@
"version": "2.3.1",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"module": "esm/plugin.mjs",
Copy link
Member

Choose a reason for hiding this comment

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

I don't see this file generated anywhere according to the rollup configs. Oversight?

"exports": {
".": {
"browser": {
"import": "./esm/standalone.mjs",
"default": "./standalone.js"
Copy link
Member

Choose a reason for hiding this comment

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

For even more compatibility, add standalone.js to the top level browser field?

},
"default": "./plugin.js"
},
"./standalone": {
"import": "./esm/standalone.mjs",
"default": "./standalone.js"
}
},
"files": [
"plugin.js",
"plugin.js.map"
"plugin.js.map",
"esm/**/*.mjs",
"esm/**/*.mjs.map"
Copy link
Member

Choose a reason for hiding this comment

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

To save some bytes when publishing: Could we only publish the plugin map, not the standalone map?

],
"scripts": {
"build": "rollup -c",
"build": "rollup -c && rollup -c rollup.standalone.cjs.config.js && rollup -c rollup.standalone.esm.config.js",
"test": "ava",
cfraz89 marked this conversation as resolved.
Show resolved Hide resolved
"prepare": "npm run build",
"prepublishOnly": "npm test"
Expand All @@ -28,6 +44,8 @@
},
"homepage": "https://github.com/sveltejs/prettier-plugin-svelte#readme",
"devDependencies": {
"@rollup/plugin-alias": "^3.1.4",
"@rollup/plugin-inject": "^4.0.2",
"@types/node": "^10.12.18",
"@types/prettier": "^2.1.6",
"ava": "3.15.0",
Expand All @@ -44,5 +62,8 @@
"peerDependencies": {
"prettier": "^1.16.4 || ^2.0.0",
"svelte": "^3.2.0"
},
"dependencies": {
"buffer": "^5.7.1"
Copy link
Member

Choose a reason for hiding this comment

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

Instead of adding a dependency on buffer, could we instead do something like

// base64-string.ts
export const stringToBase64 = (str: string) => typeof btoa !== 'undefined' ? bota : Buffer.from(str).toString('base64');
export const base64ToString = (str: string) => typeof atob !== 'undefined' ? bota : Buffer.from(str, 'base64').toString();

and use that file in the appropriate places?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree that the buffer dependency feels sketchy.

}
}
29 changes: 29 additions & 0 deletions rollup.standalone.cjs.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript';
import inject from '@rollup/plugin-inject';
import alias from '@rollup/plugin-alias';

export default {
input: 'src/index.ts',
plugins: [
alias({
entries: [{ find: /^prettier$/gm, replacement: `prettier/standalone` }],
}),
resolve({
preferBuiltins: false,
browser: true,
}),
commonjs(),
typescript({}),
inject({
Buffer: ['buffer', 'Buffer'],
}),
],
external: ['prettier/standalone', 'svelte/compiler'],
output: {
file: 'standalone.js',
format: 'cjs',
sourcemap: true,
},
};
31 changes: 31 additions & 0 deletions rollup.standalone.esm.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript';
import inject from '@rollup/plugin-inject';
import alias from '@rollup/plugin-alias';
import path from 'path';

const srcDir = path.resolve(__dirname, 'src');
export default {
input: 'src/index.ts',
plugins: [
alias({
entries: [{ find: /^prettier$/gm, replacement: `${srcDir}/standalone-shim` }],
}),
resolve({
preferBuiltins: false,
browser: true,
}),
commonjs(),
typescript({}),
inject({
Buffer: ['buffer', 'Buffer'],
}),
],
external: ['prettier/esm/standalone', 'svelte/compiler'],
output: {
file: 'esm/standalone.mjs',
format: 'esm',
sourcemap: true,
},
};
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { print } from './print';
import { ASTNode } from './print/nodes';
import { embed } from './embed';
import { snipScriptAndStyleTagContent } from './lib/snipTagContent';
import { parse as parseSvelte } from 'svelte/compiler';

function locStart(node: any) {
return node.start;
Expand All @@ -25,7 +26,8 @@ export const parsers: Record<string, Parser> = {
svelte: {
parse: (text) => {
try {
return <ASTNode>{ ...require(`svelte/compiler`).parse(text), __isRoot: true };
// @ts-ignore
return <ASTNode>{ ...parseSvelte(text), __isRoot: true };
} catch (err) {
if (err.start != null && err.end != null) {
// Prettier expects error objects to have loc.start and loc.end fields.
Expand Down
2 changes: 2 additions & 0 deletions src/standalone-shim.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import prettier from 'prettier/esm/standalone';
export const doc = prettier.doc;