|
| 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