Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-html): prevent errors arising from undeclared dependencies #13552

Closed
wants to merge 1 commit into from

Conversation

xty
Copy link

@xty xty commented Mar 31, 2023

这个 PR 做了什么? (简要描述所做更改)

With the latest stable release of Taro (v3.6.2), I ran into the following error when building with Yarn (v3.4.1):

$ yarn dev:weapp

> front_app@1.0.0 build:weapp
> taro build --type weapp --watch

👽 Taro v3.6.2

Error: @tarojs/plugin-html tried to access @babel/generator, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: @babel/generator
Required by: @tarojs/plugin-html@npm:3.6.2 (via /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-plugin-html-npm-3.6.2-79a6025293-babd09181c.zip/node_modules/@tarojs/plugin-html/dist/)

Require stack:
- /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-plugin-html-npm-3.6.2-79a6025293-babd09181c.zip/node_modules/@tarojs/plugin-html/dist/index.js
- /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-plugin-html-npm-3.6.2-79a6025293-babd09181c.zip/node_modules/@tarojs/plugin-html/index.js
- /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/dist/utils/index.js
- /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/dist/Kernel.js
- /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/dist/index.js
- /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/index.js
- /home/nobody/somewhere/front_app/.yarn/__virtual__/@tarojs-cli-virtual-b9b2585247/0/cache/@tarojs-cli-npm-3.6.2-94eed4029d-035dd606eb.zip/node_modules/@tarojs/cli/dist/cli.js
- /home/nobody/somewhere/front_app/.yarn/__virtual__/@tarojs-cli-virtual-b9b2585247/0/cache/@tarojs-cli-npm-3.6.2-94eed4029d-035dd606eb.zip/node_modules/@tarojs/cli/bin/taro
    at Function.require$$0.Module._resolveFilename (/home/nobody/somewhere/front_app/.pnp.cjs:31204:13)
    at Function.require$$0.Module._load (/home/nobody/somewhere/front_app/.pnp.cjs:31054:42)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-plugin-html-npm-3.6.2-79a6025293-babd09181c.zip/node_modules/@tarojs/plugin-html/dist/index.js:5:17)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at require$$0.Module._extensions..js (/home/nobody/somewhere/front_app/.pnp.cjs:31248:33)
    at Object.newLoader [as .js] (/home/nobody/somewhere/front_app/.yarn/cache/pirates-npm-4.0.5-22f8e827ce-c9994e61b8.zip/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/dist/utils/index.js:75
                    throw new Error(`插件依赖 "${item}" 加载失败,请检查插件配置`);
                          ^

Error: 插件依赖 "@tarojs/plugin-html" 加载失败,请检查插件配置
    at apply (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/src/utils/index.ts:72:17)
    at Kernel.initPlugin (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/src/Kernel.ts:157:5)
    at Kernel.resolvePlugins (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/src/Kernel.ts:130:12)
    at Kernel.initPresetsAndPlugins (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/src/Kernel.ts:115:10)
    at Kernel.<anonymous> (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/src/Kernel.ts:312:10)
    at Generator.next (<anonymous>)
    at /home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/dist/Kernel.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/dist/Kernel.js:4:12)
    at Kernel.run (/home/nobody/somewhere/front_app/.yarn/cache/@tarojs-service-npm-3.6.2-1974c43cf3-f438f0b30d.zip/node_modules/@tarojs/service/dist/Kernel.js:247:16)

Node.js v18.15.0

This is caused by the @tarojs/plugin-html not declaring all required dependencies in its package.json. Since the @babel/* packages are required not only for developing/building the plugin, but also at run time (i.e. when the plugin is used), they should be specified as dependencies rather than devDependencies.

import generator from '@babel/generator'
import * as parser from '@babel/parser'
import traverse from '@babel/traverse'
import * as t from '@babel/types'
import { isArray, isString } from '@tarojs/shared'
import * as path from 'path'

In the past, you could get away with it because installed packages are hoisted to the root of node_modules (a behaviour introduced by npm v3). However, phantom dependencies are known to be unsafe and has long been frowned upon. Moreover, these days, we can no longer rely on hoisting being in place, especially when newer package managers offer ways to explicitly guard against these violations (e.g. npm's install-strategy, Yarn's Plug'n'Play and pnpm's hoist=false).

这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix)
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 涉及以下平台:

  • 所有小程序
  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 字节跳动小程序
  • QQ 轻应用
  • 京东小程序
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)
  • 鸿蒙(harmony)

@taro-bot2
Copy link

taro-bot2 bot commented Mar 31, 2023

欢迎提交 PR~ Taro 非常感谢您对开源事业做出的贡献!🌷🌷🌷

一般 PR 会在一到两周内进行 review,成功合入后会随下一个版本进行发布。

Review 需要耗费大量时间,所以请遵循以下规范,协助我们提高 review 效率🙏🙏🙏

  1. 详细介绍 PR 的背景(非常重要,例如解决了什么问题,该问题如何复现等)
  2. 确保 CI 顺利运行。
  3. 最好能提供对应的测试用例。

为了更好地进行沟通,请加入 Taro 开发者微信群:

@xty xty force-pushed the fix/plugin-html-dependencies branch from c7810b0 to 50d3035 Compare April 6, 2023 19:56
@xty xty force-pushed the fix/plugin-html-dependencies branch from 50d3035 to 398aef1 Compare April 6, 2023 21:35
@xty
Copy link
Author

xty commented Apr 6, 2023

The lockfile has been updated to pass CI checks. Feel free to give it another go @AdvancedCat.

@AdvancedCat
Copy link
Member

It sounds great, good job.

@ZakaryCode
Copy link
Contributor

该分支受保护,合并前需要处理冲突 @xty

@Chen-jj Chen-jj added the question Further information is requested label Apr 27, 2023
@Chen-jj
Copy link
Contributor

Chen-jj commented Jun 7, 2023

@xty 感谢贡献~修改已迁移至 #13973

@Chen-jj Chen-jj closed this Jun 7, 2023
@xty
Copy link
Author

xty commented Jun 7, 2023

@xty 感谢贡献~修改已迁移至 #13973

@Chen-jj I'm glad to have helped. However, please note that it's customary to acknowledge your contributors in the commit history by appending a Co-authored-by trailer to the commit message.

Co-authored-by: XTY <xty@xty.io>

More can be learned here.

@AdvancedCat
Copy link
Member

We highly respect your work and apologize for forgetting to co-authored-by your name in the commit message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants