Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support native ESM configs in Node 13, support untranspiled configs (#…
…3445) * Improve coverage * Automatically discover .mjs and .cjs configs * First draft for untranspiled config support * Slightly change logic to see if this helps windows * Start improving the documentation * Add test for helpful error * Remove console-group * Refactor CLI to extract more useful loadConfigFile handler * Add test for loadConfigFile * Add documentation * Keep watching the config file on initial errors * Improve coverage * Small fixes * Improve coverage
- Loading branch information
1 parent
618e7f6
commit 7eea04a
Showing
102 changed files
with
1,374 additions
and
574 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
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,19 @@ | ||
export default function addBinShebangAndEsmImport() { | ||
let importFound = false; | ||
return { | ||
name: 'esm-dynamic-import', | ||
renderDynamicImport({ moduleId }) { | ||
importFound = true; | ||
if (moduleId.endsWith('loadConfigFile.ts')) { | ||
return { left: 'import(', right: ')' }; | ||
} | ||
}, | ||
generateBundle() { | ||
if (!importFound) { | ||
throw new Error( | ||
'Could not find dynamic import in "loadConfigFile.ts", was the file renamed?' | ||
); | ||
} | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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,74 @@ | ||
import * as path from 'path'; | ||
import { InputOptions } from '../../src/rollup/types'; | ||
import { stdinPlugin } from './stdin'; | ||
|
||
export function addCommandPluginsToInputOptions( | ||
inputOptions: InputOptions, | ||
command: any | ||
) { | ||
if (command.stdin !== false) { | ||
inputOptions.plugins!.push(stdinPlugin()); | ||
} | ||
const commandPlugin = command.plugin; | ||
if (commandPlugin) { | ||
const plugins = Array.isArray(commandPlugin) ? commandPlugin : [commandPlugin]; | ||
for (const plugin of plugins) { | ||
if (/[={}]/.test(plugin)) { | ||
// -p plugin=value | ||
// -p "{transform(c,i){...}}" | ||
loadAndRegisterPlugin(inputOptions, plugin); | ||
} else { | ||
// split out plugins joined by commas | ||
// -p node-resolve,commonjs,buble | ||
plugin.split(',').forEach((plugin: string) => loadAndRegisterPlugin(inputOptions, plugin)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) { | ||
let plugin: any = null; | ||
let pluginArg: any = undefined; | ||
if (pluginText[0] === '{') { | ||
// -p "{transform(c,i){...}}" | ||
plugin = new Function('return ' + pluginText); | ||
} else { | ||
const match = pluginText.match(/^([@.\/\\\w|^{}|-]+)(=(.*))?$/); | ||
if (match) { | ||
// -p plugin | ||
// -p plugin=arg | ||
pluginText = match[1]; | ||
pluginArg = new Function('return ' + match[3])(); | ||
} else { | ||
throw new Error(`Invalid --plugin argument format: ${JSON.stringify(pluginText)}`); | ||
} | ||
if (!/^\.|^rollup-plugin-|[@\/\\]/.test(pluginText)) { | ||
// Try using plugin prefix variations first if applicable. | ||
// Prefix order is significant - left has higher precedence. | ||
for (const prefix of ['@rollup/plugin-', 'rollup-plugin-']) { | ||
try { | ||
plugin = require(prefix + pluginText); | ||
break; | ||
} catch (ex) { | ||
// if this does not work, we try requiring the actual name below | ||
} | ||
} | ||
} | ||
if (!plugin) { | ||
try { | ||
if (pluginText[0] == '.') pluginText = path.resolve(pluginText); | ||
plugin = require(pluginText); | ||
} catch (ex) { | ||
throw new Error(`Cannot load plugin "${pluginText}"`); | ||
} | ||
} | ||
} | ||
if (typeof plugin === 'object' && pluginText in plugin) { | ||
// some plugins do not use `export default` for their entry point. | ||
// attempt to use the plugin name as the named import name. | ||
plugin = plugin[pluginText]; | ||
} | ||
inputOptions.plugins!.push( | ||
typeof plugin === 'function' ? plugin.call(plugin, pluginArg) : plugin | ||
); | ||
} |
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,41 @@ | ||
import { readdirSync, realpathSync } from 'fs'; | ||
import * as path from 'path'; | ||
import relative from 'require-relative'; | ||
import { handleError } from '../logging'; | ||
|
||
const DEFAULT_CONFIG_BASE = 'rollup.config'; | ||
|
||
export function getConfigPath(commandConfig: any): string { | ||
const cwd = process.cwd(); | ||
if (commandConfig === true) { | ||
return path.join(cwd, findConfigFileNameInCwd()); | ||
} | ||
if (commandConfig.slice(0, 5) === 'node:') { | ||
const pkgName = commandConfig.slice(5); | ||
try { | ||
return relative.resolve(`rollup-config-${pkgName}`, cwd); | ||
} catch (err) { | ||
try { | ||
return relative.resolve(pkgName, cwd); | ||
} catch (err) { | ||
if (err.code === 'MODULE_NOT_FOUND') { | ||
handleError({ | ||
code: 'MISSING_EXTERNAL_CONFIG', | ||
message: `Could not resolve config file "${commandConfig}"` | ||
}); | ||
} | ||
throw err; | ||
} | ||
} | ||
} | ||
return realpathSync(commandConfig); | ||
} | ||
|
||
function findConfigFileNameInCwd(): string { | ||
const filesInWorkingDir = new Set(readdirSync(process.cwd())); | ||
for (const extension of ['mjs', 'cjs']) { | ||
const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`; | ||
if (filesInWorkingDir.has(fileName)) return fileName; | ||
} | ||
return `${DEFAULT_CONFIG_BASE}.js`; | ||
} |
Oops, something went wrong.