Skip to content

Commit

Permalink
feat: provide an unquoted path argument for opt.resolve(path)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilic authored and madyankin committed Jul 23, 2021
1 parent 9dd0c25 commit b765440
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
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 = /['"]/;

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",
});
});

0 comments on commit b765440

Please sign in to comment.