From 78b7f5e0ab507ee2cff499dfb903076daeca6f21 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Fri, 25 Oct 2024 17:20:41 +0200 Subject: [PATCH] =?UTF-8?q?validate=20existence=20`source(=E2=80=A6)`=20ba?= =?UTF-8?q?se=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/@tailwindcss-node/src/compile.ts | 28 +++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/@tailwindcss-node/src/compile.ts b/packages/@tailwindcss-node/src/compile.ts index 9f3f58fdd..40bb9ef1f 100644 --- a/packages/@tailwindcss-node/src/compile.ts +++ b/packages/@tailwindcss-node/src/compile.ts @@ -10,11 +10,11 @@ import { } from 'tailwindcss' import { getModuleDependencies } from './get-module-dependencies' -export function compile( +export async function compile( css: string, { base, onDependency }: { base: string; onDependency: (path: string) => void }, ) { - return _compile(css, { + let compiler = await _compile(css, { base, async loadModule(id, base) { return loadModule(id, base, onDependency) @@ -23,6 +23,30 @@ export function compile( return loadStylesheet(id, base, onDependency) }, }) + + // Verify if the `source(…)` path exists (until the glob pattern starts) + if (compiler.root && compiler.root !== 'none') { + let globSymbols = /[*{]/ + let basePath = [] + for (let segment of compiler.root.pattern.split('/')) { + if (globSymbols.test(segment)) { + break + } + + basePath.push(segment) + } + + let exists = await fsPromises + .stat(path.resolve(base, basePath.join('/'))) + .then((stat) => stat.isDirectory()) + .catch(() => false) + + if (!exists) { + throw new Error(`The \`source(${compiler.root.pattern})\` does not exist`) + } + } + + return compiler } export async function __unstable__loadDesignSystem(css: string, { base }: { base: string }) {