-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from chialab/vite-plugin-commonjs
Add vite-plugin-commonjs
- Loading branch information
Showing
11 changed files
with
214 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# vite-plugin-commonjs | ||
|
||
Convert CommonJS modules to ES Modules on the fly with Vite. | ||
|
||
This plugin works only when Vite is used in `serve` mode and aims to prevent the need of optimizing dependencies. | ||
|
||
## Install | ||
|
||
::: code-group | ||
|
||
```sh[npm] | ||
npm i -D @chialab/vite-plugin-commonjs | ||
``` | ||
|
||
```sh[yarn] | ||
yarn add -D @chialab/vite-plugin-commonjs | ||
``` | ||
|
||
```sh[pnpm] | ||
pnpm add -D @chialab/vite-plugin-commonjs | ||
``` | ||
|
||
::: | ||
|
||
## Usage | ||
|
||
```ts[vite.config.ts] | ||
import { defineConfig } from 'vite'; | ||
import commonjs from '@chialab/vite-plugin-commonjs'; | ||
export default defineConfig({ | ||
plugins: [ | ||
commonjs(), | ||
], | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
The plugin accepts an options object with the following properties: | ||
|
||
### `optimizeDeps` | ||
|
||
- Type: `boolean` | ||
|
||
Re-enable dependency optimization. By the default, the plugin disables Vite optimizations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Vitest Axe matchers | ||
# vitest-axe | ||
|
||
Axe violations matchers for Vitest. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Chialab | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<p align="center"> | ||
<strong>vite-plugin-commonjs</strong> • Support for dynamic commonjs transformation in Vite dev server. | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://www.npmjs.com/package/@chialab/vite-plugin-commonjs"><img alt="NPM" src="https://img.shields.io/npm/v/@chialab/vite-plugin-commonjs.svg?style=flat-square"></a> | ||
</p> | ||
|
||
--- | ||
|
||
## Install | ||
|
||
```sh | ||
npm i @chialab/vite-plugin-commonjs -D | ||
``` | ||
|
||
```sh | ||
yarn add @chialab/vite-plugin-commonjs -D | ||
``` | ||
|
||
## Documentation | ||
|
||
Read the documentation at [chialab.github.io/rna](https://chialab.github.io/rna/guide/vite-plugin-commonjs). | ||
|
||
--- | ||
|
||
## License | ||
|
||
**vite-plugin-commonjs** is released under the [MIT](https://github.com/chialab/rna/blob/main/packages/vite-plugin-commonjs/LICENSE) license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { maybeCommonjsModule, maybeMixedModule, transform, wrapDynamicRequire } from '@chialab/cjs-to-esm'; | ||
|
||
/** | ||
* Support for dynamic commonjs transformation in Vite dev server. | ||
* @param {{ optimizeDeps?: boolean }} options | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
export default function commonjs({ optimizeDeps = false } = {}) { | ||
return { | ||
name: 'commonjs', | ||
|
||
config(config, env) { | ||
if (env.command === 'build') { | ||
return config; | ||
} | ||
|
||
if (optimizeDeps) { | ||
return config; | ||
} | ||
|
||
return { | ||
...config, | ||
optimizeDeps: { | ||
...(config.optimizeDeps || {}), | ||
include: [], | ||
noDiscovery: true, | ||
}, | ||
}; | ||
}, | ||
|
||
async transform(code, id) { | ||
if (await maybeMixedModule(code)) { | ||
return wrapDynamicRequire(code, { | ||
source: id, | ||
sourcemap: true, | ||
sourcesContent: true, | ||
}); | ||
} | ||
|
||
if (await maybeCommonjsModule(code)) { | ||
return transform(code, { | ||
source: id, | ||
sourcemap: true, | ||
sourcesContent: true, | ||
helperModule: false, | ||
}); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@chialab/vite-plugin-commonjs", | ||
"type": "module", | ||
"version": "0.19.0", | ||
"description": "Support for dynamic commonjs transformation in Vite dev server.", | ||
"main": "lib/index.js", | ||
"typings": "./types/index.d.ts", | ||
"author": "Chialab <[email protected]> (https://www.chialab.it)", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/chialab/rna", | ||
"directory": "packages/vite-plugin-commonjs" | ||
}, | ||
"keywords": [], | ||
"files": [ | ||
"lib", | ||
"types", | ||
"package.json", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"dependencies": { | ||
"@chialab/cjs-to-esm": "^0.18.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./types", | ||
"declarationDir": "./types", | ||
"baseUrl": ".", | ||
"rootDir": "./lib" | ||
}, | ||
"include": ["lib/**/*"], | ||
"references": [ | ||
{ | ||
"path": "../cjs-to-esm" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters