Skip to content

Commit 2787375

Browse files
committed
faet: support webpack and nextjs ssr
1 parent b9c5954 commit 2787375

File tree

1 file changed

+20
-15
lines changed
  • packages/unplugin-tailwindcss-mangle/src

1 file changed

+20
-15
lines changed

packages/unplugin-tailwindcss-mangle/src/index.ts

+20-15
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import type { sources } from 'webpack'
1212
import path from 'path'
1313
import fs from 'fs'
1414

15-
// const cachedHtmlSource = new Map<string, sources.Source | OutputAsset>()
16-
// const cachedJsSource = new Map<string, sources.Source | OutputChunk>()
17-
// const cachedCssSource = new Map<string, sources.Source | OutputAsset>()
18-
1915
const outputCachedMap = new Map<
2016
string,
2117
{
@@ -94,8 +90,21 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
9490
webpack(compiler) {
9591
const Compilation = compiler.webpack.Compilation
9692
const { ConcatSource } = compiler.webpack.sources
97-
function getEmitAssetPath(outputPath: string, file: string) {
98-
return path.relative(compiler.context, path.resolve(outputPath, file))
93+
function getAssetPath(outputPath: string, file: string, abs: boolean = true) {
94+
const fn = abs ? path.resolve : path.relative
95+
return fn(compiler.context, path.resolve(outputPath, file))
96+
}
97+
98+
function overwriteServerSideAsset(outputPath: string, file: string, data: string) {
99+
const abs = getAssetPath(outputPath, file)
100+
const rel = getAssetPath(outputPath, file, false)
101+
try {
102+
fs.writeFileSync(abs, data, 'utf-8')
103+
console.log('[tailwindcss-mangle]: ' + rel + ' overwrited successfully')
104+
} catch (error) {
105+
console.log('[tailwindcss-mangle]: ' + rel + ' overwrited fail!')
106+
console.log(error)
107+
}
99108
}
100109
compiler.hooks.compilation.tap(pluginName, (compilation) => {
101110
compilation.hooks.processAssets.tap(
@@ -156,8 +165,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
156165
classGenerator,
157166
runtimeSet
158167
})
159-
const source = new ConcatSource(html)
160-
compilation.emitAsset(getEmitAssetPath(key, file), source)
168+
overwriteServerSideAsset(key, file, html)
161169
})
162170
html.clear()
163171
}
@@ -183,11 +191,8 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
183191
runtimeSet,
184192
classGenerator
185193
}).code
186-
// fs.writeFileSync(path.basename(file), rawCode)
187-
// fs.writeFileSync(path.basename(file) + '.out', code)
188-
const source = new ConcatSource(code)
189-
// can not overwrite webpack server side output
190-
compilation.emitAsset(getEmitAssetPath(key, file), source)
194+
195+
overwriteServerSideAsset(key, file, code)
191196
})
192197
js.clear()
193198
}
@@ -212,8 +217,8 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
212217
classGenerator,
213218
runtimeSet
214219
})
215-
const source = new ConcatSource(newCss)
216-
compilation.emitAsset(getEmitAssetPath(key, file), source)
220+
221+
overwriteServerSideAsset(key, file, newCss)
217222
})
218223
css.clear()
219224
}

0 commit comments

Comments
 (0)