Skip to content

Commit

Permalink
1.0 API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 21, 2018
1 parent 2af076c commit fa91a2d
Show file tree
Hide file tree
Showing 32 changed files with 231 additions and 323 deletions.
40 changes: 21 additions & 19 deletions bin/src/run/build.ts
Expand Up @@ -3,11 +3,11 @@ import ms from 'pretty-ms';
import * as rollup from 'rollup';
import {
InputOptions,
OutputBundle,
OutputAsset,
OutputChunk,
OutputOptions,
RollupBuild,
RollupSingleFileBuild
RollupOutput
} from '../../../src/rollup/types';
import { mapSequence } from '../../../src/utils/promise';
import relativeId from '../../../src/utils/relativeId';
Expand All @@ -22,12 +22,7 @@ export default function build(
warnings: BatchWarnings,
silent = false
) {
const useStdout =
outputOptions.length === 1 &&
!outputOptions[0].file &&
!outputOptions[0].dir &&
inputOptions.input instanceof Array === false &&
typeof inputOptions.input !== 'object';
const useStdout = !outputOptions[0].file && !outputOptions[0].dir;

const start = Date.now();
const files = useStdout ? ['stdout'] : outputOptions.map(t => relativeId(t.file || t.dir));
Expand All @@ -47,7 +42,7 @@ export default function build(

return rollup
.rollup(inputOptions)
.then((bundle: RollupSingleFileBuild | RollupBuild) => {
.then((bundle: RollupBuild) => {
if (useStdout) {
const output = outputOptions[0];
if (output.sourcemap && output.sourcemap !== 'inline') {
Expand All @@ -57,22 +52,29 @@ export default function build(
});
}

return (<RollupSingleFileBuild>bundle).generate(output).then(({ code, map }) => {
if (!code) return;
if (output.sourcemap === 'inline') {
code += `\n//# ${SOURCEMAPPING_URL}=${map.toUrl()}\n`;
return bundle.generate(output).then(({ output: outputs }) => {
for (const file of outputs) {
let source: string | Buffer;
if ((<OutputAsset>file).isAsset) {
source = (<OutputAsset>file).source;
} else {
source = (<OutputChunk>file).code;
if (output.sourcemap === 'inline') {
source += `\n//# ${SOURCEMAPPING_URL}=${(<OutputChunk>file).map.toUrl()}\n`;
}
}
if (outputs.length > 1)
process.stdout.write(`\n${chalk.cyan(chalk.bold(file.fileName))}:\n`);
process.stdout.write(source);
}

process.stdout.write(code);
});
}

return mapSequence<OutputOptions, Promise<OutputChunk | { output: OutputBundle }>>(
outputOptions,
output => bundle.write(output)
return mapSequence<OutputOptions, Promise<RollupOutput>>(outputOptions, output =>
bundle.write(output)
).then(() => bundle);
})
.then((bundle?: RollupSingleFileBuild | RollupBuild) => {
.then((bundle?: RollupBuild) => {
warnings.flush();
if (!silent)
stderr(
Expand Down
41 changes: 18 additions & 23 deletions bin/src/run/index.ts
Expand Up @@ -20,29 +20,24 @@ export default function runRollup(command: any) {
}
}

if (command.dir) {
if (command._.length && !command._.some((input: string) => input.indexOf('=') !== -1)) {
command.input = command._;
} else if (
command._.length ||
Array.isArray(command.input) ||
typeof command.input === 'string'
) {
let input: string[];
if (command._.length) input = command._;
else input = typeof command.input === 'string' ? [command.input] : command.input;
command.input = {};
input.forEach((input: string) => {
const equalsIndex = input.indexOf('=');
const value = input.substr(equalsIndex + 1);
let key = input.substr(0, equalsIndex);
if (!key) key = getAliasName(input);
command.input[key] = value;
});
}
command._ = [];
} else if (command._.length === 1) {
command.input = command._[0];
if (command._.length && !command._.some((input: string) => input.indexOf('=') !== -1)) {
command.input = command._;
} else if (
command._.length ||
Array.isArray(command.input) ||
typeof command.input === 'string'
) {
let input: string[];
if (command._.length) input = command._;
else input = typeof command.input === 'string' ? [command.input] : command.input;
command.input = {};
input.forEach((input: string) => {
const equalsIndex = input.indexOf('=');
const value = input.substr(equalsIndex + 1);
let key = input.substr(0, equalsIndex);
if (!key) key = getAliasName(input);
command.input[key] = value;
});
}

if (command.environment) {
Expand Down
7 changes: 4 additions & 3 deletions bin/src/run/loadConfigFile.ts
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import path from 'path';
import rollup from 'rollup';
import { InputOptions, RollupSingleFileBuild } from '../../../src/rollup/types';
import { InputOptions, RollupBuild, RollupOutput } from '../../../src/rollup/types';
import relativeId from '../../../src/utils/relativeId';
import { handleError, stderr } from '../logging';
import batchWarnings from './batchWarnings';
Expand All @@ -20,12 +20,13 @@ export default function loadConfigFile(
return rollup
.rollup({
input: configFile,
treeshake: false,
external: (id: string) => {
return (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json';
},
onwarn: warnings.add
})
.then((bundle: RollupSingleFileBuild) => {
.then((bundle: RollupBuild) => {
if (!silent && warnings.count > 0) {
stderr(chalk.bold(`loaded ${relativeId(configFile)} with warnings`));
warnings.flush();
Expand All @@ -35,7 +36,7 @@ export default function loadConfigFile(
format: 'cjs'
});
})
.then(({ code }: { code: string }) => {
.then(({ output: [{ code }] }: RollupOutput) => {
// temporarily override require
const defaultLoader = require.extensions['.js'];
require.extensions['.js'] = (module: NodeModuleWithCompile, filename: string) => {
Expand Down

0 comments on commit fa91a2d

Please sign in to comment.