
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
vite-plugin-optimizer
Advanced tools
Manually Pre-Bundling of Vite
English | 简体中文
npm i vite-plugin-optimizer -D
import optimizer from 'vite-plugin-optimizer'
export default {
plugins: [
optimizer({
vue: `const vue = window.Vue; export { vue as default }`,
}),
]
}
optimizer({
// support nested module id
// support return Promise
'@scope/name': () => require('fs/promises').readFile('path', 'utf-8'),
})
optimizer({
// optimize Electron for using `ipcRenderer` in Electron-Renderer
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,
// this means that both 'fs' and 'node:fs' are supported
// e.g.
// `import fs from 'fs'`
// or
// `import fs from 'node:fs'`
fs: () => ({
// this is consistent with the `alias` behavior
find: /^(node:)?fs$/,
code: `const fs = require('fs'); export { fs as default };`
}),
})
Optimize Node.js ESM packages as CommonJs modules for Node.js/Electron.
e.g. execa, node-fetch
You can see 👉 vite-plugin-esmodule
optimizer(entries[, options])
function optimizer(entries: Entries, options?: OptimizerOptions): import('vite').Plugin;
export interface OptimizerArgs {
/** Generated file cache directory */
dir: string;
}
export interface ResultDescription {
/**
* This is consistent with the `alias` behavior.
*
* e.g.
* `import fs from 'fs'`
* or
* `import fs from 'node:fs'`
*
* @example
* {
* // This means that both 'fs' and 'node:fs' are supported.
* find: /^(node:)?fs$/,
* replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js',
* }
*/
alias?: {
find: string | RegExp;
/**
* If not explicitly specified, will use the path to the generated file as the default.
*/
replacement?: string;
};
code?: string;
}
export interface Entries {
[moduleId: string]:
| string
| ResultDescription
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void);
}
export interface OptimizerOptions {
/**
* @default ".vite-plugin-optimizer"
*/
dir?: string;
resolveId?: ((id: string) => string | Promise<string | void> | void);
}
Let's use Vue as an example
optimizer({
vue: `const vue = window.Vue; export { vue as default }`,
})
node_modules/.vite-plugin-optimizer/vue.js
and contains the following codeconst vue = window.Vue; export { vue as default }
vue
alias item and add it to resolve.alias
{
resolve: {
alias: [
{
find: 'vue',
replacement: '/User/work-directory/node_modules/.vite-plugin-optimizer/vue',
},
],
},
}
/**
* 🚧
* If you are using a function and have no return value, alias will not be registered.
* In this case, you must explicitly specify alias.
*
* e.g.
*
* optimizer({
* async vue(args) {
*
* // ① You can customize the build `vue` and output it to the specified folder.
* await require('vite').build({
* entry: require.resolve('vue'),
* outputDir: args.dir + '/vue',
* })
*
* return {
* alias: {
* find: 'vue',
* // ② Make sure `replacement` points to the `vue` outputDir
* replacement: args.dir + '/vue',
* }
* }
* },
* })
*/
vue
to the optimizeDeps.exclude
by default.export default {
optimizeDeps: {
// 🚧 You can avoid this behavior by `optimizeDeps.include`
exclude: ['vue'],
},
}
[2024-01-08] v1.4.3
FAQs
Manually Pre-Bundling of Vite
The npm package vite-plugin-optimizer receives a total of 4,718 weekly downloads. As such, vite-plugin-optimizer popularity was classified as popular.
We found that vite-plugin-optimizer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.