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

fix(index): don't concat options.outputPath and options.publicPath #246

Merged
merged 1 commit into from Feb 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/index.js
Expand Up @@ -10,7 +10,7 @@ export default function loader(content) {

validateOptions(schema, options, 'File Loader');

const context = options.context || this.rootContext || this.options && this.options.context
const context = options.context || this.rootContext || (this.options && this.options.context);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fix eslint problem.


let url = loaderUtils.interpolateName(this, options.name, {
context,
Expand All @@ -27,9 +27,8 @@ export default function loader(content) {
);
}

const filePath = this.resourcePath;

if (options.useRelativePath) {
const filePath = this.resourcePath;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filePath need only when we use options.useRelativePath

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

Expand All @@ -44,13 +43,8 @@ export default function loader(content) {
}

url = relativePath + url;
} else if (options.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath + url;

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

let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`;
Expand Down
54 changes: 52 additions & 2 deletions test/options/__snapshots__/outputPath.test.js.snap
@@ -1,5 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options outputPath {Function} 1`] = `"module.exports = __webpack_public_path__ + \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options outputPath {Function} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to tests assets and source to ensure outputPath don't concat with publicPath

}
`;

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

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

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

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

exports[`Options outputPath {String} with \`publicPath\` {String} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
18 changes: 16 additions & 2 deletions test/options/__snapshots__/publicPath.test.js.snap
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options publicPath {Function} 1`] = `"module.exports = \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options publicPath {Function} 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options publicPath {String} 1`] = `"module.exports = \\"/test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options publicPath {String} 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
88 changes: 82 additions & 6 deletions test/options/outputPath.test.js
Expand Up @@ -10,15 +10,15 @@ describe('Options', () => {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath: '/test/',
outputPath: 'output_path/',
},
},
};

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

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

test('{Function}', async () => {
Expand All @@ -27,16 +27,92 @@ describe('Options', () => {
test: /(png|jpg|svg)/,
options: {
outputPath(url) {
return `test/${url}`;
return `output_path/${url}`;
},
},
},
};

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

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

test('{String} with `publicPath` {String}', 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}`;
},
},
},
};

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

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

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

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

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

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

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

expect({ assets, source }).toMatchSnapshot();
});
});
});
12 changes: 6 additions & 6 deletions test/options/publicPath.test.js
Expand Up @@ -10,15 +10,15 @@ describe('Options', () => {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: '/test/',
publicPath: 'public_path/',
},
},
};

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

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

test('{Function}', async () => {
Expand All @@ -27,16 +27,16 @@ describe('Options', () => {
test: /(png|jpg|svg)/,
options: {
publicPath(url) {
return `test/${url}`;
return `public_path/${url}`;
},
},
},
};

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

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