{"id":17957388,"url":"https://github.com/xiaoxiangmoe/vite-plugin-commonjs-externals","last_synced_at":"2026-03-11T19:31:56.476Z","repository":{"id":50746817,"uuid":"344210783","full_name":"xiaoxiangmoe/vite-plugin-commonjs-externals","owner":"xiaoxiangmoe","description":"Add commonjs external support for vite.  Mainly to provide vite support for electron.","archived":false,"fork":false,"pushed_at":"2023-11-30T13:57:56.000Z","size":59,"stargazers_count":65,"open_issues_count":2,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T12:12:11.960Z","etag":null,"topics":["commonjs","electron","vite-plugin"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/xiaoxiangmoe.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2021-03-03T17:37:50.000Z","updated_at":"2024-12-29T15:42:43.000Z","dependencies_parsed_at":"2023-01-30T17:31:27.367Z","dependency_job_id":"af7d48a4-945d-4693-bdde-dd4fb20e1d08","html_url":"https://github.com/xiaoxiangmoe/vite-plugin-commonjs-externals","commit_stats":{"total_commits":6,"total_committers":3,"mean_commits":2.0,"dds":0.5,"last_synced_commit":"f6a71b8044ecfabd4f734cc3527384c8107f7250"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xiaoxiangmoe/vite-plugin-commonjs-externals","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaoxiangmoe%2Fvite-plugin-commonjs-externals","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaoxiangmoe%2Fvite-plugin-commonjs-externals/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaoxiangmoe%2Fvite-plugin-commonjs-externals/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaoxiangmoe%2Fvite-plugin-commonjs-externals/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xiaoxiangmoe","download_url":"https://codeload.github.com/xiaoxiangmoe/vite-plugin-commonjs-externals/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaoxiangmoe%2Fvite-plugin-commonjs-externals/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30395598,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-11T18:46:22.935Z","status":"ssl_error","status_checked_at":"2026-03-11T18:46:17.045Z","response_time":84,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["commonjs","electron","vite-plugin"],"created_at":"2024-10-29T10:53:45.840Z","updated_at":"2026-03-11T19:31:56.459Z","avatar_url":"https://github.com/xiaoxiangmoe.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vite-plugin-commonjs-externals [![npm](https://img.shields.io/npm/v/vite-plugin-commonjs-externals.svg)](https://npmjs.com/package/vite-plugin-commonjs-externals)\n\nProvides commonjs externals support for Vite.\n\n## Description\n\nPrevent bundling of certain _esm_ `import`ed packages and instead retrieve these external dependencies at runtime by _commonjs_ `require`.\n\nFor example:\n\n```ts\nimport commonjsExternals from 'vite-plugin-commonjs-externals';\n\nconst externals = ['path', /^electron(\\/.+)?$/];\n\nexport default {\n  optimizeDeps: {\n    exclude: externals,\n  },\n  plugins: commonjsExternals({\n    externals,\n  }),\n};\n```\n\nThis will convert it\n\n```ts\nimport fs from 'fs';\nimport * as path from 'path';\nimport e1 from 'electron';\nimport e2, * as e3 from 'electron/main';\n\nconsole.log({ fs, path, e1, e2, e3 });\n```\n\nto\n\n```ts\nimport * as fs from 'fs';\nconst path = (() =\u003e {\n  const mod = require('path');\n  return mod?.__esModule\n    ? mod\n    : Object.assign(Object.create(null), mod, {\n        default: mod,\n        [Symbol.toStringTag]: 'Module',\n      });\n})();\nconst { default: e1 } = (() =\u003e {\n  const mod = require('electron');\n  return mod?.__esModule\n    ? mod\n    : Object.assign(Object.create(null), mod, {\n        default: mod,\n        [Symbol.toStringTag]: 'Module',\n      });\n})();\nconst e3 = (() =\u003e {\n  const mod = require('electron/main');\n  return mod?.__esModule\n    ? mod\n    : Object.assign(Object.create(null), mod, {\n        default: mod,\n        [Symbol.toStringTag]: 'Module',\n      });\n})();\nconst { default: e2 } = e3;\nconsole.log({ fs, path, e1, e2, e3 });\n```\n\n## React + Electron renderer Config Example\n\n```ts\n// vite.config.ts\nimport { defineConfig } from 'vite';\nimport { escapeRegExp } from 'lodash';\nimport reactRefresh from '@vitejs/plugin-react-refresh';\nimport builtinModules from 'builtin-modules';\n// For two package.json structure\nimport pkg from '../the-path-to-main-process-dir/package.json';\n// For single package.json structure\nimport pkg from './package.json';\nimport commonjsExternals from 'vite-plugin-commonjs-externals';\n\nconst commonjsPackages = [\n  'electron',\n  'electron/main',\n  'electron/common',\n  'electron/renderer',\n  'original-fs',\n  ...builtinModules,\n  ...Object.keys(pkg.dependencies).map(\n    name =\u003e new RegExp('^' + escapeRegExp(name) + '(\\\\/.+)?$')\n  ),\n] as const;\n\nexport default defineConfig({\n  optimizeDeps: {\n    exclude: commonjsPackages,\n  },\n  plugins: [reactRefresh(), commonjsExternals({ externals: commonjsPackages })],\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaoxiangmoe%2Fvite-plugin-commonjs-externals","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiaoxiangmoe%2Fvite-plugin-commonjs-externals","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaoxiangmoe%2Fvite-plugin-commonjs-externals/lists"}