Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use enhanced resolve for package exports handling #2

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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