Skip to content

Commit

Permalink
feat: add cssMap env
Browse files Browse the repository at this point in the history
  • Loading branch information
northernCold committed May 29, 2024
1 parent 82e4d0a commit 9111162
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { UnpluginFactory } from "unplugin";
import type { UserConfig } from 'vite';
import type { Options, DoReplacer } from "./types";
import path from "path";
import fs from "fs";
import { execSync } from "child_process";
import { createUnplugin } from "unplugin";
import webpack from "webpack";
import { genCSSMap } from "./core/css-map/vue";
import cssReplacer from "./core/replacer/css-replacer";
import templateReplacer from "./core/replacer/template-replacer";
Expand Down Expand Up @@ -50,11 +52,12 @@ export const unpluginFactory: UnpluginFactory<Options> = (options = {}) => {
/[\\/]\.nuxt[\\/]/,
]
);

cssMap = genCSSMap(options.keyword);
return {
name: "unplugin-tailwindcss-shortener",
enforce: "pre",
buildStart() {
cssMap = genCSSMap(options.keyword);
const tailwindConfig = path.resolve(
process.cwd(),
options.tailwindConfig ?? "./tailwind.config.js"
Expand Down Expand Up @@ -93,7 +96,10 @@ export const unpluginFactory: UnpluginFactory<Options> = (options = {}) => {
return null;
},
loadInclude(id: string) {
if (tailwindcssInput && normalizeAbsolutePath(id) === normalizeAbsolutePath(tailwindcssInput))
if (
tailwindcssInput &&
normalizeAbsolutePath(id) === normalizeAbsolutePath(tailwindcssInput)
)
return true;
if (id.includes("?")) return false;
return filter(id);
Expand Down Expand Up @@ -199,10 +205,47 @@ export const unpluginFactory: UnpluginFactory<Options> = (options = {}) => {
},
vite: {
apply: options.apply,
config: (config: UserConfig) => {
return {
define: {
...config.define,
"import.meta.env.TWST_CSS_MAP": JSON.stringify(cssMap),
},
};
},
transformIndexHtml(html) {
return replacer(html, doReplacer);
},
},
webpack(compiler) {
compiler.hooks.environment.tap("unplugin-tailwindcss-shortener-plugin", () => {
const existingDefinePlugin = compiler.options.plugins.find(
(plugin) =>
plugin instanceof webpack.DefinePlugin &&
plugin.definitions["process.env"]
);

const newVariables = {
"process.env": {
TWST_CSS_MAP: JSON.stringify(cssMap),
},
};

if (existingDefinePlugin) {
(existingDefinePlugin as webpack.DefinePlugin).definitions[
"process.env"
] = {
...((existingDefinePlugin as webpack.DefinePlugin).definitions[
"process.env"
] as Record<string, string>),
...newVariables["process.env"],
};
} else {
const definePlugin = new webpack.DefinePlugin(newVariables);
definePlugin.apply(compiler);
}
});
},
};
};

Expand Down

0 comments on commit 9111162

Please sign in to comment.