-
-
Notifications
You must be signed in to change notification settings - Fork 63
refactor: migrate lib.autoExternal to output.autoExternal
#1641
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
Changes from all commits
72d0ecf
05a3a06
3f14a22
f1729be
99d1fa6
f28abb2
72c73b6
ebc1ad9
4079f62
46d48fb
37cf023
a67a5cd
204311f
dddc7b2
f0a56ba
140a9a2
d445104
a12ea92
ecc78f5
003de77
4e6b349
790f176
c5fb629
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,6 @@ import { composeExeConfig } from './exe'; | |
| import { composeEntryChunkConfig } from './plugins/EntryChunkPlugin'; | ||
| import { pluginCjsShims, pluginEsmRequireShim } from './plugins/shims'; | ||
| import type { | ||
| AutoExternal, | ||
| BannerAndFooter, | ||
| DeepRequired, | ||
| ExcludesFalse, | ||
|
|
@@ -56,7 +55,6 @@ import { | |
| isDirectory, | ||
| isEmptyObject, | ||
| isIntermediateOutputFormat, | ||
| isObject, | ||
| nodeBuiltInModules, | ||
| normalizeSlash, | ||
| omit, | ||
|
|
@@ -72,88 +70,6 @@ import { | |
| } from './utils/syntax'; | ||
| import { loadTsconfig } from './utils/tsconfig'; | ||
|
|
||
| const getAutoExternalDefaultValue = ( | ||
| format: Format, | ||
| autoExternal?: AutoExternal, | ||
| ): AutoExternal => { | ||
| return autoExternal ?? isIntermediateOutputFormat(format); | ||
| }; | ||
|
|
||
| export const composeAutoExternalConfig = (options: { | ||
| bundle: boolean; | ||
| format: Format; | ||
| autoExternal?: AutoExternal; | ||
| pkgJson?: PkgJson; | ||
| userExternals?: NonNullable<EnvironmentConfig['output']>['externals']; | ||
| }): EnvironmentConfig => { | ||
| const { bundle, format, pkgJson, userExternals } = options; | ||
|
|
||
| // If bundle is false, autoExternal will be disabled | ||
| if (!bundle) { | ||
| return {}; | ||
| } | ||
|
|
||
| const autoExternal = getAutoExternalDefaultValue( | ||
| format, | ||
| options.autoExternal, | ||
| ); | ||
|
|
||
| if (autoExternal === false) { | ||
| return {}; | ||
| } | ||
|
|
||
| if (!pkgJson) { | ||
| logger.warn( | ||
| 'The `autoExternal` configuration will not be applied due to read package.json failed', | ||
| ); | ||
| return {}; | ||
| } | ||
|
|
||
| // User externals configuration has higher priority than autoExternal | ||
| // eg: autoExternal: ['react'], user: output: { externals: { react: 'react-1' } } | ||
| // Only handle the case where the externals type is object, string / string[] does not need to be processed, other types are too complex. | ||
| const userExternalKeys = | ||
| userExternals && isObject(userExternals) ? Object.keys(userExternals) : []; | ||
|
|
||
| const externalOptions = { | ||
| dependencies: true, | ||
| optionalDependencies: true, | ||
| peerDependencies: true, | ||
| devDependencies: false, | ||
| ...(autoExternal === true ? {} : autoExternal), | ||
| }; | ||
|
|
||
| const externals = ( | ||
| [ | ||
| 'dependencies', | ||
| 'peerDependencies', | ||
| 'devDependencies', | ||
| 'optionalDependencies', | ||
| ] as const | ||
| ) | ||
| .reduce<string[]>((prev, type) => { | ||
| if (externalOptions[type]) { | ||
| return pkgJson[type] ? prev.concat(Object.keys(pkgJson[type])) : prev; | ||
| } | ||
| return prev; | ||
| }, []) | ||
| .filter((name) => !userExternalKeys.includes(name)); | ||
|
|
||
| const uniqueExternals = Array.from(new Set(externals)); | ||
|
|
||
| return externals.length | ||
| ? { | ||
| output: { | ||
| externals: [ | ||
| // Exclude dependencies, e.g. `react`, `react/jsx-runtime` | ||
| ...uniqueExternals.map((dep) => new RegExp(`^${dep}($|\\/|\\\\)`)), | ||
| ...uniqueExternals, | ||
| ], | ||
| }, | ||
| } | ||
| : {}; | ||
| }; | ||
|
|
||
| export function composeMinifyConfig(config: LibConfig): EnvironmentConfig { | ||
| const minify = config.output?.minify; | ||
| const format = config.format; | ||
|
|
@@ -406,6 +322,7 @@ const composeFormatConfig = ({ | |
| plugins: [modifyRsbuildDefaultPlugin({ disableUrlParse: true })], | ||
| output: { | ||
| filenameHash: false, | ||
| ...(bundle && { autoExternal: true }), | ||
| }, | ||
| tools: { | ||
| rspack: { | ||
|
|
@@ -451,6 +368,7 @@ const composeFormatConfig = ({ | |
| output: { | ||
| module: false, | ||
| filenameHash: false, | ||
| ...(bundle && { autoExternal: true }), | ||
| }, | ||
| tools: { | ||
| rspack: { | ||
|
|
@@ -1476,7 +1394,7 @@ const composeDtsConfig = async ( | |
| format: Format, | ||
| dtsExtension: string, | ||
| ): Promise<EnvironmentConfig> => { | ||
| const { autoExternal, banner, footer, redirect } = libConfig; | ||
| const { banner, footer, redirect } = libConfig; | ||
|
|
||
| let { dts } = libConfig; | ||
|
|
||
|
|
@@ -1499,7 +1417,10 @@ const composeDtsConfig = async ( | |
| build: dts?.build, | ||
| abortOnError: dts?.abortOnError, | ||
| dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts', | ||
| autoExternal: getAutoExternalDefaultValue(format, autoExternal), | ||
| autoExternal: | ||
| libConfig.output?.autoExternal ?? | ||
| libConfig.autoExternal ?? | ||
| isIntermediateOutputFormat(format), | ||
|
Comment on lines
+1420
to
+1423
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| alias: dts?.alias, | ||
| isolated: dts?.isolated, | ||
| banner: banner?.dts, | ||
|
|
@@ -1653,13 +1574,13 @@ async function composeLibRsbuildConfig( | |
| banner = {}, | ||
| footer = {}, | ||
| autoExtension = true, | ||
| autoExternal, | ||
| externalHelpers = false, | ||
| redirect = {}, | ||
| umdName, | ||
| experiments = {}, | ||
| } = config; | ||
| const hasExe = Boolean(experiments.exe); | ||
|
|
||
| const { rsbuildConfig: bundleConfig } = composeBundleConfig(bundle); | ||
| const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig( | ||
| format, | ||
|
|
@@ -1726,13 +1647,7 @@ async function composeLibRsbuildConfig( | |
| config?.syntax, | ||
| pkgJson, | ||
| ); | ||
| const autoExternalConfig = composeAutoExternalConfig({ | ||
| bundle, | ||
| format, | ||
| autoExternal: hasExe ? false : autoExternal, | ||
| pkgJson, | ||
| userExternals, | ||
| }); | ||
|
|
||
| const cssConfig = composeCssConfig( | ||
| outBase, | ||
| cssModulesAuto, | ||
|
|
@@ -1772,7 +1687,6 @@ async function composeLibRsbuildConfig( | |
| // it relies on other externals config to bail out the externalized modules first then resolve | ||
| // the correct path for relative imports. | ||
| userExternalsConfig, | ||
| autoExternalConfig, | ||
| targetExternalsConfig, | ||
| bundlelessExternalConfig, | ||
| // #endregion | ||
|
|
@@ -1820,11 +1734,33 @@ export async function composeCreateRsbuildConfig( | |
| } | ||
|
|
||
| const libConfigPromises = libConfigsArray.map(async (libConfig, index) => { | ||
| if (libConfig.autoExternal !== undefined) { | ||
| logger.warn( | ||
| `${color.yellow('lib.autoExternal')} is deprecated, use ${color.yellow('output.autoExternal')} instead.`, | ||
| ); | ||
| } | ||
|
|
||
| const userConfig = mergeRsbuildConfig<LibConfig>( | ||
| sharedRsbuildConfig, | ||
| libConfig, | ||
| ); | ||
|
|
||
| // Deprecation shim: migrate lib.autoExternal → output.autoExternal | ||
| if ( | ||
| libConfig.autoExternal !== undefined && | ||
| libConfig.output?.autoExternal === undefined | ||
|
Comment on lines
+1750
to
+1751
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When users still rely on the documented top-level deprecated setting, e.g. Useful? React with 👍 / 👎. |
||
| ) { | ||
| userConfig.output ??= {}; | ||
| userConfig.output.autoExternal = libConfig.autoExternal; | ||
| } | ||
|
|
||
| // Exe bundles everything into a single binary, so disable automatic externals | ||
| // after user config is merged to prevent users from re-enabling it. | ||
| if (userConfig.experiments?.exe) { | ||
| userConfig.output ??= {}; | ||
| userConfig.output.autoExternal = false; | ||
| } | ||
|
|
||
| // Merge the configuration of each environment based on the shared Rsbuild | ||
| // configuration and Lib configuration in the settings. | ||
| const libRsbuildConfig = await composeLibRsbuildConfig( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.