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/esm #54

Merged
merged 6 commits into from
Jun 11, 2021
Merged

Feat/esm #54

merged 6 commits into from
Jun 11, 2021

Conversation

dominikg
Copy link
Member

@dominikg dominikg commented Jun 9, 2021

conversion to esm. superseeds #49

Copy link
Member

@antony antony left a comment

Choose a reason for hiding this comment

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

only nitpicks

.changeset/happy-toys-boil.md Outdated Show resolved Hide resolved
.gitignore Show resolved Hide resolved
@@ -15,9 +15,9 @@
"test:ci:serve": "cross-env VITE_PRESERVE_BUILD_ARTIFACTS=1 jest --verbose --no-cache --runInBand --force-exit --ci --json --outputFile=\"temp/serve/jest-results.json\"",
"test:ci:build": "cross-env VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 jest --verbose --no-cache --runInBand --force-exit --ci --json --outputFile=\"temp/build/jest-results.json\"",
"lint": "eslint --ignore-path .gitignore '**/*.{js,ts,svelte,html,svx,md}'",
"lint:fix": "pnpm lint -- --fix",
"lint:fix": "pnpm run lint -- --fix",
Copy link
Member

Choose a reason for hiding this comment

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

I think pnpm added a feature recently to allow you to drop the run part. Either way I feel like this bloats the PR - makes it harder to review.

Copy link
Member Author

Choose a reason for hiding this comment

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

that feature made me add it back, i wasn't sure if it would prefer a lint dependency over the lint script.

packages/playground/default-svelte-template/vite.config.js Outdated Show resolved Hide resolved
Copy link
Member

@lukeed lukeed left a comment

Choose a reason for hiding this comment

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

If you use a named export you can skip the require('@sveltejs/vite-plugin-svelte').default stuff.

And since you're adding "exports" map support and type: module, this is already a breaking change, so might as well lean into it further and add the named export. The benefit of this is that it's more consistent for the user, and the type definitions will work for both ESM and CJS

@bluwy
Copy link
Member

bluwy commented Jun 9, 2021

My 2c. I'd personally prefer if we stick with default exports in contrary to @lukeed's suggestion, to be consistent with Vite's plugin ecosystem. require('@sveltejs/vite-plugin-svelte').default is still negligible, plus most Vite configs are written in ESM instead of commonjs, so it shouldn't affect too many projects.

On that topic, I also agree with @antony that we should encourage ESM in the Vite config files.

@dominikg
Copy link
Member Author

dominikg commented Jun 9, 2021

Thank you for all your comments. Greatly appreciated ❤️

I like @lukeed suggestion of a named export and had thought about it too, but ultimately went with the default setup from other vite plugins @bluwy mentioned.

Sharable typings are a valid reason to do it and maybe we can start a trend for other vite plugins to follow?
What should the export be named though? svelte is the current name used in configs and examples, any objections?

I'd love to ditch cjs completely but feel thats a bit too soon. Esp. because right now it is still needed for tests with jest.

Regarding more prominent esm examples, should the playgroud be spit into e2e and examples section?
Gotta admit its a bit messy right now.

@dominikg
Copy link
Member Author

updated to named export but still included a default export with a deprecation warning:
https://github.com/sveltejs/vite-plugin-svelte/pull/54/files#diff-71fd6d8bccf1f224607bb27c94b502d32abf70103f0a2a59ad3acb5d229c443bR228

Without the default export, sveltekit breaks until updated (which includes the test in playground/kit-node)
We should coordinate a release of sveltekit that uses the named export so users can update gracefully as soon as they encounter the warning.

@bluwy
Copy link
Member

bluwy commented Jun 10, 2021

I'm still not sure if a named svelte export is the way to go here. It could ended up like rollup-plugin-terser where no other rollup plugins actually adopts the motive.

named exports ... the type definitions will work for both ESM and CJS

Forgive me if I'm not understanding properly, but how actually does type definitions work for CJS, since types imports and exports are usually done in ESM contexts. I don't think it's a strong argument to use named exports.

And shouldn't import svelte, { Options } from "@sveltejs/vite-plugin-svelte" work too?

@dominikg
Copy link
Member Author

dominikg commented Jun 10, 2021

regarding default export vs named export:

i asked about it on vite discord and while most plugins use a default export, there is eg. vite-plugin-pwa that uses a named export.
The examples on vite docs use a default export ( https://vitejs.dev/guide/api-plugin.html#transforming-custom-file-types ) as do all the official rollup plugins.

here are the links to the discussions back at terser plugin:
TrySound/rollup-plugin-terser#31 TrySound/rollup-plugin-terser#7

if it is technically more consistent i don't care much about two additional {} in an import statement, but it seems others do care and i'd like to avoid having discussions about this in the future, so ideally we can decide on a way forward here and stick to it.

so what should it be?

  1. default export
  • less work with sveltekit transition
  • follows unwritten convention
  1. named export
  • improved cjs experience
  • needs work to ensure smooth transition for sveltekit users

@lukeed maybe you can explain more about the benefits of the named export for cjs.

@benmccann
Copy link
Member

Another option would be to export as named and default export, but only put the named export in the type definitions and docs and make that the preferred but not required way

@lukeed
Copy link
Member

lukeed commented Jun 10, 2021

TypeScript only supports 1 export format in a definition file. Using a named export is the only way to expose information across ESM/CJS access. Specifically, export default excludes module.exports and this is why require('foo').default happens, even though it's not the CommonJS equivalent – it's a forced outcome of prior tooling mistakes & we're just keeping it around because we, as an ecosystem, think that's okay/how it's supposed to be. The use of .default means that the CJS source has to look like module.exports.default = or exports.default = , which is silly.

@TrySound cares about this too & that's why rollup-plugin-terser often looks like the odd one out. He's correct, though – everyone else is either pure ESM, doesn't care about CJS access, or expects Rollup users to use import & rely on Rollup to do the pre-bundle interop/conversion for them.

@bluwy
Copy link
Member

bluwy commented Jun 11, 2021

I guess it comes down to whether we want an awkward syntax for ESM or CJS. Given a majority of our userbase is on ESM (since the default Vite starter config is in ESM), I'd prefer default exports to please the crowd.

If it all comes down to preference, perhaps a public poll could end this once and for all.

EDIT: Made a mini poll on Vite discord.

@dominikg
Copy link
Member Author

removed the default export completely (as expected this caused the kit-node tests to fail).
With a coordinated release of vite-plugin-svelte and sveltekit, users of sveltekit should not run into it (unless they update vite-plugin-svelte without updating kit).

@dominikg dominikg merged commit 0f7e256 into main Jun 11, 2021
@dominikg dominikg deleted the feat/esm branch June 11, 2021 13:11
@github-actions github-actions bot mentioned this pull request Jun 11, 2021
@github-actions github-actions bot mentioned this pull request Jul 13, 2022
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.

5 participants