@devup-ui/webpack-plugin
Version:
Zero Config, Zero FOUC, Zero Runtime, CSS in JS Preprocessor
2 lines (1 loc) • 3.16 kB
JavaScript
import{existsSync as o,mkdirSync as l,readFileSync as n,writeFileSync as r}from"node:fs";import{stat as b,writeFile as M}from"node:fs/promises";import{createRequire as c}from"node:module";import{join as s,resolve as u}from"node:path";import{loadDevupConfigSync as x}from"@devup-ui/plugin-utils";import{getCss as h,getDefaultTheme as y,getThemeInterface as P,importClassMap as f,importFileMap as d,importSheet as g,registerTheme as m,setDebug as C,setPrefix as U}from"@devup-ui/wasm";class D{options;sheetFile;classMapFile;fileMapFile;constructor({package:e="@devup-ui/react",devupFile:t="devup.json",distDir:i="df",cssDir:a=u(i,"devup-ui"),watch:p=!1,debug:v=!1,include:F=[],singleCss:k=!1,prefix:w}={}){this.options={package:e,cssDir:a,devupFile:t,distDir:i,watch:p,debug:v,include:F,singleCss:k,prefix:w},this.sheetFile=s(this.options.distDir,"sheet.json"),this.classMapFile=s(this.options.distDir,"classMap.json"),this.fileMapFile=s(this.options.distDir,"fileMap.json")}writeDataFiles(){try{let t=x(this.options.devupFile).theme??{};m(t);let i=P(this.options.package,"CustomColors","DevupThemeTypography","DevupTheme");if(i)r(s(this.options.distDir,"theme.d.ts"),i,{encoding:"utf-8"})}catch(e){console.error(e),m({})}if(!o(this.options.cssDir))l(this.options.cssDir,{recursive:!0});if(this.options.watch)r(s(this.options.cssDir,"devup-ui.css"),h(null,!1))}apply(e){if(C(this.options.debug),this.options.prefix)U(this.options.prefix);let t=o(this.options.devupFile);if(!o(this.options.distDir))l(this.options.distDir,{recursive:!0});if(r(s(this.options.distDir,".gitignore"),"*","utf-8"),this.options.watch)try{if(o(this.sheetFile))g(JSON.parse(n(this.sheetFile,"utf-8")));if(o(this.classMapFile))f(JSON.parse(n(this.classMapFile,"utf-8")));if(o(this.fileMapFile))d(JSON.parse(n(this.fileMapFile,"utf-8")))}catch(i){console.error(i),g({}),f({}),d({})}if(this.writeDataFiles(),this.options.watch){let i=null;e.hooks.watchRun.tapPromise("DevupUIWebpackPlugin",async()=>{if(t){let p=(await b(this.options.devupFile)).mtimeMs;if(i&&i!==p)this.writeDataFiles();i=p}})}if(t)e.hooks.afterCompile.tap("DevupUIWebpackPlugin",(i)=>{i.fileDependencies.add(u(this.options.devupFile))});if(e.options.plugins.push(new e.webpack.DefinePlugin({"process.env.DEVUP_UI_DEFAULT_THEME":JSON.stringify(y())})),!this.options.watch)e.hooks.done.tapPromise("DevupUIWebpackPlugin",async(i)=>{if(!i.hasErrors())await M(s(this.options.cssDir,"devup-ui.css"),h(null,!1),"utf-8")});e.options.module.rules.push({test:/\.(tsx|ts|js|mjs|jsx)$/,exclude:new RegExp(`(node_modules(?!.*(${["@devup-ui",...this.options.include].join("|").replaceAll("/","[\\/\\\\_]")})([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)`),enforce:"pre",use:[{loader:c(import.meta.url).resolve("@devup-ui/webpack-plugin/loader"),options:{package:this.options.package,cssDir:this.options.cssDir,sheetFile:this.sheetFile,classMapFile:this.classMapFile,fileMapFile:this.fileMapFile,watch:this.options.watch,singleCss:this.options.singleCss}}]},{test:this.options.cssDir,enforce:"pre",use:[{loader:c(import.meta.url).resolve("@devup-ui/webpack-plugin/css-loader"),options:{watch:this.options.watch}}]})}}export{D as DevupUIWebpackPlugin};