Skip to content

Commit 477e314

Browse files
committed
fix: atomic-wind tailwind overriding core-specific styles [fixes #271]
1 parent 9438c68 commit 477e314

4 files changed

Lines changed: 449 additions & 42 deletions

File tree

inc/plugins/class-atomic-wind-blocks.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function enqueue_tailwind_generator() {
137137
return;
138138
}
139139

140-
$asset_file = $this->build_path() . '/tailwind-generator.asset.php';
140+
$asset_file = $this->build_path() . '/tailwind-generator-editor.asset.php';
141141

142142
if ( ! file_exists( $asset_file ) ) {
143143
return;
@@ -147,7 +147,7 @@ public function enqueue_tailwind_generator() {
147147

148148
wp_enqueue_script(
149149
'atomic-wind-tailwind-generator',
150-
$this->plugin_url( 'build/atomic-wind/tailwind-generator.js' ),
150+
$this->plugin_url( 'build/atomic-wind/tailwind-generator-editor.js' ),
151151
$asset['dependencies'],
152152
$asset['version'],
153153
true
@@ -273,7 +273,7 @@ public function output_cached_css() {
273273
return;
274274
}
275275

276-
$generator_asset = $this->build_path() . '/tailwind-generator.asset.php';
276+
$generator_asset = $this->build_path() . '/tailwind-generator-frontend.asset.php';
277277

278278
if ( ! file_exists( $generator_asset ) ) {
279279
return;
@@ -282,7 +282,7 @@ public function output_cached_css() {
282282
$gen = include $generator_asset;
283283
wp_enqueue_script(
284284
'atomic-wind-tailwind-generator',
285-
$this->plugin_url( 'build/atomic-wind/tailwind-generator.js' ),
285+
$this->plugin_url( 'build/atomic-wind/tailwind-generator-frontend.js' ),
286286
$gen['dependencies'],
287287
$gen['version'],
288288
true
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// eslint-disable-next-line import/no-unresolved
2+
import * as tailwindcss from 'tailwindcss';
3+
import indexCSS from 'tailwindcss/index.css';
4+
import preflightCSS from 'tailwindcss/preflight.css';
5+
import themeCSS from 'tailwindcss/theme.css';
6+
import utilitiesCSS from 'tailwindcss/utilities.css';
7+
8+
const assets = {
9+
index: indexCSS,
10+
preflight: preflightCSS,
11+
theme: themeCSS,
12+
utilities: utilitiesCSS,
13+
};
14+
15+
const stylesheetMap = {
16+
tailwindcss: { path: 'virtual:tailwindcss/index.css', content: assets.index },
17+
'tailwindcss/preflight': { path: 'virtual:tailwindcss/preflight.css', content: assets.preflight },
18+
'tailwindcss/preflight.css': { path: 'virtual:tailwindcss/preflight.css', content: assets.preflight },
19+
'./preflight.css': { path: 'virtual:tailwindcss/preflight.css', content: assets.preflight },
20+
'tailwindcss/theme': { path: 'virtual:tailwindcss/theme.css', content: assets.theme },
21+
'tailwindcss/theme.css': { path: 'virtual:tailwindcss/theme.css', content: assets.theme },
22+
'./theme.css': { path: 'virtual:tailwindcss/theme.css', content: assets.theme },
23+
'tailwindcss/utilities': { path: 'virtual:tailwindcss/utilities.css', content: assets.utilities },
24+
'tailwindcss/utilities.css': { path: 'virtual:tailwindcss/utilities.css', content: assets.utilities },
25+
'./utilities.css': { path: 'virtual:tailwindcss/utilities.css', content: assets.utilities },
26+
};
27+
28+
let compiler;
29+
const classes = new Set();
30+
let buildQueue = Promise.resolve();
31+
const sheet = document.createElement( 'style' );
32+
sheet.id = 'atomic-wind-tailwind';
33+
34+
async function loadStylesheet( id, base ) {
35+
const entry = stylesheetMap[ id ];
36+
if ( ! entry ) {
37+
throw new Error( `Unsupported @import "${ id }"` );
38+
}
39+
return { path: entry.path, base, content: entry.content };
40+
}
41+
42+
async function loadModule() {
43+
throw new Error( 'Plugins and config files are not supported in the browser build.' );
44+
}
45+
46+
async function createCompiler() {
47+
compiler = await tailwindcss.compile(
48+
'@import "tailwindcss" important;\n',
49+
{ base: '/', loadStylesheet, loadModule }
50+
);
51+
classes.clear();
52+
}
53+
54+
async function build( kind ) {
55+
if ( ! compiler ) {
56+
return;
57+
}
58+
59+
const newClasses = new Set();
60+
61+
for ( const el of document.querySelectorAll( '[class]' ) ) {
62+
for ( const c of el.classList ) {
63+
if ( ! classes.has( c ) ) {
64+
classes.add( c );
65+
newClasses.add( c );
66+
}
67+
}
68+
}
69+
70+
if ( newClasses.size === 0 && kind === 'incremental' ) {
71+
return;
72+
}
73+
74+
sheet.textContent = compiler.build( Array.from( newClasses ) );
75+
}
76+
77+
function rebuild( kind ) {
78+
buildQueue = buildQueue
79+
.then( async () => {
80+
if ( kind === 'full' ) {
81+
await createCompiler();
82+
}
83+
await build( kind );
84+
} )
85+
.catch( console.error );
86+
}
87+
88+
new MutationObserver( ( records ) => {
89+
const full = 0;
90+
let incremental = 0;
91+
92+
for ( const record of records ) {
93+
for ( const node of record.addedNodes ) {
94+
if ( node.nodeType !== Node.ELEMENT_NODE ) {
95+
continue;
96+
}
97+
incremental++;
98+
}
99+
100+
if ( record.type === 'attributes' ) {
101+
incremental++;
102+
}
103+
}
104+
105+
if ( full > 0 ) {
106+
rebuild( 'full' );
107+
} else if ( incremental > 0 ) {
108+
rebuild( 'incremental' );
109+
}
110+
} ).observe( document.documentElement, {
111+
attributes: true,
112+
attributeFilter: [ 'class' ],
113+
childList: true,
114+
subtree: true,
115+
} );
116+
117+
document.addEventListener( 'DOMContentLoaded', () => {
118+
buildQueue = buildQueue
119+
.then( async () => {
120+
await createCompiler();
121+
await build( 'full' );
122+
document.head.append( sheet );
123+
document.dispatchEvent( new CustomEvent( 'atomic-wind:css-ready' ) );
124+
} )
125+
.catch( console.error );
126+
} );

0 commit comments

Comments
 (0)