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

Commit

Permalink
fix(index): context takes precedence over issuer.context (`option…
Browse files Browse the repository at this point in the history
…s.useRelativePath`)
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Feb 20, 2018
1 parent 1451b1e commit 3b071f5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export default function loader(content) {

if (options.useRelativePath) {
const filePath = this.resourcePath;
const issuerContext = (this._module && this._module.issuer
&& this._module.issuer.context) || context;
const issuerContext = context || (this._module && this._module.issuer

This comment has been minimized.

Copy link
@cowchimp

cowchimp Feb 27, 2018

could it be that this makes the this._module.issuer.context part unreachable?
won't context always be truthy at this point?
thanks.

This comment has been minimized.

Copy link
@alexander-akait

alexander-akait Feb 27, 2018

Member

@cowchimp maybe bug, and seems related to #257, need investigate

This comment has been minimized.

Copy link
@michael-ciniawsky
&& this._module.issuer.context);

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

Expand Down
36 changes: 34 additions & 2 deletions test/options/__snapshots__/useRelativePath.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options useRelativePath options.context 1`] = `"module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options useRelativePath \`false\` 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options useRelativePath this.options.context 1`] = `"module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options useRelativePath \`true\` 1`] = `
Object {
"assets": Array [
"nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options useRelativePath \`true\` with absolute \`context\` 1`] = `
Object {
"assets": Array [
"../file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"../file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options useRelativePath \`true\` with relative \`context\` 1`] = `
Object {
"assets": Array [
"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
51 changes: 42 additions & 9 deletions test/options/useRelativePath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,70 @@ import webpack from '../helpers/compiler';

describe('Options', () => {
describe('useRelativePath', () => {
test('this.options.context', async () => {
test('`false`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
useRelativePath: false,
},
},
};

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

expect({ assets, source }).toMatchSnapshot();
});

test('`true`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
useRelativePath: true,
},
},
};

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

expect({ assets, source }).toMatchSnapshot();
});

test('`true` with relative `context`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
context: './test/fixtures/nested/',
useRelativePath: true,
},
},
};

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

expect(source).toMatchSnapshot();
expect({ assets, source }).toMatchSnapshot();
});

test('options.context', async () => {
test('`true` with absolute `context`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
context: '/relative/',
context: '../nested/',
useRelativePath: true,
},
},
};

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

expect(source).toMatchSnapshot();
expect({ assets, source }).toMatchSnapshot();
});
});
});

0 comments on commit 3b071f5

Please sign in to comment.