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

Commit 9853db1

Browse files
refactor: loader
BREAKING CHANGE: removed the `useRelativePath` option. It is dangerously and break url when you use multiple entry points.
1 parent d5eb823 commit 9853db1

11 files changed

+203
-181
lines changed

README.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -283,23 +283,6 @@ _Note: If `[0]` is used, it will be replaced by the entire tested string,
283283
whereas `[1]` will contain the first capturing parenthesis of your regex and so
284284
on..._
285285

286-
### `useRelativePath`
287-
288-
Type: `Boolean`
289-
Default: `false`
290-
291-
Specifies whether or not to generate a relative URI for each target file context.
292-
293-
```js
294-
// webpack.config.js
295-
{
296-
loader: 'file-loader',
297-
options: {
298-
useRelativePath: process.env.NODE_ENV === "production"
299-
}
300-
}
301-
```
302-
303286
## Placeholders
304287

305288
### `[ext]`

src/index.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,17 @@ export default function loader(content) {
2828
}
2929
}
3030

31-
if (options.useRelativePath) {
32-
const filePath = this.resourcePath;
33-
34-
const issuer = options.context
35-
? context
36-
: this._module && this._module.issuer && this._module.issuer.context;
37-
38-
const relativeUrl =
39-
issuer &&
40-
path
41-
.relative(issuer, filePath)
42-
.split(path.sep)
43-
.join('/');
44-
45-
const relativePath = relativeUrl && `${path.dirname(relativeUrl)}/`;
46-
// eslint-disable-next-line no-bitwise
47-
if (~relativePath.indexOf('../')) {
48-
outputPath = path.posix.join(outputPath, relativePath, url);
49-
} else {
50-
outputPath = path.posix.join(relativePath, url);
51-
}
52-
}
53-
5431
let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
5532

5633
if (options.publicPath) {
5734
if (typeof options.publicPath === 'function') {
5835
publicPath = options.publicPath(url, this.resourcePath, context);
59-
} else if (options.publicPath.endsWith('/')) {
60-
publicPath = options.publicPath + url;
6136
} else {
62-
publicPath = `${options.publicPath}/${url}`;
37+
publicPath = `${
38+
options.publicPath.endsWith('/')
39+
? options.publicPath
40+
: `${options.publicPath}/`
41+
}${url}`;
6342
}
6443

6544
publicPath = JSON.stringify(publicPath);

src/options.json

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,60 @@
11
{
2-
"type": "object",
2+
"additionalProperties": false,
33
"properties": {
4-
"name": {},
5-
"regExp": {},
4+
"name": {
5+
"anyOf": [
6+
{
7+
"type": "string"
8+
},
9+
{
10+
"instanceof": "Function"
11+
}
12+
]
13+
},
14+
"outputPath": {
15+
"anyOf": [
16+
{
17+
"type": "string"
18+
},
19+
{
20+
"instanceof": "Function"
21+
}
22+
]
23+
},
24+
"publicPath": {
25+
"anyOf": [
26+
{
27+
"type": "string"
28+
},
29+
{
30+
"instanceof": "Function"
31+
}
32+
]
33+
},
634
"context": {
735
"type": "string"
836
},
9-
"publicPath": {},
10-
"outputPath": {},
11-
"useRelativePath": {
12-
"type": "boolean"
13-
},
1437
"emitFile": {
1538
"type": "boolean"
39+
},
40+
"regExp": {
41+
"anyOf": [
42+
{
43+
"type": "string"
44+
},
45+
{
46+
"instanceof": "RegExp"
47+
}
48+
]
1649
}
1750
},
1851
"errorMessages": {
52+
"name": "should be {String} or {Function} (https://github.com/webpack-contrib/file-loader#name)",
53+
"outputPath": "should be {String} or {Function} (https://github.com/webpack-contrib/file-loader#outputpath)",
54+
"publicPath": "should be {String} or {Function} (https://github.com/webpack-contrib/file-loader#publicpath)",
1955
"context": "should be {String} (https://github.com/webpack-contrib/file-loader#context)",
20-
"useRelativePath": "should be {Boolean} (https://github.com/webpack-contrib/file-loader#userelativepath)",
21-
"emitFile": "should be {Boolean} (https://github.com/webpack-contrib/file-loader#emitfile)"
56+
"emitFile": "should be {Boolean} (https://github.com/webpack-contrib/file-loader#emitfile)",
57+
"regExp": "should be {String} or {RegExp} (https://github.com/webpack-contrib/file-loader#regexp)"
2258
},
23-
"additionalProperties": true
59+
"type": "object"
2460
}
Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,58 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`errors validation errors 1`] = `
3+
exports[`validation errors 1`] = `
44
"File Loader Invalid Options
55
6-
options.useRelativePath should be boolean
6+
options should NOT have additional properties
7+
"
8+
`;
9+
10+
exports[`validation errors 2`] = `
11+
"File Loader Invalid Options
12+
13+
options.name should be string
14+
options.name should pass \\"instanceof\\" keyword validation
15+
options.name should match some schema in anyOf
16+
"
17+
`;
18+
19+
exports[`validation errors 3`] = `
20+
"File Loader Invalid Options
21+
22+
options.outputPath should be string
23+
options.outputPath should pass \\"instanceof\\" keyword validation
24+
options.outputPath should match some schema in anyOf
25+
"
26+
`;
27+
28+
exports[`validation errors 4`] = `
29+
"File Loader Invalid Options
30+
31+
options.publicPath should be string
32+
options.publicPath should pass \\"instanceof\\" keyword validation
33+
options.publicPath should match some schema in anyOf
34+
"
35+
`;
36+
37+
exports[`validation errors 5`] = `
38+
"File Loader Invalid Options
39+
40+
options.context should be string
41+
"
42+
`;
43+
44+
exports[`validation errors 6`] = `
45+
"File Loader Invalid Options
46+
47+
options.emitFile should be boolean
48+
"
49+
`;
50+
51+
exports[`validation errors 7`] = `
52+
"File Loader Invalid Options
53+
54+
options.regExp should be string
55+
options.regExp should pass \\"instanceof\\" keyword validation
56+
options.regExp should match some schema in anyOf
757
"
858
`;

test/__snapshots__/outputPath-option.test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,12 @@ Object {
107107
"source": "module.exports = __webpack_public_path__ + \\"output_path/file.png\\";",
108108
}
109109
`;
110+
111+
exports[`when applied with \`outputPath\` option matches snapshot without value 1`] = `
112+
Object {
113+
"assets": Array [
114+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
115+
],
116+
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
117+
}
118+
`;

test/__snapshots__/publicPath-option.test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ Object {
5353
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
5454
}
5555
`;
56+
57+
exports[`when applied with \`publicPath\` option matches snapshot without value 1`] = `
58+
Object {
59+
"assets": Array [
60+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
61+
],
62+
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
63+
}
64+
`;

test/__snapshots__/useRelativePath-option.test.js.snap

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/errors.test.js

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
11
import loader from '../src';
22

3-
describe('errors', () => {
4-
it('validation errors', () => {
5-
const err = () =>
6-
loader.call({
7-
query: { useRelativePath: 1 },
8-
emitFile: true,
9-
});
10-
11-
expect(err).toThrow();
12-
expect(err).toThrowErrorMatchingSnapshot();
3+
describe('validation', () => {
4+
it('errors', () => {
5+
const validate = (options) =>
6+
loader.call(
7+
Object.assign(
8+
{},
9+
{
10+
resourcePath: 'image.png',
11+
query: options,
12+
emitFile: () => {},
13+
}
14+
),
15+
'a { color: red; }'
16+
);
17+
18+
expect(() =>
19+
validate({ unknown: 'unknown' })
20+
).toThrowErrorMatchingSnapshot();
21+
22+
expect(() => validate({ name: '[path][name].[ext]' })).not.toThrow();
23+
expect(() =>
24+
validate({
25+
name: () => '[hash].[ext]',
26+
})
27+
).not.toThrow();
28+
expect(() => validate({ name: true })).toThrowErrorMatchingSnapshot();
29+
30+
expect(() => validate({ outputPath: 'assets' })).not.toThrow();
31+
expect(() =>
32+
validate({
33+
outputPath: () => 'assets',
34+
})
35+
).not.toThrow();
36+
expect(() => validate({ outputPath: true })).toThrowErrorMatchingSnapshot();
37+
38+
expect(() => validate({ publicPath: 'assets' })).not.toThrow();
39+
expect(() =>
40+
validate({
41+
publicPath: () => 'assets',
42+
})
43+
).not.toThrow();
44+
expect(() => validate({ publicPath: true })).toThrowErrorMatchingSnapshot();
45+
46+
expect(() => validate({ context: 'assets' })).not.toThrow();
47+
expect(() => validate({ context: true })).toThrowErrorMatchingSnapshot();
48+
49+
expect(() => validate({ emitFile: true })).not.toThrow();
50+
expect(() => validate({ emitFile: false })).not.toThrow();
51+
expect(() => validate({ emitFile: 'true' })).toThrowErrorMatchingSnapshot();
52+
53+
expect(() => validate({ regExp: /image\.png/ })).not.toThrow();
54+
expect(() => validate({ regExp: 'image\\.png' })).not.toThrow();
55+
expect(() => validate({ regExp: true })).toThrowErrorMatchingSnapshot();
1356
});
1457
});

test/outputPath-option.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import webpack from './helpers/compiler';
22

33
describe('when applied with `outputPath` option', () => {
4+
it('matches snapshot without value', async () => {
5+
const config = {
6+
loader: {
7+
test: /(png|jpg|svg)/,
8+
},
9+
};
10+
11+
const stats = await webpack('fixture.js', config);
12+
const [module] = stats.toJson().modules;
13+
const { assets, source } = module;
14+
15+
expect({ assets, source }).toMatchSnapshot();
16+
});
17+
418
it('matches snapshot for `{String}` value', async () => {
519
const config = {
620
loader: {

test/publicPath-option.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import webpack from './helpers/compiler';
22

33
describe('when applied with `publicPath` option', () => {
4+
it('matches snapshot without value', async () => {
5+
const config = {
6+
loader: {
7+
test: /(png|jpg|svg)/,
8+
},
9+
};
10+
11+
const stats = await webpack('fixture.js', config);
12+
const [module] = stats.toJson().modules;
13+
const { assets, source } = module;
14+
15+
expect({ assets, source }).toMatchSnapshot();
16+
});
17+
418
it('matches snapshot for `{String}` value', async () => {
519
const config = {
620
loader: {

0 commit comments

Comments
 (0)