Skip to content

Commit

Permalink
autoresolve configPlugin if typescipt config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealSyler authored and lukastaegert committed Jun 14, 2021
1 parent 738f32b commit cef8b2d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
18 changes: 16 additions & 2 deletions cli/run/commandPlugins.ts
Expand Up @@ -6,7 +6,8 @@ import { waitForInputPlugin } from './waitForInput';
export function addCommandPluginsToInputOptions(
inputOptions: InputOptions,
command: Record<string, unknown>,
pluginOption = 'plugin'
pluginOption = 'plugin',
extension = ''
) {
if (command.stdin !== false) {
inputOptions.plugins!.push(stdinPlugin(command.stdin));
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions cli/run/loadConfigFile.ts
Expand Up @@ -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);
}
Expand All @@ -61,7 +61,8 @@ function getDefaultFromCjs(namespace: GenericConfigObject) {

async function getDefaultFromTranspiledConfigFile(
fileName: string,
commandOptions: any
commandOptions: any,
extension: string
): Promise<unknown> {
const warnings = batchWarnings();
const inputOptions = {
Expand All @@ -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`));
Expand Down

0 comments on commit cef8b2d

Please sign in to comment.