Skip to content

Commit e7f3bba

Browse files
refactor: remove attributes.root option (#341)
BREAKING CHANGE: the `attributes.root` option was removed in favor `resolve.roots`
1 parent 3297b99 commit e7f3bba

21 files changed

+981
-772
lines changed

README.md

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ module.exports = {
153153

154154
return true;
155155
},
156-
root: '.',
157156
},
158157
},
159158
},
@@ -317,34 +316,6 @@ module.exports = {
317316
};
318317
```
319318

320-
#### `root`
321-
322-
Type: `String`
323-
Default: `undefined`
324-
325-
For urls that start with a `/`, the default behavior is to not translate them.
326-
If a `root` query parameter is set, however, it will be prepended to the url and then translated.
327-
328-
**webpack.config.js**
329-
330-
```js
331-
module.exports = {
332-
module: {
333-
rules: [
334-
{
335-
test: /\.html$/i,
336-
loader: 'html-loader',
337-
options: {
338-
attributes: {
339-
root: '.',
340-
},
341-
},
342-
},
343-
],
344-
},
345-
};
346-
```
347-
348319
### `preprocessor`
349320

350321
Type: `Function`
@@ -544,6 +515,44 @@ module.exports = {
544515

545516
## Examples
546517

518+
### roots
519+
520+
With [`resolve.roots`](https://webpack.js.org/configuration/resolve/#resolveroots) can specify a list of directories where requests of server-relative URLs (starting with '/') are resolved.
521+
522+
**webpack.config.js**
523+
524+
```js
525+
module.exports = {
526+
context: __dirname,
527+
module: {
528+
rules: [
529+
{
530+
test: /\.html$/i,
531+
loader: 'html-loader',
532+
options: {},
533+
},
534+
{
535+
test: /\.jpg$/,
536+
loader: 'file-loader',
537+
},
538+
],
539+
},
540+
resolve: {
541+
roots: [path.resolve(__dirname, 'fixtures')],
542+
},
543+
};
544+
```
545+
546+
**file.html**
547+
548+
```html
549+
<img src="/image.jpg" />
550+
```
551+
552+
```js
553+
// => image.jpg in __dirname/fixtures will be resolved
554+
```
555+
547556
### CDN
548557

549558
**webpack.config.js**
@@ -662,32 +671,6 @@ module.exports = {
662671
};
663672
```
664673

665-
### 'Root-relative' URLs
666-
667-
With the same configuration as in the CDN example:
668-
669-
**file.html**
670-
671-
```html
672-
<img src="/image.jpg" />
673-
```
674-
675-
**scripts.js**
676-
677-
```js
678-
require('html-loader!./file.html');
679-
680-
// => '<img src="/image.jpg">'
681-
```
682-
683-
**other-scripts.js**
684-
685-
```js
686-
require('html-loader?{"attributes":{"root":"."}}!./file.html');
687-
688-
// => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg">'
689-
```
690-
691674
### Templating
692675

693676
You can use any template system. Below is an example for [handlebars](https://handlebarsjs.com/).

package-lock.json

Lines changed: 63 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
},
4545
"dependencies": {
4646
"html-minifier-terser": "^5.1.1",
47-
"loader-utils": "^2.0.0",
4847
"parse5-sax-parser": "^6.0.1"
4948
},
5049
"devDependencies": {

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { stringifyRequest } from 'loader-utils';
2-
31
import { sourcePlugin, minimizerPlugin } from './plugins';
42
import {
53
pluginRunner,
64
normalizeOptions,
75
getImportCode,
86
getModuleCode,
97
getExportCode,
8+
stringifyRequest,
109
} from './utils';
1110

1211
import schema from './options.json';
@@ -28,7 +27,8 @@ export default async function loader(content) {
2827
if (options.attributes) {
2928
plugins.push(
3029
sourcePlugin({
31-
urlHandler: (url) => stringifyRequest(this, url),
30+
urlHandler: (url) =>
31+
url[0] === '/' ? `"${url}"` : stringifyRequest(this, url),
3232
attributes: options.attributes,
3333
resourcePath: this.resourcePath,
3434
imports,

src/options.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454
},
5555
"urlFilter": {
5656
"instanceof": "Function"
57-
},
58-
"root": {
59-
"type": "string"
6057
}
6158
},
6259
"additionalProperties": false

0 commit comments

Comments
 (0)