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

feat: webpack plugin #2699

Merged
merged 30 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ea31cbc
feat: 新增webapck插件
Sep 24, 2023
c4ccf55
feat: 新增webapck插件
nayonglin Sep 24, 2023
e4c4a84
feat: 完成qiankun插件,简化子应用改造难度
nayonglin Sep 25, 2023
15c8445
docs: update README.md for qiankun-plugin
nayonglin Sep 25, 2023
9612df1
fix: rename plugin to @qiankunjs/webpack-plugin
nayonglin Sep 25, 2023
aa39469
fix:Revert changes to globals.ts
nayonglin Sep 25, 2023
a8894df
refactor: migrate QiankunPlugin to TypeScript
nayonglin Sep 26, 2023
94bf900
docs: update examples
nayonglin Sep 26, 2023
7e24ee7
fix: change libraryTarget to window
nayonglin Sep 26, 2023
5413104
feat: feat: plugin add automatic 'entry' attribute addition to main s…
nayonglin Sep 27, 2023
8ccbfbb
refactor: rename qiankun plugin directory to webpack-plugin
nayonglin Sep 28, 2023
86a349d
fix: delete new pluginDemo
nayonglin Oct 7, 2023
d37111e
fix: change plugin to esm
nayonglin Oct 8, 2023
4284143
docs: add comment to plugin
nayonglin Oct 8, 2023
1d95b5a
fix: eslint to plugin
nayonglin Oct 8, 2023
92cf01d
feat: add English documentation for @qiankunjs/webpack-plugin
nayonglin Oct 11, 2023
a1f2c81
feat(tests): add automated test cases for QiankunPlugin
nayonglin Oct 19, 2023
5760f84
feat(tests): add automated test cases for QiankunPlugin
nayonglin Oct 19, 2023
24ba3c2
Merge branch 'next' into feat/webpack_plugin
kuitos Oct 23, 2023
eb97056
chore(deps): update pnpm lockfile
nayonglin Oct 23, 2023
ca4f1e8
chore(deps): update pnpm lockfile
nayonglin Oct 23, 2023
070295f
chore: update lockfile
aladdin-add Oct 24, 2023
87c8293
Merge remote-tracking branch 'upstream/next' into feat/webpack_plugin
nayonglin Oct 27, 2023
06da38d
Create modern-kiwis-tap.md
kuitos Oct 27, 2023
82a1c22
fix: eslint for plugin
nayonglin Oct 27, 2023
a3b3c9d
fix: prettier for plugin
nayonglin Oct 27, 2023
aa38e16
chore: update build cmd
nayonglin Oct 29, 2023
8852c46
Update package.json
kuitos Oct 30, 2023
d06d26b
Update package.json
kuitos Oct 30, 2023
996f340
Update package.json
kuitos Oct 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>QianKun</h1>
</header>
<div class="mainapp-main">
<!-- 侧边栏 -->
<ul class="mainapp-sidemenu">
<ul class="mainapp-sidemenu">
<li data-value='react16'>React16</li>
<li data-value='react15'>React15</li>
</ul>
Expand Down
4 changes: 4 additions & 0 deletions examples/react15/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { name } = require('./package');
const QiankunPlugin = require('../../packages/webpack-plugin/dist/cjs');



module.exports = {
entry: './index.js',
Expand Down Expand Up @@ -51,6 +54,7 @@ module.exports = {
collapseWhitespace: true,
},
}),
new QiankunPlugin(),
],
externals: {
react: 'React',
Expand Down
8 changes: 3 additions & 5 deletions examples/react16/.rescriptsrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { name } = require('./package');
const QiankunPlugin = require('../../packages/webpack-plugin/dist/cjs');


module.exports = {
webpack: config => {
config.output.library = `${name}-[name]`;
config.output.libraryTarget = 'umd';
config.output.jsonpFunction = `webpackJsonp_${name}`;
config.output.globalObject = 'window';
config.plugins.push(new QiankunPlugin());

return config;
},
Expand Down
6 changes: 6 additions & 0 deletions packages/webpack-plugin/.fatherrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cfg from '../../.fatherrc.cjs';

export default {
umd: {},
kuitos marked this conversation as resolved.
Show resolved Hide resolved
...cfg,
};
54 changes: 54 additions & 0 deletions packages/webpack-plugin/README.md
Copy link
Member

Choose a reason for hiding this comment

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

README-zh.md 吧,还需要一个英文版本的

Copy link
Author

Choose a reason for hiding this comment

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

README-zh.md 吧,还需要一个英文版本的

done

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# @qiankunjs/webpack-plugin

`@qiankunjs/webpack-plugin` 是一个为 [qiankun](https://github.com/umijs/qiankun) 微前端框架设计的 Webpack 插件,用于简化和自动化与 qiankun 集成的一些常见配置。

## 功能

- 自动设置输出库的名称和格式。
- 确保 `jsonpFunction` 名称的唯一性。
- 设置全局对象为 `window`,确保库可以在浏览器中运行。
- 自动为 html 中的入口script标签加上entry标记

## 安装

使用 npm:

```bash
npm install @qiankunjs/webpack-plugin --save-dev
```

或使用 yarn:

```bash
yarn add @qiankunjs/webpack-plugin --dev
```

## 使用

在您的 `webpack.config.js` 或其他配置文件中:

```javascript
const QiankunPlugin = require('@qiankunjs/webpack-plugin');

module.exports = {
// ... 其他配置
plugins: [
new QiankunPlugin({
packageName: 'optionalPackageName', // 可选,如果不提供,将使用 package.json 中的名称
})
]
};
```

## 选项

- `packageName`: 指定输出库的名称。如果未提供,将使用 `package.json` 中的名称。

## 贡献

欢迎任何形式的贡献!请提交 PR 或开启 issue 讨论。

## 许可证

MIT

24 changes: 24 additions & 0 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@qiankunjs/webpack-plugin",
"version": "0.0.1",
"description": "",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "father build"
},
"files": [
"dist",
"src"
],
"peerDependencies": {
"webpack": "^4.0.0 || ^5.0.0"
},
"author": "Hermanna",
"license": "MIT",
"devDependencies": {
"@types/webpack-sources": "^3.2.1",
"webpack-sources": "^3.2.3"
kuitos marked this conversation as resolved.
Show resolved Hide resolved
}
}
56 changes: 56 additions & 0 deletions packages/webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fs from 'fs';
import path from 'path';
import { Compiler, Configuration } from 'webpack';
import { RawSource } from 'webpack-sources';

const projectRoot: string = process.cwd();
const packageJsonPath: string = path.join(projectRoot, 'package.json');
const packageJson: { name?: string } = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));

interface QiankunPluginOptions {
packageName?: string;
}

class QiankunPlugin {
private packageName: string;

constructor(options: QiankunPluginOptions = {}) {
this.packageName = options.packageName || packageJson.name || '';
}

apply(compiler: Compiler): void {
const webpackCompilerOptions = compiler.options as Configuration & { output: { jsonpFunction?: string } };
webpackCompilerOptions.output.library = `${this.packageName}`;
webpackCompilerOptions.output.libraryTarget = 'window';
webpackCompilerOptions.output.jsonpFunction = `webpackJsonp_${this.packageName}`;
webpackCompilerOptions.output.globalObject = 'window';
webpackCompilerOptions.output.chunkLoadingGlobal = `webpackJsonp_${this.packageName}`;

compiler.hooks.emit.tapAsync('QiankunPlugin', (compilation, callback) => {
Object.keys(compilation.assets).forEach(filename => {
if (filename.endsWith('.html')) {
const htmlSource = compilation.assets[filename].source();
const htmlString = typeof htmlSource === 'string' ? htmlSource : htmlSource.toString('utf-8');

// 找到所有的 <script> 标签
const scriptTags = htmlString.match(/<script[^>]*src="[^"]+"[^>]*><\/script>/g) || [];
// 筛选出不包含 defer 和 async 属性的标签
const nonAsyncOrDeferScripts = scriptTags.filter(tag => !(/defer|async/.test(tag)));

if (nonAsyncOrDeferScripts.length) {
// 获取最后一个 <script> 标签
const lastScriptTag = nonAsyncOrDeferScripts[nonAsyncOrDeferScripts.length - 1];
// 添加 entry 属性
const modifiedScriptTag = lastScriptTag.replace('<script', '<script entry');
const modifiedHtml = htmlString.replace(lastScriptTag, modifiedScriptTag);
compilation.assets[filename] = new RawSource(modifiedHtml) as any;
}
}
});
callback();
});
}
}

module.exports = QiankunPlugin;
nayonglin marked this conversation as resolved.
Show resolved Hide resolved

3 changes: 3 additions & 0 deletions packages/webpack-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}
Loading