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

Commit

Permalink
fix(index): don't append outputPath to the original url (`options…
Browse files Browse the repository at this point in the history
….outputPath` `{Function}`)
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Feb 20, 2018
1 parent 3b071f5 commit 4c1ccaa
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 28 deletions.
21 changes: 7 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ export default function loader(content) {

const context = options.context || this.rootContext || (this.options && this.options.context);

let url = loaderUtils.interpolateName(this, options.name, {
const url = loaderUtils.interpolateName(this, options.name, {
context,
content,
regExp: options.regExp,
});

let outputPath = '';

if (options.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = (
typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath
);
}
let outputPath = (
typeof options.outputPath === 'function' ? options.outputPath(url) : path.join(options.outputPath || '', url)
);

if (options.useRelativePath) {
const filePath = this.resourcePath;
Expand All @@ -41,19 +36,17 @@ export default function loader(content) {
} else {
outputPath = relativePath + url;
}

url = relativePath + url;
} else {
outputPath += url;
}

let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`;
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) : options.publicPath + url,
);
} else {
publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
}

if (options.emitFile === undefined || options.emitFile) {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/fixture-nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable */
import png from './nested/file.png';

export default png;
Binary file added test/fixtures/nested/file.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 43 additions & 7 deletions test/options/__snapshots__/outputPath.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
exports[`Options outputPath {Function} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = __webpack_public_path__ + \\"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {Function} with \`options.name\` 1`] = `
Object {
"assets": Array [
"output_path_func/file.png",
],
"source": "module.exports = __webpack_public_path__ + \\"output_path_func/file.png\\";",
}
`;

exports[`Options outputPath {Function} with \`publicPath\` {Function} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {Function} with \`publicPath\` {String} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
Expand All @@ -32,7 +41,16 @@ Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} with \`options.name\` 1`] = `
Object {
"assets": Array [
"output_path/file.png",
],
"source": "module.exports = __webpack_public_path__ + \\"output_path/file.png\\";",
}
`;

Expand All @@ -41,7 +59,7 @@ Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

Expand All @@ -53,3 +71,21 @@ Object {
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} with \`publicPath\` {String} without trailing slash 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} without trailing slash 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
9 changes: 9 additions & 0 deletions test/options/__snapshots__/publicPath.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ Object {
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options publicPath {String} without trailing slash 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
4 changes: 2 additions & 2 deletions test/options/__snapshots__/useRelativePath.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Object {
exports[`Options useRelativePath \`true\` with absolute \`context\` 1`] = `
Object {
"assets": Array [
"../file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
"file-loader/test/fixtures/nested/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/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

Expand Down
79 changes: 74 additions & 5 deletions test/options/outputPath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,65 @@ describe('Options', () => {
expect({ assets, source }).toMatchSnapshot();
});

test('{String} without trailing slash', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath: 'output_path',
},
},
};

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

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

test('{String} with `options.name`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
name: '[path][name].[ext]',
outputPath: 'output_path/',
},
},
};

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

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

test('{Function}', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath(url) {
return `output_path/${url}`;
return `output_path_func/${url}`;
},
},
},
};

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

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

test('{Function} with `options.name`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
name: '[name].[ext]',
outputPath(url) {
return `output_path_func/${url}`;
},
},
},
Expand Down Expand Up @@ -56,14 +108,31 @@ describe('Options', () => {
expect({ assets, source }).toMatchSnapshot();
});

test('{String} with `publicPath` {String} without trailing slash', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath: 'output_path',
publicPath: 'public_path',
},
},
};

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

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

test('{Function} with `publicPath` {String}', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
outputPath(url) {
return `output_path/${url}`;
return `output_path_func/${url}`;
},
},
},
Expand All @@ -82,7 +151,7 @@ describe('Options', () => {
options: {
outputPath: 'output_path/',
publicPath(url) {
return `public_path/${url}`;
return `public_path_func/${url}`;
},
},
},
Expand All @@ -100,10 +169,10 @@ describe('Options', () => {
test: /(png|jpg|svg)/,
options: {
outputPath(url) {
return `output_path/${url}`;
return `output_path_func/${url}`;
},
publicPath(url) {
return `public_path/${url}`;
return `public_path_func/${url}`;
},
},
},
Expand Down
16 changes: 16 additions & 0 deletions test/options/publicPath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ describe('Options', () => {
expect({ assets, source }).toMatchSnapshot();
});

test('{String} without trailing slash', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path',
},
},
};

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

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

test('{Function}', async () => {
const config = {
loader: {
Expand Down

0 comments on commit 4c1ccaa

Please sign in to comment.