https://github.com/oscarmarina/vite-plugin-prevent-rewrite-imports-type-module
A Vite plugin that prevents Vite from rewriting ES module imports by targeting <script type="module">
https://github.com/oscarmarina/vite-plugin-prevent-rewrite-imports-type-module
Last synced: 6 months ago
JSON representation
A Vite plugin that prevents Vite from rewriting ES module imports by targeting <script type="module">
- Host: GitHub
- URL: https://github.com/oscarmarina/vite-plugin-prevent-rewrite-imports-type-module
- Owner: oscarmarina
- Created: 2024-12-26T12:13:22.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-12-26T12:44:38.000Z (6 months ago)
- Last Synced: 2024-12-26T12:51:38.709Z (6 months ago)
- Language: JavaScript
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Vite Plugin Prevent Rewrite Imports Type Module
A plugin that prevents Vite from rewriting ES module imports by targeting `` tags with `type="module"`. This ensures that bare module imports remain unchanged during development.
### Technical Details
The plugin utilizes Vite's `transformIndexHtml` hooks to change `<script type="module">` to `<script type="nomodule">` during the `pre serve` phase. This prevents Vite from rewriting the imports. After the `post serve` phase, it reverts the changes to maintain the original HTML structure.
- [The plugin ensures it only operates during serve, and does not interfere with the build process.](https://vite.dev/guide/api-plugin.html#conditional-application)
### Installation
```bash
npm i -D @blockquote/vite-plugin-prevent-rewrite-imports-type-module
```### Usage
Add the `preventRewriteImportsTypeModule` to your Vite configuration:
```js
// vite.config.*
import {defineConfig} from 'vite';
import {preventRewriteImportsTypeModule} from '@blockquote/vite-plugin-prevent-rewrite-imports-type-module';export default defineConfig({
plugins: [preventRewriteImportsTypeModule()],
});```
### Configuration Options
| Option | Type | Default | Description |
| -------------- | ------- | -------- | ---------------------------------------------------- |
| `folderName` | String | `'demo'` | The target folder name to apply the plugin. |
| `excludeIndex` | Boolean | `true` | Whether to exclude `index.html` from transformation. |```js
// vite.config.*
import {defineConfig} from 'vite';
import {preventRewriteImportsTypeModule} from '@blockquote/vite-plugin-prevent-rewrite-imports-type-module';export default defineConfig({
plugins: [preventRewriteImportsTypeModule({excludeIndex: false})],
});```
### License
MIT