Skip to content

Commit

Permalink
options refactor, bin and watch types complete
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 22, 2017
1 parent 49c87b0 commit 90f3eaa
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 90 deletions.
7 changes: 6 additions & 1 deletion bin/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
declare module "minimist";
declare module 'minimist';
declare module 'help.md' {
let str: string;
export default str;
}
declare module 'package.json';
4 changes: 2 additions & 2 deletions bin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference path="./index.d.ts" />

import minimist from 'minimist';
import help from './help.md';
import { version } from '../../package.json';
import help from 'help.md';
import { version } from 'package.json';
import run from './run/index.js';

const command = minimist(process.argv.slice(2), {
Expand Down
14 changes: 7 additions & 7 deletions bin/src/run/batchWarnings.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import chalk from 'chalk';
import { stderr } from '../logging.js';
import relativeId from '../../../src/utils/relativeId.js';
import { Warning } from '../../../src/rollup/index';
import { RollupWarning } from '../../../src/rollup/index';

export interface BatchWarnings {
readonly count: number;
add: (warning: string | Warning) => void;
add: (warning: string | RollupWarning) => void;
flush: () => void;
}

export default function batchWarnings () {
let allWarnings = new Map<string, Warning[]>();
let allWarnings = new Map<string, RollupWarning[]>();
let count = 0;

return {
get count () {
return count;
},

add: (warning: string | Warning) => {
add: (warning: string | RollupWarning) => {
if (typeof warning === 'string') {
warning = { code: 'UNKNOWN', message: warning };
}
Expand Down Expand Up @@ -86,7 +86,7 @@ export default function batchWarnings () {
}

const immediateHandlers: {
[code: string]: (warning: Warning) => void
[code: string]: (warning: RollupWarning) => void
} = {
DEPRECATED_OPTIONS: warning => {
title(`Some options have been renamed`);
Expand Down Expand Up @@ -129,7 +129,7 @@ const immediateHandlers: {
const deferredHandlers: {
[code: string]: {
priority: number;
fn: (warnings: Warning[]) => void;
fn: (warnings: RollupWarning[]) => void;
}
} = {
UNUSED_EXTERNAL_IMPORT: {
Expand Down Expand Up @@ -335,7 +335,7 @@ function nest<T> (array: T[], prop: string) {
return nested;
}

function showTruncatedWarnings (warnings: Warning[]) {
function showTruncatedWarnings (warnings: RollupWarning[]) {
const nestedByModule = nest(warnings, 'id');

const sliced =
Expand Down
9 changes: 4 additions & 5 deletions bin/src/run/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { handleError, stderr } from '../logging.js';
import relativeId from '../../../src/utils/relativeId.js';
import { mapSequence } from '../../../src/utils/promise.js';
import SOURCEMAPPING_URL from '../sourceMappingUrl.js';
import { InputOptions, OutputOptions } from '../../../src/rollup/index';
import { InputOptions, OutputOptions, OutputBundle } from '../../../src/rollup/index';
import { BatchWarnings } from './batchWarnings';
import Bundle from '../../../src/Bundle';
import { RawSourceMap } from 'source-map';
import { SourceMap } from 'magic-string';

export default function build (inputOptions: InputOptions, outputOptions: OutputOptions[], warnings: BatchWarnings, silent = false) {
const useStdout = outputOptions.length === 1 && !outputOptions[0].file;
Expand All @@ -30,7 +29,7 @@ export default function build (inputOptions: InputOptions, outputOptions: Output

return rollup
.rollup(inputOptions)
.then((bundle: Bundle) => {
.then((bundle: OutputBundle) => {
if (useStdout) {
const output = outputOptions[0];
if (output.sourcemap && output.sourcemap !== 'inline') {
Expand All @@ -41,7 +40,7 @@ export default function build (inputOptions: InputOptions, outputOptions: Output
});
}

return bundle.generate(output).then(({ code, map }: { code: string, map: RawSourceMap}) => {
return bundle.generate(output).then(({ code, map }: { code: string, map: SourceMap }) => {
if (output.sourcemap === 'inline') {
code += `\n//# ${SOURCEMAPPING_URL}=${map.toUrl()}\n`;
}
Expand Down
5 changes: 2 additions & 3 deletions bin/src/run/loadConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import * as rollup from 'rollup';
import batchWarnings from './batchWarnings.js';
import relativeId from '../../../src/utils/relativeId.js';
import { handleError, stderr } from '../logging.js';
import Bundle from '../../../src/Bundle';
import { InputOptions } from '../../../src/rollup/index';
import { InputOptions, OutputBundle } from '../../../src/rollup/index';

export default function loadConfigFile (configFile: string, silent = false): Promise<InputOptions[]> {
const warnings = batchWarnings();
Expand All @@ -24,7 +23,7 @@ export default function loadConfigFile (configFile: string, silent = false): Pro
onwarn: warnings.add,
plugins: [buble({ objectAssign: 'Object.assign' })]
})
.then((bundle: Bundle) => {
.then((bundle: OutputBundle) => {
if (!silent && warnings.count > 0) {
stderr(chalk.bold(`loaded ${relativeId(configFile)} with warnings`));
warnings.flush();
Expand Down
14 changes: 7 additions & 7 deletions bin/src/run/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import loadConfigFile from './loadConfigFile.js';
import relativeId from '../../../src/utils/relativeId.js';
import { handleError, stderr } from '../logging.js';
import { RollupError } from '../../../src/utils/error';
import { InputOptions } from '../../../src/rollup/index';
import { RollupWatchOptions } from '../../../src/watch/index';

interface WatchEvent {
code: string;
Expand All @@ -27,7 +27,7 @@ interface Watcher {
close: () => void;
};

export default function watch (configFile: string, configs: InputOptions[], command: any, silent = false) {
export default function watch (configFile: string, configs: RollupWatchOptions[], command: any, silent = false) {
const isTTY = Boolean(process.stderr.isTTY);

const screen = alternateScreen(isTTY);
Expand All @@ -38,7 +38,7 @@ export default function watch (configFile: string, configs: InputOptions[], comm
let watcher: Watcher;
let configWatcher: Watcher;

function start (configs: InputOptions[]) {
function start (configs: RollupWatchOptions[]) {
screen.reset(chalk.underline(`rollup v${rollup.VERSION}`));

let screenWriter = screen.reset;
Expand All @@ -53,7 +53,7 @@ export default function watch (configFile: string, configs: InputOptions[], comm
merged.inputOptions.onwarn = warnings.add;
}

const result = Object.assign({}, merged.inputOptions, {
const result: RollupWatchOptions = Object.assign({}, merged.inputOptions, {
output: merged.outputOptions
});

Expand All @@ -63,8 +63,8 @@ export default function watch (configFile: string, configs: InputOptions[], comm
}

if (
merged.inputOptions.watch &&
merged.inputOptions.watch.clearScreen === false
(<RollupWatchOptions>merged.inputOptions).watch &&
(<RollupWatchOptions>merged.inputOptions).watch.clearScreen === false
) {
screenWriter = stderr;
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export default function watch (configFile: string, configs: InputOptions[], comm
restarting = true;

loadConfigFile(configFile, silent)
.then((configs: InputOptions[]) => {
.then((configs: RollupWatchOptions[]) => {
restarting = false;

if (aborted) {
Expand Down
6 changes: 5 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function resolveTypescript() {
return {
name: 'resolve-typescript',
resolveId(importee, importer) {
// work around typescript's inability to resolve other extensions
if ( ~importee.indexOf( 'help.md' ) ) return path.resolve('bin/src/help.md');
if ( ~importee.indexOf( 'package.json' ) ) return path.resolve('package.json');

// bit of a hack — TypeScript only really works if it can resolve imports,
// but they misguidedly chose to reject imports with file extensions. This
// means we need to resolve them here
Expand Down Expand Up @@ -109,7 +113,7 @@ export default [
commonjs({
include: 'node_modules/**'
}),
resolve()
resolve(),
],
external: [
'fs',
Expand Down
6 changes: 3 additions & 3 deletions src/Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import first from './utils/first';
import { find } from './utils/array';
import { blank, forOwn, keys } from './utils/object';
import Module, { IdMap } from './Module';
import Module, { IdMap, ModuleJSON } from './Module';
import ExternalModule from './ExternalModule';
import finalisers from './finalisers/index';
import ensureArray from './utils/ensureArray';
Expand Down Expand Up @@ -37,7 +37,7 @@ import Program from './ast/nodes/Program';

export default class Bundle {
acornOptions: any;
cachedModules: Map<string, Module>;
cachedModules: Map<string, ModuleJSON>;
context: string;
entry: string;
entryId: string;
Expand Down Expand Up @@ -645,7 +645,7 @@ export default class Bundle {
this.plugins,
bundleSourcemapChain,
options
).then(code => {
).then((code: string) => {
if (options.sourcemap) {
timeStart('sourcemap');

Expand Down
14 changes: 13 additions & 1 deletion src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ function includeFully (node: Node) {
node.eachChild(includeFully);
}

export interface ModuleJSON {
id: string;
dependencies: string[];
code: string;
originalCode: string;
originalSourcemap: RawSourceMap;
ast: Program;
sourcemapChain: RawSourceMap[];
resolvedIds: IdMap;
resolvedExternalIds: IdMap;
}

export default class Module {
type: 'Module';
bundle: Bundle;
Expand Down Expand Up @@ -500,7 +512,7 @@ export default class Module {
return (<any> magicString).trim();
}

toJSON () {
toJSON (): ModuleJSON {
return {
id: this.id,
dependencies: this.dependencies.map(module => module.id),
Expand Down
2 changes: 1 addition & 1 deletion src/browser-entry.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as rollup } from './rollup/index';
export { version as VERSION } from '../package.json';
export { version as VERSION } from 'package.json';
2 changes: 1 addition & 1 deletion src/node-entry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as rollup } from './rollup/index';
export { default as watch } from './watch/index';
export { version as VERSION } from '../package.json';
export { version as VERSION } from 'package.json';

0 comments on commit 90f3eaa

Please sign in to comment.