{"id":16927850,"url":"https://github.com/yuzhanglong/node-require-webpack-plugin","last_synced_at":"2026-05-10T14:42:27.138Z","repository":{"id":53460014,"uuid":"349271538","full_name":"yuzhanglong/node-require-webpack-plugin","owner":"yuzhanglong","description":"✨为 node.js 环境下的 webpack 打包结果提供动态 require 支持。","archived":false,"fork":false,"pushed_at":"2021-03-30T08:54:40.000Z","size":7,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-12T23:02:17.556Z","etag":null,"topics":["node","nodejs","webpack","webpack-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/yuzhanglong.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}},"created_at":"2021-03-19T02:00:21.000Z","updated_at":"2023-04-18T10:58:55.000Z","dependencies_parsed_at":"2022-08-24T03:10:50.288Z","dependency_job_id":null,"html_url":"https://github.com/yuzhanglong/node-require-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzhanglong%2Fnode-require-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzhanglong%2Fnode-require-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzhanglong%2Fnode-require-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzhanglong%2Fnode-require-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuzhanglong","download_url":"https://codeload.github.com/yuzhanglong/node-require-webpack-plugin/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244191575,"owners_count":20413321,"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":["node","nodejs","webpack","webpack-plugin"],"created_at":"2024-10-13T20:35:16.904Z","updated_at":"2026-05-10T14:42:22.109Z","avatar_url":"https://github.com/yuzhanglong.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# node-require-webpack-plugin\n\n为 node.js 环境下的 webpack 打包结果提供动态 require 支持。\n\n## 为什么要使用\n\n在某些情况下，我们需要让 webpack 打包的文件在 node.js 环境下运行，例如 GitHub Action 的开发，但是会有一些问题：\n\n```javascript\nconst a = require(process.argv)\n```\n\n上面的代码中，我们动态 `require` 了用户传入的参数，但 webpack 并不知道 -- 在 build 时它会尝试寻找 `process.argv` 的值并读取其内容以供打包，这很明显是无法做到的。\n\n因此在我们执行打包后的代码时会得到 `module not found` 异常。\n\n## 用法\n\n首先安装依赖：\n\n```bash\nnpm install node-require-webpack-plugin -D\n```\n\n添加 plugin 配置到 `webpack.config.js`:\n\n```javascript\nmodule.exports = {\n  plugins: [\n    new NodeRequireWebpackPlugin()\n  ]\n}\n```\n\n如果你的项目基于 `ts-loader`，请将 `transpileOnly` 置为 `true`：\n\n```javascript\n  module: {\n  rules: [\n    {\n      test: /\\.tsx?$/,\n      loader: 'ts-loader',\n      options: {\n        transpileOnly: true\n      }\n    }\n  ]\n}\n```\n\n在你需要动态 require 的语句前增加 `/*#__PURE_REQUIRE__*/` 标记，例如：\n\n```javascript\nconst requireResult =  /*#__PURE_REQUIRE__*/ require(`${configPath}`);\n```\n\n## 工作原理\n\n- 将所有用户标记 `/*#__PURE_REQUIRE__*/` 的模块全部换成一个函数 `__WEBPACK_PURE_REQUIRE__`\n- 为每个入口文件添加 `__WEBPACK_PURE_REQUIRE__` 的实现：\n\n```javascript\nfunction __WEBPACK_PURE_REQUIRE__(content) {\n  /******/\n  return require(content)\n}\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuzhanglong%2Fnode-require-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuzhanglong%2Fnode-require-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuzhanglong%2Fnode-require-webpack-plugin/lists"}