diff --git a/src/index.js b/src/index.js index ffff9f2..fc613a3 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'; @@ -18,16 +21,22 @@ export default function loader(content) { regExp: options.regExp, }); - let outputPath = ( - typeof options.outputPath === 'function' ? options.outputPath(url) : path.join(options.outputPath || '', url) - ); + let outputPath = typeof options.outputPath === 'function' + ? options.outputPath(url) + : path.join(options.outputPath || '', url); if (options.useRelativePath) { const filePath = this.resourcePath; - const issuerContext = context || (this._module && this._module.issuer - && this._module.issuer.context); - const relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join('/'); + const issuerContext = context || ( + this._module && + this._module.issuer && + this._module.issuer.context + ); + + const relativeUrl = issuerContext && path.relative(issuerContext, filePath) + .split(path.sep) + .join('/'); const relativePath = relativeUrl && `${path.dirname(relativeUrl)}/`; // eslint-disable-next-line no-bitwise @@ -40,11 +49,16 @@ export default function loader(content) { let publicPath = null; - if (options.publicPath !== undefined) { - // support functions as publicPath to generate them dynamically - publicPath = JSON.stringify( - typeof options.publicPath === 'function' ? options.publicPath(url) : path.join(options.publicPath || '', url), - ); + if (options.publicPath) { + if (typeof options.publicPath === 'function') { + publicPath = options.publicPath(url); + } else if (options.publicPath.endsWith('/')) { + publicPath = options.publicPath + url; + } else { + publicPath = `${options.publicPath}/${url}`; + } + + publicPath = JSON.stringify(publicPath); } else { publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`; } diff --git a/test/options/__snapshots__/publicPath.test.js.snap b/test/options/__snapshots__/publicPath.test.js.snap index ce726ca..72c97dd 100644 --- a/test/options/__snapshots__/publicPath.test.js.snap +++ b/test/options/__snapshots__/publicPath.test.js.snap @@ -9,7 +9,16 @@ Object { } `; -exports[`Options publicPath {String} 1`] = ` +exports[`Options publicPath {String} - URL 1`] = ` +Object { + "assets": Array [ + "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + ], + "source": "module.exports = \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", +} +`; + +exports[`Options publicPath {String} - Without trailing slash 1`] = ` Object { "assets": Array [ "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -18,7 +27,7 @@ Object { } `; -exports[`Options publicPath {String} without trailing slash 1`] = ` +exports[`Options publicPath {String} 1`] = ` Object { "assets": Array [ "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", diff --git a/test/options/publicPath.test.js b/test/options/publicPath.test.js index e70d1f8..dfaaa9b 100644 --- a/test/options/publicPath.test.js +++ b/test/options/publicPath.test.js @@ -21,7 +21,7 @@ describe('Options', () => { expect({ assets, source }).toMatchSnapshot(); }); - test('{String} without trailing slash', async () => { + test('{String} - Without trailing slash', async () => { const config = { loader: { test: /(png|jpg|svg)/, @@ -37,6 +37,22 @@ describe('Options', () => { expect({ assets, source }).toMatchSnapshot(); }); + test('{String} - URL', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'https://cdn.com/', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const { assets, source } = stats.toJson().modules[1]; + + expect({ assets, source }).toMatchSnapshot(); + }); + test('{Function}', async () => { const config = { loader: {