Add optional SCSS entry support
This commit is contained in:
+49
-7
@@ -3,6 +3,8 @@ import { basename, dirname, extname, join, relative, resolve } from 'node:path';
|
||||
|
||||
export const normalizePath = (path) => path.replaceAll('\\', '/');
|
||||
|
||||
const styleExtensions = ['.scss', '.css'];
|
||||
|
||||
export const shouldSkipDirectory = (name) =>
|
||||
['.git', 'dist', 'docs', 'node_modules', 'storybook-static'].includes(name);
|
||||
|
||||
@@ -30,6 +32,7 @@ export const shouldCopyFile = (name) => {
|
||||
|
||||
return ![
|
||||
'.css',
|
||||
'.scss',
|
||||
'.gz',
|
||||
'.gif',
|
||||
'.jpeg',
|
||||
@@ -68,13 +71,47 @@ export const walkFiles = (directory, callback) => {
|
||||
export const hasRuntimeSelfRegistration = (absolutePath) =>
|
||||
readFileSync(absolutePath, 'utf8').includes('window.initBlock');
|
||||
|
||||
const collectStyleCandidates = (relativePath) => {
|
||||
const extension = extname(relativePath);
|
||||
|
||||
if (!styleExtensions.includes(extension)) {
|
||||
return [relativePath];
|
||||
}
|
||||
|
||||
const stem = relativePath.slice(0, -extension.length);
|
||||
const candidates = [relativePath];
|
||||
|
||||
for (const styleExtension of styleExtensions) {
|
||||
const candidate = `${stem}${styleExtension}`;
|
||||
if (!candidates.includes(candidate)) {
|
||||
candidates.push(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
};
|
||||
|
||||
const findFirstExistingStyle = (sourceRoot, relativePath) => {
|
||||
for (const candidate of collectStyleCandidates(relativePath)) {
|
||||
const absolutePath = join(sourceRoot, candidate);
|
||||
if (existsSync(absolutePath)) {
|
||||
return {
|
||||
absolutePath,
|
||||
relativePath: candidate
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export const collectEntries = (sourceDir, config) => {
|
||||
const sourceRoot = resolve(sourceDir);
|
||||
const entries = {};
|
||||
const globalCss = join(sourceRoot, config.entry.globalStyles);
|
||||
const globalStyle = findFirstExistingStyle(sourceRoot, config.entry.globalStyles);
|
||||
|
||||
if (existsSync(globalCss)) {
|
||||
entries[normalizePath(config.entry.globalStyles)] = globalCss;
|
||||
if (globalStyle) {
|
||||
entries[normalizePath(globalStyle.relativePath)] = globalStyle.absolutePath;
|
||||
}
|
||||
|
||||
walkFiles(sourceRoot, (absolutePath) => {
|
||||
@@ -87,8 +124,10 @@ export const collectEntries = (sourceDir, config) => {
|
||||
const relativeSpecPath = normalizePath(relative(sourceRoot, absolutePath));
|
||||
const isComponentSpec = relativeSpecPath.startsWith(`${config.paths.components}/`);
|
||||
|
||||
for (const extension of ['.css', '.js']) {
|
||||
if (isComponentSpec && extension === '.css') {
|
||||
for (const extension of ['.scss', '.css', '.js']) {
|
||||
const isStyle = styleExtensions.includes(extension);
|
||||
|
||||
if (isComponentSpec && isStyle) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -102,8 +141,11 @@ export const collectEntries = (sourceDir, config) => {
|
||||
}
|
||||
|
||||
const relativePath = normalizePath(relative(sourceRoot, entryPath));
|
||||
entries[extension === '.js' ? relativePath.slice(0, -extension.length) : relativePath] =
|
||||
entryPath;
|
||||
entries[extension === '.js' ? relativePath.slice(0, -extension.length) : relativePath] = entryPath;
|
||||
|
||||
if (isStyle) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user