Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Finally, run `webpack` using the method you normally use (e.g., via CLI or an np

Type:

<!-- eslint-skip -->

```ts
type stylusOptions =
| {
Expand Down Expand Up @@ -334,6 +336,8 @@ module.exports = {

Type:

<!-- eslint-skip -->

```ts
type additionalData =
| string
Expand Down Expand Up @@ -454,6 +458,8 @@ module.exports = {

Type:

<!-- eslint-skip -->
Copy link
Member

Choose a reason for hiding this comment

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

Don't use skip, fix types, we need to fix it in other repos too

Copy link
Contributor Author

@ersachin3112 ersachin3112 Aug 2, 2025

Choose a reason for hiding this comment

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

i used this for README.md file only because eslint is reading it as JS file resulting code-block lint fail.


```ts
type implementation = Function | string;
```
Expand Down
2 changes: 1 addition & 1 deletion bench/fixtures/imports/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
context: __dirname,
entry: "./index.js",
output: {
path: `${__dirname}/tmp`,
path: require("node:path").join(__dirname, "tmp"),
filename: "bundle.js",
},
module: {
Expand Down
100 changes: 48 additions & 52 deletions bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ const path = require("node:path");

const Benchmark = require("benchmark");

// eslint-disable-next-line import/no-unresolved
const MemoryFileSystem = require("memory-fs");
const stylus = require("stylus");
const webpack = require("webpack");

const importWebpackConfig = require("./fixtures/imports/webpack.config");

function resolveOnComplete(fn) {
return () => {
return (...args) => {
const _this = this;
const args = arguments;

return new Promise((resolve) => {
const result = fn.apply(_this, args);
Expand All @@ -31,57 +31,53 @@ const styl = stylus(source);
const compiler = webpack(importWebpackConfig);
compiler.outputFileSystem = new MemoryFileSystem();

Promise.resolve()
.then(
resolveOnComplete(() => {
const suite = new Benchmark.Suite();
suite
.add("Native stylus", {
defer: true,
fn(deferred) {
styl
.set("filename", sourceFile)
// eslint-disable-next-line no-unused-vars
.render((error, css) => {
if (error) {
throw error;
}
await resolveOnComplete(() => {
const suite = new Benchmark.Suite();
suite
.add("Native stylus", {
defer: true,
fn(deferred) {
styl
.set("filename", sourceFile)
// eslint-disable-next-line no-unused-vars
.render((error, css) => {
if (error) {
throw error;
}

deferred.resolve();
});
},
})
.on("cycle", (event) => {
// eslint-disable-next-line no-console
console.log(String(event.target));
})
.run({ async: true });
deferred.resolve();
});
},
})
.on("cycle", (event) => {
// eslint-disable-next-line no-console
console.log(String(event.target));
})
.run({ async: true });

return suite;
}),
)
.then(
resolveOnComplete(() => {
const suite = new Benchmark.Suite();
suite
.add("Stylus loader", {
defer: true,
fn(deferred) {
compiler.run((error, stats) => {
if (error) {
throw error;
}
return suite;
})();

deferred.resolve();
});
},
})
.on("cycle", (event) => {
// eslint-disable-next-line no-console
console.log(String(event.target));
})
.run({ async: true });
await resolveOnComplete(() => {
const suite = new Benchmark.Suite();
suite
.add("Stylus loader", {
defer: true,
fn(deferred) {
compiler.run((error, _stats) => {
if (error) {
throw error;
}

return suite;
}),
);
deferred.resolve();
});
},
})
.on("cycle", (event) => {
// eslint-disable-next-line no-console
console.log(String(event.target));
})
.run({ async: true });

return suite;
})();
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"commitlint": "commitlint --from=master",
"security": "npm audit --production",
"lint:prettier": "prettier --cache --list-different .",
"lint:code": "eslint --cache .",
"lint:js": "eslint --cache .",
"lint:spelling": "cspell --cache --no-must-find-files --quiet \"**/*.*\"",
"lint": "npm-run-all -l -p \"lint:**\"",
"fix:code": "npm run lint:code -- --fix",
"fix:js": "npm run lint:js -- --fix",
"fix:prettier": "npm run lint:prettier -- --write",
"fix": "npm-run-all -l fix:code fix:prettier",
"fix": "npm-run-all -l fix:js fix:prettier",
"test:only": "cross-env NODE_ENV=test jest",
"test:watch": "npm run test:only -- --watch",
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
Expand Down
89 changes: 42 additions & 47 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from "node:path";
import { parse } from "node:url";

import fastGlob from "fast-glob";
import normalizePath from "normalize-path";
Expand Down Expand Up @@ -103,6 +102,28 @@ function getPossibleRequests(loaderContext, filename) {
return [...new Set([request, filename])];
}

async function resolveRequests(context, possibleRequests, resolve) {
if (possibleRequests.length === 0) {
throw new Error("No possible requests provided");
}

let result;

try {
result = await resolve(context, possibleRequests[0]);
} catch (error) {
const [, ...tailPossibleRequests] = possibleRequests;

if (tailPossibleRequests.length === 0) {
throw error;
}

result = await resolveRequests(context, tailPossibleRequests, resolve);
}

return result;
}

async function resolveFilename(
loaderContext,
fileResolver,
Expand Down Expand Up @@ -132,17 +153,11 @@ async function resolveFilename(
globTask.base,
);

let globResult;

try {
globResult = await resolveRequests(
context,
possibleGlobRequests,
globResolver,
);
} catch (err) {
throw err;
}
const globResult = await resolveRequests(
context,
possibleGlobRequests,
globResolver,
);

loaderContext.addContextDependency(globResult);

Expand All @@ -165,26 +180,16 @@ async function resolveFilename(
return result;
}

async function resolveRequests(context, possibleRequests, resolve) {
if (possibleRequests.length === 0) {
throw undefined;
}

let result;

try {
result = await resolve(context, possibleRequests[0]);
} catch (error) {
const [, ...tailPossibleRequests] = possibleRequests;

if (tailPossibleRequests.length === 0) {
throw error;
}

result = await resolveRequests(context, tailPossibleRequests, resolve);
}
function readFile(inputFileSystem, filepath) {
return new Promise((resolve, reject) => {
inputFileSystem.readFile(filepath, (error, stats) => {
if (error) {
reject(error);
}

return result;
resolve(stats);
});
});
}

const URL_RE = /^(?:url\s*\(\s*)?['"]?(?:[#/]|(?:https?:)?\/\/)/i;
Expand Down Expand Up @@ -228,7 +233,7 @@ async function getDependencies(
if (!firstNode.val) {
const evaluator = new Evaluator(ast);

firstNode = evaluator.visit.call(evaluator, firstNode).first;
firstNode = evaluator.visit(firstNode).first;
}

const originalNodePath =
Expand Down Expand Up @@ -378,7 +383,9 @@ function mergeBlocks(blocks) {

for (const block of blocks) {
if (finalBlock) {
block.nodes.forEach(adding);
for (const item of block.nodes) {
adding(item);
}
} else {
finalBlock = block;
}
Expand Down Expand Up @@ -614,7 +621,7 @@ function urlResolver(options = {}) {
const visitedUrl = url.nodes.map((node) => compiler.visit(node)).join("");
const splitted = visitedUrl.split("!");

const parsedUrl = parse(splitted.pop());
const parsedUrl = new URL(splitted.pop());

// Parse literal
const literal = new nodes.Literal(`url("${parsedUrl.href}")`);
Expand All @@ -632,7 +639,7 @@ function urlResolver(options = {}) {
if (!options.nocheck) {
const _paths = options.paths || [];

pathname = utils.lookup(pathname, _paths.concat(this.paths));
pathname = utils.lookup(pathname, [..._paths, ...this.paths]);

if (!pathname) {
return literal;
Expand Down Expand Up @@ -678,18 +685,6 @@ function urlResolver(options = {}) {
return resolver;
}

function readFile(inputFileSystem, filepath) {
return new Promise((resolve, reject) => {
inputFileSystem.readFile(filepath, (error, stats) => {
if (error) {
reject(error);
}

resolve(stats);
});
});
}

const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
const ABSOLUTE_SCHEME = /^[A-Za-z0-9+\-.]+:/;

Expand Down
Loading
Loading