diff --git a/src/Chunk.ts b/src/Chunk.ts index 73b48b867d1..b6ead70e594 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -644,19 +644,8 @@ export default class Chunk { ) { timeStart('render format', 3); - if (!this.renderedSource) - throw new Error('Internal error: Chunk render called before preRender'); - const format = options.format as string; const finalise = finalisers[format]; - if (!finalise) { - error({ - code: 'INVALID_OPTION', - message: `Invalid format: ${format} - valid options are ${Object.keys(finalisers).join( - ', ' - )}.` - }); - } if (options.dynamicImportFunction && format !== 'es') { this.graph.warn({ code: 'INVALID_OPTION', @@ -709,7 +698,7 @@ export default class Chunk { } const magicString = finalise( - this.renderedSource, + this.renderedSource as MagicStringBundle, { accessedGlobals, dependencies: this.renderedDeclarations.dependencies, diff --git a/src/utils/PluginCache.ts b/src/utils/PluginCache.ts index 02b631eab4f..54feb3cbcf6 100644 --- a/src/utils/PluginCache.ts +++ b/src/utils/PluginCache.ts @@ -63,39 +63,36 @@ export const NO_CACHE: PluginCache = { } }; -function uncacheablePluginError(pluginName: string) { +function uncacheablePluginError(pluginName: string): never { if ( pluginName.startsWith(ANONYMOUS_PLUGIN_PREFIX) || pluginName.startsWith(ANONYMOUS_OUTPUT_PLUGIN_PREFIX) - ) - error({ + ) { + return error({ code: 'ANONYMOUS_PLUGIN_CACHE', message: 'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.' }); - else - error({ - code: 'DUPLICATE_PLUGIN_NAME', - message: `The plugin name ${pluginName} is being used twice in the same build. Plugin names must be distinct or provide a cacheKey (please post an issue to the plugin if you are a plugin user).` - }); + } + return error({ + code: 'DUPLICATE_PLUGIN_NAME', + message: `The plugin name ${pluginName} is being used twice in the same build. Plugin names must be distinct or provide a cacheKey (please post an issue to the plugin if you are a plugin user).` + }); } export function getCacheForUncacheablePlugin(pluginName: string): PluginCache { return { has() { - uncacheablePluginError(pluginName); - return false; + return uncacheablePluginError(pluginName); }, get() { - uncacheablePluginError(pluginName); - return undefined as any; + return uncacheablePluginError(pluginName); }, set() { - uncacheablePluginError(pluginName); + return uncacheablePluginError(pluginName); }, delete() { - uncacheablePluginError(pluginName); - return false; + return uncacheablePluginError(pluginName); } }; } diff --git a/test/function/samples/invalid-top-level-await/_config.js b/test/function/samples/invalid-top-level-await/_config.js new file mode 100644 index 00000000000..d0ffc48dbd9 --- /dev/null +++ b/test/function/samples/invalid-top-level-await/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: 'throws for invalid top-level-await format', + options: { + experimentalTopLevelAwait: true + }, + generateError: { + code: 'INVALID_TLA_FORMAT', + message: + 'Module format cjs does not support top-level await. Use the "es" or "system" output formats rather.' + } +}; diff --git a/test/function/samples/invalid-top-level-await/main.js b/test/function/samples/invalid-top-level-await/main.js new file mode 100644 index 00000000000..fb59627860e --- /dev/null +++ b/test/function/samples/invalid-top-level-await/main.js @@ -0,0 +1 @@ +await Promise.resolve(); diff --git a/test/function/samples/plugin-cache/anonymous-delete/_config.js b/test/function/samples/plugin-cache/anonymous-delete/_config.js new file mode 100644 index 00000000000..8d9a9ef6e1a --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-delete/_config.js @@ -0,0 +1,18 @@ +module.exports = { + description: 'throws for anonymous plugins deleting from the cache', + options: { + plugins: { + buildStart() { + this.cache.delete('asdf'); + } + } + }, + error: { + code: 'PLUGIN_ERROR', + hook: 'buildStart', + message: + 'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.', + plugin: 'at position 1', + pluginCode: 'ANONYMOUS_PLUGIN_CACHE' + } +}; diff --git a/test/function/samples/plugin-cache/anonymous-delete/main.js b/test/function/samples/plugin-cache/anonymous-delete/main.js new file mode 100644 index 00000000000..65804ade90a --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-delete/main.js @@ -0,0 +1 @@ +assert.equal( 1, 1 ); diff --git a/test/function/samples/plugin-cache/anonymous-get/_config.js b/test/function/samples/plugin-cache/anonymous-get/_config.js new file mode 100644 index 00000000000..9a25655aa12 --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-get/_config.js @@ -0,0 +1,18 @@ +module.exports = { + description: 'throws for anonymous plugins reading the cache', + options: { + plugins: { + buildStart() { + this.cache.get('asdf'); + } + } + }, + error: { + code: 'PLUGIN_ERROR', + hook: 'buildStart', + message: + 'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.', + plugin: 'at position 1', + pluginCode: 'ANONYMOUS_PLUGIN_CACHE' + } +}; diff --git a/test/function/samples/plugin-cache/anonymous-get/main.js b/test/function/samples/plugin-cache/anonymous-get/main.js new file mode 100644 index 00000000000..65804ade90a --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-get/main.js @@ -0,0 +1 @@ +assert.equal( 1, 1 ); diff --git a/test/function/samples/plugin-cache/anonymous-has/_config.js b/test/function/samples/plugin-cache/anonymous-has/_config.js new file mode 100644 index 00000000000..f6d37f0485e --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-has/_config.js @@ -0,0 +1,18 @@ +module.exports = { + description: 'throws for anonymous plugins checking the cache', + options: { + plugins: { + buildStart() { + this.cache.has('asdf'); + } + } + }, + error: { + code: 'PLUGIN_ERROR', + hook: 'buildStart', + message: + 'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.', + plugin: 'at position 1', + pluginCode: 'ANONYMOUS_PLUGIN_CACHE' + } +}; diff --git a/test/function/samples/plugin-cache/anonymous-has/main.js b/test/function/samples/plugin-cache/anonymous-has/main.js new file mode 100644 index 00000000000..65804ade90a --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-has/main.js @@ -0,0 +1 @@ +assert.equal( 1, 1 ); diff --git a/test/function/samples/plugin-cache/anonymous-set/_config.js b/test/function/samples/plugin-cache/anonymous-set/_config.js new file mode 100644 index 00000000000..c12e533586c --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-set/_config.js @@ -0,0 +1,18 @@ +module.exports = { + description: 'throws for anonymous plugins adding to the cache', + options: { + plugins: { + buildStart() { + this.cache.set('asdf', 'asdf'); + } + } + }, + error: { + code: 'PLUGIN_ERROR', + hook: 'buildStart', + message: + 'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.', + plugin: 'at position 1', + pluginCode: 'ANONYMOUS_PLUGIN_CACHE' + } +}; diff --git a/test/function/samples/plugin-cache/anonymous-set/main.js b/test/function/samples/plugin-cache/anonymous-set/main.js new file mode 100644 index 00000000000..65804ade90a --- /dev/null +++ b/test/function/samples/plugin-cache/anonymous-set/main.js @@ -0,0 +1 @@ +assert.equal( 1, 1 ); diff --git a/test/function/samples/plugin-cache/duplicate-names-no-cache/_config.js b/test/function/samples/plugin-cache/duplicate-names-no-cache/_config.js new file mode 100644 index 00000000000..5acf2e8b233 --- /dev/null +++ b/test/function/samples/plugin-cache/duplicate-names-no-cache/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'allows plugins to have the same name if they do not access the cache', + options: { + plugins: [ + { + name: 'test-plugin', + buildStart() {} + }, + { + name: 'test-plugin', + buildStart() {} + } + ] + } +}; diff --git a/test/function/samples/plugin-cache/duplicate-names-no-cache/main.js b/test/function/samples/plugin-cache/duplicate-names-no-cache/main.js new file mode 100644 index 00000000000..65804ade90a --- /dev/null +++ b/test/function/samples/plugin-cache/duplicate-names-no-cache/main.js @@ -0,0 +1 @@ +assert.equal( 1, 1 ); diff --git a/test/function/samples/plugin-cache/duplicate-names/_config.js b/test/function/samples/plugin-cache/duplicate-names/_config.js new file mode 100644 index 00000000000..bf9f5a7af39 --- /dev/null +++ b/test/function/samples/plugin-cache/duplicate-names/_config.js @@ -0,0 +1,27 @@ +module.exports = { + description: 'throws if two plugins with the same name and no cache key access the cache', + options: { + plugins: [ + { + name: 'test-plugin', + buildStart() { + this.cache.set('asdf', 'asdf'); + } + }, + { + name: 'test-plugin', + buildStart() { + this.cache.set('asdf', 'asdf'); + } + } + ] + }, + error: { + code: 'PLUGIN_ERROR', + hook: 'buildStart', + message: + 'The plugin name test-plugin is being used twice in the same build. Plugin names must be distinct or provide a cacheKey (please post an issue to the plugin if you are a plugin user).', + plugin: 'test-plugin', + pluginCode: 'DUPLICATE_PLUGIN_NAME' + } +}; diff --git a/test/function/samples/plugin-cache/duplicate-names/main.js b/test/function/samples/plugin-cache/duplicate-names/main.js new file mode 100644 index 00000000000..65804ade90a --- /dev/null +++ b/test/function/samples/plugin-cache/duplicate-names/main.js @@ -0,0 +1 @@ +assert.equal( 1, 1 ); diff --git a/test/hooks/index.js b/test/hooks/index.js index 84555d91047..0e49c4cbe22 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -58,7 +58,7 @@ describe('hooks', () => { }) ) .then(({ output }) => { - assert.equal(output[0].code, `new banner\n'use strict';\n\nalert('hello');\n`); + assert.strictEqual(output[0].code, `new banner\n'use strict';\n\nalert('hello');\n`); })); it('allows to replace file with dir in the outputOptions hook', () => @@ -118,8 +118,8 @@ describe('hooks', () => { ] }) .then(bundle => { - assert.equal(buildStartCnt, 1); - assert.equal(buildEndCnt, 1); + assert.strictEqual(buildStartCnt, 1); + assert.strictEqual(buildEndCnt, 1); return rollup.rollup({ input: 'input', @@ -140,8 +140,8 @@ describe('hooks', () => { assert.ok(err); }) .then(() => { - assert.equal(buildStartCnt, 2); - assert.equal(buildEndCnt, 2); + assert.strictEqual(buildStartCnt, 2); + assert.strictEqual(buildEndCnt, 2); }); }); @@ -152,10 +152,10 @@ describe('hooks', () => { input: 'input', onwarn(warning) { if (callCnt === 0) { - assert.equal(warning.message, 'build start'); + assert.strictEqual(warning.message, 'build start'); callCnt++; } else if (callCnt === 1) { - assert.equal(warning.message, 'build end'); + assert.strictEqual(warning.message, 'build end'); callCnt++; } }, @@ -172,7 +172,7 @@ describe('hooks', () => { ] }) .then(() => { - assert.equal(callCnt, 2); + assert.strictEqual(callCnt, 2); }); }); @@ -188,7 +188,7 @@ describe('hooks', () => { this.error('build start error'); }, buildEnd(error) { - assert.equal(error.message, 'build start error'); + assert.strictEqual(error.message, 'build start error'); handledError = true; } } @@ -196,7 +196,7 @@ describe('hooks', () => { }) .catch(error => { assert.ok(handledError); - assert.equal(error.message, 'build start error'); + assert.strictEqual(error.message, 'build start error'); }) .then(() => { assert.ok(handledError); @@ -211,8 +211,8 @@ describe('hooks', () => { loader({ input: `alert('hello')` }), { buildStart() { - assert.equal(this.isExternal('test'), true); - assert.equal(this.isExternal('another'), false); + assert.strictEqual(this.isExternal('test'), true); + assert.strictEqual(this.isExternal('another'), false); } } ] @@ -239,7 +239,7 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output: [output] }) => { - assert.equal(output.code, `alert('hello');\n`); + assert.strictEqual(output.code, `alert('hello');\n`); })); it('caches chunk emission in transform hook', () => { @@ -266,12 +266,12 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-928cb70b.js'); - assert.equal(output[1].code, `console.log('chunk');\n`); + assert.strictEqual(output[1].fileName, 'chunk-928cb70b.js'); + assert.strictEqual(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ cache, @@ -291,12 +291,12 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-928cb70b.js'); - assert.equal(output[1].code, `console.log('chunk');\n`); + assert.strictEqual(output[1].fileName, 'chunk-928cb70b.js'); + assert.strictEqual(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ cache, @@ -313,12 +313,12 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-928cb70b.js'); - assert.equal(output[1].code, `console.log('chunk');\n`); + assert.strictEqual(output[1].fileName, 'chunk-928cb70b.js'); + assert.strictEqual(output[1].code, `console.log('chunk');\n`); }); }); @@ -342,12 +342,12 @@ describe('hooks', () => { ]) ) .then(([{ output: output1 }, { output: output2 }]) => { - assert.equal(output1.length, 2, 'output1'); - assert.equal(output1[1].fileName, 'asset'); - assert.equal(output1[1].source, 'es'); - assert.equal(output2.length, 2, 'output2'); - assert.equal(output2[1].fileName, 'asset'); - assert.equal(output2[1].source, 'cjs'); + assert.strictEqual(output1.length, 2, 'output1'); + assert.strictEqual(output1[1].fileName, 'asset'); + assert.strictEqual(output1[1].source, 'es'); + assert.strictEqual(output2.length, 2, 'output2'); + assert.strictEqual(output2[1].fileName, 'asset'); + assert.strictEqual(output2[1].source, 'cjs'); }); }); @@ -374,12 +374,12 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); return rollup.rollup({ cache, @@ -399,12 +399,12 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); return rollup.rollup({ cache, @@ -421,12 +421,12 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); }); }); @@ -468,14 +468,14 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `console.log('imported');\n\n` + `var input = new URL('assets/test-09aeb845.ext', import.meta.url).href;\n\n` + `export default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-09aeb845.ext'); - assert.equal(output[1].source, 'first run'); + assert.strictEqual(output[1].fileName, 'assets/test-09aeb845.ext'); + assert.strictEqual(output[1].source, 'first run'); return rollup.rollup({ cache, @@ -501,14 +501,14 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `console.log('imported');\n\n` + `var input = new URL('assets/test-ce5fc71b.ext', import.meta.url).href;\n\n` + `export default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-ce5fc71b.ext'); - assert.equal(output[1].source, 'second run'); + assert.strictEqual(output[1].fileName, 'assets/test-ce5fc71b.ext'); + assert.strictEqual(output[1].source, 'second run'); }); }); @@ -540,14 +540,14 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); return rollup.rollup({ cache, @@ -566,9 +566,9 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output }) => { - assert.equal(runs, 2); - assert.equal(output[0].code.trim(), `alert('hello world');`); - assert.equal(output.length, 1); + assert.strictEqual(runs, 2); + assert.strictEqual(output[0].code.trim(), `alert('hello world');`); + assert.strictEqual(output.length, 1); }); }); @@ -587,14 +587,14 @@ describe('hooks', () => { const chunk = outputBundle['input.js']; // can detect that b has been tree-shaken this way - assert.equal(chunk.modules['dep'].renderedExports[0], 'a'); - assert.equal(chunk.modules['dep'].renderedExports.length, 1); + assert.strictEqual(chunk.modules['dep'].renderedExports[0], 'a'); + assert.strictEqual(chunk.modules['dep'].renderedExports.length, 1); - assert.equal(chunk.modules['dep'].removedExports[0], 'b'); - assert.equal(chunk.modules['dep'].removedExports.length, 1); + assert.strictEqual(chunk.modules['dep'].removedExports[0], 'b'); + assert.strictEqual(chunk.modules['dep'].removedExports.length, 1); - assert.equal(chunk.modules['dep'].renderedLength, 10); - assert.equal(chunk.modules['dep'].originalLength, 35); + assert.strictEqual(chunk.modules['dep'].renderedLength, 10); + assert.strictEqual(chunk.modules['dep'].originalLength, 35); } } ] @@ -658,78 +658,15 @@ describe('hooks', () => { name: 'cachePlugin', buildStart() { assert.ok(this.cache.has('asdf')); - assert.equal(this.cache.get('asdf'), 'asdf'); + assert.strictEqual(this.cache.get('asdf'), 'asdf'); + assert.strictEqual(this.cache.delete('asdf'), true); + assert.ok(!this.cache.has('asdf')); } } ] }) )); - it('throws for anonymous plugins using the cache', () => - rollup - .rollup({ - input: 'input', - plugins: [ - loader({ input: `alert('hello')` }), - { - buildStart() { - this.cache.set('asdf', 'asdf'); - } - } - ] - }) - .then(() => { - assert.fail('Should have thrown'); - }) - .catch(err => { - assert.equal(err.code, 'PLUGIN_ERROR'); - assert.equal(err.pluginCode, 'ANONYMOUS_PLUGIN_CACHE'); - })); - - it('throws for two plugins using the same name and the cache', () => { - // we don't throw for duplicate names unless there is cache access - return rollup - .rollup({ - input: 'input', - plugins: [ - loader({ input: `alert('hello')` }), - { - name: 'a' - }, - { - name: 'a' - } - ] - }) - .then(() => { - const name = 'MyTestPluginName'; - return rollup - .rollup({ - input: 'input', - plugins: [ - loader({ input: `alert('hello')` }), - { - name, - buildStart() { - this.cache.set('asdf', 'asdf'); - } - }, - { - name, - buildStart() { - this.cache.set('asdf', 'asdf'); - } - } - ] - }) - .catch(err => { - assert.equal(err.code, 'PLUGIN_ERROR'); - assert.equal(err.pluginCode, 'DUPLICATE_PLUGIN_NAME'); - assert.equal(err.message.includes(name), true); - }); - }); - }); - it('Allows plugins with any names using a shared cacheKey', () => rollup.rollup({ input: 'input', @@ -746,14 +683,14 @@ describe('hooks', () => { name: 'a', cacheKey: 'a9b6', buildEnd() { - assert.equal(this.cache.get('asdf'), 'asdf'); + assert.strictEqual(this.cache.get('asdf'), 'asdf'); } }, { name: 'b', cacheKey: 'a9b6', buildEnd() { - assert.equal(this.cache.get('asdf'), 'asdf'); + assert.strictEqual(this.cache.get('asdf'), 'asdf'); } } ] @@ -788,7 +725,7 @@ describe('hooks', () => { { name: 'x', buildStart() { - if (i === 4) assert.equal(this.cache.has('second'), true); + if (i === 4) assert.strictEqual(this.cache.has('second'), true); } } ] @@ -806,9 +743,9 @@ describe('hooks', () => { { name: 'x', buildStart() { - assert.equal(this.cache.has('first'), false); - assert.equal(this.cache.get('first'), undefined); - assert.equal(this.cache.get('second'), 'second'); + assert.strictEqual(this.cache.has('first'), false); + assert.strictEqual(this.cache.get('first'), undefined); + assert.strictEqual(this.cache.get('second'), 'second'); } } ] @@ -826,14 +763,15 @@ describe('hooks', () => { name: 'x', buildStart() { this.cache.set('x', 'x'); - assert.equal(this.cache.has('x'), false); - assert.equal(this.cache.get('x'), undefined); + assert.ok(!this.cache.has('x')); + assert.strictEqual(this.cache.get('x'), undefined); + this.cache.delete('x'); } } ] }) .then(bundle => { - assert.equal(bundle.cache, undefined); + assert.strictEqual(bundle.cache, undefined); })); it('Disables the default transform cache when using cache in transform only', () => @@ -860,7 +798,10 @@ describe('hooks', () => { { name: 'x', transform() { - assert.equal(this.cache.get('asdf'), 'asdf'); + assert.ok(this.cache.has('asdf')); + assert.strictEqual(this.cache.get('asdf'), 'asdf'); + this.cache.delete('asdf'); + assert.ok(!this.cache.has('asdf')); return `alert('hello')`; } } @@ -873,7 +814,7 @@ describe('hooks', () => { }) ) .then(({ output }) => { - assert.equal(output[0].code.trim(), `alert('hello');`); + assert.strictEqual(output[0].code.trim(), `alert('hello');`); })); it('supports renderStart hook', () => { @@ -888,13 +829,13 @@ describe('hooks', () => { { renderStart() { renderStartCount++; - assert.equal(generateBundleCount, 0); - assert.equal(renderErrorCount, 0); + assert.strictEqual(generateBundleCount, 0); + assert.strictEqual(renderErrorCount, 0); }, generateBundle() { generateBundleCount++; - assert.equal(renderStartCount, 1); - assert.equal(renderErrorCount, 0); + assert.strictEqual(renderStartCount, 1); + assert.strictEqual(renderErrorCount, 0); }, renderError() { renderErrorCount++; @@ -904,9 +845,9 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'esm' })) .then(() => { - assert.equal(renderStartCount, 1, 'renderStart count'); - assert.equal(generateBundleCount, 1, 'generateBundle count'); - assert.equal(renderErrorCount, 0, 'renderError count'); + assert.strictEqual(renderStartCount, 1, 'renderStart count'); + assert.strictEqual(generateBundleCount, 1, 'generateBundle count'); + assert.strictEqual(renderErrorCount, 0, 'renderError count'); }); }); @@ -931,8 +872,8 @@ describe('hooks', () => { }, renderError(error) { assert(error); - assert.equal(error.message, 'renderChunk error'); - assert.equal(renderStartCount, 1); + assert.strictEqual(error.message, 'renderChunk error'); + assert.strictEqual(renderStartCount, 1); renderErrorCount++; } } @@ -943,24 +884,18 @@ describe('hooks', () => { assert.ok(err); }) .then(() => { - assert.equal(renderStartCount, 1, 'renderStart count'); - assert.equal(generateBundleCount, 0, 'generateBundle count'); - assert.equal(renderErrorCount, 1, 'renderError count'); + assert.strictEqual(renderStartCount, 1, 'renderStart count'); + assert.strictEqual(generateBundleCount, 0, 'generateBundle count'); + assert.strictEqual(renderErrorCount, 1, 'renderError count'); }); }); - it('Warns when using deprecated this.watcher in plugins', () => { - let warned = false; + it('Warns once when using deprecated this.watcher in plugins', () => { + const warnings = []; const watcher = rollup.watch({ input: 'input', onwarn(warning) { - warned = true; - assert.equal(warning.code, 'PLUGIN_WARNING'); - assert.equal(warning.pluginCode, 'PLUGIN_WATCHER_DEPRECATED'); - assert.equal( - warning.message, - 'this.watcher usage is deprecated in plugins. Use the watchChange plugin hook and this.addWatchFile() instead.' - ); + warnings.push(warning); }, output: { format: 'esm' @@ -971,18 +906,29 @@ describe('hooks', () => { name: 'x', buildStart() { this.watcher.on('change', () => {}); + this.watcher.on('change', () => {}); } } ] }); return new Promise((resolve, reject) => { - watcher.on('event', evt => { - if (evt.code === 'BUNDLE_END') resolve(); - else if (evt.code === 'ERROR' || evt.code === 'FATAL') reject(evt.error); + watcher.on('event', event => { + if (event.code === 'BUNDLE_END') resolve(); + else if (event.code === 'ERROR' || event.code === 'FATAL') reject(event.error); }); }).catch(err => { - assert.equal(err.message, 'You must specify "output.file" or "output.dir" for the build.'); - assert.equal(warned, true); + assert.strictEqual( + err.message, + 'You must specify "output.file" or "output.dir" for the build.' + ); + assert.strictEqual(warnings.length, 1); + const warning = warnings[0]; + assert.strictEqual(warning.code, 'PLUGIN_WARNING'); + assert.strictEqual(warning.pluginCode, 'PLUGIN_WATCHER_DEPRECATED'); + assert.strictEqual( + warning.message, + 'this.watcher usage is deprecated in plugins. Use the watchChange plugin hook and this.addWatchFile() instead.' + ); }); }); @@ -1069,7 +1015,7 @@ describe('hooks', () => { }) ) .then(output => { - assert.equal(augmentChunkHashCalls, 1); + assert.strictEqual(augmentChunkHashCalls, 1); }); }); @@ -1083,14 +1029,14 @@ describe('hooks', () => { onwarn(warning) { deprecationCnt++; if (deprecationCnt === 1) { - assert.equal(warning.code, 'DEPRECATED_FEATURE'); - assert.equal( + assert.strictEqual(warning.code, 'DEPRECATED_FEATURE'); + assert.strictEqual( warning.message, 'The "ongenerate" hook used by plugin at position 2 is deprecated. The "generateBundle" hook should be used instead.' ); } else { - assert.equal(warning.code, 'DEPRECATED_FEATURE'); - assert.equal( + assert.strictEqual(warning.code, 'DEPRECATED_FEATURE'); + assert.strictEqual( warning.message, 'The "onwrite" hook used by plugin at position 2 is deprecated. The "generateBundle/writeBundle" hook should be used instead.' ); @@ -1104,7 +1050,7 @@ describe('hooks', () => { }, onwrite(bundle, out) { - assert.equal(out.ongenerate, true); + assert.strictEqual(out.ongenerate, true); } } ] @@ -1116,7 +1062,7 @@ describe('hooks', () => { }) ) .then(() => { - assert.equal(deprecationCnt, 2); + assert.strictEqual(deprecationCnt, 2); return sander.rimraf(TEMP_DIR); }); }); @@ -1193,8 +1139,8 @@ describe('hooks', () => { input: 'input', onwarn(warning) { deprecationCnt++; - assert.equal(warning.code, 'DEPRECATED_FEATURE'); - assert.equal( + assert.strictEqual(warning.code, 'DEPRECATED_FEATURE'); + assert.strictEqual( warning.message, 'The "transformChunk" hook used by plugin at position 2 is deprecated. The "renderChunk" hook should be used instead.' ); @@ -1207,7 +1153,7 @@ describe('hooks', () => { try { this.emitAsset('test.ext', 'hello world'); } catch (e) { - assert.equal(e.code, 'ASSETS_ALREADY_FINALISED'); + assert.strictEqual(e.code, 'ASSETS_ALREADY_FINALISED'); } } } @@ -1220,8 +1166,8 @@ describe('hooks', () => { }) ) .then(() => { - assert.equal(deprecationCnt, 1); - assert.equal(calledHook, true); + assert.strictEqual(deprecationCnt, 1); + assert.strictEqual(calledHook, true); }); }); @@ -1237,8 +1183,8 @@ describe('hooks', () => { return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`; }, generateBundle(options, outputBundle, isWrite) { - assert.equal(outputBundle['assets/test-0a676135.ext'].source, 'hello world'); - assert.equal( + assert.strictEqual(outputBundle['assets/test-0a676135.ext'].source, 'hello world'); + assert.strictEqual( outputBundle['input.js'].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); @@ -1269,12 +1215,12 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-928cb70b.js'); - assert.equal(output[1].code, `console.log('chunk');\n`); + assert.strictEqual(output[1].fileName, 'chunk-928cb70b.js'); + assert.strictEqual(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ cache, @@ -1294,12 +1240,12 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-928cb70b.js'); - assert.equal(output[1].code, `console.log('chunk');\n`); + assert.strictEqual(output[1].fileName, 'chunk-928cb70b.js'); + assert.strictEqual(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ cache, @@ -1316,12 +1262,12 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-928cb70b.js'); - assert.equal(output[1].code, `console.log('chunk');\n`); + assert.strictEqual(output[1].fileName, 'chunk-928cb70b.js'); + assert.strictEqual(output[1].code, `console.log('chunk');\n`); }); }); @@ -1345,14 +1291,14 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); return rollup.rollup({ cache, @@ -1372,14 +1318,14 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); return rollup.rollup({ cache, @@ -1396,14 +1342,14 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); }); }); @@ -1431,14 +1377,14 @@ describe('hooks', () => { return bundle.generate({ format: 'es' }); }) .then(({ output }) => { - assert.equal( + assert.strictEqual( output[0].code, `var input = new URL('assets/test-0a676135.ext', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); - assert.equal(output[1].fileName, 'assets/test-0a676135.ext'); - assert.equal(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); + assert.strictEqual(output[1].fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output[1].source, 'hello world'); return rollup.rollup({ cache, @@ -1457,9 +1403,9 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output }) => { - assert.equal(runs, 2); - assert.equal(output[0].code.trim(), `alert('hello world');`); - assert.equal(output.length, 1); + assert.strictEqual(runs, 2); + assert.strictEqual(output[0].code.trim(), `alert('hello world');`); + assert.strictEqual(output.length, 1); }); }); @@ -1483,7 +1429,7 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output: [, output] }) => { - assert.equal(output.source, 'hello world'); + assert.strictEqual(output.source, 'hello world'); }); }); @@ -1507,8 +1453,8 @@ describe('hooks', () => { }) .then(bundle => bundle.generate({ format: 'es' })) .then(({ output: [, output] }) => { - assert.equal(output.fileName, 'assets/test-0a676135.ext'); - assert.equal(output.source, 'hello world'); + assert.strictEqual(output.fileName, 'assets/test-0a676135.ext'); + assert.strictEqual(output.source, 'hello world'); }); }); });