{"id":13417215,"url":"https://github.com/vite-plugin/vite-plugin-optimizer","last_synced_at":"2025-04-30T14:29:53.497Z","repository":{"id":45696792,"uuid":"502486966","full_name":"vite-plugin/vite-plugin-optimizer","owner":"vite-plugin","description":"Manually Pre-Bundling of Vite","archived":false,"fork":false,"pushed_at":"2024-01-08T03:05:46.000Z","size":48,"stargazers_count":39,"open_issues_count":1,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-30T14:29:45.411Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/vite-plugin-optimizer","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vite-plugin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-12T00:44:32.000Z","updated_at":"2025-04-11T03:37:18.000Z","dependencies_parsed_at":"2024-06-18T19:45:43.765Z","dependency_job_id":"8c7e9a09-096c-4f19-9a4e-0308394a4f4c","html_url":"https://github.com/vite-plugin/vite-plugin-optimizer","commit_stats":{"total_commits":29,"total_committers":4,"mean_commits":7.25,"dds":"0.13793103448275867","last_synced_commit":"756b9fdfa4fa4a1e263f6d4c3329f312b251cd18"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vite-plugin%2Fvite-plugin-optimizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vite-plugin%2Fvite-plugin-optimizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vite-plugin%2Fvite-plugin-optimizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vite-plugin%2Fvite-plugin-optimizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vite-plugin","download_url":"https://codeload.github.com/vite-plugin/vite-plugin-optimizer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251721321,"owners_count":21632807,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T22:00:34.019Z","updated_at":"2025-04-30T14:29:53.472Z","avatar_url":"https://github.com/vite-plugin.png","language":"JavaScript","funding_links":[],"categories":["Plugins"],"sub_categories":["Framework-agnostic Plugins"],"readme":"# vite-plugin-optimizer\n\nManually Pre-Bundling of Vite\n\n[![NPM version](https://img.shields.io/npm/v/vite-plugin-optimizer.svg)](https://npmjs.org/package/vite-plugin-optimizer)\n[![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-optimizer.svg)](https://npmjs.org/package/vite-plugin-optimizer)\n[![awesome-vite](https://awesome.re/badge.svg)](https://github.com/vitejs/awesome-vite)\n\nEnglish | [简体中文](https://github.com/vite-plugin/vite-plugin-optimizer/blob/main/README.zh-CN.md)\n\n- Compatible Browser, Node.js and Electron\n- Custom Vite [Pre-Bundling](https://vitejs.dev/guide/dep-pre-bundling.html) content\n\n## Install\n\n```bash\nnpm i vite-plugin-optimizer -D\n```\n\n## Usage\n\n```ts\nimport optimizer from 'vite-plugin-optimizer'\n\nexport default {\n  plugins: [\n    optimizer({\n      vue: `const vue = window.Vue; export { vue as default }`,\n    }),\n  ]\n}\n```\n\n#### Load a local file\n\n```ts\noptimizer({\n  // support nested module id\n  // support return Promise\n  '@scope/name': () =\u003e require('fs/promises').readFile('path', 'utf-8'),\n})\n```\n\n#### Node.js and Electron\n\n```ts\noptimizer({\n  // optimize Electron for using `ipcRenderer` in Electron-Renderer\n  electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,\n\n  // this means that both 'fs' and 'node:fs' are supported\n  // e.g.\n  //   `import fs from 'fs'`\n  //   or\n  //   `import fs from 'node:fs'`\n  fs: () =\u003e ({\n    // this is consistent with the `alias` behavior\n    find: /^(node:)?fs$/,\n    code: `const fs = require('fs'); export { fs as default };`\n  }),\n})\n```\n\n## Advance\n\nOptimize Node.js ESM packages as CommonJs modules for Node.js/Electron.  \n**e.g.** [execa](https://www.npmjs.com/package/execa), [node-fetch](https://www.npmjs.com/package/node-fetch)\n\nYou can see 👉 [vite-plugin-esmodule](https://github.com/vite-plugin/vite-plugin-esmodule)\n\n## API \u003csub\u003e\u003csup\u003e(Define)\u003c/sup\u003e\u003c/sub\u003e\n\n`optimizer(entries[, options])`\n\n```ts\nfunction optimizer(entries: Entries, options?: OptimizerOptions): import('vite').Plugin;\n```\n\n```ts\nexport interface OptimizerArgs {\n  /** Generated file cache directory */\n  dir: string;\n}\n\nexport interface ResultDescription {\n  /**\n   * This is consistent with the `alias` behavior.\n   * \n   * e.g.  \n   *   `import fs from 'fs'`  \n   *   or  \n   *   `import fs from 'node:fs'`  \n   * \n   * @example\n   * {\n   *   // This means that both 'fs' and 'node:fs' are supported.\n   *   find: /^(node:)?fs$/,\n   *   replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js',\n   * }\n   */\n  alias?: {\n    find: string | RegExp;\n    /**\n     * If not explicitly specified, will use the path to the generated file as the default.\n     */\n    replacement?: string;\n  };\n  code?: string;\n}\n\nexport interface Entries {\n  [moduleId: string]:\n  | string\n  | ResultDescription\n  | ((args: OptimizerArgs) =\u003e string | ResultDescription | Promise\u003cstring | ResultDescription | void\u003e | void);\n}\n\nexport interface OptimizerOptions {\n  /**\n   * @default \".vite-plugin-optimizer\"\n   */\n  dir?: string;\n  resolveId?: ((id: string) =\u003e string | Promise\u003cstring | void\u003e | void);\n}\n```\n\n## How to work\n\nLet's use Vue as an example\n\n```js\noptimizer({\n  vue: `const vue = window.Vue; export { vue as default }`,\n})\n```\n\n1. Create `node_modules/.vite-plugin-optimizer/vue.js` and contains the following code\n\n```js\nconst vue = window.Vue; export { vue as default }\n```\n\n2. Register a `vue` alias item and add it to `resolve.alias`\n\n```js\n{\n  resolve: {\n    alias: [\n      {\n        find: 'vue',\n        replacement: '/User/work-directory/node_modules/.vite-plugin-optimizer/vue',\n      },\n    ],\n  },\n}\n\n/**\n * 🚧\n * If you are using a function and have no return value, alias will not be registered.\n * In this case, you must explicitly specify alias.\n * \n * e.g.\n * \n * optimizer({\n *   async vue(args) {\n * \n *     // ① You can customize the build `vue` and output it to the specified folder.\n *     await require('vite').build({\n *       entry: require.resolve('vue'),\n *       outputDir: args.dir + '/vue',\n *     })\n * \n *     return {\n *       alias: {\n *         find: 'vue',\n *         // ② Make sure `replacement` points to the `vue` outputDir\n *         replacement: args.dir + '/vue',\n *       }\n *     }\n *   },\n * })\n */\n```\n\n3. Add `vue` to the `optimizeDeps.exclude` by default.  \n\n```js\nexport default {\n  optimizeDeps: {\n    // 🚧 You can avoid this behavior by `optimizeDeps.include`\n    exclude: ['vue'],\n  },\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvite-plugin%2Fvite-plugin-optimizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvite-plugin%2Fvite-plugin-optimizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvite-plugin%2Fvite-plugin-optimizer/lists"}