Skip to content

Commit

Permalink
Refine error handling to include dyamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 6, 2018
1 parent 845450f commit 18cba9d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 41 deletions.
81 changes: 41 additions & 40 deletions src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,45 +176,11 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise<Ro
let optimized = false;

function generate(rawOutputOptions: GenericConfigObject, isWrite: boolean) {
const outputOptions = normalizeOutputOptions(inputOptions, rawOutputOptions);

if (typeof outputOptions.file === 'string') {
if (typeof outputOptions.dir === 'string')
error({
code: 'INVALID_OPTION',
message:
'You must set either output.file for a single-file build or output.dir when generating multiple chunks.'
});
if (inputOptions.preserveModules) {
error({
code: 'INVALID_OPTION',
message:
'You must set output.dir instead of output.file when using the preserveModules option.'
});
}
if (Array.isArray(inputOptions.input)) {
if (inputOptions.input.length > 1)
error({
code: 'INVALID_OPTION',
message:
'You must set output.dir instead of output.file when providing multiple inputs.'
});
} else if (typeof inputOptions.input === 'object')
error({
code: 'INVALID_OPTION',
message:
'You must set output.dir instead of output.file when providing named inputs.'
});
}

if (
chunks.length > 1 &&
(outputOptions.format === 'umd' || outputOptions.format === 'iife')
)
error({
code: 'INVALID_OPTION',
message: 'UMD and IIFE output formats are not supported for code-splitting builds.'
});
const outputOptions = normalizeOutputOptions(
inputOptions,
rawOutputOptions,
chunks.length > 1
);

timeStart('GENERATE', 1);

Expand Down Expand Up @@ -440,7 +406,8 @@ function writeOutputFile(

function normalizeOutputOptions(
inputOptions: GenericConfigObject,
rawOutputOptions: GenericConfigObject
rawOutputOptions: GenericConfigObject,
hasMultipleChunks: boolean
): OutputOptions {
if (!rawOutputOptions) {
throw new Error('You must supply an options object');
Expand All @@ -466,5 +433,39 @@ function normalizeOutputOptions(
if (deprecations.length) addDeprecations(deprecations, inputOptions.onwarn);
checkOutputOptions(outputOptions);

if (typeof outputOptions.file === 'string') {
if (typeof outputOptions.dir === 'string')
error({
code: 'INVALID_OPTION',
message:
'You must set either output.file for a single-file build or output.dir when generating multiple chunks.'
});
if (inputOptions.preserveModules) {
error({
code: 'INVALID_OPTION',
message:
'You must set output.dir instead of output.file when using the preserveModules option.'
});
}
if (typeof inputOptions.input === 'object' && !Array.isArray(inputOptions.input))
error({
code: 'INVALID_OPTION',
message: 'You must set output.dir instead of output.file when providing named inputs.'
});
}

if (hasMultipleChunks) {
if (outputOptions.format === 'umd' || outputOptions.format === 'iife')
error({
code: 'INVALID_OPTION',
message: 'UMD and IIFE output formats are not supported for code-splitting builds.'
});
if (typeof outputOptions.file === 'string')
error({
code: 'INVALID_OPTION',
message: 'You must set output.dir instead of output.file when generating multiple chunks.'
});
}

return outputOptions;
}
31 changes: 30 additions & 1 deletion test/misc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('sanity checks', () => {
.then(bundle => {
assert.throws(() => {
bundle.generate({ file: 'x', format: 'es' });
}, /You must set output\.dir instead of output\.file when providing multiple inputs\./);
}, /You must set output\.dir instead of output\.file when generating multiple chunks\./);
});
});

Expand All @@ -188,6 +188,35 @@ describe('sanity checks', () => {
.then(bundle => bundle.generate({ file: 'x', format: 'es' }));
});

it('throws when using dynamic imports with the "file" option', () => {
const warnings = [];

return rollup
.rollup({
input: 'x',
plugins: [loader({ x: 'console.log( "x" );import("y");', y: 'console.log( "y" );' })],
onwarn: warning => warnings.push(warning)
})
.then(bundle => {
assert.throws(() => {
bundle.generate({ file: 'x', format: 'es' });
}, /You must set output\.dir instead of output\.file when generating multiple chunks\./);
});
});

it.only('does not throw when using dynamic imports with the "file" option and "inlineDynamicImports"', () => {
const warnings = [];

return rollup
.rollup({
input: 'x',
inlineDynamicImports: true,
plugins: [loader({ x: 'console.log( "x" );import("y");', y: 'console.log( "y" );' })],
onwarn: warning => warnings.push(warning)
})
.then(bundle => bundle.generate({ file: 'x', format: 'es' }));
});

it('throws when using the object form of "input" together with the "file" option', () => {
const warnings = [];

Expand Down

0 comments on commit 18cba9d

Please sign in to comment.