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
chore(): #77 export types, interfaces, and enums #90
Conversation
Thanks for the PR! The problem is we cannot actually use this approach as a lot of users use it in the browser as |
@horprogs Oh, I see! I hadn't realized you could just include the I see that there are already two files:
In that case, I think you could just use different entry points to build each file and support both formats. Disclaimer: I'm not really experienced with Vite, but I managed to make it work without too many changes (feel free to ignore the changes I'm proposing if you think they're too intrusive and/or they complicate the configuration a bit much 😇) Exporting everything from export default JustValidate;
export * from './modules/interfaces'; would allow people that import the ES module to use it like this: import JustValidate, { Rules } from 'just-validate'; Then, we could create another file (e.g. // `src/main.umd.ts`
import JustValidate from './main';
export default JustValidate; and change the
Since this project is still using Vite 2.x, we cannot directly set multiple entries, but we can work around that using an environment variable and calling So, the current configuration looks like this: entry: path.resolve(__dirname, 'src/main.ts'),
fileName: (format) =>
`just-validate.${format === 'umd' ? 'production.min' : format}.js`, And we can use const config = {
es: {
entry: resolve(__dirname, 'src/main.ts'),
fileName: () => 'just-validate.es.js',
},
umd: {
entry: resolve(__dirname, 'src/main.umd.ts'),
fileName: () => 'just-validate.production.min.js',
},
};
const currentConfig = config[process.env.OUTPUT_FORMAT]; Then, in return {
build: {
outDir: 'dist',
lib: {
...currentConfig, // <-- this is the object we built previously
formats: [process.env.OUTPUT_FORMAT], // by default this has a value of `['es', 'umd']`
name: 'JustValidate',
},
minify: 'terser',
rollupOptions: {
output: {
// `default` export for UMD modules, `named` exports for ES modules
exports: process.env.OUTPUT_FORMAT === 'es' ? 'named' : 'default',
},
},
}; Since we're going to run
Finally, we have to tweak a few things in our Introduce a couple of scripts to run {
"scripts": {
"build:es": "OUTPUT_FORMAT=es vite build",
"build:umd": "OUTPUT_FORMAT=umd vite build", Update the This looks like that:
That's it! Now running I've committed the changes I described in this PR and tested both:
Both cases seem to be working fine. Thanks for developing the library and taking the time to consider my PR 🙂 |
Great solution! Actually I just tried to upgrade to Vite 3 to use multiple entries to avoid running it twice with different env variables, but it seems it's impossible to use umd with multiple entries (they have a quite recent PR but it has not been merged yet vitejs/vite#10609). I'll try your way, test it and we'll see! |
Good job! Thanks! I merged, but forgot that |
Thanks for looking into this so quickly. I really appreciate it! 😄 |
🎉 This PR is included in version 3.9.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Closes #77.
Exports everything from
./modules/interfaces
, including theRules
enum, which was mentioned in #77 (and it was my use case as well).I've also set the
output.exports
rollup option to'named'
(through thebuild.rollupOptions
in the Vite config file) to disable this warning:If you take a look at the currently released version on
npm
,dist/just-validate.es.js
ends with:Building the modified version of this PR, generates a
dist/just-validate.es.js
ending with:So, I think this change won't break anything (feel free to correct me if I'm wrong).
I've built it via
yarn install && yarn build
and tested it on my TypeScript project — everything seems to be working fine.I'm using it like this: