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

revert(index): context takes precedence over issuer.context (options.useRelativePath) #260

Merged
merged 1 commit into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable
multiline-ternary,
*/
import path from 'path';
import loaderUtils from 'loader-utils';
import validateOptions from 'schema-utils';
Expand Down Expand Up @@ -31,13 +34,15 @@ export default function loader(content) {
if (options.useRelativePath) {
const filePath = this.resourcePath;

const issuerContext = context || (
this._module &&
this._module.issuer &&
this._module.issuer.context
);
const issuer = options.context
? context
: (
this._module &&
this._module.issuer &&
this._module.issuer.context
);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable */
import png from './nested/file.png';
import png from './assets/file.png';

export default png;
20 changes: 10 additions & 10 deletions test/options/__snapshots__/useRelativePath.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options useRelativePath \`false\` 1`] = `
exports[`Options useRelativePath {Boolean} - \`false\` 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -9,29 +9,29 @@ Object {
}
`;

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

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

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

describe('Options', () => {
describe('useRelativePath', () => {
test('`false`', async () => {
test('{Boolean} - `false`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
Expand All @@ -15,13 +15,13 @@ describe('Options', () => {
},
};

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

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

test('`true`', async () => {
test('{Boolean} - `true`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
Expand All @@ -31,13 +31,13 @@ describe('Options', () => {
},
};

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

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

test('`true` with relative `context`', async () => {
test('{Boolean} - `true` with relative `context`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
Expand All @@ -48,13 +48,13 @@ describe('Options', () => {
},
};

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

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

test('`true` with absolute `context`', async () => {
test('{Boolean} - `true` with absolute `context`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
Expand All @@ -65,7 +65,7 @@ describe('Options', () => {
},
};

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

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