Skip to content

Commit

Permalink
Merge branch 'umijs:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jaykou25 authored Sep 4, 2024
2 parents 8c14eef + 8ae268e commit a41b4b8
Show file tree
Hide file tree
Showing 33 changed files with 195 additions and 97 deletions.
2 changes: 1 addition & 1 deletion did-you-know/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default (api: any) => {

const cacheDir = path.join(api.paths.absNodeModulesPath, '.cache');
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir);
fs.mkdirSync(cacheDir, { recursive: true });
}
fs.writeFileSync(recordJSONPath, JSON.stringify(records), 'utf-8');
});
Expand Down
16 changes: 8 additions & 8 deletions examples/ssr-demo/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ export default {
routePrefetch: {},
manifest: {},
clientLoader: {},
mako: {
plugins: [
{
load: () => {},
},
],
},
// mako: {
// plugins: [
// {
// load: () => {},
// },
// ],
// },
ssr: {
builder: 'mako',
// builder: 'mako',
},
exportStatic: {},
styles: [`body { color: red; }`],
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "4.3.11",
"version": "4.3.18",
"workspaces": ["packages/*"]
}
2 changes: 1 addition & 1 deletion packages/ast/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/ast",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/ast",
"homepage": "https://github.com/umijs/umi/tree/master/packages/ast#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-umi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/babel-preset-umi",
"version": "4.3.11",
"version": "4.3.18",
"description": "Official babel preset for umi.",
"homepage": "https://github.com/umijs/umi/tree/master/packages/babel-preset-umi#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-esbuild/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/bundler-esbuild",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/bundler-esbuild",
"homepage": "https://github.com/umijs/umi/tree/master/packages/bundler-esbuild#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/bundler-utils",
"version": "4.3.11",
"version": "4.3.18",
"homepage": "https://github.com/umijs/umi/tree/master/packages/bundler-utils#readme",
"bugs": "https://github.com/umijs/umi/issues",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-vite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/bundler-vite",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/bundler-vite",
"homepage": "https://github.com/umijs/umi/tree/master/packages/bundler-vite#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-vite/src/config/transformer/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default (function target(userConfig) {
return false;
}

const isLegacy = isLegacyBrowser(userConfig.targets);
const isLegacy = isLegacyBrowser(userConfig.targets || {});

// convert { ie: 11 } to ['ie11']
// 低版本浏览器需要使用 legacy 插件 同时设置会有 warning
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-webpack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/bundler-webpack",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/bundler-webpack",
"homepage": "https://github.com/umijs/umi/tree/master/packages/bundler-webpack#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/core",
"version": "4.3.11",
"version": "4.3.18",
"homepage": "https://github.com/umijs/umi/tree/master/packages/core#readme",
"bugs": "https://github.com/umijs/umi/issues",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-umi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-umi",
"version": "4.3.11",
"version": "4.3.18",
"description": "create-umi",
"homepage": "https://github.com/umijs/umi/tree/master/packages/create-umi#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/lint/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/lint",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/lint",
"homepage": "https://github.com/umijs/umi/tree/master/packages/lint#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/max/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/max",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/max",
"homepage": "https://github.com/umijs/umi/tree/master/packages/max#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/mfsu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/mfsu",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/mfsu",
"homepage": "https://github.com/umijs/umi/tree/master/packages/mfsu#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/plugin-docs",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/plugin-docs",
"homepage": "https://github.com/umijs/umi/tree/master/packages/plugin-docs#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-run/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/plugin-run",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/plugin-run",
"homepage": "https://github.com/umijs/umi/tree/master/packages/plugin-run#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/plugins",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/plugins",
"homepage": "https://github.com/umijs/umi/tree/master/packages/plugins#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-umi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/preset-umi",
"version": "4.3.11",
"version": "4.3.18",
"description": "@umijs/preset-umi",
"homepage": "https://github.com/umijs/umi/tree/master/packages/preset-umi#readme",
"bugs": "https://github.com/umijs/umi/issues",
Expand Down Expand Up @@ -30,7 +30,7 @@
"@umijs/ast": "workspace:*",
"@umijs/babel-preset-umi": "workspace:*",
"@umijs/bundler-esbuild": "workspace:*",
"@umijs/bundler-mako": "0.7.9",
"@umijs/bundler-mako": "0.8.7",
"@umijs/bundler-utils": "workspace:*",
"@umijs/bundler-vite": "workspace:*",
"@umijs/bundler-webpack": "workspace:*",
Expand Down
58 changes: 57 additions & 1 deletion packages/preset-umi/src/features/mako/mako.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import path from 'path';
import { IApi } from '../../types';
import {
EntryAssets,
extractEntryAssets,
} from '../../utils/extractEntryAssets';
import { isWindows } from '../../utils/platform';

export default (api: IApi) => {
Expand Down Expand Up @@ -45,17 +49,52 @@ export default (api: IApi) => {
enableBy: api.EnableBy.config,
});

// html 处理逻辑
const assets: EntryAssets = {
// Will contain all js and mjs files
js: [],
// Will contain all css files
css: [],
};

api.modifyConfig((memo) => {
// @TODO remove this when mako support windows
if (isWindows) {
memo.mako = false;
process.env.OKAM = '';
}
const makoPlugins = memo.mako?.plugins || [];
if (!api.config.mpa) {
makoPlugins.push({
name: 'UmiHtmlGenerationMako',
generateEnd: ({ stats }: any) => {
const entryPointFiles = new Set<string>();

for (const chunk of stats.entrypoints['umi']?.chunks || []) {
const files = stats.chunks.find((c: any) => c.id === chunk).files;
for (const file of files) {
entryPointFiles.add(file);
}
}

let entryAssets = extractEntryAssets(Array.from(entryPointFiles));
Object.entries(entryAssets).forEach(([ext, files]) => {
if (!Array.isArray(assets[ext])) {
assets[ext] = [];
}
assets[ext].push(...files);
});
},
});
}
return {
...memo,
mfsu: false,
hmrGuardian: false,
makoPlugins: memo.mako?.plugins || [],
mako: {
...memo.mako,
plugins: makoPlugins,
},
};
});

Expand All @@ -78,4 +117,21 @@ export default (api: IApi) => {
console.error(e);
}
});

api.addHTMLStyles(() => {
const { publicPath } = api.config;
const displayPublicPath = publicPath === 'auto' ? '/' : publicPath;
return assets.css.map((css) => {
return `${displayPublicPath}${css}`;
});
});

api.addHTMLHeadScripts(() => {
const { publicPath } = api.config;
const displayPublicPath = publicPath === 'auto' ? '/' : publicPath;

return assets.js.map((js) => {
return `${displayPublicPath}${js}`;
});
});
};
49 changes: 10 additions & 39 deletions packages/preset-umi/src/features/webpack/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type {
Compiler,
} from '@umijs/bundler-webpack/compiled/webpack';
import { IApi } from '../../types';
import {
EntryAssets,
extractEntryAssets,
} from '../../utils/extractEntryAssets';

export default (api: IApi) => {
api.describe({
Expand All @@ -11,7 +15,7 @@ export default (api: IApi) => {
});

// html 处理逻辑
const assets: { js: string[]; css: string[]; [key: string]: string[] } = {
const assets: EntryAssets = {
// Will contain all js and mjs files
js: [],
// Will contain all css files
Expand All @@ -26,45 +30,12 @@ export default (api: IApi) => {
const entryPointFiles = compilation.entrypoints
.get('umi')!
.getFiles();

// Extract paths to .js, .mjs and .css files from the current compilation
const entryPointPublicPathMap: Record<string, boolean> = {};
const extensionRegexp = /\.(css|js|mjs)(\?|$)/;

const UMI_ASSETS_REG = {
js: /^umi(\..+)?\.js$/,
css: /^umi(\..+)?\.css$/,
};

entryPointFiles.forEach((entryPointPublicPath) => {
const extMatch = extensionRegexp.exec(entryPointPublicPath);
// Skip if the public path is not a .css, .mjs or .js file
if (!extMatch) {
return;
}

if (entryPointPublicPath.includes('.hot-update')) {
return;
let entryAssets = extractEntryAssets(entryPointFiles);
Object.entries(entryAssets).forEach(([ext, files]) => {
if (!Array.isArray(assets[ext])) {
assets[ext] = [];
}

// Skip if this file is already known
// (e.g. because of common chunk optimizations)
if (entryPointPublicPathMap[entryPointPublicPath]) {
return;
}

// umi html 默认会注入 不做处理
if (
UMI_ASSETS_REG.js.test(entryPointPublicPath) ||
UMI_ASSETS_REG.css.test(entryPointPublicPath)
) {
return;
}

entryPointPublicPathMap[entryPointPublicPath] = true;
// ext will contain .js or .css, because .mjs recognizes as .js
const ext = extMatch[1] === 'mjs' ? 'js' : extMatch[1];
assets[ext].push(entryPointPublicPath);
assets[ext].push(...files);
});
},
);
Expand Down
60 changes: 60 additions & 0 deletions packages/preset-umi/src/utils/extractEntryAssets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export type EntryAssets = {
js: string[];
css: string[];
[key: string]: string[];
};

export function extractEntryAssets(entryPointFiles: string[]): EntryAssets {
const assets: {
js: string[];
css: string[];
[key: string]: string[];
} = {
// Will contain all js and mjs files
js: [],
// Will contain all css files
css: [],
};

// Extract paths to .js, .mjs and .css files from the current compilation
const entryPointPublicPathMap: Record<string, boolean> = {};
const extensionRegexp = /\.(css|js|mjs)(\?|$)/;

const UMI_ASSETS_REG = {
js: /^umi(\..+)?\.js$/,
css: /^umi(\..+)?\.css$/,
};

entryPointFiles.forEach((entryPointPublicPath) => {
const extMatch = extensionRegexp.exec(entryPointPublicPath);
// Skip if the public path is not a .css, .mjs or .js file
if (!extMatch) {
return;
}

if (entryPointPublicPath.includes('.hot-update')) {
return;
}

// Skip if this file is already known
// (e.g. because of common chunk optimizations)
if (entryPointPublicPathMap[entryPointPublicPath]) {
return;
}

// umi html 默认会注入 不做处理
if (
UMI_ASSETS_REG.js.test(entryPointPublicPath) ||
UMI_ASSETS_REG.css.test(entryPointPublicPath)
) {
return;
}

entryPointPublicPathMap[entryPointPublicPath] = true;
// ext will contain .js or .css, because .mjs recognizes as .js
const ext = extMatch[1] === 'mjs' ? 'js' : extMatch[1];
assets[ext].push(entryPointPublicPath);
});

return assets;
}
Loading

0 comments on commit a41b4b8

Please sign in to comment.