Skip to content

feat: add basic alias resolver and @use support #49

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

Closed
wants to merge 3 commits into from
Closed
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: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ Use this option to configure how the rule solve paths of `@import` rules.
"resolver": {
"extensions": [".css"], // => default to [".css"]
"paths": ["./assets/css", "./static/css"], // => paths to look for files, default to []
"moduleDirectories": ["node_modules"] // => modules folder to look for files, default to ["node_modules"]
"moduleDirectories": ["node_modules"], // => modules folder to look for files, default to ["node_modules"]
"alias": {
"@": resolve(import.meta.dirname, 'src') // => Alias for import path
}
}
}]
}
Expand Down
43 changes: 43 additions & 0 deletions index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ accept = [
{ code: '@import url("./test/import-custom-properties.css" url-mod); body { color: var(--brand-red); }' },
{ code: '@import \'./test/import-custom-properties.css\'; @import \'./test/import-custom-properties123.css\'; body { color: var(--brand-red); }' },
{ code: 'color: var(--my-undefined-color, #ffffff);' },
{ code: '@use \'./test/import-custom-properties.css\'; body { color: var(--brand-red); }' },
{ code: '@use "./test/import-custom-properties.css" screen; body { color: var(--brand-red); }' },
{ code: '@use "./test/import-custom-properties.css"/**/; body { color: var(--brand-red); }' },
{ code: '@use url(./test/import-custom-properties.css); body { color: var(--brand-red); }' },
{ code: '@use url(\'./test/import-custom-properties.css\'); body { color: var(--brand-red); }' },
{ code: '@use url( \'./test/import-custom-properties.css\'/**/)/**/; body { color: var(--brand-red); }' },
{ code: '@use url(\t\'./test/import-custom-properties.css\'\t)\t; body { color: var(--brand-red); }' },
{ code: '@use url(./test/import-custom-properties.css) screen; body { color: var(--brand-red); }' },
{ code: '@use url("./test/import-custom-properties.css") screen; body { color: var(--brand-red); }' },
{ code: '@use url("./test/import-custom-properties.css" url-mod); body { color: var(--brand-red); }' },
{ code: '@use \'./test/import-custom-properties.css\'; @import \'./test/import-custom-properties123.css\'; body { color: var(--brand-red); }' },
];
reject = [
{ code: 'body { color: var(--brand-blue); }', message: messages.unexpected('--brand-blue', 'color') },
{ code: '@import \'./test/import-custom-properties123.css\'; body { color: var(--brand-red); }', message: messages.unexpected('--brand-red', 'color') },
{ code: '@use \'./test/import-custom-properties123.css\'; body { color: var(--brand-red); }', message: messages.unexpected('--brand-red', 'color') },
];

testRule({ plugins: ['.'], ruleName: rule.ruleName, config: true, accept, reject });
Expand Down Expand Up @@ -145,3 +157,34 @@ testRule({
accept,
reject,
});

/* Test enabled: { resolver: { alias } }
/* ========================================================================== */

accept = [
{ code: '@import \'@/import-custom-properties.css\'; body { color: var(--brand-red); }' },
{ code: '@import \'@test/import-custom-properties.css\'; body { color: var(--brand-red); }' },
{ code: '@use \'@/import-custom-properties.css\'; body { color: var(--brand-red); }' },
{ code: '@use \'@test/import-custom-properties.css\'; body { color: var(--brand-red); }' },
];
reject = [
{ code: '@import \'@/import-custom-properties.css\'; body { color: var(--brand-re); }', message: messages.unexpected('--brand-re', 'color') },
{ code: '@import \'@/import-custom-properties.css\'; body { color: var(--brand-redz); }', message: messages.unexpected('--brand-redz', 'color') },
{ code: '@use \'@/import-custom-properties.css\'; body { color: var(--brand-re); }', message: messages.unexpected('--brand-re', 'color') },
{ code: '@use \'@/import-custom-properties.css\'; body { color: var(--brand-redz); }', message: messages.unexpected('--brand-redz', 'color') },
];

testRule({
plugins: ['.'],
ruleName: rule.ruleName,
config: [true, {
resolver: {
alias: {
'@': './test',
'@test': './test',
},
},
}],
accept,
reject,
});
62 changes: 48 additions & 14 deletions src/lib/get-custom-properties-from-root.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ export default async function getCustomPropertiesFromRoot(root, resolver) {
// recursively add custom properties from @import statements
const importPromises = [];
root.walkAtRules('import', atRule => {
const fileName = parseImportParams(atRule.params);
if (!fileName) {
return;
const promise = getImportPromise(atRule, resolver, sourceDir);
if (promise) {
importPromises.push(promise);
}
});

if (path.isAbsolute(fileName)) {
importPromises.push(getCustomPropertiesFromCSSFile(fileName, resolver));
} else {
const promise = resolveId(fileName, sourceDir, {
paths: resolver.paths,
extensions: resolver.extensions,
moduleDirectories: resolver.moduleDirectories,
})
.then((filePath) => getCustomPropertiesFromCSSFile(filePath, resolver))
.catch(() => {});

root.walkAtRules('use', atRule => {
const promise = getImportPromise(atRule, resolver, sourceDir);
if (promise) {
importPromises.push(promise);
}
});
Expand Down Expand Up @@ -107,3 +100,44 @@ function parseImportParams(params) {

return false;
}

function getFileNameFromAlias(fileName, resolver) {
if (!resolver.alias) {
return fileName;
}

const split = fileName.split('/');

if (split.length > 0) {
const resolvedAlias = Object.entries(resolver.alias).find(([key]) => key === split[0]);

if (resolvedAlias) {
const [alias, value] = resolvedAlias;
return fileName.replace(alias, value);
}
}
return fileName;
}

function getImportPromise(atRule, resolver, sourceDir) {
const fileName = parseImportParams(atRule.params);
if (!fileName) {
return;
}

const aliasedFileName = getFileNameFromAlias(fileName, resolver);

if (path.isAbsolute(fileName)) {
return getCustomPropertiesFromCSSFile(aliasedFileName, resolver);
} else {
const promise = resolveId(aliasedFileName, sourceDir, {
paths: resolver.paths,
extensions: resolver.extensions,
moduleDirectories: resolver.moduleDirectories,
})
.then((filePath) => getCustomPropertiesFromCSSFile(filePath, resolver))
.catch(() => {});

return promise;
}
}