Skip to content

Commit 33b0d59

Browse files
committed
Add files.
0 parents  commit 33b0d59

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
<div align="center">
3+
<a href="https://github.com/webpack/webpack">
4+
<img width="200" height="200"
5+
src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
6+
</a>
7+
<h1>URL Replace Loader</h1>
8+
<p>Loads files as `base64` encoded URL</p>
9+
</div>
10+
11+
12+
<h2 align="center">Install</h2>
13+
14+
```bash
15+
npm install --save-dev url-replace-loader
16+
```
17+
18+
<h2 align="center"><a href="https://webpack.js.org/concepts/loaders">Usage</a></h2>
19+
20+
The `url-loader` works like the [`file-loader`](https://github.com/webpack-contrib/file-loader), but can return a DataURL if the file is smaller than a byte limit.
21+
22+
23+
```js
24+
import img from './image.png'
25+
```
26+
27+
**webpack.config.js**
28+
```js
29+
module.exports = {
30+
module: {
31+
rules: [
32+
{
33+
test: /\.(svg|png|bmp|jpg|jpeg|gif)$/,
34+
loader: require.resolve('url-loader'),
35+
options: {
36+
limit: 8192,
37+
name: 'img/[name].[hash:8].[ext]',
38+
replace: [
39+
{
40+
test: /rdoc\.logo\.svg$/,
41+
path: 'path/to/logo.svg',
42+
}
43+
]
44+
},
45+
},
46+
]
47+
}
48+
}
49+
```
50+
51+
52+
<h2 align="center">Options</h2>
53+
54+
|Name|Type|Default|Description|
55+
|:--:|:--:|:-----:|:----------|
56+
|**`limit`**|`{Number}`|`undefined`|Byte limit to inline files as Data URL|
57+
|**`mimetype`**|`{String}`|`extname`|Specify MIME type for the file (Otherwise it's inferred from the file extension)|
58+
|**`fallback`**|`{String}`|`file-loader`|Specify `loader` for the file when file is greater than the limit (in bytes)|
59+
60+
### `limit`
61+
62+
If the file is greater than the limit (in bytes) the [`file-loader`](https://github.com/webpack-contrib/file-loader) is used by default and all query parameters are passed to it.
63+
You can use other loader using `fallback` option.
64+
65+
The limit can be specified via loader options and defaults to no limit.
66+
67+
**webpack.config.js**
68+
69+
```js
70+
{
71+
loader: 'url-loader',
72+
options: {
73+
limit: 8192
74+
}
75+
}
76+
```
77+
78+
### `mimetype`
79+
80+
Set the MIME type for the file. If unspecified the file extensions will be used to lookup the MIME type.
81+
82+
**webpack.config.js**
83+
```js
84+
{
85+
loader: 'url-loader',
86+
options: {
87+
mimetype: 'image/png'
88+
}
89+
}
90+
```
91+
92+
### `fallback`
93+
94+
**webpack.config.js**
95+
```js
96+
{
97+
loader: 'url-loader',
98+
options: {
99+
fallback: 'responsive-loader'
100+
}
101+
}
102+
```
103+
104+
### `replace`
105+
106+
Replace with the specified picture.
107+
108+
**webpack.config.js**
109+
```js
110+
{
111+
loader: 'url-loader',
112+
options: {
113+
replace: [
114+
{
115+
test: /rdoc\.logo\.svg$/,
116+
path: 'path/to/logo.svg',
117+
}
118+
]
119+
}
120+
}
121+
```

index.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var loaderUtils = require("loader-utils");
6+
var validateOptions = require("schema-utils");
7+
8+
var FS = require("fs");
9+
var mime = require("mime");
10+
11+
module.exports = function (content) {
12+
this.cacheable && this.cacheable();
13+
14+
var options = loaderUtils.getOptions(this) || {};
15+
16+
validateOptions(require("./options"), options, "URL Loader")
17+
// Options `dataUrlLimit` is backward compatibility with first loader versions
18+
var limit = options.limit || (this.options && this.options.url && this.options.url.dataUrlLimit);
19+
20+
if (limit) {
21+
limit = parseInt(limit, 10);
22+
}
23+
24+
var replace = options.replace || [];
25+
replace.forEach(function (params) {
26+
if (params.test && params.path && FS.existsSync(params.path)) {
27+
content = FS.readFileSync(params.path);
28+
this.resourcePath = params.path;
29+
}
30+
})
31+
32+
var mimetype = options.mimetype || options.minetype || mime.lookup(this.resourcePath);
33+
// No limits or limit more than content length
34+
if (!limit || content.length < limit) {
35+
if (typeof content === "string") {
36+
content = new Buffer(content);
37+
}
38+
39+
return "module.exports = " + JSON.stringify("data:" + (mimetype ? mimetype + ";" : "") + "base64," + content.toString("base64"));
40+
}
41+
42+
var fallback = options.fallback || "file-loader";
43+
var fallbackLoader = require(fallback);
44+
45+
return fallbackLoader.call(this, content);
46+
}
47+
48+
module.exports.raw = true;

options.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"limit": {
5+
"type": ["number", "string"]
6+
},
7+
"prefix": {
8+
"type": "string"
9+
},
10+
"mimetype": {
11+
"type": "string"
12+
},
13+
"encoding": {
14+
"type": "string"
15+
}
16+
},
17+
"additionalProperties": true
18+
}

package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "url-replace-loader",
3+
"version": "1.0.0",
4+
"description": "url loader module for webpack",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"dependencies": {
10+
"loader-utils": "^1.0.2",
11+
"mime": "^1.4.1",
12+
"schema-utils": "^0.3.0"
13+
},
14+
"peerDependencies": {
15+
"file-loader": "*"
16+
},
17+
"bugs": {
18+
"url": "https://github.com/react-doc/url-replace-loader/issues"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/react-doc/url-replace-loader.git"
23+
},
24+
"keywords": [],
25+
"author": "kenny wong <[email protected]>",
26+
"license": "MIT"
27+
}

0 commit comments

Comments
 (0)