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

Commit 3b071f5

Browse files
evilebottnawimichael-ciniawsky
evilebottnawi
authored andcommittedFeb 20, 2018
fix(index): context takes precedence over issuer.context (options.useRelativePath)
1 parent 1451b1e commit 3b071f5

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed
 

‎src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export default function loader(content) {
2929

3030
if (options.useRelativePath) {
3131
const filePath = this.resourcePath;
32-
const issuerContext = (this._module && this._module.issuer
33-
&& this._module.issuer.context) || context;
32+
const issuerContext = context || (this._module && this._module.issuer
Has comments. Original line has comments.
33+
&& this._module.issuer.context);
3434

3535
const relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join('/');
3636

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options useRelativePath options.context 1`] = `"module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
3+
exports[`Options useRelativePath \`false\` 1`] = `
4+
Object {
5+
"assets": Array [
6+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
7+
],
8+
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
9+
}
10+
`;
411

5-
exports[`Options useRelativePath this.options.context 1`] = `"module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
12+
exports[`Options useRelativePath \`true\` 1`] = `
13+
Object {
14+
"assets": Array [
15+
"nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "module.exports = __webpack_public_path__ + \\"nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;
20+
21+
exports[`Options useRelativePath \`true\` with absolute \`context\` 1`] = `
22+
Object {
23+
"assets": Array [
24+
"../file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
25+
],
26+
"source": "module.exports = __webpack_public_path__ + \\"../file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
27+
}
28+
`;
29+
30+
exports[`Options useRelativePath \`true\` with relative \`context\` 1`] = `
31+
Object {
32+
"assets": Array [
33+
"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
34+
],
35+
"source": "module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
36+
}
37+
`;

‎test/options/useRelativePath.test.js

+42-9
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,70 @@ import webpack from '../helpers/compiler';
55

66
describe('Options', () => {
77
describe('useRelativePath', () => {
8-
test('this.options.context', async () => {
8+
test('`false`', async () => {
99
const config = {
1010
loader: {
1111
test: /(png|jpg|svg)/,
1212
options: {
13+
useRelativePath: false,
14+
},
15+
},
16+
};
17+
18+
const stats = await webpack('fixture-nested.js', config);
19+
const { assets, source } = stats.toJson().modules[1];
20+
21+
expect({ assets, source }).toMatchSnapshot();
22+
});
23+
24+
test('`true`', async () => {
25+
const config = {
26+
loader: {
27+
test: /(png|jpg|svg)/,
28+
options: {
29+
useRelativePath: true,
30+
},
31+
},
32+
};
33+
34+
const stats = await webpack('fixture-nested.js', config);
35+
const { assets, source } = stats.toJson().modules[1];
36+
37+
expect({ assets, source }).toMatchSnapshot();
38+
});
39+
40+
test('`true` with relative `context`', async () => {
41+
const config = {
42+
loader: {
43+
test: /(png|jpg|svg)/,
44+
options: {
45+
context: './test/fixtures/nested/',
1346
useRelativePath: true,
1447
},
1548
},
1649
};
1750

18-
const stats = await webpack('fixture.js', config);
19-
const { source } = stats.toJson().modules[1];
51+
const stats = await webpack('fixture-nested.js', config);
52+
const { assets, source } = stats.toJson().modules[1];
2053

21-
expect(source).toMatchSnapshot();
54+
expect({ assets, source }).toMatchSnapshot();
2255
});
2356

24-
test('options.context', async () => {
57+
test('`true` with absolute `context`', async () => {
2558
const config = {
2659
loader: {
2760
test: /(png|jpg|svg)/,
2861
options: {
29-
context: '/relative/',
62+
context: '../nested/',
3063
useRelativePath: true,
3164
},
3265
},
3366
};
3467

35-
const stats = await webpack('fixture.js', config);
36-
const { source } = stats.toJson().modules[1];
68+
const stats = await webpack('fixture-nested.js', config);
69+
const { assets, source } = stats.toJson().modules[1];
3770

38-
expect(source).toMatchSnapshot();
71+
expect({ assets, source }).toMatchSnapshot();
3972
});
4073
});
4174
});

0 commit comments

Comments
 (0)
This repository has been archived.