Skip to content

Commit

Permalink
feat: improve url() resolving algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Jul 16, 2020
1 parent d139ec1 commit bc19ddd
Show file tree
Hide file tree
Showing 13 changed files with 456 additions and 206 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Thumbs.db
*.sublime-project
*.sublime-workspace
/test/fixtures/import/import-absolute.css
/test/fixtures/url/url-absolute.css
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,6 +22,7 @@ import {
getModulesPlugins,
normalizeSourceMap,
shouldUseModulesPlugins,
isUrlRequestable,
} from './utils';

export default function loader(content, map, meta) {
Expand Down Expand Up @@ -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),
})
);
Expand Down
Loading

0 comments on commit bc19ddd

Please sign in to comment.