-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support automatically enable Rsdoctor
- Loading branch information
1 parent
ff81b97
commit 6010f5e
Showing
5 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { color, logger, type BundlerPluginInstance } from '@rsbuild/shared'; | ||
import type { RsbuildPlugin } from '../types'; | ||
|
||
export function pluginRsdoctor(): RsbuildPlugin { | ||
return { | ||
name: 'rsbuild:rsdoctor', | ||
|
||
setup(api) { | ||
api.onBeforeCreateCompiler(async ({ bundlerConfigs }) => { | ||
if (process.env.RSDOCTOR !== 'true') { | ||
return; | ||
} | ||
|
||
const isRspack = api.context.bundlerType === 'rspack'; | ||
const packageName = isRspack | ||
? '@rsdoctor/rspack-plugin' | ||
: '@rsdoctor/webpack-plugin'; | ||
|
||
let module: { | ||
RsdoctorRspackPlugin: { new (): BundlerPluginInstance }; | ||
RsdoctorWebpackPlugin: { new (): BundlerPluginInstance }; | ||
}; | ||
|
||
try { | ||
const path = require.resolve(packageName, { | ||
paths: [api.context.rootPath], | ||
}); | ||
module = await import(path); | ||
} catch (err) { | ||
logger.warn( | ||
`\`process.env.RSDOCTOR\` enabled, please install ${color.bold(color.yellow(packageName))} package.`, | ||
); | ||
return; | ||
} | ||
|
||
const pluginName = isRspack | ||
? 'RsdoctorRspackPlugin' | ||
: 'RsdoctorWebpackPlugin'; | ||
|
||
if (!module || !module[pluginName]) { | ||
return; | ||
} | ||
|
||
let isAutoRegister = false; | ||
|
||
bundlerConfigs.forEach((config) => { | ||
const registered = config.plugins?.some( | ||
(plugin) => plugin?.constructor?.name === pluginName, | ||
); | ||
|
||
if (registered) { | ||
return; | ||
} | ||
|
||
config.plugins ||= []; | ||
config.plugins.push(new module[pluginName]()); | ||
isAutoRegister = true; | ||
}); | ||
|
||
if (isAutoRegister) { | ||
logger.info(`${color.bold(color.yellow(packageName))} enabled.`); | ||
} | ||
}); | ||
}, | ||
}; | ||
} |
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