Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: webpack-contrib/file-loader
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 884f22a727fb26b449c61674b46b6732ad9f2194
Choose a base ref
...
head repository: webpack-contrib/file-loader
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 743aef27df5a594d8c02df407ae03d15b7a92a64
Choose a head ref

Commits on Oct 25, 2015

  1. Remove unused dependency

    kossnocorp committed Oct 25, 2015
    Copy the full SHA
    ece1212 View commit details

Commits on Nov 25, 2015

  1. Copy the full SHA
    36a373b View commit details

Commits on Feb 17, 2016

  1. Added support for publicPath option

    Jakub Wolny committed Feb 17, 2016
    Copy the full SHA
    958aae2 View commit details
  2. Fixed tests

    Jakub Wolny committed Feb 17, 2016
    Copy the full SHA
    623e6b2 View commit details

Commits on Feb 28, 2016

  1. Update README.md

    Add installation instructions. 
    
    Many of the other webpack modules include installation instructions, 
    this one should, too.
    mrpoptart committed Feb 28, 2016
    Copy the full SHA
    011fa30 View commit details

Commits on Jun 16, 2016

  1. Make emitting a file optional.

    When building server side packages there may be no need to emit a file;
    this makes file emission optional, while keeping the default behaviour
    as is.
    Liam O'Boyle committed Jun 16, 2016
    Copy the full SHA
    20c8fff View commit details
  2. Merge pull request #74 from elyobo/optional-emit

    Make emitting a file optional.
    sokra authored Jun 16, 2016
    Copy the full SHA
    0241876 View commit details
  3. Merge pull request #58 from mrpoptart/patch-1

    Update README.md
    sokra authored Jun 16, 2016
    Copy the full SHA
    dba9318 View commit details
  4. Merge branch 'public-path' #57

    sokra committed Jun 16, 2016
    Copy the full SHA
    5ae3077 View commit details
  5. added missing piece from #42

    sokra committed Jun 16, 2016
    Copy the full SHA
    6a0317e View commit details
  6. Merge pull request #47 from tedpennings/patch-2

    Typo fix, plus some hash details
    sokra authored Jun 16, 2016
    Copy the full SHA
    6dd8c0d View commit details
  7. Merge pull request #41 from kossnocorp/patch-1

    Remove unused dependency
    sokra authored Jun 16, 2016
    Copy the full SHA
    f21695e View commit details

Commits on Jun 20, 2016

  1. 0.9.0

    sokra committed Jun 20, 2016
    Copy the full SHA
    162c8fa View commit details

Commits on Nov 16, 2016

  1. Verified

    This commit was signed with the committer’s verified signature.
    kevinzwhuang Kevin Huang
    Copy the full SHA
    9fdd47e View commit details

Commits on Jan 28, 2017

  1. chore(issues): add issue templates

    SpaceK33z authored and joshwiens committed Jan 28, 2017
    Copy the full SHA
    0727efe View commit details
  2. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    6833c70 View commit details
  3. Copy the full SHA
    1462d4b View commit details
  4. Copy the full SHA
    b5ea427 View commit details
  5. Copy the full SHA
    3e5985a View commit details
  6. Copy the full SHA
    6fec719 View commit details
  7. docs(readme): fix icon url

    joshwiens committed Jan 28, 2017
    Copy the full SHA
    eaeaa0e View commit details
  8. Copy the full SHA
    d82e8de View commit details
  9. Copy the full SHA
    ba2e876 View commit details
  10. chore(release): 0.10.0

    joshwiens committed Jan 28, 2017
    Copy the full SHA
    2d239df View commit details

Commits on Feb 4, 2017

  1. Remove license link

    To allow inclusion in https://webpack.js.org/ docs.
    simon04 committed Feb 4, 2017
    Copy the full SHA
    32aada7 View commit details

Commits on Feb 5, 2017

  1. Merge pull request #123 from simon04/master

    Remove license link
    bebraw authored Feb 5, 2017
    Copy the full SHA
    b01526a View commit details

Commits on Feb 25, 2017

  1. Copy the full SHA
    a8358a0 View commit details
  2. chore(release): 0.10.1

    joshwiens committed Feb 25, 2017
    Copy the full SHA
    5d8f73e View commit details

Commits on Mar 31, 2017

  1. feat: Emit files with relative urls (#135)

    * new configuration to emit files with relative URLs
    
    * Added support for relativePath option
    
    * Changed the 'relativePath' parameter name to 'useRelativePath'
    
    * feat: 'useRelativePath' working on dev mode too
    
    * style: more readable code
    
    * fix: posix on windows
    adriancmiranda authored and joshwiens committed Mar 31, 2017
    Copy the full SHA
    dbcd6cc View commit details
  2. chore(release): 0.11.0

    joshwiens committed Mar 31, 2017
    Copy the full SHA
    46cb916 View commit details

Commits on Apr 1, 2017

  1. Copy the full SHA
    80cdee2 View commit details
  2. chore(release): 0.11.1

    joshwiens committed Apr 1, 2017
    Copy the full SHA
    9afc205 View commit details

Commits on Apr 15, 2017

  1. Copy the full SHA
    26ab81a View commit details

Commits on Jun 2, 2017

  1. Copy the full SHA
    d4d8bbc View commit details

Commits on Jun 3, 2017

  1. Copy the full SHA
    e4c0b2a View commit details

Commits on Jun 5, 2017

  1. chore(release): 0.11.2

    joshwiens committed Jun 5, 2017
    Copy the full SHA
    743aef2 View commit details
Showing with 540 additions and 84 deletions.
  1. +15 −0 .github/ISSUE_TEMPLATE.md
  2. +18 −0 .github/PULL_REQUEST_TEMPLATE.md
  3. +36 −0 .github/assets/file_loader_icon.svg
  4. +8 −0 .gitignore
  5. +28 −3 .travis.yml
  6. +58 −0 CHANGELOG.md
  7. +20 −0 LICENSE
  8. +156 −64 README.md
  9. +68 −7 index.js
  10. +6 −3 package.json
  11. +98 −7 test/correct-filename.test.js
  12. +29 −0 test/optional-file-emission.test.js
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Before creating an issue please make sure you are using the latest version of file-loader. -->

**Do you want to request a *feature* or report a *bug*?**
<!-- Please ask questions on StackOverflow or the webpack Gitter (https://gitter.im/webpack/webpack). Questions will be closed. -->

**What is the current behavior?**

**If the current behavior is a bug, please provide the steps to reproduce.**
<!-- A great way to do this is to provide your configuration via a GitHub gist. -->

**What is the expected behavior?**

**If this is a feature request, what is motivation or use case for changing the behavior?**

**Please mention other relevant information such as your webpack version, Node.js version and Operating System.**
18 changes: 18 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**What kind of change does this PR introduce?**
<!-- E.g. a bugfix, feature, refactoring, build related change, etc… -->

**Did you add tests for your changes?**

**If relevant, did you update the README?**

**Summary**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->
<!-- Try to link to an open issue for more information. -->

**Does this PR introduce a breaking change?**
<!-- If this PR introduces a breaking change, please describe the impact and a migration path for existing applications. -->

**Other information**
36 changes: 36 additions & 0 deletions .github/assets/file_loader_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# development
/node_modules
/coverage
*.log

# dotfiles
.*
!.gitignore
*~
*#*
31 changes: 28 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.11"
branches:
only:
- master
matrix:
fast_finish: true
include:
# - os: linux
# node_js: "7"
# env: WEBPACK_VERSION="2.2.0" JOB_PART=lint
- os: linux
node_js: "6"
env: WEBPACK_VERSION="2.2.0" JOB_PART=test
- os: linux
node_js: "4.3"
env: WEBPACK_VERSION="2.2.0" JOB_PART=test
- os: linux
node_js: "7"
env: WEBPACK_VERSION="2.2.0" JOB_PART=test
before_install:
- nvm --version
- node --version
before_script:
- 'if [ "$WEBPACK_VERSION" ]; then npm install webpack@^$WEBPACK_VERSION; fi'
script:
- npm run travis:$JOB_PART
# after_success:
# - bash <(curl -s https://codecov.io/bash)
58 changes: 58 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Change Log

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="0.11.2"></a>
## [0.11.2](https://github.com/webpack/file-loader/compare/v0.11.1...v0.11.2) (2017-06-05)


### Bug Fixes

* **index:** allow to override publicPath with an empty string ([#145](https://github.com/webpack/file-loader/issues/145)) ([26ab81a](https://github.com/webpack/file-loader/commit/26ab81a))
* init `publicPath` to undefined ([#159](https://github.com/webpack/file-loader/issues/159)) ([e4c0b2a](https://github.com/webpack/file-loader/commit/e4c0b2a))



<a name="0.11.1"></a>
## [0.11.1](https://github.com/webpack/file-loader/compare/v0.11.0...v0.11.1) (2017-04-01)


### Bug Fixes

* outputPath function overriden by useRelativePath ([#139](https://github.com/webpack/file-loader/issues/139)) ([80cdee2](https://github.com/webpack/file-loader/commit/80cdee2))



<a name="0.11.0"></a>
# [0.11.0](https://github.com/webpack/file-loader/compare/v0.10.1...v0.11.0) (2017-03-31)


### Features

* Emit files with relative urls ([#135](https://github.com/webpack/file-loader/issues/135)) ([dbcd6cc](https://github.com/webpack/file-loader/commit/dbcd6cc))



<a name="0.10.1"></a>
## [0.10.1](https://github.com/webpack/file-loader/compare/v0.10.0...v0.10.1) (2017-02-25)


### Bug Fixes

* **getOptions:** deprecation warn in loaderUtils ([#129](https://github.com/webpack/file-loader/issues/129)) ([a8358a0](https://github.com/webpack/file-loader/commit/a8358a0))



<a name="0.10.0"></a>
# [0.10.0](https://github.com/webpack/file-loader/compare/v0.9.0...v0.10.0) (2017-01-28)


### Features

* **resources:** specify custom public file name ([6833c70](https://github.com/webpack/file-loader/commit/6833c70))



# Change Log

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright JS Foundation and other contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
220 changes: 156 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,156 @@
# file loader for webpack

## Usage

[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)

``` javascript
var url = require("file!./file.png");
// => emits file.png as file in the output directory and returns the public url
// => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png"
```

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.

## Filename templates

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]`.

### Filename template placeholders

* `[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 or the content
* `[<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`

## Examples

``` javascript
require("file?name=js/[hash].script.[ext]!./javascript.js");
// => js/0dcbbaa701328a3c262cfd45869e351f.script.js

require("file?name=html-[hash:6].html!./page.html");
// => html-109fa8.html

require("file?name=[hash]!./flash.txt");
// => c31e9820c001c9c4a86bce33ce43b679

require("file?name=[sha512:hash:base64:7].[ext]!./image.png");
// => gdyb21L.png
// use sha512 hash instead of md5 and with only 7 chars of base64

require("file?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

require("file?name=picture.png!./myself.png");
// => picture.png

require("file?name=[path][name].[ext]?[hash]!./dir/file.png")
// => dir/file.png?e43b20c069c4a01867c31e98cbce33c9
```

## License

MIT (http://www.opensource.org/licenses/mit-license.php)
[![npm][npm]][npm-url]
[![node][node]][node-url]
[![deps][deps]][deps-url]
[![tests][tests]][tests-url]
[![coverage][cover]][cover-url]
[![chat][chat]][chat-url]

<div align="center">
<a href="https://github.com/webpack/webpack">
<img width="200" height="200"
src="https://webpack.js.org/assets/icon-square-big.svg">
</a>
<h1>File Loader</h1>
<p>Instructs webpack to emit the required object as file and to return its public url.</p>
</div>

<h2 align="center">Install</h2>

```bash
npm install --save-dev file-loader
```

<h2 align="center">Usage</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.

``` 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"
```

By default a file is emitted, however this can be disabled if required (e.g. for server
side packages).

``` 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"
```

#### Filename templates

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]`.

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:

```
use: "file-loader?name=[name].[ext]&publicPath=assets/foo/&outputPath=app/images/"
```

`useRelativePath` should be `true` if you wish to generate relative URL to the each file context
```javascript
{
loader: 'file-loader',
query: {
useRelativePath: process.env.NODE_ENV === "production"
}
}
```

#### Filename template placeholders

* `[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`

#### Examples

``` javascript
require("file-loader?name=js/[hash].script.[ext]!./javascript.js");
// => js/0dcbbaa701328a3c262cfd45869e351f.script.js

require("file-loader?name=html-[hash:6].html!./page.html");
// => html-109fa8.html

require("file-loader?name=[hash]!./flash.txt");
// => c31e9820c001c9c4a86bce33ce43b679

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

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

require("file-loader?name=picture.png!./myself.png");
// => picture.png

require("file-loader?name=[path][name].[ext]?[hash]!./dir/file.png")
// => dir/file.png?e43b20c069c4a01867c31e98cbce33c9
```

<h2 align="center">Contributing</h2>

Don't hesitate to create a pull request. Every contribution is appreciated. In development you can start the tests by calling `npm test`.

<h2 align="center">Maintainers</h2>

<table>
<tbody>
<tr>
<td align="center">
<img width="150" height="150"
src="https://avatars.githubusercontent.com/sokra?v=3">
<br />
<a href="https://github.com/">Tobias Koppers</a>
</td>
<td align="center">
<img width="150" height="150"
src="https://avatars.githubusercontent.com/SpaceK33z?v=3">
<br />
<a href="https://github.com/">Kees Kluskens</a>
</td>
<td align="center">
<img width="150" height="150"
src="https://avatars.githubusercontent.com/mobitar?v=3">
<br />
<a href="https://github.com/">Mo Bitar</a>
</td>
</tr>
</tbody>
</table>


<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
[deps-url]: https://david-dm.org/webpack-contrib/file-loader

[tests]: http://img.shields.io/travis/webpack-contrib/file-loader.svg
[tests-url]: https://travis-ci.org/webpack-contrib/file-loader

[cover]: https://coveralls.io/repos/github/webpack-contrib/file-loader/badge.svg
[cover-url]: https://coveralls.io/github/webpack-contrib/file-loader

[chat]: https://badges.gitter.im/webpack/webpack.svg
[chat-url]: https://gitter.im/webpack/webpack
75 changes: 68 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -2,18 +2,79 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var path = require("path");
var loaderUtils = require("loader-utils");

module.exports = function(content) {
this.cacheable && this.cacheable();
if(!this.emitFile) throw new Error("emitFile is required from module system");
var query = loaderUtils.parseQuery(this.query);
var url = loaderUtils.interpolateName(this, query.name || "[hash].[ext]", {
context: query.context || this.options.context,

var query = loaderUtils.getOptions(this) || {};
var configKey = query.config || "fileLoader";
var options = this.options[configKey] || {};
var config = {
publicPath: undefined,
useRelativePath: false,
name: "[hash].[ext]"
};

// options takes precedence over config
Object.keys(options).forEach(function(attr) {
config[attr] = options[attr];
});

// 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 = loaderUtils.interpolateName(this, config.name, {
context: context,
content: content,
regExp: query.regExp
regExp: config.regExp
});
this.emitFile(url, content);
return "module.exports = __webpack_public_path__ + " + JSON.stringify(url) + ";";
}

var outputPath = "";

var filePath = this.resourcePath;
if (config.useRelativePath) {
var issuerContext = this._module && this._module.issuer && this._module.issuer.context || context;
var relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join("/");
var relativePath = relativeUrl && path.dirname(relativeUrl) + "/";
if (~relativePath.indexOf("../")) {
outputPath = path.posix.join(outputPath, relativePath, url);
} else {
outputPath = relativePath + url;
}
url = relativePath + url;
} else if (config.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = (
typeof config.outputPath === "function"
? config.outputPath(url)
: config.outputPath + url
);
url = outputPath;
} else {
outputPath = url;
}

var publicPath = "__webpack_public_path__ + " + JSON.stringify(url);
if (config.publicPath !== undefined) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof config.publicPath === "function"
? config.publicPath(url)
: config.publicPath + url
);
}

if (query.emitFile === undefined || query.emitFile) {
this.emitFile(outputPath, content);
}

return "module.exports = " + publicPath + ";";
};

module.exports.raw = true;
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{
"name": "file-loader",
"version": "0.8.5",
"version": "0.11.2",
"author": "Tobias Koppers @sokra",
"description": "file loader module for webpack",
"files": [
"index.js"
],
"dependencies": {
"loader-utils": "~0.2.5"
"loader-utils": "^1.0.2"
},
"devDependencies": {
"should": "~4.0.4",
"standard-version": "^4.0.0",
"mocha": "~1.21.3"
},
"scripts": {
"test": "mocha -R spec"
"test": "mocha -R spec",
"travis:test": "npm run test",
"release": "standard-version"
},
"repository": {
"type": "git",
105 changes: 98 additions & 7 deletions test/correct-filename.test.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ var fileLoader = require("../");

function run(resourcePath, query, content) {
content = content || new Buffer("1234");
var result = null;
var file = null;
var context = {
resourcePath: resourcePath,
query: "?" + query,
@@ -13,15 +13,43 @@ function run(resourcePath, query, content) {
},
emitFile: function(url, content2) {
content2.should.be.eql(content);
result = url;
file = url;
}
};
fileLoader.call(context, content);
return result;

var result = fileLoader.call(context, content)

return {
file: file,
result: result
}
}
function run_with_options(resourcePath,options, content) {
content = content || new Buffer("1234");
var file = null;

var context = {
resourcePath: resourcePath,
options: {
"fileLoader": options,
context: "/this/is/the/context"
},
emitFile: function(url, content2) {
content2.should.be.eql(content);
file = url;
}
};

var result = fileLoader.call(context, content)

return {
file: file,
result: result
}
}

function test(excepted, resourcePath, query, content) {
run(resourcePath, query, content).should.be.eql(excepted);
run(resourcePath, query, content).file.should.be.eql(excepted);
}

describe("correct-filename", function() {
@@ -56,5 +84,68 @@ describe("correct-filename", function() {
test("sntmopgidsdqrofkjywoyldtiij.txt", "/file.txt", "name=[hash:base26].[ext]");
test("sntmopgids.txt", "/file.txt", "name=[hash:base26:10].[ext]");
});

});
});

describe("publicPath option", function() {
it("should be supported", function() {
run("/file.txt", "publicPath=http://cdn/").result.should.be.eql(
'module.exports = "http://cdn/81dc9bdb52d04dc20036dbd8313ed055.txt";'
);
});

it("should override public path when given empty string", function() {
run("file.txt", "publicPath=").result.should.be.eql(
'module.exports = "81dc9bdb52d04dc20036dbd8313ed055.txt";'
);
});

it("should use webpack public path when not set", function() {
run("file.txt").result.should.be.eql(
'module.exports = __webpack_public_path__ + "81dc9bdb52d04dc20036dbd8313ed055.txt";'
);
});
});

describe("useRelativePath option", function() {
it("should be supported", function() {
run("/this/is/the/context/file.txt", "useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/is/file.txt", "useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/file.txt", "context=/this/is/the/&useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/file.txt", "context=/&useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"this/81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
});
});
describe("outputPath function", function() {
it("should be supported", function() {
outputFunc = function(value) {
return("/path/set/by/func");

};
var options = {};
options.outputPath = outputFunc;
run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql(
'module.exports = __webpack_public_path__ + \"/path/set/by/func\";'
);

});
it("should be ignored if you set useRelativePath", function() {
outputFunc = function(value) {
return("/path/set/by/func");

};
var options = {};
options.outputPath = outputFunc;
options.useRelativePath = true;
run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql(
'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);

});
});
29 changes: 29 additions & 0 deletions test/optional-file-emission.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var should = require("should");
var fileLoader = require("../");

function run(resourcePath, query, content) {
content = content || new Buffer("1234");
var result = false;
var context = {
resourcePath: resourcePath,
query: "?" + query,
options: {
context: "/this/is/the/context"
},
emitFile: function(url, content2) {
result = true;
}
};
fileLoader.call(context, content);
return result;
}

describe("optional-emission", function() {
it("should emit a file by default", function() {
run("whatever.txt", "").should.be.true;
});

it("should not emit a file if disabled", function() {
run("whatever.txt", "emitFile=false").should.be.false;
});
});