From 52761da46d8be83b912fcd2bdfa763a11fca3fcc Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 20 Jul 2022 06:13:53 +0200 Subject: [PATCH] Extend documentation --- cli/logging.ts | 7 +++--- docs/02-javascript-api.md | 2 ++ docs/999-big-list-of-options.md | 6 +++-- rollup.config.ts | 2 +- src/Chunk.ts | 24 ------------------- .../missing-dependency-resolution/_config.js | 2 +- .../missing-entry-resolution/_config.js | 2 +- .../_expected/amd/entry-main-5940aabc-amd.js | 4 ---- .../_expected/cjs/entry-main-9cf3a232-cjs.js | 4 ---- .../_config.js | 4 ++-- 10 files changed, 15 insertions(+), 42 deletions(-) diff --git a/cli/logging.ts b/cli/logging.ts index 4eb7e9174b5..9a319c27a61 100644 --- a/cli/logging.ts +++ b/cli/logging.ts @@ -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())}`))); diff --git a/docs/02-javascript-api.md b/docs/02-javascript-api.md index 4a19bc5d86c..e59df36eb0b 100755 --- a/docs/02-javascript-api.md +++ b/docs/02-javascript-api.md @@ -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'; diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index 0017841bfd1..4397339e004 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -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 @@ -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); diff --git a/rollup.config.ts b/rollup.config.ts index 0136660c52f..01783d5ca12 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -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 = { diff --git a/src/Chunk.ts b/src/Chunk.ts index 2d9a251d600..90a1f258e51 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -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(); - 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 = /[?#]/; diff --git a/test/browser/samples/missing-dependency-resolution/_config.js b/test/browser/samples/missing-dependency-resolution/_config.js index 123ab0a95e2..a2c0c5febcd 100644 --- a/test/browser/samples/missing-dependency-resolution/_config.js +++ b/test/browser/samples/missing-dependency-resolution/_config.js @@ -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'] } }; diff --git a/test/browser/samples/missing-entry-resolution/_config.js b/test/browser/samples/missing-entry-resolution/_config.js index 727dfa793a2..d575c881f01 100644 --- a/test/browser/samples/missing-entry-resolution/_config.js +++ b/test/browser/samples/missing-entry-resolution/_config.js @@ -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".' } }; diff --git a/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/amd/entry-main-5940aabc-amd.js b/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/amd/entry-main-5940aabc-amd.js index 7f055597818..bed6a1ef6b4 100644 --- a/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/amd/entry-main-5940aabc-amd.js +++ b/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/amd/entry-main-5940aabc-amd.js @@ -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 })); diff --git a/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/cjs/entry-main-9cf3a232-cjs.js b/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/cjs/entry-main-9cf3a232-cjs.js index 71f9d21b13d..e28e6fa0c72 100644 --- a/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/cjs/entry-main-9cf3a232-cjs.js +++ b/test/chunking-form/samples/emit-file/filenames-function-patterns/_expected/cjs/entry-main-9cf3a232-cjs.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 diff --git a/test/function/samples/namespace-reassign-import-fails/_config.js b/test/function/samples/namespace-reassign-import-fails/_config.js index fd14abb760d..4f89a5e533e 100644 --- a/test/function/samples/namespace-reassign-import-fails/_config.js +++ b/test/function/samples/namespace-reassign-import-fails/_config.js @@ -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', @@ -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',