Skip to content

Commit

Permalink
refactor: use enhanced resolve for package exports handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Dec 28, 2022
1 parent af06296 commit ee6db79
Show file tree
Hide file tree
Showing 4 changed files with 928 additions and 731 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-clocks-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chialab/stylelint-config": patch
---

Handle package exports.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"stylelint": "^14.0.0"
},
"dependencies": {
"enhanced-resolve": "^5.12.0",
"postcss": "^8.4.0",
"postcss-values-parser": "^6.0.0",
"resolve": "^1.20.0",
"stylelint-order": "^5.0.0"
},
"devDependencies": {
Expand Down
54 changes: 35 additions & 19 deletions plugins/value-no-unknown-custom-properties/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,39 @@

const fs = require('fs');
const path = require('path');
const nodeResolve = require('resolve');
const nodeResolve = require('enhanced-resolve');
const postcss = require('postcss');
const stylelint = require('stylelint');
const postcssValuesParser = require('postcss-values-parser');

async function resolve(source, options) {
return new Promise((ok, ko) => {
nodeResolve(source, options, (err, res) => {
const styleResolve = nodeResolve.create({
extensions: ['.css'],
mainFields: ['style'],
preferRelative: true,
symlinks: false,
});

/**
* Check if the given path is a valid url.
* @param {string} url
* @returns True if the given string is a valid url.
*/
function isUrl(url) {
try {
return !!(new URL(url));
} catch (err) {
//
}
return false;
}

async function resolve(source, importer) {
return new Promise((resolve, reject) => {
styleResolve({}, importer, source, {}, (err, res) => {
if (err) {
ko(err);
reject(err);
} else {
ok(res);
resolve(res);
}
});
});
Expand All @@ -31,28 +52,23 @@ async function getCustomPropertiesFromRoot(root) {
sourceDir = path.dirname(root.source.input.file);
}


const importPromises = [];
root.walkAtRules('import', (atRule) => {
const fileName = atRule.params
.replace(/url\(/, '')
.replace(/\)$/, '')
.replace(/['|"]/g, '')
.replace(/^~/, '');
if (fileName.match(/^https?:\/\//)) {

if (isUrl(fileName)) {
return;
}
importPromises.push(resolve(fileName, {
basedir: sourceDir,
extensions: ['.css'],
preserveSymlinks: true,
packageFilter(pkg) {
if (pkg.style) {
pkg.main = pkg.style;
}
return pkg;
},
}).then((resolvedFileName) => getCustomPropertiesFromCSSFile(resolvedFileName)).catch(() => ({})));

importPromises.push(
resolve(fileName, sourceDir)
.then((resolvedFileName) => getCustomPropertiesFromCSSFile(resolvedFileName))
.catch(() => ({}))
);
});

const properties = await Promise.all(importPromises);
Expand Down
Loading

0 comments on commit ee6db79

Please sign in to comment.