Skip to content

Commit

Permalink
Merge branch 'apply-prettier' into update-rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 6, 2018
2 parents cb05ea4 + 6c15302 commit 840ff5f
Show file tree
Hide file tree
Showing 435 changed files with 4,460 additions and 4,097 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"singleQuote": true,
"useTabs": true
"useTabs": true,
"printWidth": 100
}
12 changes: 5 additions & 7 deletions bin/src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ if (!chalk.supportsColor) chalk.enabled = false;
// log to stderr to keep `rollup main.js > bundle.js` from breaking
export const stderr = console.error.bind(console); // eslint-disable-line no-console

export function handleError (err: RollupError, recover = false) {
export function handleError(err: RollupError, recover = false) {
let description = err.message || err;
if (err.name) description = `${err.name}: ${description}`;
const message =
((<{ plugin?: string }>err).plugin ? `(${(<{ plugin?: string }>err).plugin} plugin) ${description}` : description) || err;
((<{ plugin?: string }>err).plugin
? `(${(<{ plugin?: string }>err).plugin} plugin) ${description}`
: description) || err;

stderr(chalk.bold.red(`[!] ${chalk.bold(message.toString())}`));

Expand All @@ -21,11 +23,7 @@ export function handleError (err: RollupError, recover = false) {
}

if (err.loc) {
stderr(
`${relativeId(err.loc.file || err.id)} (${err.loc.line}:${
err.loc.column
})`
);
stderr(`${relativeId(err.loc.file || err.id)} (${err.loc.line}:${err.loc.column})`);
} else if (err.id) {
stderr(relativeId(err.id));
}
Expand Down
14 changes: 7 additions & 7 deletions bin/src/run/alternateScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const isMintty = isWindows && !!(process.env.SHELL || process.env.TERM);
const isConEmuAnsiOn = (process.env.ConEmuANSI || '').toLowerCase() === 'on';
const supportsAnsi = !isWindows || isMintty || isConEmuAnsiOn;

export default function alternateScreen (enabled: boolean) {
export default function alternateScreen(enabled: boolean) {
if (!enabled) {
let needAnnounce = true;
return {
open () { },
close () { },
reset (heading: string) {
open() {},
close() {},
reset(heading: string) {
if (needAnnounce) {
stderr(heading);
needAnnounce = false;
Expand All @@ -25,17 +25,17 @@ export default function alternateScreen (enabled: boolean) {
}

return {
open () {
open() {
if (supportsAnsi) {
process.stderr.write(SHOW_ALTERNATE_SCREEN);
}
},
close () {
close() {
if (supportsAnsi) {
process.stderr.write(HIDE_ALTERNATE_SCREEN);
}
},
reset (heading: string) {
reset(heading: string) {
stderr(`${ansiEscape.eraseScreen}${ansiEscape.cursorTo(0, 0)}${heading}`);
}
};
Expand Down
104 changes: 34 additions & 70 deletions bin/src/run/batchWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ export interface BatchWarnings {
flush: () => void;
}

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

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

add: (warning: string | RollupWarning) => {
add: (warning: string | RollupWarning) => {
if (typeof warning === 'string') {
warning = { code: 'UNKNOWN', message: warning };
}
Expand Down Expand Up @@ -55,20 +55,14 @@ export default function batchWarnings () {
handler.fn(warnings);
} else {
warnings.forEach(warning => {
stderr(
`${chalk.bold.yellow('(!)')} ${chalk.bold.yellow(
warning.message
)}`
);
stderr(`${chalk.bold.yellow('(!)')} ${chalk.bold.yellow(warning.message)}`);

if (warning.url) info(warning.url);

const id = (warning.loc && warning.loc.file) || warning.id;
if (id) {
const loc = warning.loc
? `${relativeId(id)}: (${warning.loc.line}:${
warning.loc.column
})`
? `${relativeId(id)}: (${warning.loc.line}:${warning.loc.column})`
: relativeId(id);

stderr(chalk.bold(relativeId(loc)));
Expand All @@ -86,7 +80,7 @@ export default function batchWarnings () {
}

const immediateHandlers: {
[code: string]: (warning: RollupWarning) => void
[code: string]: (warning: RollupWarning) => void;
} = {
UNKNOWN_OPTION: warning => {
title(`You have passed an unrecognized option`);
Expand All @@ -95,9 +89,7 @@ const immediateHandlers: {

DEPRECATED_OPTIONS: warning => {
title(`Some options have been renamed`);
info(
`https://gist.github.com/Rich-Harris/d472c50732dab03efeb37472b08a3f32`
);
info(`https://gist.github.com/Rich-Harris/d472c50732dab03efeb37472b08a3f32`);
warning.deprecations.forEach(option => {
stderr(`${chalk.bold(option.old)} is now ${option.new}`);
});
Expand All @@ -110,9 +102,9 @@ const immediateHandlers: {
warning.modules.length === 1
? `'${warning.modules[0]}'`
: `${warning.modules
.slice(0, -1)
.map(name => `'${name}'`)
.join(', ')} and '${warning.modules.slice(-1)}'`;
.slice(0, -1)
.map(name => `'${name}'`)
.join(', ')} and '${warning.modules.slice(-1)}'`;
stderr(
`Creating a browser bundle that depends on ${detail}. You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins`
);
Expand All @@ -135,18 +127,14 @@ const deferredHandlers: {
[code: string]: {
priority: number;
fn: (warnings: RollupWarning[]) => void;
}
};
} = {
UNUSED_EXTERNAL_IMPORT: {
priority: 1,
fn: warnings => {
title('Unused external imports');
warnings.forEach(warning => {
stderr(
`${warning.names} imported from external module '${
warning.source
}' but never used`
);
stderr(`${warning.names} imported from external module '${warning.source}' but never used`);
});
}
},
Expand All @@ -161,16 +149,13 @@ const deferredHandlers: {

const dependencies = new Map();
warnings.forEach(warning => {
if (!dependencies.has(warning.source))
dependencies.set(warning.source, []);
if (!dependencies.has(warning.source)) dependencies.set(warning.source, []);
dependencies.get(warning.source).push(warning.importer);
});

Array.from(dependencies.keys()).forEach(dependency => {
const importers = dependencies.get(dependency);
stderr(
`${chalk.bold(dependency)} (imported by ${importers.join(', ')})`
);
stderr(`${chalk.bold(dependency)} (imported by ${importers.join(', ')})`);
});
}
},
Expand All @@ -179,9 +164,7 @@ const deferredHandlers: {
priority: 1,
fn: warnings => {
title('Missing exports');
info(
'https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module'
);
info('https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module');

warnings.forEach(warning => {
stderr(chalk.bold(warning.importer));
Expand All @@ -195,9 +178,7 @@ const deferredHandlers: {
priority: 1,
fn: warnings => {
title('`this` has been rewritten to `undefined`');
info(
'https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined'
);
info('https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined');
showTruncatedWarnings(warnings);
}
},
Expand All @@ -206,19 +187,15 @@ const deferredHandlers: {
priority: 1,
fn: warnings => {
title('Use of eval is strongly discouraged');
info(
'https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval'
);
info('https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval');
showTruncatedWarnings(warnings);
}
},

NON_EXISTENT_EXPORT: {
priority: 1,
fn: warnings => {
title(
`Import of non-existent ${warnings.length > 1 ? 'exports' : 'export'}`
);
title(`Import of non-existent ${warnings.length > 1 ? 'exports' : 'export'}`);
showTruncatedWarnings(warnings);
}
},
Expand All @@ -230,7 +207,7 @@ const deferredHandlers: {
warnings.forEach(warning => {
stderr(
`${chalk.bold(relativeId(warning.reexporter))} re-exports '${
warning.name
warning.name
}' from both ${relativeId(warning.sources[0])} and ${relativeId(
warning.sources[1]
)} (will be ignored)`
Expand All @@ -242,9 +219,7 @@ const deferredHandlers: {
MISSING_GLOBAL_NAME: {
priority: 1,
fn: warnings => {
title(
`Missing global variable ${warnings.length > 1 ? 'names' : 'name'}`
);
title(`Missing global variable ${warnings.length > 1 ? 'names' : 'name'}`);
stderr(
`Use output.globals to specify browser global variable names corresponding to external modules`
);
Expand All @@ -262,22 +237,18 @@ const deferredHandlers: {
'https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect'
);

const plugins = Array.from(
new Set(warnings.map(w => w.plugin).filter(Boolean))
);
const plugins = Array.from(new Set(warnings.map(w => w.plugin).filter(Boolean)));
const detail =
plugins.length === 0
? ''
: plugins.length > 1
? ` (such as ${plugins
.slice(0, -1)
.map(p => `'${p}'`)
.join(', ')} and '${plugins.slice(-1)}')`
.slice(0, -1)
.map(p => `'${p}'`)
.join(', ')} and '${plugins.slice(-1)}')`
: ` (such as '${plugins[0]}')`;

stderr(
`Plugins that transform code${detail} should generate accompanying sourcemaps`
);
stderr(`Plugins that transform code${detail} should generate accompanying sourcemaps`);
}
},

Expand All @@ -297,9 +268,7 @@ const deferredHandlers: {
if (warning.url !== lastUrl) info((lastUrl = warning.url));

const loc = warning.loc
? `${relativeId(warning.id)}: (${warning.loc.line}:${
warning.loc.column
})`
? `${relativeId(warning.id)}: (${warning.loc.line}:${warning.loc.column})`
: relativeId(warning.id);

stderr(chalk.bold(relativeId(loc)));
Expand All @@ -311,17 +280,17 @@ const deferredHandlers: {
}
};

function title (str: string) {
function title(str: string) {
stderr(`${chalk.bold.yellow('(!)')} ${chalk.bold.yellow(str)}`);
}

function info (url: string) {
function info(url: string) {
stderr(chalk.grey(url));
}

function nest<T> (array: T[], prop: string) {
const nested: { key: string, items: T[] }[] = [];
const lookup = new Map<string, { key: string, items: T[] }>();
function nest<T>(array: T[], prop: string) {
const nested: { key: string; items: T[] }[] = [];
const lookup = new Map<string, { key: string; items: T[] }>();

array.forEach(item => {
const key = (<any>item)[prop];
Expand All @@ -340,21 +309,16 @@ function nest<T> (array: T[], prop: string) {
return nested;
}

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

const sliced =
nestedByModule.length > 5 ? nestedByModule.slice(0, 3) : nestedByModule;
const sliced = nestedByModule.length > 5 ? nestedByModule.slice(0, 3) : nestedByModule;
sliced.forEach(({ key: id, items }) => {
stderr(chalk.bold(relativeId(id)));
stderr(chalk.grey(items[0].frame));

if (items.length > 1) {
stderr(
`...and ${items.length - 1} other ${
items.length > 2 ? 'occurrences' : 'occurrence'
}`
);
stderr(`...and ${items.length - 1} other ${items.length > 2 ? 'occurrences' : 'occurrence'}`);
}
});

Expand Down
30 changes: 18 additions & 12 deletions bin/src/run/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ import { InputOptions, OutputOptions, OutputChunk } from '../../../src/rollup/in
import { BatchWarnings } from './batchWarnings';
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 && inputOptions.input instanceof Array === false;
export default function build(
inputOptions: InputOptions,
outputOptions: OutputOptions[],
warnings: BatchWarnings,
silent = false
) {
const useStdout =
outputOptions.length === 1 &&
!outputOptions[0].file &&
inputOptions.input instanceof Array === false;

const start = Date.now();
const files = useStdout
? ['stdout']
: outputOptions.map(t => relativeId(t.file || t.dir));
const files = useStdout ? ['stdout'] : outputOptions.map(t => relativeId(t.file || t.dir));
if (!silent)
stderr(
chalk.cyan(
`\n${chalk.bold(typeof inputOptions.input === 'string' ? inputOptions.input : inputOptions.input && inputOptions.input.join(', '))}${chalk.bold(
files.join(', ')
)}...`
`\n${chalk.bold(
typeof inputOptions.input === 'string'
? inputOptions.input
: inputOptions.input && inputOptions.input.join(', ')
)}${chalk.bold(files.join(', '))}...`
)
);

Expand All @@ -38,7 +46,7 @@ export default function build (inputOptions: InputOptions, outputOptions: Output
});
}

return bundle.generate(output).then(({ code, map }: { code: string, map: SourceMap }) => {
return bundle.generate(output).then(({ code, map }: { code: string; map: SourceMap }) => {
if (output.sourcemap === 'inline') {
code += `\n//# ${SOURCEMAPPING_URL}=${map.toUrl()}\n`;
}
Expand All @@ -56,9 +64,7 @@ export default function build (inputOptions: InputOptions, outputOptions: Output
if (!silent)
stderr(
chalk.green(
`created ${chalk.bold(files.join(', '))} in ${chalk.bold(
ms(Date.now() - start)
)}`
`created ${chalk.bold(files.join(', '))} in ${chalk.bold(ms(Date.now() - start))}`
)
);
})
Expand Down

0 comments on commit 840ff5f

Please sign in to comment.