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: correct entry script identification and webpack version detectio… #2800

Merged
merged 20 commits into from
Dec 14, 2023

Conversation

nayonglin
Copy link

@nayonglin nayonglin commented Nov 8, 2023

  1. 修正Qiankun插件的识别逻辑,以兼容html-webpack-plugin的defer行为。
  2. 修改默认读取webpack版本的逻辑由读取业务package.json字段优化为读取运行时webpack提供的version字段判断
  3. 修复test脚本导致CICD卡住的问题
  4. 用户已手动指定entry标签,则插件不再注入entry
Checklist
  • npm test passes
  • tests are included
  • documentation is changed or added
  • commit message follows commit guidelines
Description of change

Copy link

changeset-bot bot commented Nov 8, 2023

🦋 Changeset detected

Latest commit: b40dba4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@qiankunjs/webpack-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Nov 8, 2023

@nayonglin is attempting to deploy a commit to a Personal Account owned by @umijs on Vercel.

@umijs first needs to authorize it.

Copy link
Member

@kuitos kuitos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果用户已经指定了 entry 就不需要处理了,这块好像漏了?

packages/webpack-plugin/README-zh.md Outdated Show resolved Hide resolved
@nayonglin
Copy link
Author

如果用户已经指定了 entry 就不需要处理了,这块好像漏了?

已加上

packages/webpack-plugin/README-zh.md Outdated Show resolved Hide resolved
@@ -20,6 +20,6 @@ describe('QiankunPlugin', () => {
expect(htmlContent).toContain('<script entry'); // 检查是否正确标记了 entry js

const jsChunkContent = fs.readFileSync(path.join(__dirname, 'tests/webpack5/dist/bundle.js'), 'utf-8');
expect(jsChunkContent).toContain('window.'); // 检查是否包含了 window. 关键字
expect(jsChunkContent).toMatch(/window(\["[^"]+"\]|\.\w+)/); // 检查是否包含了 window. 或 window["..."] 关键字
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接检查 window.${packageName} 吧

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接检查 window.${packageName} 吧

已加上包名,不过同时判断了.符号和中括号的形式,因为发现在某些场景下webpack产物是中括号的这种形式

let alreadyHasEntry = false;

// 检查是否已经有带有entry属性的script标签
scriptTags.forEach((tag) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里换成 scriptTags.some 更好

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果 html 里找到了多个符合 entrySrcPattern 的 script,应该报错提示用户填的正则可能有问题

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果 html 里找到了多个符合 entrySrcPattern 的 script,应该报错提示用户填的正则可能有问题

这部分已用cheerio重写

private static packageJson: PackageJson = QiankunPlugin.readPackageJson();

constructor(options: QiankunPluginOptions = {}) {
this.packageName = options.packageName || QiankunPlugin.packageJson.name || '';
this.entrySrcPattern = options.entrySrcPattern
? new RegExp(`<script[^>]*src="[^"]*${options.entrySrcPattern.source}[^"]*"[^>]*></script>`, 'g')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用正则去匹配 script 很难写出没 bug 的版本,比如 script 这里有换行了

<script
  src=""
>

更极端的 html 里有一个 script 的注释。

直接引一个 cheerio 来做 html 匹配的事情吧,可以直接用 css selector 做元素查找,会少很多 bug

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用正则去匹配 script 很难写出没 bug 的版本,比如 script 这里有换行了

<script
  src=""
>

更极端的 html 里有一个 script 的注释。

直接引一个 cheerio 来做 html 匹配的事情吧,可以直接用 css selector 做元素查找,会少很多 bug

用cheerio可以,不过用户需要传入什么会比较简单一点?还需要传入正则吗?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像没有比正则更简单的吧?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像没有比正则更简单的吧?

done

@kuitos kuitos linked an issue Nov 15, 2023 that may be closed by this pull request
@kuitos
Copy link
Member

kuitos commented Nov 29, 2023

@nayonglin 这周能处理一下吗?准备发 3.0 正式版了😆

@nayonglin
Copy link
Author

@nayonglin 这周能处理一下吗?准备发 3.0 正式版了😆

嗯嗯,我明天处理一下

@qiYuei
Copy link
Contributor

qiYuei commented Dec 6, 2023

🙋‍♂️这块可以到 cli 测试了吗?

}
return htmlString;

return $.html();
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里还是改成 export class QiankunPlugin 使用 const { QiankunPlugin } = require('@qiankunjs/webpack-plugin')module.exports = QiankunPlugin 获取不到类型

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review 定位的行号有问题

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Member

@kuitos kuitos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先 approve 了,代码细节后面单独 pr 调整吧

if (!alreadyHasEntry) {
if (!this.entrySrcPattern) {
// 如果没有提供正则表达式,则选择最后一个 script 标签
$('script').last().attr('entry', '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要是最后一个外联 script,inline 的不支持

$('script').last().attr('entry', '');
} else {
// 使用提供的正则表达式过滤 script 标签
const matchingScriptTags = $('script').filter((_, el) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以直接取 srcipt[src] 的标签,不用 filter 里再过滤一遍

@kuitos kuitos merged commit b7ec9e7 into umijs:next Dec 14, 2023
5 of 6 checks passed
@kuitos kuitos linked an issue Feb 26, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Vue CLI 5项目中Webpack插件错误 [Bug] webpack-plugin 单测 ci 上无法运行
3 participants