Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide an unquoted path argument for opt.resolve(path) #127

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import postcss from "postcss";
import camelCase from "lodash.camelcase";
import genericNames from "generic-names";
import unquote from './unquote';

import Parser from "./css-loader-core/parser";
import FileSystemLoader from "./css-loader-core/loader";
Expand Down Expand Up @@ -86,11 +87,12 @@ module.exports = (opts = {}) => {
const loaderPlugins = [...earlierPlugins, ...pluginList];
const loader = getLoader(opts, loaderPlugins);
const fetcher = ((file, relativeTo, depTrace) => {
const resolvedResult = (typeof opts.resolve === 'function' && opts.resolve(file));
const unquoteFile = unquote(file);
const resolvedResult = (typeof opts.resolve === 'function' && opts.resolve(unquoteFile));
const resolvedFile = resolvedResult instanceof Promise ? resolvedResult : Promise.resolve(resolvedResult);

return resolvedFile.then((f = file) => {
return loader.fetch.call(loader, f || file, relativeTo, depTrace);
return resolvedFile.then((f) => {
return loader.fetch.call(loader, `"${f || unquoteFile}"`, relativeTo, depTrace);
});
});
const parser = new Parser(fetcher);
Expand Down
16 changes: 16 additions & 0 deletions src/unquote/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// copied from https://github.com/lakenen/node-unquote

var reg = /['"]/;
Copy link
Owner

Choose a reason for hiding this comment

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

I suspect this regexp will replace only the quotes and leave the backslashes. Shouldn't it be [\'\"]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

eslint tells me it can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

Copy link
Owner

Choose a reason for hiding this comment

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

My bad, I have read the code not thorough enough the first time. Everything is ok

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@madyankin If you want to keep it like where copied from, I will change it to [\'\"] and add a // eslint-disable no-useless-escape line to disable this rule.

Copy link
Contributor Author

@kamilic kamilic Jul 23, 2021

Choose a reason for hiding this comment

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


export default function unquote(str) {
if (!str) {
return "";
}
if (reg.test(str.charAt(0))) {
str = str.substr(1);
}
if (reg.test(str.charAt(str.length - 1))) {
str = str.substr(0, str.length - 1);
}
return str;
}
8 changes: 7 additions & 1 deletion test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,15 @@ exports[`processes resolve option: processes resolve option 1`] = `
width: 200px;
}._composes_a_hello {
foo: bar;
}._compose_resolve_figure {
}

._compose_resolve_figure {
display: flex;
}

._compose_resolve_figure-single-quote {
display: block;
}
"
`;

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/in/compose.resolve.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
composes: hello from "test-fixture-in/composes.a.css";
display: flex;
}

.figure-single-quote {
composes: hello from 'test-fixture-in/composes.a.css';
display: block;
}
8 changes: 6 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ it("processes resolve option", async () => {
plugin({
generateScopedName,
resolve: async (file) => {
return file.replace(/test-fixture-in/, path.dirname(sourceFile));
return file.replace(/^test-fixture-in/, path.dirname(sourceFile));
},
getJSON: (_, result) => {
json = result;
Expand All @@ -405,5 +405,9 @@ it("processes resolve option", async () => {
]).process(source, { from: sourceFile });

expect(result.css).toMatchSnapshot("processes resolve option");
expect(json).toMatchObject({"figure": "_compose_resolve_figure _composes_a_hello"});
expect(json).toStrictEqual({
figure: "_compose_resolve_figure _composes_a_hello",
"figure-single-quote":
"_compose_resolve_figure-single-quote _composes_a_hello",
});
});