file-loader
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="1.1.0"></a> | ||
# [1.1.0](https://github.com/webpack/file-loader/compare/v1.0.0...v1.1.0) (2017-09-29) | ||
### Features | ||
* add `options` validation (`schema-utils`) ([#184](https://github.com/webpack/file-loader/issues/184)) ([696ca77](https://github.com/webpack/file-loader/commit/696ca77)) | ||
<a name="1.0.0"></a> | ||
@@ -10,3 +20,20 @@ # [1.0.0](https://github.com/webpack/file-loader/compare/v1.0.0-rc.0...v1.0.0) (2017-07-26) | ||
### Bug Fixes | ||
* remove `=` from default export (`SyntaxError`) ([#178](https://github.com/webpack/file-loader/issues/178)) ([3fe2d12](https://github.com/webpack/file-loader/commit/3fe2d12)) | ||
### Code Refactoring | ||
* Upgrade to defaults 1.3.0 ([#170](https://github.com/webpack-contrib/file-loader/pull/170)) ([632ed72](https://github.com/webpack/file-loader/commit/acd6c2f)) | ||
* Apply webpack-defaults ([#167](https://github.com/webpack/file-loader/issues/167)) ([632ed72](https://github.com/webpack/file-loader/commit/632ed72)) | ||
### BREAKING CHANGES | ||
* Enforces Webpack standard NodeJS engines range. | ||
at the time of merge `>= 4.3 < 5.0.0 || >= 5.10`. | ||
<a name="1.0.0-rc.0"></a> | ||
@@ -13,0 +40,0 @@ # [1.0.0-rc.0](https://github.com/webpack/file-loader/compare/v1.0.0-beta.1...v1.0.0-rc.0) (2017-07-26) |
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports.raw = undefined; | ||
exports.default = fileLoader; | ||
exports.default = loader; | ||
@@ -18,51 +18,44 @@ var _path = require('path'); | ||
var _schemaUtils = require('schema-utils'); | ||
var _schemaUtils2 = _interopRequireDefault(_schemaUtils); | ||
var _options = require('./options.json'); | ||
var _options2 = _interopRequireDefault(_options); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
*/ | ||
function fileLoader(content) { | ||
if (!this.emitFile) throw new Error('emitFile is required from module system'); | ||
function loader(content) { | ||
if (!this.emitFile) throw new Error('File Loader\n\nemitFile is required from module system'); | ||
var query = _loaderUtils2.default.getOptions(this) || {}; | ||
var configKey = query.config || 'fileLoader'; | ||
var options = this.options[configKey] || {}; | ||
var options = _loaderUtils2.default.getOptions(this) || {}; | ||
var config = { | ||
publicPath: undefined, | ||
useRelativePath: false, | ||
name: '[hash].[ext]' | ||
}; | ||
(0, _schemaUtils2.default)(_options2.default, options, 'File Loader'); | ||
// options takes precedence over config | ||
Object.keys(options).forEach(function (attr) { | ||
config[attr] = options[attr]; | ||
}); | ||
var context = options.context || this.options.context; | ||
// query takes precedence over config and options | ||
Object.keys(query).forEach(function (attr) { | ||
config[attr] = query[attr]; | ||
}); | ||
var context = config.context || this.options.context; | ||
var url = _loaderUtils2.default.interpolateName(this, config.name, { | ||
var url = _loaderUtils2.default.interpolateName(this, options.name, { | ||
context, | ||
content, | ||
regExp: config.regExp | ||
regExp: options.regExp | ||
}); | ||
var outputPath = ''; | ||
if (config.outputPath) { | ||
if (options.outputPath) { | ||
// support functions as outputPath to generate them dynamically | ||
outputPath = typeof config.outputPath === 'function' ? config.outputPath(url) : config.outputPath; | ||
outputPath = typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath; | ||
} | ||
var filePath = this.resourcePath; | ||
if (config.useRelativePath) { | ||
var issuerContext = this._module && this._module.issuer && this._module.issuer.context || context; // eslint-disable-line no-mixed-operators | ||
if (options.useRelativePath) { | ||
var issuerContext = this._module && this._module.issuer && this._module.issuer.context || context; | ||
var relativeUrl = issuerContext && _path2.default.relative(issuerContext, filePath).split(_path2.default.sep).join('/'); | ||
var relativePath = relativeUrl && `${_path2.default.dirname(relativeUrl)}/`; | ||
// eslint-disable-next-line no-bitwise | ||
if (~relativePath.indexOf('../')) { | ||
// eslint-disable-line no-bitwise | ||
outputPath = _path2.default.posix.join(outputPath, relativePath, url); | ||
@@ -72,6 +65,8 @@ } else { | ||
} | ||
url = relativePath + url; | ||
} else if (config.outputPath) { | ||
} else if (options.outputPath) { | ||
// support functions as outputPath to generate them dynamically | ||
outputPath = typeof config.outputPath === 'function' ? config.outputPath(url) : config.outputPath + url; | ||
outputPath = typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath + url; | ||
url = outputPath; | ||
@@ -83,8 +78,9 @@ } else { | ||
var publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`; | ||
if (config.publicPath !== undefined) { | ||
if (options.publicPath !== undefined) { | ||
// support functions as publicPath to generate them dynamically | ||
publicPath = JSON.stringify(typeof config.publicPath === 'function' ? config.publicPath(url) : config.publicPath + url); | ||
publicPath = JSON.stringify(typeof options.publicPath === 'function' ? options.publicPath(url) : options.publicPath + url); | ||
} | ||
if (query.emitFile === undefined || query.emitFile) { | ||
if (options.emitFile === undefined || options.emitFile) { | ||
this.emitFile(outputPath, content); | ||
@@ -91,0 +87,0 @@ } |
{ | ||
"name": "file-loader", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Tobias Koppers @sokra", | ||
"description": "file loader module for webpack", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 4.3 < 5.0.0 || >= 5.10" | ||
}, | ||
"main": "dist/cjs.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"dependencies": { | ||
"loader-utils": "^1.0.2" | ||
"loader-utils": "^1.0.2", | ||
"schema-utils": "^0.3.0" | ||
}, | ||
@@ -36,3 +45,3 @@ "devDependencies": { | ||
"appveyor:test": "npm run test", | ||
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js'", | ||
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js' --copy-files", | ||
"clean": "del-cli dist", | ||
@@ -61,10 +70,2 @@ "lint": "eslint --cache src test", | ||
"homepage": "https://github.com/webpack/file-loader", | ||
"main": "dist/cjs.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 4.3 < 5.0.0 || >= 5.10" | ||
}, | ||
"pre-commit": "lint-staged", | ||
@@ -71,0 +72,0 @@ "lint-staged": { |
250
README.md
[![npm][npm]][npm-url] | ||
[![node][node]][node-url] | ||
[![deps][deps]][deps-url] | ||
@@ -13,3 +14,3 @@ [![tests][tests]][tests-url] | ||
<h1>File Loader</h1> | ||
<p>Instructs webpack to emit the required object as file and to return its public url.</p> | ||
<p>Instructs webpack to emit the required object as file and to return its public URL</p> | ||
</div> | ||
@@ -23,87 +24,212 @@ | ||
<h2 align="center">Usage</h2> | ||
<h2 align="center"><a href="https://webpack.js.org/concepts/loaders">Usage</a></h2> | ||
By default the filename of the resulting file is the MD5 hash of the file's contents | ||
with the original extension of the required resource. | ||
By default the filename of the resulting file is the MD5 hash of the file's contents with the original extension of the required resource. | ||
``` javascript | ||
var url = require("file-loader!./file.png"); | ||
// => emits file.png as file in the output directory and returns the public url | ||
// => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png" | ||
```js | ||
import img from './file.png' | ||
``` | ||
By default a file is emitted, however this can be disabled if required (e.g. for server | ||
side packages). | ||
**webpack.config.js** | ||
```js | ||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(png|jpg|gif)$/, | ||
use: [ | ||
{ | ||
loader: 'file-loader', | ||
options: {} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
``` javascript | ||
var url = require("file-loader?emitFile=false!./file.png"); | ||
// => returns the public url but does NOT emit a file | ||
// => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png" | ||
Emits `file.png` as file in the output directory and returns the public URL | ||
``` | ||
"/public/path/0dcbbaa7013869e351f.png" | ||
``` | ||
#### Filename templates | ||
<h2 align="center">Options</h2> | ||
You can configure a custom filename template for your file using the query parameter `name`. For instance, to copy a file from your `context` directory into the output directory retaining the full directory structure, you might use `?name=[path][name].[ext]`. | ||
|Name|Type|Default|Description| | ||
|:--:|:--:|:-----:|:----------| | ||
|**`name`**|`{String}`|`[hash].[ext]`|Configure a custom filename template for your file| | ||
|**`context`**|`{String}`|`this.options.context`|Configure a custom file context, defaults to `webpack.config.js` [context](https://webpack.js.org/configuration/entry-context/#context)| | ||
|**`publicPath`**|`{String\|Function}`|[`__webpack_public_path__ `](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)|Configure a custom `public` path for your files| | ||
|**`outputPath`**|`{String\|Function}`|`'undefined'`|Configure a custom `output` path for your files| | ||
|**`useRelativePath`**|`{Boolean}`|`false`|Should be `true` if you wish to generate a `context` relative URL for each file| | ||
|**`emitFile`**|`{Boolean}`|`true`|By default a file is emitted, however this can be disabled if required (e.g. for server side packages)| | ||
### `name` | ||
You can configure a custom filename template for your file using the query parameter `name`. For instance, to copy a file from your `context` directory into the output directory retaining the full directory structure, you might use | ||
**webpack.config.js** | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
name: '[path][name].[ext]' | ||
} | ||
} | ||
``` | ||
#### `placeholders` | ||
|Name|Type|Default|Description| | ||
|:--:|:--:|:-----:|:----------| | ||
|**`[ext]`**|`{String}`|`file.extname`|The extension of the resource| | ||
|**`[name]`**|`{String}`|`file.basename`|The basename of the resource| | ||
|**`[path]`**|`{String}`|`file.dirname`|The path of the resource relative to the `context`| | ||
|**`[hash]`**|`{String}`|`md5`|The hash of the content, hashes below for more info| | ||
|**`[N]`**|`{Number}`|``|The `n-th` match obtained from matching the current file name against the query param `regExp`| | ||
#### `hashes` | ||
`[<hashType>:hash:<digestType>:<length>]` optionally you can configure | ||
|Name|Type|Default|Description| | ||
|:--:|:--:|:-----:|:----------| | ||
|**`hashType`**|`{String}`|`md5`|`sha1`, `md5`, `sha256`, `sha512`| | ||
|**`digestType`**|`{String}`|`base64`|`hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`| | ||
|**`length`**|`{Number}`|`8`|The length in chars| | ||
By default, the path and name you specify will output the file in that same directory and will also use that same URL path to access the file. | ||
You can specify custom output and public paths by using the `outputPath`, `publicPath` and `useRelativePath` query name parameters: | ||
### `context` | ||
**webpack.config.js** | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
name: '[path][name].[ext]', | ||
context: '' | ||
} | ||
} | ||
``` | ||
use: "file-loader?name=[name].[ext]&publicPath=assets/foo/&outputPath=app/images/" | ||
You can specify custom `output` and `public` paths by using `outputPath`, `publicPath` and `useRelativePath` | ||
### `publicPath` | ||
**webpack.config.js** | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
name: '[path][name].[ext]', | ||
publicPath: 'assets' | ||
} | ||
} | ||
``` | ||
`useRelativePath` should be `true` if you wish to generate relative URL to the each file context | ||
```javascript | ||
### `outputPath` | ||
**webpack.config.js** | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
query: { | ||
useRelativePath: process.env.NODE_ENV === "production" | ||
} | ||
loader: 'file-loader', | ||
options: { | ||
name: '[path][name].[ext]', | ||
outputPath: 'images' | ||
} | ||
} | ||
``` | ||
#### Filename template placeholders | ||
### `useRelativePath` | ||
* `[ext]` the extension of the resource | ||
* `[name]` the basename of the resource | ||
* `[path]` the path of the resource relative to the `context` query parameter or option. | ||
* `[hash]` the hash of the content, `hex`-encoded `md5` by default | ||
* `[<hashType>:hash:<digestType>:<length>]` optionally you can configure | ||
* other `hashType`s, i. e. `sha1`, `md5`, `sha256`, `sha512` | ||
* other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64` | ||
* and `length` the length in chars | ||
* `[N]` the N-th match obtained from matching the current file name against the query param `regExp` | ||
`useRelativePath` should be `true` if you wish to generate a relative URL to the for each file context. | ||
#### Examples | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
useRelativePath: process.env.NODE_ENV === "production" | ||
} | ||
} | ||
``` | ||
``` javascript | ||
require("file-loader?name=js/[hash].script.[ext]!./javascript.js"); | ||
// => js/0dcbbaa701328a3c262cfd45869e351f.script.js | ||
### `emitFile` | ||
require("file-loader?name=html-[hash:6].html!./page.html"); | ||
// => html-109fa8.html | ||
By default a file is emitted, however this can be disabled if required (e.g. for server side packages). | ||
require("file-loader?name=[hash]!./flash.txt"); | ||
// => c31e9820c001c9c4a86bce33ce43b679 | ||
```js | ||
import img from './file.png' | ||
``` | ||
require("file-loader?name=[sha512:hash:base64:7].[ext]!./image.png"); | ||
// => gdyb21L.png | ||
// use sha512 hash instead of md5 and with only 7 chars of base64 | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
emitFile: false | ||
} | ||
} | ||
``` | ||
require("file-loader?name=img-[sha512:hash:base64:7].[ext]!./image.jpg"); | ||
// => img-VqzT5ZC.jpg | ||
// use custom name, sha512 hash instead of md5 and with only 7 chars of base64 | ||
> ⚠️ Returns the public URL but does **not** emit a file | ||
require("file-loader?name=picture.png!./myself.png"); | ||
// => picture.png | ||
``` | ||
`${publicPath}/0dcbbaa701328e351f.png` | ||
``` | ||
require("file-loader?name=[path][name].[ext]?[hash]!./dir/file.png") | ||
// => dir/file.png?e43b20c069c4a01867c31e98cbce33c9 | ||
<h2 align="center">Examples</h2> | ||
```js | ||
import png from 'image.png' | ||
``` | ||
<h2 align="center">Contributing</h2> | ||
**webpack.config.js** | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
name: 'dirname/[hash].[ext]' | ||
} | ||
} | ||
``` | ||
Don't hesitate to create a pull request. Every contribution is appreciated. In development you can start the tests by calling `npm test`. | ||
``` | ||
dirname/0dcbbaa701328ae351f.png | ||
``` | ||
**webpack.config.js** | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
name: '[sha512:hash:base64:7].[ext]' | ||
} | ||
} | ||
``` | ||
``` | ||
gdyb21L.png | ||
``` | ||
```js | ||
import png from 'path/to/file.png' | ||
``` | ||
**webpack.config.js** | ||
```js | ||
{ | ||
loader: 'file-loader', | ||
options: { | ||
name: '[path][name].[ext]?[hash]' | ||
} | ||
} | ||
``` | ||
``` | ||
path/to/file.png?e43b20c069c4a01867c31e98cbce33c9 | ||
``` | ||
<h2 align="center">Maintainers</h2> | ||
@@ -129,9 +255,2 @@ | ||
<td align="center"> | ||
<a href="https://github.com/sapegin"> | ||
<img width="150" height="150" src="https://github.com/sapegin.png?v=3&s=150"> | ||
</br> | ||
Artem Sapegin | ||
</a> | ||
</td> | ||
<td align="center"> | ||
<a href="https://github.com/michael-ciniawsky"> | ||
@@ -155,9 +274,8 @@ <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150"> | ||
<h2 align="center">LICENSE</h2> | ||
MIT | ||
[npm]: https://img.shields.io/npm/v/file-loader.svg | ||
[npm-url]: https://npmjs.com/package/file-loader | ||
[node]: https://img.shields.io/node/v/file-loader.svg | ||
[node-url]: https://nodejs.org | ||
[deps]: https://david-dm.org/webpack-contrib/file-loader.svg | ||
@@ -169,3 +287,3 @@ [deps-url]: https://david-dm.org/webpack-contrib/file-loader | ||
[cover]: https://codecov.io/gh/webpack-contrib/file-loader/branch/master/graph/badge.svg | ||
[cover]: https://img.shields.io/codecov/c/github/webpack-contrib/file-loader.svg | ||
[cover-url]: https://codecov.io/gh/webpack-contrib/file-loader | ||
@@ -172,0 +290,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17494
7
81
288
3
+ Addedschema-utils@^0.3.0
+ Addedajv@5.5.2(transitive)
+ Addedco@4.6.0(transitive)
+ Addedfast-deep-equal@1.1.0(transitive)
+ Addedjson-schema-traverse@0.3.1(transitive)
+ Addedschema-utils@0.3.0(transitive)