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: 0df6c8d129630f1dd1c44125a7b42cfe38fe88ae
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: c423008dce1b16e1253b89b792f03774ffeb47de
Choose a head ref

Commits on Nov 22, 2019

  1. refactor: next (#345)

    evilebottnawi authored Nov 22, 2019
    2
    Copy the full SHA
    98a6c1d View commit details

Commits on Nov 25, 2019

  1. chore(release): 5.0.0

    alexander-akait committed Nov 25, 2019
    Copy the full SHA
    2a18cba View commit details
  2. 6
    Copy the full SHA
    31d6589 View commit details
  3. chore(release): 5.0.1

    alexander-akait committed Nov 25, 2019
    Copy the full SHA
    90302cd View commit details
  4. Copy the full SHA
    6431101 View commit details
  5. chore(release): 5.0.2

    alexander-akait committed Nov 25, 2019
    Copy the full SHA
    f1b071c View commit details
  6. Copy the full SHA
    190829e View commit details

Commits on Dec 30, 2019

  1. Copy the full SHA
    5ffac2e View commit details
  2. Copy the full SHA
    521bff2 View commit details

Commits on Feb 19, 2020

  1. Copy the full SHA
    5703c58 View commit details
  2. Copy the full SHA
    cd8698b View commit details
  3. chore(release): 5.1.0

    alexander-akait committed Feb 19, 2020
    Copy the full SHA
    c2aded7 View commit details

Commits on Mar 17, 2020

  1. docs: update README.md (#368)

    xoyimi authored Mar 17, 2020
    Copy the full SHA
    e1fe27c View commit details
  2. chore(deps): update (#369)

    BREAKING CHANGE: use `md4` by default for hashing
    evilebottnawi authored Mar 17, 2020
    Copy the full SHA
    ad39022 View commit details
  3. chore(release): 6.0.0

    alexander-akait committed Mar 17, 2020
    Copy the full SHA
    e44eb73 View commit details

Commits on Apr 24, 2020

  1. Copy the full SHA
    3846b33 View commit details

Commits on Jun 22, 2020

  1. Copy the full SHA
    8bc9950 View commit details
  2. Copy the full SHA
    718aef5 View commit details

Commits on Aug 31, 2020

  1. Copy the full SHA
    40fcde8 View commit details
  2. test: fix

    evilebottnawi authored Aug 31, 2020
    Copy the full SHA
    6e22f6e View commit details
  3. chore(release): 6.1.0

    alexander-akait committed Aug 31, 2020
    Copy the full SHA
    25e2668 View commit details

Commits on Oct 9, 2020

  1. chore(deps): update

    evilebottnawi authored Oct 9, 2020
    Copy the full SHA
    60508cf View commit details
  2. Copy the full SHA
    6fadfbe View commit details

Commits on Oct 13, 2020

  1. Copy the full SHA
    14ed4c9 View commit details

Commits on Oct 27, 2020

  1. Copy the full SHA
    381d8bd View commit details
  2. Copy the full SHA
    654e0d6 View commit details
  3. chore(release): 6.2.0

    alexander-akait committed Oct 27, 2020
    Copy the full SHA
    c423008 View commit details
Showing with 8,248 additions and 6,114 deletions.
  1. +1 −2 .editorconfig
  2. +95 −0 .github/workflows/nodejs.yml
  3. +0 −5 .prettierrc
  4. +1 −5 .prettierrc.js
  5. +66 −0 CHANGELOG.md
  6. +70 −15 README.md
  7. +0 −192 azure-pipelines.yml
  8. +1 −1 babel.config.js
  9. +2 −2 lint-staged.config.js
  10. +6,490 −4,555 package-lock.json
  11. +40 −38 package.json
  12. +40 −17 src/index.js
  13. +2 −1 src/options.json
  14. +38 −0 src/utils.js
  15. +11 −1 test/__snapshots__/context-option.test.js.snap
  16. +37 −1 test/__snapshots__/emitFile-option.test.js.snap
  17. +19 −0 test/__snapshots__/esModule-option.test.js.snap
  18. +0 −28 test/__snapshots__/esModules-option.test.js.snap
  19. +46 −109 test/__snapshots__/loader.test.js.snap
  20. +22 −2 test/__snapshots__/name-option.test.js.snap
  21. +119 −93 test/__snapshots__/outputPath-option.test.js.snap
  22. +53 −80 test/__snapshots__/postTransformPublicPath-option.test.js.snap
  23. +23 −56 test/__snapshots__/publicPath-option.test.js.snap
  24. +26 −25 test/__snapshots__/{validation-options.test.js.snap → validate-options.test.js.snap}
  25. +5 −5 test/cjs.test.js
  26. +38 −15 test/context-option.test.js
  27. +47 −30 test/emitFile-option.test.js
  28. +52 −0 test/esModule-option.test.js
  29. +0 −51 test/esModules-option.test.js
  30. +6 −0 test/fixtures/cdn.js
  31. +0 −4 test/fixtures/emitFile/fixture.js
  32. BIN test/fixtures/file.gif
  33. BIN test/fixtures/file.jpg
  34. +0 −48 test/fixtures/file.svg
  35. +0 −4 test/fixtures/fixture.js
  36. BIN test/fixtures/nested/{assets → }/file.png
  37. +0 −4 test/fixtures/nested/fixture.js
  38. +6 −0 test/fixtures/simple.js
  39. +11 −0 test/helpers/compile.js
  40. +0 −72 test/helpers/compiler.js
  41. +22 −0 test/helpers/execute.js
  42. +46 −0 test/helpers/getCompiler.js
  43. +7 −0 test/helpers/index.js
  44. +19 −0 test/helpers/normalizeErrors.js
  45. +15 −0 test/helpers/readAsset.js
  46. +94 −73 test/loader.test.js
  47. +194 −30 test/name-option.test.js
  48. +178 −218 test/outputPath-option.test.js
  49. +145 −153 test/postTransformPublicPath-option.test.js
  50. +77 −116 test/publicPath-option.test.js
  51. +84 −0 test/validate-options.test.js
  52. +0 −63 test/validation-options.test.js
3 changes: 1 addition & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -9,5 +9,4 @@ insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
insert_final_newline = true
trim_trailing_whitespace = false
trim_trailing_whitespace = false
95 changes: 95 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: file-loader

on:
push:
branches:
- master
- next
pull_request:
branches:
- master
- next

jobs:
lint:
name: Lint - ${{ matrix.os }} - Node v${{ matrix.node-version }}

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

strategy:
matrix:
os: [ubuntu-latest]
node-version: [12.x]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Use Node.js ${{ env.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ env.node-version }}

- name: Use latest NPM
run: sudo npm i -g npm

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint

- name: Security audit
run: npm run security

- name: Check commit message
uses: wagoid/commitlint-github-action@v1

test:
name: Test - ${{ matrix.os }} - Node v${{ matrix.node-version }}, Webpack ${{ matrix.webpack-version }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 14.x]
webpack-version: [4, latest]

runs-on: ${{ matrix.os }}

steps:
- name: Setup Git
if: matrix.os == 'windows-latest'
run: git config --global core.autocrlf input

- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Use latest NPM on ubuntu/macos
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
run: sudo npm i -g npm

- name: Use latest NPM on windows
if: matrix.os == 'windows-latest'
run: npm i -g npm

- name: Install dependencies
run: npm ci

- name: Install webpack ${{ matrix.webpack-version }}
run: npm i webpack@${{ matrix.webpack-version }}

- name: Run tests for webpack version ${{ matrix.webpack-version }}
run: npm run test:coverage -- --ci

- name: Submit coverage data to codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

6 changes: 1 addition & 5 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
singleQuote: true,
trailingComma: 'es5',
arrowParens: 'always',
};
module.exports = { singleQuote: true };
66 changes: 66 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,72 @@

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.

## [6.2.0](https://github.com/webpack-contrib/file-loader/compare/v6.1.1...v6.2.0) (2020-10-27)


### Features

* added the `sourceFilename` property to asset info with original filename ([#393](https://github.com/webpack-contrib/file-loader/issues/393)) ([654e0d6](https://github.com/webpack-contrib/file-loader/commit/654e0d641ec067089f6a2d12e30ec3520f00d808))


### Bug Fixes

* immutable flag when the `name` option have hash in query string ([#392](https://github.com/webpack-contrib/file-loader/issues/392)) ([381d8bd](https://github.com/webpack-contrib/file-loader/commit/381d8bda3f2494487bfe840386e365b97b6025fe))

### [6.1.1](https://github.com/webpack-contrib/file-loader/compare/v6.1.0...v6.1.1) (2020-10-09)

### Chore

* update `schema-utils`

## [6.1.0](https://github.com/webpack-contrib/file-loader/compare/v6.0.0...v6.1.0) (2020-08-31)


### Features

* pass immutable flag to asset info ([#383](https://github.com/webpack-contrib/file-loader/issues/383)) ([40fcde8](https://github.com/webpack-contrib/file-loader/commit/40fcde81681d4f8ee19d2ee3845fd34e24459195))

## [6.0.0](https://github.com/webpack-contrib/file-loader/compare/v5.1.0...v6.0.0) (2020-03-17)


### ⚠ BREAKING CHANGES

* use `md4` by default for hashing ([#369](https://github.com/webpack-contrib/file-loader/issues/369)) ([ad39022](https://github.com/webpack-contrib/file-loader/commit/ad3902284d28adeddf667212a39faa4c6bfb2964))

## [5.1.0](https://github.com/webpack-contrib/file-loader/compare/v5.0.2...v5.1.0) (2020-02-19)


### Features

* support the `query` template for the `name` option ([#366](https://github.com/webpack-contrib/file-loader/issues/366)) ([cd8698b](https://github.com/webpack-contrib/file-loader/commit/cd8698b1d9fd560d85e912acca9a1e24f00e18f8))

### [5.0.2](https://github.com/webpack-contrib/file-loader/compare/v5.0.1...v5.0.2) (2019-11-25)


### Chore

* add the `funding` field in `package.json`



### [5.0.1](https://github.com/webpack-contrib/file-loader/compare/v5.0.0...v5.0.1) (2019-11-25)


### Bug Fixes

* name of `esModule` option in source code ([#346](https://github.com/webpack-contrib/file-loader/issues/346)) ([31d6589](https://github.com/webpack-contrib/file-loader/commit/31d6589b71b471f83908e80380dff9b9eada2d06))

## [5.0.0](https://github.com/webpack-contrib/file-loader/compare/v4.2.0...v5.0.0) (2019-11-22)


### BREAKING CHANGES

* minimum required nodejs version is `10.13.0`
* rename the `esModules` option to `esModule`
* switch to ES modules by default (the option `esModule` is `true` by default)



## [4.3.0](https://github.com/webpack-contrib/file-loader/compare/v4.2.0...v4.3.0) (2019-11-21)


85 changes: 70 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -102,7 +102,10 @@ module.exports = {
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
name(file) {
name(resourcePath, resourceQuery) {
// `resourcePath` - `/absolute/path/to/file.js`
// `resourceQuery` - `?foo=bar`

if (process.env.NODE_ENV === 'development') {
return '[path][name].[ext]';
}
@@ -184,7 +187,7 @@ module.exports = {
### `publicPath`

Type: `String|Function`
Default: [`__webpack_public_path__`](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)
Default: [`__webpack_public_path__`](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)+outputPath

Specifies a custom public path for the target file(s).

@@ -375,12 +378,15 @@ module.exports = {

> ℹ️ If `[0]` is used, it will be replaced by the entire tested string, whereas `[1]` will contain the first capturing parenthesis of your regex and so on...
### `esModules`
### `esModule`

Type: `Boolean`
Default: `false`
Default: `true`

By default, `file-loader` generates JS modules that use the ES modules syntax.
There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).

By default, `file-loader` generates JS modules that use the CommonJS syntax. However, there are some cases in which using ES2015 modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
You can enable a CommonJS module syntax using:

**webpack.config.js**

@@ -394,7 +400,7 @@ module.exports = {
{
loader: 'file-loader',
options: {
esModules: true,
esModule: false,
},
},
],
@@ -436,6 +442,13 @@ Default: `file.folder`

The folder of the resource is in.

### `[query]`

Type: `String`
Default: `file.query`

The query of the resource, i.e. `?foo=bar`.

### `[emoji]`

Type: `String`
@@ -453,14 +466,14 @@ Same as above, but with a customizable number of emojis
### `[hash]`

Type: `String`
Default: `md5`
Default: `md4`

Specifies the hash method to use for hashing the file content.

### `[contenthash]`

Type: `String`
Default: `md5`
Default: `md4`

Specifies the hash method to use for hashing the file content.

@@ -482,10 +495,9 @@ base49, base52, base58, base62, base64, and hex.
#### `hashType`

Type: `String`
Default: `'md5'`
Default: `'md4'`

The type of hash that the has function should use. Valid values include: `md5`,
`sha1`, `sha256`, and `sha512`.
The type of hash that the has function should use. Valid values include: `md4`, `md5`, `sha1`, `sha256`, and `sha512`.

#### `length`

@@ -616,14 +628,55 @@ Result:
path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
```

### CDN

The following examples show how to use `file-loader` for CDN uses query params.

**file.js**

```js
import png from './directory/image.png?width=300&height=300';
```

**webpack.config.js**

```js
module.exports = {
output: {
publicPath: 'https://cdn.example.com/',
},
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext][query]',
},
},
],
},
],
},
};
```

Result:

```bash
# result
https://cdn.example.com/directory/image.png?width=300&height=300
```

### Dynamic public path depending on environment variable at run time

An application might want to configure different CDN hosts depending on an environment variable that is only available when running the application. This can be an advantage, as only one build of the application is necessary, which behaves differntly depending on environment variables of the deployment environment. Since file-loader is applied when compiling the application, and not when running it, the environment variable cannot be used in the file-loader configuration. A way around this is setting the `__webpack_public_path__` to the desired CDN host depending on the environment variable at the entrypoint of the application. The option `postTransformPublicPath` can be used to configure a custom path depending on a variable like `__webpack_public_path__`.
An application might want to configure different CDN hosts depending on an environment variable that is only available when running the application. This can be an advantage, as only one build of the application is necessary, which behaves differently depending on environment variables of the deployment environment. Since file-loader is applied when compiling the application, and not when running it, the environment variable cannot be used in the file-loader configuration. A way around this is setting the `__webpack_public_path__` to the desired CDN host depending on the environment variable at the entrypoint of the application. The option `postTransformPublicPath` can be used to configure a custom path depending on a variable like `__webpack_public_path__`.

**main.js**

```js
const namespace = process.env.NAMESPACE;
const assetPrefixForNamespace = (namespace) => {
switch (namespace) {
case 'prod':
@@ -638,6 +691,8 @@ const assetPrefixForNamespace = (namespace) => {
return '';
}
};
const namespace = process.env.NAMESPACE;

__webpack_public_path__ = `${assetPrefixForNamespace(namespace)}/`;
```

@@ -698,8 +753,8 @@ Please take a moment to read our contributing guidelines if you haven't yet done
[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]: https://dev.azure.com/webpack-contrib/file-loader/_apis/build/status/webpack-contrib.file-loader?branchName=master
[tests-url]: https://dev.azure.com/webpack-contrib/file-loader/_build/latest?definitionId=2&branchName=master
[tests]: https://github.com/webpack-contrib/file-loader/workflows/file-loader/badge.svg
[tests-url]: https://github.com/webpack-contrib/file-loader/actions
[cover]: https://codecov.io/gh/webpack-contrib/file-loader/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/webpack-contrib/file-loader
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
Loading