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

Conversation

cfraz89
Copy link

@cfraz89 cfraz89 commented Jul 18, 2021

The standalone build allows the plugin to be used in the browser, prettier/standalone. Allows for features such as in-browser formatting and codemods.

Have added an exports field to package.json to ensure the correct standalone/node/esm/cjs version of the package is used based on environment.

I've also changed the 'svelte' exclude to 'svelte/compiler' in the standalone rollup configs, as the compiler was getting bundled in and I have the feeling it isnt intended to be. If this is the case, you may want to adjust the standard rollup config too.

@cfraz89 cfraz89 force-pushed the standalone branch 7 times, most recently from da065cc to 9fc5b76 Compare July 18, 2021 19:00
@cfraz89 cfraz89 changed the title Add standalone build Add standalone build and ESM version Jul 18, 2021
@cfraz89 cfraz89 force-pushed the standalone branch 2 times, most recently from e656a28 to 86883ed Compare July 18, 2021 20:13
@cfraz89 cfraz89 changed the title Add standalone build and ESM version Add standalone build Jul 18, 2021
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Thanks! Could you tell me how to test this out locally?

@cfraz89
Copy link
Author

cfraz89 commented Jul 21, 2021

Thanks! Could you tell me how to test this out locally?

Try putting together a svelte page like this:

<script>
import * as sveltePlugin from 'prettier-plugin-svelte';
import prettier from 'prettier/esm/standalone';
import babelParser from 'prettier/esm/parser-babel';
import cssParser from 'prettier/esm/parser-postcss';

const formatted = prettier.format('<script>const thisIsCode = 1;</script><style></style><div>Svelte source code here</div>', {
		parser: 'svelte',
		plugins: [sveltePlugin, babelParser, cssParser]
	});
</script>

<div>{formatted}</div>

I havent used that exact code myself, so there may be a small issue in there somewhere.

If you're using it in sveltekit, you'll need a svelte.config.js kit.vite section like

	kit: {
	       ...,
		vite: {
			optimizeDeps: {
				exclude: ['prettier-plugin-svelte', 'prettier']
			},
			ssr: {
				noExternal: ['prettier']
			}
		}
	}

Let me know if you have any troubles and I can put up a demo page.

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Sorry for the long delay. I left some comments, let me know what you think.

@@ -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.

@@ -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?

"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?

".": {
"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?

@dummdidumm dummdidumm mentioned this pull request Oct 20, 2021
@Conduitry Conduitry mentioned this pull request Sep 29, 2023
This was referenced Jan 27, 2024
@curran
Copy link
Contributor

curran commented Jan 29, 2024

See also #417

dummdidumm added a commit that referenced this pull request Feb 13, 2024
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 <curran.kelleher@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants