Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Fix .babelrc issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Oct 15, 2018
1 parent f673bac commit b4b639f
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 172 deletions.
41 changes: 27 additions & 14 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const fs = require('fs');
const path = require('path').posix;
const camelCase = require('camelcase');

const pkgName = pkg => pkg.name ? pkg.name.split('/').slice(-1)[0] : '';

function parseOpts(flags, pkg) {
// Use package.json config as default flags
if (pkg.lyo && typeof pkg.lyo === 'object') {
Expand All @@ -13,29 +15,40 @@ function parseOpts(flags, pkg) {
}

// Check if .babelrc exists
if (fs.existsSync('.babelrc')) {
flags.babelConfig = '.babelrc';
const babelrcPath = flags.inputDir ? path.join(flags.inputDir, '.babelrc') : path.resolve('.babelrc');
if (fs.existsSync(babelrcPath)) {
flags.babelConfig = babelrcPath;
}

// Set input
flags.input = flags.input || pkg.main || 'index.js';
if (flags.remote && flags.inputDir) {
flags.input = path.join(flags.inputDir, flags.input);
}
flags.input = input(flags, pkg);

// Set output
const pkgName = pkg.name ? pkg.name.split('/').slice(-1)[0] : '';
const defaultName = pkg.name ? (pkgName + '.min.js') : path.basename(flags.input).replace(/.js$/, '.min.js');
if (!flags.output) {
flags.output = flags.remote ? defaultName : path.join('dist', defaultName);
} else if (!path.extname(flags.output)) {
flags.output = path.join(flags.output, defaultName);
}
flags.output = output(flags, pkg);

// Set name
flags.name = flags.name || camelCase(pkgName || '') ||
flags.name = flags.name || camelCase(pkgName(pkg) || '') ||
(pkg.main ? camelCase(pkg.main.replace(/.js$/, '')) : '') || 'module';
return flags;
}

function output(flags, pkg) {
const defaultName = pkg.name ? (pkgName(pkg) + '.min.js') : path.basename(flags.input).replace(/.js$/, '.min.js');
if (!flags.output) {
return flags.remote ? defaultName : path.join('dist', defaultName);
}
if (!path.extname(flags.output)) {
return path.join(flags.output, defaultName);
}
return flags.output;
}

function input(flags, pkg) {
const i = flags.input || pkg.main || 'index.js';
if (flags.remote && flags.inputDir) {
return path.join(flags.inputDir, i);
}
return i;
}

module.exports = parseOpts;
3 changes: 2 additions & 1 deletion lib/task.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const path = require('path');
const chalk = require('chalk');
const getStream = require('get-stream');
const ora = require('ora');
Expand Down Expand Up @@ -40,7 +41,7 @@ const runBrowserify = opts => runTask('Browserify', (resolve, reject) => {

const runBabel = (code, opts) => runTask('Babel', (resolve, reject) => {
if (opts.babelConfig) {
opts = {configFile: opts.babelConfig, compact: false, babelrc: false};
opts = {babelrcRoots: path.dirname(opts.babelConfig), filename: opts.input, compact: false};
} else {
const configItem = babel.createConfigItem(babelEnvPreset);
opts = {presets: [configItem], compact: false};
Expand Down
Loading

0 comments on commit b4b639f

Please sign in to comment.