Skip to content

Commit

Permalink
Observe modified output options in bundle.write (#2802)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 10, 2019
1 parent 947067a commit 351cf69
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
15 changes: 10 additions & 5 deletions src/rollup/index.ts
Expand Up @@ -171,14 +171,16 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise<Ro
// ensure we only do one optimization pass per build
let optimized = false;

function generate(rawOutputOptions: GenericConfigObject, isWrite: boolean) {
const outputOptions = normalizeOutputOptions(
function getOutputOptions(rawOutputOptions: GenericConfigObject) {
return normalizeOutputOptions(
inputOptions,
rawOutputOptions,
chunks.length > 1,
graph.pluginDriver
);
}

function generate(outputOptions: OutputOptions, isWrite: boolean) {
timeStart('GENERATE', 1);

const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]';
Expand Down Expand Up @@ -282,14 +284,17 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise<Ro
const result: RollupBuild = {
cache,
generate: <any>((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: <any>((outputOptions: OutputOptions) => {
if (!outputOptions || (!outputOptions.dir && !outputOptions.file)) {
write: <any>((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.'
Expand Down
58 changes: 49 additions & 9 deletions test/hooks/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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')` }),
{
Expand Down
4 changes: 2 additions & 2 deletions test/misc/write-bundle.js
Expand Up @@ -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"/);
});
});
Expand Down

0 comments on commit 351cf69

Please sign in to comment.