diff --git a/src/index.js b/src/index.js index a628fea..af9eb78 100644 --- a/src/index.js +++ b/src/index.js @@ -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._module.issuer.context); const relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join('/'); diff --git a/test/options/__snapshots__/useRelativePath.test.js.snap b/test/options/__snapshots__/useRelativePath.test.js.snap index 6dd64c2..bd70c4f 100644 --- a/test/options/__snapshots__/useRelativePath.test.js.snap +++ b/test/options/__snapshots__/useRelativePath.test.js.snap @@ -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\\";", +} +`; diff --git a/test/options/useRelativePath.test.js b/test/options/useRelativePath.test.js index 7858f29..83633a6 100644 --- a/test/options/useRelativePath.test.js +++ b/test/options/useRelativePath.test.js @@ -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(); }); }); });