Skip to content

Commit

Permalink
✨ inline/load css to style element
Browse files Browse the repository at this point in the history
  • Loading branch information
salamaashoush committed Jul 25, 2023
1 parent 40d7e72 commit 3beb1a3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
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

0 comments on commit 3beb1a3

Please sign in to comment.