diff --git a/src/index.js b/src/index.js index 0e144fe..62a0297 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,6 @@ +/* eslint-disable + multiline-ternary, +*/ import path from 'path'; import loaderUtils from 'loader-utils'; import validateOptions from 'schema-utils'; @@ -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('/'); diff --git a/test/fixtures/nested/file.png b/test/fixtures/nested/assets/file.png similarity index 100% rename from test/fixtures/nested/file.png rename to test/fixtures/nested/assets/file.png diff --git a/test/fixtures/fixture-nested.js b/test/fixtures/nested/fixture.js similarity index 53% rename from test/fixtures/fixture-nested.js rename to test/fixtures/nested/fixture.js index bd8db2f..af5f4ed 100644 --- a/test/fixtures/fixture-nested.js +++ b/test/fixtures/nested/fixture.js @@ -1,4 +1,4 @@ /* eslint-disable */ -import png from './nested/file.png'; +import png from './assets/file.png'; export default png; diff --git a/test/options/__snapshots__/useRelativePath.test.js.snap b/test/options/__snapshots__/useRelativePath.test.js.snap index 08bd912..698b5f9 100644 --- a/test/options/__snapshots__/useRelativePath.test.js.snap +++ b/test/options/__snapshots__/useRelativePath.test.js.snap @@ -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", @@ -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\\";", } `; diff --git a/test/options/useRelativePath.test.js b/test/options/useRelativePath.test.js index 83633a6..d8cc429 100644 --- a/test/options/useRelativePath.test.js +++ b/test/options/useRelativePath.test.js @@ -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)/, @@ -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)/, @@ -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)/, @@ -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)/, @@ -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();