Skip to content

Commit

Permalink
refactor: remove attributes.root option (#341)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `attributes.root` option was removed in favor `resolve.roots`
  • Loading branch information
cap-Bernardito authored Feb 6, 2021
1 parent 3297b99 commit e7f3bba
Show file tree
Hide file tree
Showing 21 changed files with 981 additions and 772 deletions.
93 changes: 38 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ module.exports = {

return true;
},
root: '.',
},
},
},
Expand Down Expand Up @@ -317,34 +316,6 @@ module.exports = {
};
```

#### `root`

Type: `String`
Default: `undefined`

For urls that start with a `/`, the default behavior is to not translate them.
If a `root` query parameter is set, however, it will be prepended to the url and then translated.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: 'html-loader',
options: {
attributes: {
root: '.',
},
},
},
],
},
};
```

### `preprocessor`

Type: `Function`
Expand Down Expand Up @@ -544,6 +515,44 @@ module.exports = {

## Examples

### roots

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.

**webpack.config.js**

```js
module.exports = {
context: __dirname,
module: {
rules: [
{
test: /\.html$/i,
loader: 'html-loader',
options: {},
},
{
test: /\.jpg$/,
loader: 'file-loader',
},
],
},
resolve: {
roots: [path.resolve(__dirname, 'fixtures')],
},
};
```

**file.html**

```html
<img src="/image.jpg" />
```

```js
// => image.jpg in __dirname/fixtures will be resolved
```

### CDN

**webpack.config.js**
Expand Down Expand Up @@ -662,32 +671,6 @@ module.exports = {
};
```

### 'Root-relative' URLs

With the same configuration as in the CDN example:

**file.html**

```html
<img src="/image.jpg" />
```

**scripts.js**

```js
require('html-loader!./file.html');

// => '<img src="/image.jpg">'
```

**other-scripts.js**

```js
require('html-loader?{"attributes":{"root":"."}}!./file.html');

// => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg">'
```

### Templating

You can use any template system. Below is an example for [handlebars](https://handlebarsjs.com/).
Expand Down
110 changes: 63 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"dependencies": {
"html-minifier-terser": "^5.1.1",
"loader-utils": "^2.0.0",
"parse5-sax-parser": "^6.0.1"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { stringifyRequest } from 'loader-utils';

import { sourcePlugin, minimizerPlugin } from './plugins';
import {
pluginRunner,
normalizeOptions,
getImportCode,
getModuleCode,
getExportCode,
stringifyRequest,
} from './utils';

import schema from './options.json';
Expand All @@ -28,7 +27,8 @@ export default async function loader(content) {
if (options.attributes) {
plugins.push(
sourcePlugin({
urlHandler: (url) => stringifyRequest(this, url),
urlHandler: (url) =>
url[0] === '/' ? `"${url}"` : stringifyRequest(this, url),
attributes: options.attributes,
resourcePath: this.resourcePath,
imports,
Expand Down
3 changes: 0 additions & 3 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@
},
"urlFilter": {
"instanceof": "Function"
},
"root": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
Loading

0 comments on commit e7f3bba

Please sign in to comment.