Skip to content

Commit

Permalink
Extend documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jul 30, 2022
1 parent 941763b commit e61eb25
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 42 deletions.
7 changes: 4 additions & 3 deletions cli/logging.ts
Expand Up @@ -7,9 +7,10 @@ import relativeId from '../src/utils/relativeId';
export const stderr = (...args: readonly unknown[]) => process.stderr.write(`${args.join('')}\n`);

export function handleError(err: RollupError, recover = false): void {
let description = err.message || err;
if (err.name || err.cause?.name) description = `${err.cause?.name || err.name}: ${description}`;
const message = (err.plugin ? `(plugin ${err.plugin}) ${description}` : description) || err;
const name = err.name || err.cause?.name;
const nameSection = name ? `${name}: ` : '';
const pluginSection = err.plugin ? `(plugin ${err.plugin}) ` : '';
const message = `${pluginSection}${nameSection}${err.message}`;

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

Expand Down
2 changes: 2 additions & 0 deletions docs/02-javascript-api.md
Expand Up @@ -12,6 +12,8 @@ On a `bundle` object, you can call `bundle.generate` multiple times with differe

Once you're finished with the `bundle` object, you should call `bundle.close()`, which will let plugins clean up their external processes or services via the [`closeBundle`](guide/en/#closebundle) hook.

If an error occurs at either stage, it will return a Promise rejected with an Error, which you can identify via their `code` property. Besides `code` and `message`, many errors have additional properties you can use for custom reporting, see [`utils/error.ts`](https://github.com/rollup/rollup/blob/master/src/utils/error.ts) for a complete list of errors and warnings together with their codes and properties.

```javascript
import { rollup } from 'rollup';

Expand Down
6 changes: 4 additions & 2 deletions docs/999-big-list-of-options.md
Expand Up @@ -370,7 +370,7 @@ Type: `(warning: RollupWarning, defaultHandler: (warning: string | RollupWarning
A function that will intercept warning messages. If not supplied, warnings will be deduplicated and printed to the console. When using the [`--silent`](guide/en/#--silent) CLI option, this handler is the only way to get notified about warnings.
The function receives two arguments: the warning object and the default handler. Warnings objects have, at a minimum, a `code` and a `message` property, allowing you to control how different kinds of warnings are handled. Other properties are added depending on the type of warning.
The function receives two arguments: the warning object and the default handler. Warnings objects have, at a minimum, a `code` and a `message` property, allowing you to control how different kinds of warnings are handled. Other properties are added depending on the type of warning. See [`utils/error.ts`](https://github.com/rollup/rollup/blob/master/src/utils/error.ts) for a complete list of errors and warnings together with their codes and properties.
```js
// rollup.config.js
Expand All @@ -381,7 +381,9 @@ export default {
if (warning.code === 'UNUSED_EXTERNAL_IMPORT') return;

// throw on others
if (warning.code === 'MISSING_EXPORT') throw new Error(warning.message);
// Using Object.assign over new Error(warning.message) will make the CLI
// print additional information such as warning location and help url.
if (warning.code === 'MISSING_EXPORT') throw Object.assign(new Error(), warning);

// Use default for everything else
warn(warning);
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.ts
Expand Up @@ -47,7 +47,7 @@ const onwarn: WarningHandlerWithDefault = warning => {
'Building Rollup produced warnings that need to be resolved. ' +
'Please keep in mind that the browser build may never have external dependencies!'
);
throw new Error(warning.message);
throw Object.assign(new Error(), warning);
};

const moduleAliases = {
Expand Down
1 change: 1 addition & 0 deletions src/Bundle.ts
Expand Up @@ -51,6 +51,7 @@ export default class Bundle {

timeEnd('initialize render', 2);
timeStart('generate chunks', 2);

const getHashPlaceholder = getHashPlaceholderGenerator();
const chunks = await this.generateChunks(outputBundle, getHashPlaceholder);
if (chunks.length > 1) {
Expand Down
24 changes: 0 additions & 24 deletions src/Chunk.ts
Expand Up @@ -1353,28 +1353,4 @@ function getImportedBindingsPerDependency(
return importedBindingsPerDependency;
}

function getImportedBindingsPerDependency(
renderedDependencies: RenderedDependencies,
resolveFileName: (dependency: Chunk | ExternalChunk) => string
): {
[imported: string]: string[];
} {
const importedBindingsPerDependency: { [imported: string]: string[] } = {};
for (const [dependency, declaration] of renderedDependencies) {
const specifiers = new Set<string>();
if (declaration.imports) {
for (const { imported } of declaration.imports) {
specifiers.add(imported);
}
}
if (declaration.reexports) {
for (const { imported } of declaration.reexports) {
specifiers.add(imported);
}
}
importedBindingsPerDependency[resolveFileName(dependency)] = [...specifiers];
}
return importedBindingsPerDependency;
}

const QUERY_HASH_REGEX = /[?#]/;
Expand Up @@ -10,7 +10,7 @@ console.log(foo);`
},
error: {
message:
"Unexpected warnings (UNRESOLVED_IMPORT): 'dep' is imported by main, but could not be resolved – treating it as an external dependency\nIf you expect warnings, list their codes in config.expectedWarnings",
'Unexpected warnings (UNRESOLVED_IMPORT): "dep" is imported by "main", but could not be resolved – treating it as an external dependency.\nIf you expect warnings, list their codes in config.expectedWarnings',
watchFiles: ['main']
}
};
2 changes: 1 addition & 1 deletion test/browser/samples/missing-entry-resolution/_config.js
Expand Up @@ -2,6 +2,6 @@ module.exports = {
description: 'fails if an entry cannot be resolved',
error: {
code: 'UNRESOLVED_ENTRY',
message: 'Could not resolve entry module (main).'
message: 'Could not resolve entry module "main".'
}
};
@@ -1,10 +1,6 @@
define(['require'], (function (require) { 'use strict';

console.log('main');
<<<<<<<< HEAD:test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/amd/entry-main-5940aabc-amd.js
new Promise(function (resolve, reject) { require(['./chunk-deb-87ce45a9-amd'], resolve, reject); }).then(console.log);
========
new Promise(function (resolve, reject) { require(['./chunk-deb-faae56f2-amd'], resolve, reject); }).then(console.log);
>>>>>>>> 113353e74 ([v3.0] New hashing algorithm that "fixes (nearly) everything" (#4543)):test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/amd/entry-main-aaf15a0c-amd.js

}));
@@ -1,8 +1,4 @@
'use strict';

console.log('main');
<<<<<<<< HEAD:test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/cjs/entry-main-9cf3a232-cjs.js
Promise.resolve().then(function () { return require('./chunk-deb-69905607-cjs.js'); }).then(console.log);
========
Promise.resolve().then(function () { return require('./chunk-deb-2221fa83-cjs.js'); }).then(console.log);
>>>>>>>> 113353e74 ([v3.0] New hashing algorithm that "fixes (nearly) everything" (#4543)):test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/cjs/entry-main-69b6552e-cjs.js
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const path = require('path');
const { assertIncludes } = require('../../../utils.js');
const ID_MAIN = path.join(__dirname, 'main.js');
const ID_FOO = path.join(__dirname, 'foo.js');

module.exports = {
description: 'warns for reassignments to namespace exports',
Expand All @@ -13,8 +14,7 @@ module.exports = {
{
binding: 'bar',
code: 'MISSING_EXPORT',
exporter:
'/Users/lukastaegert/Github/rollup/test/function/samples/namespace-reassign-import-fails/foo.js',
exporter: ID_FOO,
id: ID_MAIN,
message: '"bar" is not exported by "foo.js", imported by "main.js".',
url: 'https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module',
Expand Down

0 comments on commit e61eb25

Please sign in to comment.