Skip to content

Commit

Permalink
perf: tiny optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Jun 11, 2020
1 parent 2ab86be commit 309e37b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/loaders/postcss/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ const loader: Loader<PostCSSLoaderOptions> = {
} else {
const injectorVarName = saferId("injector");

if (!injectorId)
if (!injectorId) {
injectorId = await resolveAsync("./inject-css", {
basedir: path.join(
process.env.NODE_ENV === "test" ? process.cwd() : __dirname,
"runtime",
),
}).then(normalizePath);
}

const injectorData =
typeof options.inject === "object" ? `,${JSON.stringify(options.inject)}` : "";
Expand Down
13 changes: 7 additions & 6 deletions src/loaders/postcss/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,21 @@ const plugin: postcss.Plugin<UrlOptions> = postcss.plugin(
node.type = "string";
node.value = inlineFile(from, source);
} else {
let safeTo, to;
to = safeTo = normalizePath(generateName(placeholder, from, source));
const unsafeTo = normalizePath(generateName(placeholder, from, source));
let to = unsafeTo;

// Avoid file overrides
for (let i = 1; usedNames.has(safeTo) && usedNames.get(safeTo) !== from; i++)
safeTo = firstExtRe.test(to) ? to.replace(firstExtRe, `${i}$1`) : `${to}${i}`;
const hasExt = firstExtRe.test(unsafeTo);
for (let i = 1; usedNames.has(to) && usedNames.get(to) !== from; i++) {
to = hasExt ? unsafeTo.replace(firstExtRe, `${i}$1`) : `${unsafeTo}${i}`;
}

to = safeTo;
usedNames.set(to, from);

node.type = "string";
node.value = publicPath + (/[/\\]$/.test(publicPath) ? "" : "/") + path.basename(to);

to = normalizePath(assetDir, safeTo);
to = normalizePath(assetDir, to);
res.messages.push({ plugin: name, type: "asset", to, source });
}

Expand Down
9 changes: 5 additions & 4 deletions src/utils/load-module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { sync as resolveSync } from "resolve";
import { sync as resolveSync, SyncOpts } from "resolve";

const loaded: Record<string, unknown> = {};
export default function (moduleId: string, basedir = process.cwd()): unknown | undefined {
const options: SyncOpts = { basedir: process.cwd(), preserveSymlinks: false };
export default function (moduleId: string): unknown {
if (loaded[moduleId]) return loaded[moduleId];
if (loaded[moduleId] === null) return;

try {
try {
loaded[moduleId] = require(resolveSync(moduleId, { basedir })) as unknown;
loaded[moduleId] = require(resolveSync(moduleId, options)) as unknown;
} catch {
loaded[moduleId] = require(resolveSync(`./${moduleId}`, { basedir })) as unknown;
loaded[moduleId] = require(resolveSync(`./${moduleId}`, options)) as unknown;
}
} catch {
loaded[moduleId] = null;
Expand Down

0 comments on commit 309e37b

Please sign in to comment.