Skip to content

Commit

Permalink
refactor: resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 28, 2020
1 parent 90ff982 commit d4e87c1
Show file tree
Hide file tree
Showing 10 changed files with 6,942 additions and 190 deletions.
252 changes: 145 additions & 107 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Expand Up @@ -45,7 +45,6 @@
"fast-glob": "^3.2.4",
"klona": "^2.0.4",
"loader-utils": "^2.0.0",
"normalize-path": "^3.0.0",
"schema-utils": "^2.7.1"
},
"devDependencies": {
Expand All @@ -62,11 +61,11 @@
"bootstrap-styl": "^5.0.9",
"cross-env": "^7.0.2",
"css-loader": "^4.3.0",
"del": "^5.1.0",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^4.3.0",
"jest": "^26.4.2",
"lint-staged": "^10.4.0",
Expand Down
27 changes: 15 additions & 12 deletions src/utils.js
Expand Up @@ -15,6 +15,7 @@ function getStylusOptions(loaderContext, loaderOptions) {
);

stylusOptions.filename = loaderContext.resourcePath;
stylusOptions.dest = path.dirname(loaderContext.resourcePath);

// Keep track of imported files (used by Stylus CLI watch mode)
// eslint-disable-next-line no-underscore-dangle
Expand Down Expand Up @@ -68,6 +69,7 @@ async function resolveFilename(
if (isGlob && result) {
loaderContext.addContextDependency(result);

// TODO improve
const patterns = parsedGlob.patterns.map((item) =>
item.slice(parsedGlob.base.length + 1)
);
Expand Down Expand Up @@ -221,8 +223,11 @@ async function getDependencies(
return;
}

// `stylus` can return files with glob characters, we should escape them to avid re globbing
// eslint-disable-next-line no-param-reassign
result.resolved = resolved;
result.resolved = Array.isArray(resolved)
? resolved.map((item) => fastGlob.escapePath(item))
: fastGlob.escapePath(resolved);

resolved = Array.isArray(resolved) ? resolved : [resolved];

Expand Down Expand Up @@ -420,22 +425,20 @@ function urlResolver(options = {}) {

compiler.isURL = true;

// eslint-disable-next-line no-param-reassign
const visitedUrl = url.nodes.map((node) => compiler.visit(node)).join('');
const splitted = visitedUrl.split('!');

// eslint-disable-next-line no-param-reassign
url = parse(splitted.pop());
const parsedUrl = parse(splitted.pop());

// Parse literal
const literal = new nodes.Literal(`url("${url.href}")`);
let { pathname } = url;
const literal = new nodes.Literal(`url("${parsedUrl.href}")`);
let { pathname } = parsedUrl;
let { dest } = this.options;
let tail = '';
let res;

// Absolute or hash
if (url.protocol || !pathname || pathname[0] === '/') {
if (parsedUrl.protocol || !pathname || pathname[0] === '/') {
return literal;
}

Expand All @@ -452,15 +455,15 @@ function urlResolver(options = {}) {
}

if (this.includeCSS && path.extname(pathname) === '.css') {
return new nodes.Literal(url.href);
return new nodes.Literal(parsedUrl.href);
}

if (url.search) {
tail += url.search;
if (parsedUrl.search) {
tail += parsedUrl.search;
}

if (url.hash) {
tail += url.hash;
if (parsedUrl.hash) {
tail += parsedUrl.hash;
}

if (dest && path.extname(dest) === '.css') {
Expand Down

0 comments on commit d4e87c1

Please sign in to comment.