Skip to content

Commit

Permalink
perf: replace some sets with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed May 27, 2020
1 parent fbba337 commit 2b7c28e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/loaders/postcss/import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const plugin: postcss.Plugin<ImportOptions> = postcss.plugin(
delete opts?.map;

const { file } = css.source.input;
const importSet = new Set<{ importRule: postcss.AtRule; url: string }>();
const importList: { importRule: postcss.AtRule; url: string }[] = [];
const basedir = path.dirname(file);

css.walkAtRules(/^import$/i, importRule => {
Expand Down Expand Up @@ -95,10 +95,10 @@ const plugin: postcss.Plugin<ImportOptions> = postcss.plugin(
return;
}

importSet.add({ importRule, url });
importList.push({ importRule, url });
});

for await (const { importRule, url } of importSet) {
for await (const { importRule, url } of importList) {
try {
const { source, from } = await resolve(url, basedir, extensions);

Expand Down
15 changes: 6 additions & 9 deletions src/loaders/postcss/url/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import path from "path";
import postcss from "postcss";
import valueParser, { Node, ParsedValue } from "postcss-value-parser";

import { mm } from "../../../utils/sourcemap";
import { normalizePath, isAbsolutePath } from "../../../utils/path";

import { firstExtRe, dataURIRe } from "../common";

import resolveDefault, { UrlResolve } from "./resolve";
import generateName from "./generate";
import { walkUrls, isDeclWithUrl } from "./utils";
Expand Down Expand Up @@ -77,13 +74,13 @@ const plugin: postcss.Plugin<UrlOptions> = postcss.plugin(
const { file } = css.source.input;
const map = await mm(css.source.input.map?.text).resolve(path.dirname(file)).toConsumer();

const nodeSet = new Set<{
const urlList: {
node: Node;
url: string;
decl: postcss.Declaration;
parsed: ParsedValue;
basedir: string;
}>();
}[] = [];

const imported = res.messages
.filter(msg => msg.type === "dependency")
Expand Down Expand Up @@ -121,7 +118,7 @@ const plugin: postcss.Plugin<UrlOptions> = postcss.plugin(
// Use PostCSS imports
if (decl.source?.input.file && imported.includes(decl.source.input.file)) {
const basedir = path.dirname(decl.source.input.file);
nodeSet.add({ node, url, decl, parsed, basedir });
urlList.push({ node, url, decl, parsed, basedir });
return;
}

Expand All @@ -131,21 +128,21 @@ const plugin: postcss.Plugin<UrlOptions> = postcss.plugin(
const realPos = map?.originalPositionFor(pos);
const basedir = realPos?.source && path.dirname(realPos.source);
if (basedir) {
nodeSet.add({ node, url, decl, parsed, basedir });
urlList.push({ node, url, decl, parsed, basedir });
return;
}
}

// Use current file
const basedir = path.dirname(file);
nodeSet.add({ node, url, decl, parsed, basedir });
urlList.push({ node, url, decl, parsed, basedir });
});
});

map?.destroy();
const usedNames = new Map<string, string>();

for await (const { node, url, decl, parsed, basedir } of nodeSet) {
for await (const { node, url, decl, parsed, basedir } of urlList) {
try {
const { source, from } = await resolve(url, basedir);

Expand Down
1 change: 1 addition & 0 deletions src/shims/less.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ declare namespace less {
}

interface Less {
// eslint-disable-next-line @typescript-eslint/naming-convention
AbstractFileManager: typeof AbstractFileManager;
render(input: string, options?: Options): Promise<RenderOutput>;
}
Expand Down

0 comments on commit 2b7c28e

Please sign in to comment.