diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 20d82f14808..1f1e69f3171 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -171,14 +171,16 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise 1, graph.pluginDriver ); + } + function generate(outputOptions: OutputOptions, isWrite: boolean) { timeStart('GENERATE', 1); const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]'; @@ -282,14 +284,17 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise((rawOutputOptions: GenericConfigObject) => { - const promise = generate(rawOutputOptions, false).then(result => createOutput(result)); + const promise = generate(getOutputOptions(rawOutputOptions), false).then(result => + createOutput(result) + ); Object.defineProperty(promise, 'code', throwAsyncGenerateError); Object.defineProperty(promise, 'map', throwAsyncGenerateError); return promise; }), watchFiles: Object.keys(graph.watchFiles), - write: ((outputOptions: OutputOptions) => { - if (!outputOptions || (!outputOptions.dir && !outputOptions.file)) { + write: ((rawOutputOptions: OutputOptions) => { + const outputOptions = getOutputOptions(rawOutputOptions); + if (!outputOptions.dir && !outputOptions.file) { error({ code: 'MISSING_OPTION', message: 'You must specify "output.file" or "output.dir" for the build.' diff --git a/test/hooks/index.js b/test/hooks/index.js index 99234a6b18f..dd26ae632c5 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -4,6 +4,8 @@ const sander = require('sander'); const { loader } = require('../utils.js'); const rollup = require('../../dist/rollup.js'); +const TEMP_DIR = path.join(__dirname, 'tmp'); + describe('hooks', () => { it('allows to read and modify options in the options hook', () => { return rollup @@ -61,6 +63,41 @@ describe('hooks', () => { }); }); + it('allows to replace file with dir in the outputOptions hook', () => { + return rollup + .rollup({ + input: 'input', + treeshake: false, + plugins: [ + loader({ + input: `console.log('input');import('other');`, + other: `console.log('other');` + }), + { + outputOptions(options) { + const newOptions = Object.assign({}, options, { + dir: TEMP_DIR, + chunkFileNames: 'chunk.js' + }); + delete newOptions.file; + return newOptions; + } + } + ] + }) + .then(bundle => + bundle.write({ + file: path.join(TEMP_DIR, 'bundle.js'), + format: 'esm' + }) + ) + .then(() => { + const fileNames = sander.readdirSync(TEMP_DIR).sort(); + assert.deepStrictEqual(fileNames, ['chunk.js', 'input.js']); + return sander.rimraf(TEMP_DIR); + }); + }); + it('supports buildStart and buildEnd hooks', () => { let buildStartCnt = 0; let buildEndCnt = 0; @@ -213,8 +250,6 @@ describe('hooks', () => { }); it('passes bundle & output object to ongenerate & onwrite hooks, with deprecation warnings', () => { - const file = path.join(__dirname, 'tmp/bundle.js'); - let deprecationCnt = 0; return rollup @@ -251,13 +286,13 @@ describe('hooks', () => { }) .then(bundle => { return bundle.write({ - file, + file: path.join(TEMP_DIR, 'bundle.js'), format: 'es' }); }) .then(() => { assert.equal(deprecationCnt, 2); - return sander.unlink(file); + return sander.rimraf(TEMP_DIR); }); }); @@ -289,7 +324,7 @@ describe('hooks', () => { it('calls onwrite hooks in sequence', () => { const result = []; - const file = path.join(__dirname, 'tmp/bundle.js'); + const file = path.join(TEMP_DIR, 'bundle.js'); return rollup .rollup({ @@ -321,8 +356,7 @@ describe('hooks', () => { }) .then(() => { assert.deepEqual(result, [{ a: file, format: 'cjs' }, { b: file, format: 'cjs' }]); - - return sander.unlink(file); + return sander.rimraf(TEMP_DIR); }); }); @@ -863,7 +897,7 @@ module.exports = input; }); it('supports writeBundle hook', () => { - const file = path.join(__dirname, 'tmp/bundle.js'); + const file = path.join(TEMP_DIR, 'bundle.js'); let bundle; let callCount = 0; return rollup @@ -889,7 +923,10 @@ module.exports = input; ] }) .then(bundle => bundle.write({ format: 'esm', file })) - .then(() => assert.strictEqual(callCount, 1)); + .then(() => { + assert.strictEqual(callCount, 1); + return sander.rimraf(TEMP_DIR); + }); }); it('supports this.cache for plugins', () => { @@ -1227,6 +1264,9 @@ module.exports = input; 'this.watcher usage is deprecated in plugins. Use the watchChange plugin hook and this.addWatchFile() instead.' ); }, + output: { + format: 'esm' + }, plugins: [ loader({ input: `alert('hello')` }), { diff --git a/test/misc/write-bundle.js b/test/misc/write-bundle.js index 9a14204457f..1ef523fe9cc 100644 --- a/test/misc/write-bundle.js +++ b/test/misc/write-bundle.js @@ -21,10 +21,10 @@ describe('bundle.write()', () => { .then(bundle => { assert.throws(() => { bundle.write(); - }, /You must specify "output\.file"/); + }, /You must supply an options object/); assert.throws(() => { - bundle.write({}); + bundle.write({format: 'esm'}); }, /You must specify "output\.file"/); }); });