-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(uni-builder): compat builder plugin api & type (#5174)
- Loading branch information
Showing
8 changed files
with
234 additions
and
30 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
88 changes: 88 additions & 0 deletions
88
packages/builder/uni-builder/src/shared/compatLegacyPlugin.ts
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,88 @@ | ||
import type { | ||
UniBuilderPlugin, | ||
UniBuilderPluginAPI, | ||
UniBuilderContext, | ||
} from '../types'; | ||
import type { RsbuildPlugin } from '@rsbuild/core'; | ||
import { logger } from '@rsbuild/shared'; | ||
import { join } from 'path'; | ||
|
||
function addDeprecatedWarning( | ||
pluginName: string, | ||
name: string, | ||
newName?: string, | ||
) { | ||
logger.warn( | ||
`Plugin(${pluginName})'s api '${name}' is deprecated${ | ||
newName ? `, please use '${newName}' instead.` : '.' | ||
}`, | ||
); | ||
} | ||
export function compatLegacyPlugin( | ||
plugin: UniBuilderPlugin, | ||
extraInfo: { | ||
cwd: string; | ||
}, | ||
): RsbuildPlugin { | ||
return { | ||
...plugin, | ||
setup: api => { | ||
const builderContext = new Proxy(api.context, { | ||
get(target, prop: keyof UniBuilderContext) { | ||
switch (prop) { | ||
case 'target': | ||
addDeprecatedWarning( | ||
plugin.name, | ||
'context.target', | ||
'context.targets', | ||
); | ||
return target.targets; | ||
case 'srcPath': | ||
addDeprecatedWarning(plugin.name, 'context.srcPath'); | ||
return join(extraInfo.cwd, 'src'); | ||
case 'framework': | ||
addDeprecatedWarning(plugin.name, 'context.framework'); | ||
return ''; | ||
default: { | ||
if (prop in target) { | ||
return target[prop]; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
} | ||
}, | ||
set(_target, prop: keyof UniBuilderContext) { | ||
logger.error( | ||
`Context is readonly, you can not assign to the "context.${prop}" prop.`, | ||
); | ||
return true; | ||
}, | ||
}) as UniBuilderContext; | ||
|
||
const legacyAPI: UniBuilderPluginAPI = { | ||
...api, | ||
context: builderContext, | ||
getBuilderConfig: () => { | ||
addDeprecatedWarning( | ||
plugin.name, | ||
'getBuilderConfig', | ||
'getRsbuildConfig', | ||
); | ||
return api.getRsbuildConfig(); | ||
}, | ||
modifyBuilderConfig: fn => { | ||
api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => { | ||
addDeprecatedWarning( | ||
plugin.name, | ||
'modifyBuilderConfig', | ||
'modifyRsbuildConfig', | ||
); | ||
return fn(config, { mergeBuilderConfig: mergeRsbuildConfig }); | ||
}); | ||
}, | ||
}; | ||
return plugin.setup(legacyAPI); | ||
}, | ||
}; | ||
} |
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,47 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { builderPluginNodePolyfill } from '@modern-js/builder-plugin-node-polyfill'; | ||
import { createUniBuilder } from '../src'; | ||
|
||
describe('uni-builder legacy plugins', () => { | ||
it('legacy plugin should works well', async () => { | ||
const rsbuild = await createUniBuilder({ | ||
bundlerType: 'rspack', | ||
config: {}, | ||
cwd: '', | ||
}); | ||
|
||
rsbuild.addPlugins([ | ||
// plugin type check should passed | ||
builderPluginNodePolyfill(), | ||
{ | ||
name: 'builder-plugin-test', | ||
setup: api => { | ||
api.modifyBuilderConfig((config, { mergeBuilderConfig }) => { | ||
const builderConfig = api.getBuilderConfig(); | ||
|
||
expect(api.context.target).toBe(api.context.targets); | ||
|
||
expect(builderConfig.source).toBeDefined(); | ||
|
||
return mergeBuilderConfig(config, { | ||
tools: { | ||
bundlerChain: (chain: any) => { | ||
chain.devtool(false); | ||
}, | ||
}, | ||
}); | ||
}); | ||
}, | ||
}, | ||
]); | ||
|
||
const { | ||
origin: { bundlerConfigs }, | ||
} = await rsbuild.inspectConfig(); | ||
|
||
expect(bundlerConfigs[0].devtool).toBeFalsy(); | ||
expect( | ||
Object.keys(bundlerConfigs[0].resolve!.fallback!).length, | ||
).toBeGreaterThan(1); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.