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

✨ add option to inline/load css to style element for esbuild plugin #1150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 34 additions & 1 deletion packages/esbuild-plugin-next/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@ import type { Plugin } from 'esbuild';

const vanillaCssNamespace = 'vanilla-extract-css-ns';

async function hashString(str: string) {
const encoder = new TextEncoder();
const data = encoder.encode(str);
const hash = await crypto.subtle.digest('SHA-256', data);
return hash;
}

interface VanillaExtractPluginOptions {
outputCss?: boolean;
runtime?: boolean;
processCss?: (css: string) => Promise<string>;
identifiers?: IdentifierOption;
compilerVitePlugins?: CreateCompilerOptions['vitePlugins'];
useStyleLoader?: boolean;
}
export function vanillaExtractPlugin({
outputCss = true,
runtime = false,
processCss,
identifiers: identOption,
compilerVitePlugins: vitePlugins,
useStyleLoader,
}: VanillaExtractPluginOptions = {}): Plugin {
if (runtime) {
// If using runtime CSS then just apply fileScopes and debug IDs to code
Expand Down Expand Up @@ -60,11 +69,35 @@ export function vanillaExtractPlugin({
if (typeof processCss === 'function') {
css = await processCss(css);
}
const resolveDir = dirname(filePath);

if (useStyleLoader) {
// replace slashes with dashes to make it a valid CSS identifier
const elementId = `vanilla-extract-${
build.initialOptions.minify
? await hashString(filePath)
: filePath.replace(/\//g, '__')
}`;
const contents = `
if (document.getElementById('${elementId}') === null) {
const element = document.createElement('style');
element.id = '${elementId}';
element.textContent = \`${css}\`;
document.head.append(element);
}
export default {};
`;
return {
contents,
loader: 'js',
resolveDir,
};
}

return {
contents: css,
loader: 'css',
resolveDir: dirname(filePath),
resolveDir,
};
},
);
Expand Down
35 changes: 33 additions & 2 deletions packages/esbuild-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import type { Plugin } from 'esbuild';

const vanillaCssNamespace = 'vanilla-extract-css-ns';

async function hashString(str: string) {
const encoder = new TextEncoder();
const data = encoder.encode(str);
const hash = await crypto.subtle.digest('SHA-256', data);
return hash;
}

interface VanillaExtractPluginOptions {
outputCss?: boolean;
/**
Expand All @@ -24,6 +31,7 @@ interface VanillaExtractPluginOptions {
processCss?: (css: string) => Promise<string>;
identifiers?: IdentifierOption;
esbuildOptions?: CompileOptions['esbuildOptions'];
useStyleLoader?: boolean;
}
export function vanillaExtractPlugin({
outputCss,
Expand All @@ -32,6 +40,7 @@ export function vanillaExtractPlugin({
processCss,
identifiers,
esbuildOptions,
useStyleLoader,
}: VanillaExtractPluginOptions = {}): Plugin {
if (runtime) {
// If using runtime CSS then just apply fileScopes and debug IDs to code
Expand All @@ -58,9 +67,31 @@ export function vanillaExtractPlugin({
}

const rootDir = build.initialOptions.absWorkingDir ?? process.cwd();
const filePath = join(rootDir, fileName);
const resolveDir = dirname(filePath);

const resolveDir = dirname(join(rootDir, fileName));

// If using style-loader then we need to return a JS file that inserts the CSS into the DOM
if (useStyleLoader) {
const elementId = `vanilla-extract-${
build.initialOptions.minify
? await hashString(fileName)
: fileName.replace(/[^a-zA-Z0-9-_]/g, '-')
}`;
const contents = `
if (document.getElementById('${elementId}') === null) {
const element = document.createElement('style');
element.id = '${elementId}';
element.textContent = \`${source}\`;
document.head.append(element);
}
export default {};
`;
return {
contents,
loader: 'js',
resolveDir,
};
}
return {
contents: source,
loader: 'css',
Expand Down