Skip to content
Permalink
Browse files

feat: improve `url()` resolving algorithm

  • Loading branch information
cap-Bernardito committed Jul 16, 2020
1 parent d139ec1 commit bc19ddd8779dafbc2a420870a3cb841041ce9c7c
@@ -17,3 +17,4 @@ Thumbs.db
*.sublime-project
*.sublime-workspace
/test/fixtures/import/import-absolute.css
/test/fixtures/url/url-absolute.css
@@ -125,7 +125,7 @@ Type: `Boolean|Function`
Default: `true`

Enables/Disables `url`/`image-set` functions handling.
Control `url()` resolving. Absolute URLs and root-relative URLs are not resolving.
Control `url()` resolving. Absolute URLs are not resolving.

Examples resolutions:

@@ -1174,6 +1174,39 @@ module.exports = {
};
```

### Resolve unresolved URLs using an alias

**index.css**

```css
.class {
background: url(/assets/unresolved/img.png);
}
```

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
alias: {
'/assets/unresolved/img.png': path.resolve(
__dirname,
'assets/real-path-to-img/img.png'
),
},
},
};
```

## Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.
@@ -40,7 +40,7 @@
"dist"
],
"peerDependencies": {
"webpack": "^4.0.0 || ^5.0.0"
"webpack": "^4.27.0 || ^5.0.0"
},
"dependencies": {
"camelcase": "^6.0.0",
@@ -2,7 +2,7 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import { getOptions, isUrlRequest, stringifyRequest } from 'loader-utils';
import { getOptions, stringifyRequest } from 'loader-utils';
import postcss from 'postcss';
import postcssPkg from 'postcss/package.json';
import validateOptions from 'schema-utils';
@@ -22,6 +22,7 @@ import {
getModulesPlugins,
normalizeSourceMap,
shouldUseModulesPlugins,
isUrlRequestable,
} from './utils';

export default function loader(content, map, meta) {
@@ -95,11 +96,19 @@ export default function loader(content, map, meta) {
}

if (options.url !== false && exportType === 'full') {
const urlResolver = this.getResolve({
mainFields: ['asset'],
conditionNames: ['asset'],
});

plugins.push(
urlParser({
context: this.context,
rootContext: this.rootContext,
filter: getFilter(options.url, this.resourcePath, (value) =>
isUrlRequest(value)
isUrlRequestable(value)
),
resolver: urlResolver,
urlHandler: (url) => stringifyRequest(this, url),
})
);

0 comments on commit bc19ddd

Please sign in to comment.
You can’t perform that action at this time.