Dav*_*vid 10 javascript external-dependencies typescript umd rollupjs
我正在使用 rollup 来捆绑一个库,并且我希望将外部依赖项与我的代码一起包含在 UMD 捆绑包中。我在文档中找不到任何关于此的有用信息。可能我遗漏了一些明显的东西,但似乎文档仅演示了如何将相关模块标记为外部模块。我一直在努力实现这一目标,但没有成功。可行吗?如果可行,如何实现?
\n我的代码使用外部组件: src/index.ts
import { ExternalComponent } from \'external-component\'\n\nfunction MyComponent()\xc2\xa0{\n const externalComponent = ExternalComponent()\n // ...\n}\n\nexport default MyComponent\nRun Code Online (Sandbox Code Playgroud)\n期望的输出: bundle.umd.js
function ExternalComponent() {\n // ...\n}\n\nfunction MyComponent()\xc2\xa0{\n const externalComponent = ExternalComponent()\n // ...\n}\nRun Code Online (Sandbox Code Playgroud)\nrollup.config.js
import babel from \'@rollup/plugin-babel\'\nimport typescript from \'rollup-plugin-typescript2\'\nimport resolve from \'@rollup/plugin-node-resolve\'\nimport { terser } from \'rollup-plugin-terser\'\nimport localTypescript from \'typescript\'\n\nconst CONFIG_BABEL = {\n extensions: [\'.js\', \'.jsx\', \'.ts\', \'.tsx\'],\n exclude: \'node_modules/**\',\n babelHelpers: \'bundled\',\n}\n\nconst CONFIG_TYPESCRIPT = {\n tsconfig: \'tsconfig.json\',\n typescript: localTypescript,\n}\n\nconst kebabCaseToPascalCase = (string = \'\') => {\n return string.replace(/(^\\w|-\\w)/g, (replaceString) =>\n replaceString.replace(/-/, \'\').toUpperCase(),\n )\n}\n\nexport default [\n {\n input: \'src/index.ts\',\n output: [\n {\n file: `${packageJson.name}.umd.js`,\n format: \'umd\',\n strict: true,\n sourcemap: false,\n name: kebabCaseToPascalCase(packageJson.name),\n plugins: [terser()],\n }\n ],\n plugins: [resolve(), typescript(CONFIG_TYPESCRIPT), babel(CONFIG_BABEL)],\n },\n]\nRun Code Online (Sandbox Code Playgroud)\npackage.json
{\n "types": "index.d.ts",\n "scripts": {\n "build": "rollup -c",\n "start": "rollup -c --watch",\n },\n "devDependencies": {\n "@babel/core": "7.17.0",\n "@rollup/plugin-babel": "^5.3.0",\n "@rollup/plugin-node-resolve": "13.1.3",\n "husky": "^4.3.8",\n "npm-run-all": "^4.1.5",\n "prettier": "2.5.1",\n "rollup": "^2.67.0",\n "rollup-plugin-terser": "^7.0.2",\n "rollup-plugin-typescript2": "^0.31.2",\n "typescript": "^4.5.5"\n },\n}\nRun Code Online (Sandbox Code Playgroud)\n提前致谢,
\n大卫
我从汇总文档中找到了一些内容:
如果您确实想将该模块包含在捆绑包中,则需要告诉 Rollup 如何找到它。大多数情况下,这是使用@rollup/plugin-node-resolve的问题。
但@rollup/plugin-node-resolve医生没有帮助。
| 归档时间: |
|
| 查看次数: |
2611 次 |
| 最近记录: |