|
| 1 | +import autoprefixer from 'autoprefixer'; |
| 2 | +import { extname, relative } from 'node:path'; |
| 3 | +import { pathToFileURL } from 'node:url'; |
| 4 | +import { workerData } from 'node:worker_threads'; |
| 5 | +import postcss from 'postcss'; |
| 6 | +import postcssUrl from 'postcss-url'; |
| 7 | +import { EsbuildExecutor } from '../esbuild/esbuild-executor'; |
| 8 | +import { generateKey, readCacheEntry, saveCacheEntry } from '../utils/cache'; |
| 9 | +import * as log from '../utils/log'; |
| 10 | +import { CssUrl } from './stylesheet-processor'; |
| 11 | + |
| 12 | +const { tailwindConfigPath, projectBasePath, browserslistData, targets, cssUrl, styleIncludePaths } = workerData as { |
| 13 | + tailwindConfigPath: string | undefined; |
| 14 | + browserslistData: string; |
| 15 | + targets: string[]; |
| 16 | + projectBasePath: string; |
| 17 | + cssUrl: CssUrl; |
| 18 | + styleIncludePaths: string[]; |
| 19 | + cacheDirectory: string | undefined; |
| 20 | +}; |
| 21 | + |
| 22 | +let cacheDirectory = workerData.cacheDirectory; |
| 23 | +let postCssProcessor: ReturnType<typeof postcss>; |
| 24 | +let esbuild: EsbuildExecutor; |
| 25 | + |
| 26 | +interface RenderRequest { |
| 27 | + content: string; |
| 28 | + filePath: string; |
| 29 | +} |
| 30 | + |
| 31 | +async function render({ content, filePath }: RenderRequest): Promise<string> { |
| 32 | + let key: string | undefined; |
| 33 | + if (cacheDirectory && !content.includes('@import') && !content.includes('@use')) { |
| 34 | + // No transitive deps, we can cache more aggressively. |
| 35 | + key = await generateKey(content, ...browserslistData); |
| 36 | + const result = await readCacheEntry(cacheDirectory, key); |
| 37 | + if (result) { |
| 38 | + result.warnings.forEach(msg => log.warn(msg)); |
| 39 | + |
| 40 | + return result.css; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Render pre-processor language (sass, styl, less) |
| 45 | + const renderedCss = await renderCss(filePath, content); |
| 46 | + |
| 47 | + // We cannot cache CSS re-rendering phase, because a transitive dependency via (@import) can case different CSS output. |
| 48 | + // Example a change in a mixin or SCSS variable. |
| 49 | + if (!key) { |
| 50 | + key = await generateKey(renderedCss, ...browserslistData); |
| 51 | + } |
| 52 | + |
| 53 | + if (cacheDirectory) { |
| 54 | + const cachedResult = await readCacheEntry(cacheDirectory, key); |
| 55 | + if (cachedResult) { |
| 56 | + cachedResult.warnings.forEach(msg => log.warn(msg)); |
| 57 | + |
| 58 | + return cachedResult.css; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Render postcss (autoprefixing and friends) |
| 63 | + const result = await postCssProcessor.process(renderedCss, { |
| 64 | + from: filePath, |
| 65 | + to: filePath.replace(extname(filePath), '.css'), |
| 66 | + }); |
| 67 | + |
| 68 | + const warnings = result.warnings().map(w => w.toString()); |
| 69 | + const { code, warnings: esBuildWarnings } = await esbuild.transform(result.css, { |
| 70 | + loader: 'css', |
| 71 | + minify: true, |
| 72 | + target: targets, |
| 73 | + sourcefile: filePath, |
| 74 | + }); |
| 75 | + |
| 76 | + if (esBuildWarnings.length > 0) { |
| 77 | + warnings.push(...(await esbuild.formatMessages(esBuildWarnings, { kind: 'warning' }))); |
| 78 | + } |
| 79 | + |
| 80 | + if (cacheDirectory) { |
| 81 | + await saveCacheEntry( |
| 82 | + cacheDirectory, |
| 83 | + key, |
| 84 | + JSON.stringify({ |
| 85 | + css: code, |
| 86 | + warnings, |
| 87 | + }), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + warnings.forEach(msg => log.warn(msg)); |
| 92 | + |
| 93 | + return code; |
| 94 | +} |
| 95 | + |
| 96 | +async function renderCss(filePath: string, css: string): Promise<string> { |
| 97 | + const ext = extname(filePath); |
| 98 | + |
| 99 | + switch (ext) { |
| 100 | + case '.sass': |
| 101 | + case '.scss': { |
| 102 | + return (await import('sass')).compileString(css, { |
| 103 | + url: pathToFileURL(filePath), |
| 104 | + syntax: '.sass' === ext ? 'indented' : 'scss', |
| 105 | + loadPaths: styleIncludePaths, |
| 106 | + }).css; |
| 107 | + } |
| 108 | + case '.less': { |
| 109 | + const { css: content } = await ( |
| 110 | + await import('less') |
| 111 | + ).default.render(css, { |
| 112 | + filename: filePath, |
| 113 | + math: 'always', |
| 114 | + javascriptEnabled: true, |
| 115 | + paths: styleIncludePaths, |
| 116 | + }); |
| 117 | + |
| 118 | + return content; |
| 119 | + } |
| 120 | + |
| 121 | + case '.css': |
| 122 | + default: |
| 123 | + return css; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +function getTailwindPlugin() { |
| 128 | + // Attempt to setup Tailwind CSS |
| 129 | + // Only load Tailwind CSS plugin if configuration file was found. |
| 130 | + // This acts as a guard to ensure the project actually wants to use Tailwind CSS. |
| 131 | + // The package may be unknowningly present due to a third-party transitive package dependency. |
| 132 | + if (tailwindConfigPath) { |
| 133 | + let tailwindPackagePath; |
| 134 | + try { |
| 135 | + tailwindPackagePath = require.resolve('tailwindcss', { paths: [projectBasePath] }); |
| 136 | + } catch { |
| 137 | + const relativeTailwindConfigPath = relative(projectBasePath, tailwindConfigPath); |
| 138 | + log.warn( |
| 139 | + `Tailwind CSS configuration file found (${relativeTailwindConfigPath})` + |
| 140 | + ` but the 'tailwindcss' package is not installed.` + |
| 141 | + ` To enable Tailwind CSS, please install the 'tailwindcss' package.`, |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + if (tailwindPackagePath) { |
| 146 | + return require(tailwindPackagePath)({ config: tailwindConfigPath }); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +async function initialize() { |
| 152 | + const postCssPlugins = []; |
| 153 | + const tailwinds = getTailwindPlugin(); |
| 154 | + if (tailwinds) { |
| 155 | + postCssPlugins.push(tailwinds); |
| 156 | + cacheDirectory = undefined; |
| 157 | + } |
| 158 | + |
| 159 | + if (cssUrl !== CssUrl.none) { |
| 160 | + postCssPlugins.push(postcssUrl({ url: cssUrl })); |
| 161 | + } |
| 162 | + |
| 163 | + postCssPlugins.push( |
| 164 | + autoprefixer({ |
| 165 | + ignoreUnknownVersions: true, |
| 166 | + overrideBrowserslist: browserslistData, |
| 167 | + }), |
| 168 | + ); |
| 169 | + |
| 170 | + postCssProcessor = postcss(postCssPlugins); |
| 171 | + |
| 172 | + esbuild = new EsbuildExecutor(); |
| 173 | + |
| 174 | + // Return the render function for use |
| 175 | + return render; |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * The default export will be the promise returned by the initialize function. |
| 180 | + * This is awaited by piscina prior to using the Worker. |
| 181 | + */ |
| 182 | +export default initialize(); |
0 commit comments