From cef8b2d8d09e42d907811a613d2f81f4f7ba93d0 Mon Sep 17 00:00:00 2001 From: syler Date: Thu, 29 Oct 2020 08:56:52 +0100 Subject: [PATCH] autoresolve configPlugin if typescipt config file. --- cli/run/commandPlugins.ts | 18 ++++++++++++++++-- cli/run/loadConfigFile.ts | 7 ++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 2e5896347a9..270f998a206 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -6,7 +6,8 @@ import { waitForInputPlugin } from './waitForInput'; export function addCommandPluginsToInputOptions( inputOptions: InputOptions, command: Record, - pluginOption = 'plugin' + pluginOption = 'plugin', + extension = '' ) { if (command.stdin !== false) { inputOptions.plugins!.push(stdinPlugin(command.stdin)); @@ -15,7 +16,7 @@ export function addCommandPluginsToInputOptions( inputOptions.plugins!.push(waitForInputPlugin()); } - const commandPlugin = command[pluginOption]; + const commandPlugin = resolveCommandPlugin(command, pluginOption, extension); if (commandPlugin) { const plugins = Array.isArray(commandPlugin) ? commandPlugin : [commandPlugin]; for (const plugin of plugins) { @@ -32,6 +33,19 @@ export function addCommandPluginsToInputOptions( } } +function resolveCommandPlugin(command: any, pluginOption: string, extension: string) { + if ( + (pluginOption === 'configPlugin' && !command[pluginOption]) || + command[pluginOption] === true + ) { + switch (extension) { + case '.ts': + return 'typescript'; + } + } + return command[pluginOption]; +} + function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) { let plugin: any = null; let pluginArg: any = undefined; diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 4103a8a3655..16a900f36af 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -50,7 +50,7 @@ async function loadConfigFile( ? (await import(pathToFileURL(fileName).href)).default : extension === '.cjs' ? getDefaultFromCjs(require(fileName)) - : await getDefaultFromTranspiledConfigFile(fileName, commandOptions); + : await getDefaultFromTranspiledConfigFile(fileName, commandOptions, extension); return getConfigList(configFileExport, commandOptions); } @@ -61,7 +61,8 @@ function getDefaultFromCjs(namespace: GenericConfigObject) { async function getDefaultFromTranspiledConfigFile( fileName: string, - commandOptions: any + commandOptions: any, + extension: string ): Promise { const warnings = batchWarnings(); const inputOptions = { @@ -72,7 +73,7 @@ async function getDefaultFromTranspiledConfigFile( plugins: [], treeshake: false }; - addCommandPluginsToInputOptions(inputOptions, commandOptions, 'configPlugin'); + addCommandPluginsToInputOptions(inputOptions, commandOptions, 'configPlugin', extension); const bundle = await rollup.rollup(inputOptions); if (!commandOptions.silent && warnings.count > 0) { stderr(bold(`loaded ${relativeId(fileName)} with warnings`));