Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
fix: fallback options behaviour (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Aug 17, 2018
1 parent e4d10a2 commit 03e631f
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 233 deletions.
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ export default function loader(src) {
}

// Normalize the fallback.
const { loader: fallbackLoader, query: fallbackQuery } = normalizeFallback(
options.fallback,
options
);
const {
loader: fallbackLoader,
options: fallbackOptions,
} = normalizeFallback(options.fallback, options);

// Require the fallback.
const fallback = require(fallbackLoader);

// Call the fallback, passing a copy of the loader context. The copy has the query replaced. This way, the fallback
// loader receives the query which was intended for it instead of the query which was intended for url-loader.
const fallbackLoaderContext = Object.assign({}, this, {
query: fallbackQuery,
query: fallbackOptions,
});

return fallback.call(fallbackLoaderContext, src);
Expand Down
55 changes: 18 additions & 37 deletions src/utils/normalizeFallback.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
function normalizeFallbackString(fallbackString, originalOptions) {
const index = fallbackString.indexOf('?');
if (index >= 0) {
return {
loader: fallbackString.substr(0, index),
query: fallbackString.substr(index),
};
}
import loaderUtils from 'loader-utils';

// To remain consistent with version 1.0.1, pass any other options which were provided to url-loader to the fallback loader.
const { fallback, limit, mimetype, ...otherOptions } = originalOptions;
export default function normalizeFallback(fallback, originalOptions) {
let loader = 'file-loader';
let options = {};

return {
loader: fallbackString,
query: otherOptions,
};
}
if (typeof fallback === 'string') {
loader = fallback;

function normalizeFallbackObject(fallbackObject) {
return {
loader: fallbackObject.loader,
query: fallbackObject.options,
};
}
const index = fallback.indexOf('?');

/**
* Converts the fallback option, which can be a string or an object, to an object with a loader and a query. The result
* has this form:
* {
* loader: 'file-loader',
* query: '?name=[name].[ext]'
* }
* Note that the returned query can be either a string or an object.
*/
export default function normalizeFallback(fallback, originalOptions) {
// If no fallback was provided, use file-loader.
if (!fallback) {
return normalizeFallbackString('file-loader', originalOptions);
if (index >= 0) {
loader = fallback.substr(0, index);
options = loaderUtils.parseQuery(fallback.substr(index));
}
}

if (typeof fallback === 'string') {
return normalizeFallbackString(fallback, originalOptions);
if (fallback !== null && typeof fallback === 'object') {
({ loader, options } = fallback);
}

return normalizeFallbackObject(fallback);
options = Object.assign({}, originalOptions, options);

delete options.fallback;

return { loader, options };
}
9 changes: 9 additions & 0 deletions test/__snapshots__/fallback-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fallback option {Object} 1`] = `"module.exports={\\"limit\\":-9007199254740991,\\"name\\":\\"fallback-[hash].[ext]\\",\\"unknown\\":\\"fallback-other-value\\"}"`;

exports[`fallback option {String} (with query) 1`] = `"module.exports={\\"limit\\":-9007199254740991,\\"name\\":\\"fallback-[hash].[ext]\\",\\"unknown\\":\\"fallback-value\\"}"`;

exports[`fallback option {String} 1`] = `"module.exports={\\"limit\\":-9007199254740991,\\"name\\":\\"[name].[hash].[ext]\\",\\"unknown\\":\\"value\\"}"`;

exports[`fallback option {undefined} 1`] = `"module.exports = \\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAYAAAA8AXHiAAAAAXNSR0IArs4c6QAAGjNJREFUeAHtnQewFMXWx/uSxZxRUTGAAcyYnwpiJhgRFRCM6GeBZaklopaUWoUo9cwIqPhhAjGAWRQTRjAgijnnLOqnAgL3fv3rR+/r29uzO7t3e3Z2t0/V1uzM9Mx0n/nP6dPnnD4tRKDAgcCBwIFK4UCdq6JDhw5dZeHChRfKc4c2NDR0kNtWrnLhWE1woKGuru4X2dKX5fbym2666aU4rc4C1pAhQ/ZetmzZ7RJQG8W5QShTOxyQwKqXuPj3hhtueN7IkSPrc7W8uXny1FNPXa++vv55eXE783j4HziwnAMIoj3+T9Ibb7zxci6uNDNPSkk1VoJqdfNY+B84YHNAYuQyKY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