-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensible Rollup configuration (#183)
* Just default to React's url for errors by default * Add ability to extend babel and rollup * Document customization * update docs for boolean extractErrors * fix minor misunderstanding in docs * make warning and invariant docs more descriptive * document error extraction warning * add fixture
- Loading branch information
1 parent
7bf3032
commit cb2cf7c
Showing
23 changed files
with
1,868 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ node_modules | |
.rts2_cache_cjs | ||
.rts2_cache_esm | ||
.rts2_cache_umd | ||
dist | ||
dist | ||
tester |
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,153 @@ | ||
import { createConfigItem } from '@babel/core'; | ||
import babelPlugin from 'rollup-plugin-babel'; | ||
import merge from 'lodash.merge'; | ||
|
||
export const isTruthy = (obj?: any) => { | ||
if (!obj) { | ||
return false; | ||
} | ||
|
||
return obj.constructor !== Object || Object.keys(obj).length > 0; | ||
}; | ||
|
||
const replacements = [{ original: 'lodash', replacement: 'lodash-es' }]; | ||
|
||
export const mergeConfigItems = (type: any, ...configItemsToMerge: any[]) => { | ||
const mergedItems: any[] = []; | ||
|
||
configItemsToMerge.forEach(configItemToMerge => { | ||
configItemToMerge.forEach((item: any) => { | ||
const itemToMergeWithIndex = mergedItems.findIndex( | ||
mergedItem => mergedItem.file.resolved === item.file.resolved | ||
); | ||
|
||
if (itemToMergeWithIndex === -1) { | ||
mergedItems.push(item); | ||
return; | ||
} | ||
|
||
mergedItems[itemToMergeWithIndex] = createConfigItem( | ||
[ | ||
mergedItems[itemToMergeWithIndex].file.resolved, | ||
merge(mergedItems[itemToMergeWithIndex].options, item.options), | ||
], | ||
{ | ||
type, | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
return mergedItems; | ||
}; | ||
|
||
export const createConfigItems = (type: any, items: any[]) => { | ||
return items.map(({ name, ...options }) => { | ||
return createConfigItem([require.resolve(name), options], { type }); | ||
}); | ||
}; | ||
|
||
export const babelPluginTsdx = babelPlugin.custom((babelCore: any) => ({ | ||
// Passed the plugin options. | ||
options({ custom: customOptions, ...pluginOptions }: any) { | ||
return { | ||
// Pull out any custom options that the plugin might have. | ||
customOptions, | ||
|
||
// Pass the options back with the two custom options removed. | ||
pluginOptions, | ||
}; | ||
}, | ||
config(config: any, { customOptions }: any) { | ||
const defaultPlugins = createConfigItems( | ||
'plugin', | ||
[ | ||
// { | ||
// name: '@babel/plugin-transform-react-jsx', | ||
// pragma: customOptions.jsx || 'h', | ||
// pragmaFrag: customOptions.jsxFragment || 'Fragment', | ||
// }, | ||
{ name: 'babel-plugin-annotate-pure-calls' }, | ||
{ name: 'babel-plugin-dev-expression' }, | ||
{ | ||
name: 'babel-plugin-transform-rename-import', | ||
replacements, | ||
}, | ||
isTruthy(customOptions.defines) && { | ||
name: 'babel-plugin-transform-replace-expressions', | ||
replace: customOptions.defines, | ||
}, | ||
{ | ||
name: 'babel-plugin-transform-async-to-promises', | ||
inlineHelpers: true, | ||
externalHelpers: true, | ||
}, | ||
{ | ||
name: '@babel/plugin-proposal-class-properties', | ||
loose: true, | ||
}, | ||
{ | ||
name: '@babel/plugin-transform-regenerator', | ||
async: false, | ||
}, | ||
{ | ||
name: 'babel-plugin-macros', | ||
}, | ||
isTruthy(customOptions.extractErrors) && { | ||
name: './errors/transformErrorMessages', | ||
}, | ||
].filter(Boolean) | ||
); | ||
|
||
const babelOptions = config.options || {}; | ||
|
||
const envIdx = (babelOptions.presets || []).findIndex((preset: any) => | ||
preset.file.request.includes('@babel/preset-env') | ||
); | ||
|
||
if (envIdx !== -1) { | ||
const preset = babelOptions.presets[envIdx]; | ||
babelOptions.presets[envIdx] = createConfigItem( | ||
[ | ||
preset.file.resolved, | ||
merge( | ||
{ | ||
loose: true, | ||
targets: customOptions.targets, | ||
}, | ||
preset.options, | ||
{ | ||
modules: false, | ||
exclude: merge( | ||
['transform-async-to-generator', 'transform-regenerator'], | ||
preset.options.exclude || [] | ||
), | ||
} | ||
), | ||
], | ||
{ | ||
type: `preset`, | ||
} | ||
); | ||
} else { | ||
babelOptions.presets = createConfigItems('preset', [ | ||
{ | ||
name: '@babel/preset-env', | ||
targets: customOptions.targets, | ||
modules: false, | ||
loose: true, | ||
exclude: ['transform-async-to-generator', 'transform-regenerator'], | ||
}, | ||
]); | ||
} | ||
|
||
// Merge babelrc & our plugins together | ||
babelOptions.plugins = mergeConfigItems( | ||
'plugin', | ||
defaultPlugins, | ||
babelOptions.plugins || [] | ||
); | ||
|
||
return babelOptions; | ||
}, | ||
})); |
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
Oops, something went wrong.