Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit 9ea5f8b

Browse files
committed
feat(plugin-react): support htmlScriptCORS htmlScriptPriority
1 parent 972eb2b commit 9ea5f8b

File tree

12 files changed

+70
-10
lines changed

12 files changed

+70
-10
lines changed

‎packages/plugin-react/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## CHANGELOG
2+
3+
### 1.4.2
4+
- FEAT: add htmlScriptCORS in options

‎packages/plugin-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alicloud/console-toolkit-plugin-react",
3-
"version": "1.3.27",
3+
"version": "1.4.2",
44
"description": "console toolkit plugin for base react app",
55
"main": "lib/index.js",
66
"scripts": {

‎packages/plugin-react/src/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ interface BreezrReactOptions {
8181
htmlXmlMode?: boolean;
8282
htmInject?: boolean;
8383
htmlScriptLoading?: 'defer' | 'block';
84+
htmlScriptCORS?: boolean;
8485

8586
// experiment
8687
experiment?: {

‎packages/plugin-react/src/webpack/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { analyzerPlugin } from './plugins/analyzer';
1212
import { htmlInjectPlugin } from './plugins/htmlInject';
1313
import { BreezrReactOptions, CssConditionType } from '../types';
1414
import { momentPlugin } from './plugins/moment';
15+
import { HtmlData } from '../html';
1516

1617
const defaultOptions = {
1718
cwd: process.cwd(),
@@ -86,6 +87,7 @@ export const common = (config: Chain, options: BreezrReactOptions = defaultOptio
8687
disableConsoleOS,
8788
appId,
8889
tailwindcss,
90+
htmlScriptCORS
8991
} = options;
9092

9193
if (!cwd) {
@@ -169,7 +171,7 @@ export const common = (config: Chain, options: BreezrReactOptions = defaultOptio
169171

170172
// plugins
171173
if (!disableHtml) {
172-
const htmlData = api.dispatchSync('getHtmlData');
174+
const htmlData = api.dispatchSync<HtmlData>('getHtmlData');
173175

174176
htmlPlugin(config, {
175177
minify: { // 压缩HTML文件
@@ -196,6 +198,7 @@ export const common = (config: Chain, options: BreezrReactOptions = defaultOptio
196198
htmlInjectPlugin(config, {
197199
data: htmlData,
198200
htmlXmlMode,
201+
cors: htmlScriptCORS,
199202
});
200203
}
201204

‎packages/plugin-react/src/webpack/plugins/htmlInject.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,48 @@ import { HtmlData } from '../../html';
99

1010
interface HtmlInjectOption {
1111
data: HtmlData;
12-
htmlXmlMode: boolean;
12+
htmlXmlMode?: boolean;
13+
cors?: boolean;
1314
}
1415

1516
class HtmlInjectPlugin {
1617
private data: HtmlData;
18+
private cors: boolean;
1719

1820
public constructor(options: HtmlInjectOption) {
1921
this.data = options.data;
22+
this.cors = !!options.cors;
2023
}
2124

2225
public apply(compiler: webpack.Compiler) {
2326
compiler.hooks.compilation.tap(
2427
'HtmlInjectPlugin',
2528
(compilation) => {
29+
// @ts-ignore
30+
if (this.cors && compilation.hooks.htmlWebpackPluginAlterAssetTags) {
31+
// @ts-ignore
32+
compilation.hooks.htmlWebpackPluginAlterAssetTags.tap("HtmlInjectPlugin", (htmlPluginData, callback) => {
33+
if (htmlPluginData.body?.length) {
34+
htmlPluginData.body.forEach((tag: any) => {
35+
if (tag.tagName === 'script') {
36+
tag.attributes.crossorigin = 'anonymous';
37+
}
38+
});
39+
}
40+
41+
if (typeof callback === 'function') {
42+
callback(null, htmlPluginData);
43+
} else {
44+
return htmlPluginData;
45+
}
46+
});
47+
}
48+
2649
// @ts-ignore
2750
if (!compilation.hooks.htmlWebpackPluginAfterHtmlProcessing) {
2851
return;
2952
}
53+
3054
// @ts-ignore
3155
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('HtmlInjectPlugin', (data, callback) => {
3256
const $ = cheerio.load(data.html, {
@@ -85,7 +109,7 @@ class HtmlInjectPlugin {
85109
}
86110
}
87111

88-
export function htmlInjectPlugin(config: Chain, options: {[key: string]: any}) {
112+
export function htmlInjectPlugin(config: Chain, options: HtmlInjectOption) {
89113
createPlugin(
90114
config,
91115
'HtmlInjectPlugin',

‎packages/plugin-react/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"dom",
1111
"es2017.string"
1212
],
13-
"sourceMap": true,
1413
"rootDir": "src",
1514
"skipLibCheck": true,
1615
/* Strict Type-Checking Option */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## CHANGELOG
2+
3+
### 1.3.0
4+
- FEAT: add htmlScriptCORS in options
5+
- FEAT: add htmlScriptPriority in options

‎packages/plugin-webpack5-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alicloud/console-toolkit-plugin-webpack5-react",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"description": "console toolkit plugin for base react app",
55
"main": "lib/index.js",
66
"scripts": {

‎packages/plugin-webpack5-react/src/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ interface BreezrReactOptions {
8181
htmlXmlMode?: boolean;
8282
htmInject?: boolean;
8383
htmlScriptLoading?: 'defer' | 'block' | 'module';
84+
htmlScriptCORS?: boolean;
85+
htmlScriptPriority?: 'high' | 'low' | 'auto';
8486
mf?: {
8587
sharedOS?: [string, string][];
8688
};

‎packages/plugin-webpack5-react/src/webpack/common.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { BreezrReactOptions, CssConditionType } from '../types';
1414
import { momentPlugin } from './plugins/moment';
1515
import { providePlugin } from './plugins/provide';
1616
import { ModuleFederationPlugin } from './plugins/mf';
17+
import { HtmlData } from '../html';
1718

1819
const defaultOptions = {
1920
cwd: process.cwd(),
@@ -76,6 +77,8 @@ export const common = (config: Chain, options: BreezrReactOptions = defaultOptio
7677
analyze = false,
7778
hashPrefix = '',
7879
htmInject = true,
80+
htmlScriptCORS,
81+
htmlScriptPriority,
7982
disableAutoPrefixer = false,
8083
es5ImcompatibleVersions = false,
8184
es5IncompatibleVersions = false,
@@ -182,7 +185,7 @@ export const common = (config: Chain, options: BreezrReactOptions = defaultOptio
182185

183186
// plugins
184187
if (!disableHtml) {
185-
const htmlData = api.dispatchSync('getHtmlData');
188+
const htmlData = api.dispatchSync<HtmlData>('getHtmlData');
186189

187190
htmlPlugin(config, {
188191
minify: { // 压缩HTML文件
@@ -199,6 +202,8 @@ export const common = (config: Chain, options: BreezrReactOptions = defaultOptio
199202
htmlInjectPlugin(config, {
200203
data: htmlData,
201204
htmlXmlMode,
205+
cors: htmlScriptCORS,
206+
priority: htmlScriptPriority,
202207
});
203208
}
204209

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /