-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to extend babel and rollup
- Loading branch information
1 parent
d346cc3
commit 50f2d5e
Showing
8 changed files
with
271 additions
and
75 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
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
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.