This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support fallback loader in options.fallback (#123)
Resolves #118 It is now possible to explicitly specify options for the fallback loader. The new definition (schema) for the fallback option is a lighter variant of the one for module.rules.use. See schemas/WebpackOptions.json in the webpack repository.
- Loading branch information
1 parent
9946374
commit 017adc7
Showing
7 changed files
with
255 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function normalizeFallbackString(fallbackString, originalOptions) { | ||
const index = fallbackString.indexOf('?'); | ||
if (index >= 0) { | ||
return { | ||
loader: fallbackString.substr(0, index), | ||
query: fallbackString.substr(index), | ||
}; | ||
} | ||
|
||
// To remain consistent with version 1.0.1, pass the options which were provided to url-loader to the fallback loader. | ||
// Perhaps it would make sense to strip out ‒ or "consume" ‒ the options we know were meant for url-loader: limit and | ||
// mimetype. | ||
return { | ||
loader: fallbackString, | ||
query: originalOptions, | ||
}; | ||
} | ||
|
||
function normalizeFallbackObject(fallbackObject) { | ||
return { | ||
loader: fallbackObject.loader, | ||
query: fallbackObject.options, | ||
}; | ||
} | ||
|
||
/** | ||
* 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 { | ||
loader: 'file-loader', | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
alexander-akait
Member
|
||
}; | ||
} | ||
|
||
if (typeof fallback === 'string') { | ||
return normalizeFallbackString(fallback, originalOptions); | ||
} | ||
|
||
return normalizeFallbackObject(fallback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Options fallback {String} 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; | ||
|
||
exports[`Options fallback {String} 2`] = `"module.exports = __webpack_public_path__ + \\"file.png\\";"`; | ||
|
||
exports[`Options fallback {String} 3`] = `"module.exports = __webpack_public_path__ + \\"name-for-file-loader.png\\";"`; | ||
|
||
exports[`Options fallback {String} 4`] = `"module.exports = __webpack_public_path__ + \\"name-for-file-loader.png\\";"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`normalizeFallback object 1`] = ` | ||
Object { | ||
"loader": "file-loader", | ||
"query": undefined, | ||
} | ||
`; | ||
|
||
exports[`normalizeFallback object-with-options 1`] = ` | ||
Object { | ||
"loader": "file-loader", | ||
"query": Object { | ||
"name": "name-for-file-loader.[ext]", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`normalizeFallback string 1`] = ` | ||
Object { | ||
"loader": "file-loader", | ||
"query": Object { | ||
"limit": 8192, | ||
"name": "name-for-url-loader.[ext]", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`normalizeFallback string-with-query 1`] = ` | ||
Object { | ||
"loader": "file-loader", | ||
"query": "?name=name-for-file-loader.[ext]", | ||
} | ||
`; | ||
|
||
exports[`normalizeFallback undefined 1`] = ` | ||
Object { | ||
"loader": "file-loader", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable | ||
prefer-destructuring, | ||
*/ | ||
import normalizeFallback from '../../src/utils/normalizeFallback'; | ||
|
||
describe('normalizeFallback', () => { | ||
test('undefined', () => { | ||
const result = normalizeFallback(undefined, { | ||
limit: 8192, | ||
name: 'name-for-url-loader.[ext]', | ||
}); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
test('string', () => { | ||
const result = normalizeFallback('file-loader', { | ||
limit: 8192, | ||
name: 'name-for-url-loader.[ext]', | ||
}); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
test('string-with-query', () => { | ||
const result = normalizeFallback( | ||
'file-loader?name=name-for-file-loader.[ext]', | ||
{ limit: 8192, name: 'name-for-url-loader.[ext]' } | ||
); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
test('object', () => { | ||
const result = normalizeFallback( | ||
{ loader: 'file-loader' }, | ||
{ limit: 8192, name: 'name-for-url-loader.[ext]' } | ||
); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
test('object-with-options', () => { | ||
const result = normalizeFallback( | ||
{ | ||
loader: 'file-loader', | ||
options: { name: 'name-for-file-loader.[ext]' }, | ||
}, | ||
{ limit: 8192, name: 'name-for-url-loader.[ext]' } | ||
); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
}); |
I encoutered a bug when I bundle my application with webpack. I didn't specified the fallback option, it seems to be incompatible with previous 1.0.1 versions. The output path is not consistent with the name option specified. Shouldn't be return the below object if i didn't specified the fallback option?
{ loader:‘file-loader’, query: originalOptions }