-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respond to review feedback. Actually cache configs.
- Loading branch information
1 parent
65ed764
commit 1a6cae1
Showing
3 changed files
with
156 additions
and
104 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 |
---|---|---|
@@ -1,5 +1,18 @@ | ||
const p = require('path') | ||
const resolve = require('resolve') | ||
const configExplorer = require('cosmiconfig')('babel-plugin-macros', { | ||
searchPlaces: [ | ||
'package.json', | ||
'.babel-plugin-macrosrc', | ||
'.babel-plugin-macrosrc.json', | ||
'.babel-plugin-macrosrc.yaml', | ||
'.babel-plugin-macrosrc.yml', | ||
'.babel-plugin-macrosrc.js', | ||
'babel-plugin-macros.config.js', | ||
], | ||
packageProp: 'babelMacros', | ||
sync: true, | ||
}) | ||
// const printAST = require('ast-pretty-print') | ||
|
||
const macrosRegex = /[./]macro(\.js)?$/ | ||
|
@@ -230,77 +243,73 @@ function applyMacros({ | |
return result | ||
} | ||
|
||
function getConfig(macro, filename, source, options) { | ||
const {configName} = macro.options | ||
if (configName) { | ||
let callOptions, configOptions, configError, configPath | ||
function getConfigFromFile(configName, filename) { | ||
try { | ||
const loaded = configExplorer.searchSync(filename) | ||
|
||
if (Object.prototype.hasOwnProperty.call(options, configName)) { | ||
if (options[configName] && typeof options[configName] !== 'object') { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`The macro plugin options' ${configName} property was not an object or null.`, | ||
) | ||
} else { | ||
callOptions = options[configName] | ||
if (loaded) { | ||
return { | ||
options: loaded.config[configName], | ||
path: loaded.filepath, | ||
} | ||
} | ||
} catch (e) { | ||
return {error: e} | ||
} | ||
return {} | ||
} | ||
|
||
try { | ||
// lazy-loading it here to avoid perf issues of loading it up front. | ||
// No I did not measure. Yes I'm a bad person. | ||
// FWIW, this thing told me that cosmiconfig is 227.1 kb of minified JS | ||
// so that's probably significant... https://bundlephobia.com/[email protected] | ||
// Note that cosmiconfig will cache the babel-plugin-macros config 👍 | ||
const explorer = require('cosmiconfig')('babel-plugin-macros', { | ||
searchPlaces: [ | ||
'package.json', | ||
'.babel-plugin-macrosrc', | ||
'.babel-plugin-macrosrc.json', | ||
'.babel-plugin-macrosrc.yaml', | ||
'.babel-plugin-macrosrc.yml', | ||
'.babel-plugin-macrosrc.js', | ||
'babel-plugin-macros.config.js', | ||
], | ||
packageProp: 'babelMacros', | ||
sync: true, | ||
}) | ||
const loaded = explorer.searchSync(filename) | ||
if (loaded) { | ||
configOptions = loaded.config[configName] | ||
configPath = loaded.filepath | ||
} | ||
} catch (e) { | ||
configError = e | ||
function getConfigFromOptions(configName, options) { | ||
if (Object.prototype.hasOwnProperty.call(options, configName)) { | ||
if (options[configName] && typeof options[configName] !== 'object') { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`The macro plugin options' ${configName} property was not an object or null.`, | ||
) | ||
} else { | ||
return {options: options[configName]} | ||
} | ||
} | ||
return {} | ||
} | ||
|
||
if (callOptions === undefined && configOptions === undefined) { | ||
function getConfig(macro, filename, source, options) { | ||
const {configName} = macro.options | ||
if (configName) { | ||
const fileConfig = getConfigFromFile(configName, filename) | ||
const optionsConfig = getConfigFromOptions(configName, options) | ||
|
||
if ( | ||
optionsConfig.options === undefined && | ||
fileConfig.options === undefined | ||
) { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`There was an error trying to load the config "${configName}" ` + | ||
`for the macro imported from "${source}. ` + | ||
`Please see the error thrown for more information.`, | ||
) | ||
if (configError !== undefined) { | ||
throw configError | ||
if (fileConfig.error !== undefined) { | ||
throw fileConfig.error | ||
} | ||
} | ||
|
||
if ( | ||
configOptions && | ||
callOptions !== undefined && | ||
typeof configOptions !== 'object' | ||
fileConfig.options !== undefined && | ||
optionsConfig.options !== undefined && | ||
typeof fileConfig.options !== 'object' | ||
) { | ||
throw new Error( | ||
`${configPath} specified a ${configName} config of type ${typeof configOptions}, ` + | ||
`but the the macros plugin's options.${configName} did contain an object.` + | ||
`Both configs must contain objects for their options to be mergeable.`, | ||
`${fileConfig.path} specified a ${configName} config of type ` + | ||
`${typeof optionsConfig.options}, but the the macros plugin's ` + | ||
`options.${configName} did contain an object. Both configs must ` + | ||
`contain objects for their options to be mergeable.`, | ||
) | ||
} | ||
|
||
return { | ||
...configOptions, | ||
...callOptions, | ||
...fileConfig.options, | ||
...optionsConfig.options, | ||
} | ||
} | ||
return undefined | ||
|