Skip to content

Commit

Permalink
feat: Support custom babel config (#1205)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this may change babel configuration loading, and is a
major change to the documentation.js approach to Babel.
  • Loading branch information
dougalg authored and tmcw committed Mar 12, 2019
1 parent f3511f9 commit 746d0a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
6 changes: 6 additions & 0 deletions src/commands/shared_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
* Adds shared options to any command that runs documentation
*/
module.exports.sharedInputOptions = {
babel: {
describe:
'path to babelrc or babel.options.js to override default babel config',
type: 'string',
default: null
},
shallow: {
describe:
'shallow mode turns off dependency resolution, ' +
Expand Down
70 changes: 33 additions & 37 deletions src/input/dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@ const concat = require('concat-stream');
const moduleFilters = require('../module_filters');
const smartGlob = require('../smart_glob.js');

const STANDARD_BABEL_CONFIG = {
sourceMaps: false,
compact: false,
cwd: path.resolve(__dirname, '../../'),
presets: ['@babel/preset-react', '@babel/preset-env', '@babel/preset-flow'],
plugins: [
// Stage 0
'@babel/plugin-proposal-function-bind',
// Stage 1
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-logical-assignment-operators',
'@babel/plugin-proposal-optional-chaining',
['@babel/plugin-proposal-pipeline-operator', { proposal: 'minimal' }],
['@babel/plugin-proposal-nullish-coalescing-operator', { loose: false }],
'@babel/plugin-proposal-do-expressions',
// Stage 2
['@babel/plugin-proposal-decorators', { legacy: true }],
'@babel/plugin-proposal-function-sent',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-numeric-separator',
'@babel/plugin-proposal-throw-expressions',
// Stage 3
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-meta',
['@babel/plugin-proposal-class-properties', { loose: false }],
'@babel/plugin-proposal-json-strings'
]
};

/**
* Returns a readable stream of dependencies, given an array of entry
* points and an object of options to provide to module-deps.
Expand All @@ -17,6 +46,9 @@ const smartGlob = require('../smart_glob.js');
* @returns results
*/
function dependencyStream(indexes, config) {
const babelConfig = config.babel
? { configFile: path.resolve(__dirname, '../../../../', config.babel) }
: STANDARD_BABEL_CONFIG;
const md = mdeps({
/**
* Determine whether a module should be included in documentation
Expand All @@ -28,43 +60,7 @@ function dependencyStream(indexes, config) {
.concat(config.requireExtension || [])
.map(ext => '.' + ext.replace(/^\./, ''))
.concat(['.mjs', '.js', '.json', '.es6', '.jsx']),
transform: [
babelify.configure({
sourceMaps: false,
compact: false,
cwd: path.resolve(__dirname, '../../'),
presets: [
'@babel/preset-react',
'@babel/preset-env',
'@babel/preset-flow'
],
plugins: [
// Stage 0
'@babel/plugin-proposal-function-bind',
// Stage 1
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-logical-assignment-operators',
'@babel/plugin-proposal-optional-chaining',
['@babel/plugin-proposal-pipeline-operator', { proposal: 'minimal' }],
[
'@babel/plugin-proposal-nullish-coalescing-operator',
{ loose: false }
],
'@babel/plugin-proposal-do-expressions',
// Stage 2
['@babel/plugin-proposal-decorators', { legacy: true }],
'@babel/plugin-proposal-function-sent',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-numeric-separator',
'@babel/plugin-proposal-throw-expressions',
// Stage 3
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-meta',
['@babel/plugin-proposal-class-properties', { loose: false }],
'@babel/plugin-proposal-json-strings'
]
})
],
transform: [babelify.configure(babelConfig)],
postFilter: moduleFilters.externals(indexes, config),
resolve:
config.resolve === 'node' &&
Expand Down

0 comments on commit 746d0a9

Please sign in to comment.