-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preset-umi): codeSplitting support auto load chunk (#9859)
* feat(preset-umi): codeSplitting support auto load chunk * feat(preset-umi): codeSplitting config update * feat(preset-umi): add custom htmlWebpackPlugin * feat(preset-umi): dynamic loading of chunk is only enabled for production
- Loading branch information
1 parent
5e6abc6
commit 34bf7ef
Showing
7 changed files
with
104 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type webpack from '@umijs/bundler-webpack/compiled/webpack'; | ||
import { IApi } from '../../types'; | ||
|
||
export default (api: IApi) => { | ||
api.describe({ | ||
key: 'preset-umi:webpack', | ||
enableBy: () => api.env === 'production', | ||
}); | ||
|
||
// html 处理逻辑 | ||
const assets: { js: string[]; css: string[]; [key: string]: string[] } = { | ||
// Will contain all js and mjs files | ||
js: [], | ||
// Will contain all css files | ||
css: [], | ||
}; | ||
|
||
class HtmlWebpackPlugin { | ||
apply(compiler: webpack.Compiler) { | ||
compiler.hooks.emit.tapPromise( | ||
'UmiHtmlGeneration', | ||
async (compilation) => { | ||
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; | ||
} | ||
|
||
// 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); | ||
}); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
api.modifyWebpackConfig((config) => { | ||
// 处理 代码拆分时, 拆分的 非 入口文件, 自动注入到 html 文件中 | ||
config.plugins?.push(new HtmlWebpackPlugin()); | ||
return config; | ||
}); | ||
|
||
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}`; | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters