Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/egoist/vite-plugin-remove-exports
A Vite plugin to remove certain exports
https://github.com/egoist/vite-plugin-remove-exports
Last synced: about 1 month ago
JSON representation
A Vite plugin to remove certain exports
- Host: GitHub
- URL: https://github.com/egoist/vite-plugin-remove-exports
- Owner: egoist
- License: mit
- Created: 2021-11-30T16:37:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-04T14:54:21.000Z (over 1 year ago)
- Last Synced: 2024-05-02T01:16:36.891Z (about 2 months ago)
- Language: TypeScript
- Homepage:
- Size: 46.9 KB
- Stars: 24
- Watchers: 2
- Forks: 2
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Lists
- awesome-server-side-rendering - vite-plugin-remove-exports
README
**💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).**
---
# vite-plugin-remove-exports
[![npm version](https://badgen.net/npm/v/vite-plugin-remove-exports)](https://npm.im/vite-plugin-remove-exports) [![npm downloads](https://badgen.net/npm/dm/vite-plugin-remove-exports)](https://npm.im/vite-plugin-remove-exports)
## Use Case
It's useful if you want to implement something like Next.js' `getServerSideProps` or the `loader` function from Remix.js. This is done by creating a proxy of your code with the exports you specfied excluded and using esbuild to bundle the proxy code in order to eliminate unneeded exports.
Despite that we added an extra step to the build process, the plugin is still very fast due the how fast esbuild is.
## Install
```bash
npm i vite-plugin-remove-exports
```## Usage
`vite.config.ts`:
```ts
export default {
plugins: [
removeExports({
match() {
return ['getServerSideProps']
},
}),
],
}
```Now if you have a `index.tsx`:
```tsx
import fs from 'fs'export const getServerSideProps = () => {
return {
content: fs.readFileSync('./foo.txt', 'utf-8'),
}
}export default ({ content }) => {
return{content}
}
```The output will be:
```tsx
export default ({ content }) => {
return{content}
}
```Advanced usage:
```ts
removeExports({
match(filepath, ssr) {
// Ignore SSR build
if (!ssr) return// Remove getServerSideProps in "pages" in browser build
if (filepath.startsWith(pagesDir)) {
return ['getServerSideProps']
}
},
}),
```## Caveats
- `export *` is not supported.
- When using both Vue's `` and `<script>` tags in a single file, the imports in regular `<script>` tags can't be removed even if they're only used in removed exports.## Sponsors
[![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)
## License
MIT © [EGOIST](https://github.com/sponsors/egoist)