Skip to content

Commit 09c0686

Browse files
committed
feat: webpack dev mangle fail issue
1 parent 707f746 commit 09c0686

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

packages/unplugin-tailwindcss-mangle/rollup.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export default createRollupConfig({
88
rollup: 'src/rollup.ts',
99
vite: 'src/vite.ts',
1010
webpack: 'src/webpack.ts',
11-
loader: 'src/loader.ts'
11+
'twm-css': 'src/loader/twm-css.ts',
12+
'twm-js': 'src/loader/twm-js.ts'
1213
},
1314
// external: ['tailwindcss'],
1415
output: [

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const outputCachedMap = new Map<
2222
>()
2323

2424
export const unplugin = createUnplugin((options: Options | undefined = {}, meta) => {
25-
const { classGenerator, getCachedClassSet, isInclude, classMapOutputOptions, twPatcher } = getOptions(options)
25+
const { classGenerator, getCachedClassSet, isInclude, classMapOutputOptions } = getOptions(options)
2626

2727
return {
2828
name: pluginName,
@@ -96,30 +96,38 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
9696
}
9797
}
9898

99-
const loader = path.resolve(__dirname, 'loader.js')
100-
99+
const twmCssloader = path.resolve(__dirname, 'twm-css.js')
100+
// const twmJsloader = path.resolve(__dirname, 'twm-js.js')
101101
compiler.hooks.compilation.tap(pluginName, (compilation) => {
102102
NormalModule.getCompilationHooks(compilation).loader.tap(pluginName, (loaderContext, module) => {
103103
const idx = module.loaders.findIndex((x) => x.loader.includes('css-loader'))
104104
// vue-loader/dist/stylePostLoader.
105105
// vue-style-loader
106106
if (idx > -1) {
107107
module.loaders.splice(idx + 1, 0, {
108-
loader,
108+
loader: twmCssloader,
109109
options: {
110110
classGenerator,
111111
getCachedClassSet
112112
},
113113
ident: null,
114114
type: null
115115
})
116-
117-
// console.log(module.resource, module.loaders.map(x => x.loader))
118-
// if(/css/.test(module.resource)){
119-
// const idx = module.loaders.findIndex((x) => x.loader === 'style-loader')
120-
// console.log(idx)
121-
// }
122116
}
117+
118+
// idx = module.loaders.findIndex((x) => x.loader.includes('babel-loader'))
119+
120+
// if (idx > -1) {
121+
// module.loaders.splice(idx + 1, 0, {
122+
// loader: twmJsloader,
123+
// options: {
124+
// classGenerator,
125+
// getCachedClassSet
126+
// },
127+
// ident: null,
128+
// type: null
129+
// })
130+
// }
123131
})
124132
compilation.hooks.processAssets.tap(
125133
{

packages/unplugin-tailwindcss-mangle/src/loader.ts renamed to packages/unplugin-tailwindcss-mangle/src/loader/twm-css.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as webpack from 'webpack'
2-
import ClassGenerator from './classGenerator'
3-
import { cssHandler } from './css'
2+
import ClassGenerator from '../classGenerator'
3+
import { cssHandler } from '../css'
44

5-
export default function loader(
5+
export default function cssloader(
66
this: webpack.LoaderContext<{
77
classGenerator: ClassGenerator
88
getCachedClassSet: (() => Set<string>) | undefined
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as webpack from 'webpack'
2+
import ClassGenerator from '../classGenerator'
3+
import { jsHandler } from '../js'
4+
5+
export default function cssloader(
6+
this: webpack.LoaderContext<{
7+
classGenerator: ClassGenerator
8+
getCachedClassSet: (() => Set<string>) | undefined
9+
}>,
10+
content: string
11+
) {
12+
this.cacheable && this.cacheable()
13+
const opt = this.getOptions()
14+
if (opt.getCachedClassSet) {
15+
const runtimeSet = opt.getCachedClassSet()
16+
const code = jsHandler(content, {
17+
runtimeSet,
18+
classGenerator: opt.classGenerator
19+
}).code
20+
if (code) {
21+
return code
22+
}
23+
}
24+
25+
return content
26+
}

packages/unplugin-tailwindcss-mangle/test/fixtures/webpack-dev-content.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/unplugin-tailwindcss-mangle/test/js.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ describe('js handler', () => {
3232
expect(code).toMatchSnapshot()
3333
})
3434

35+
it('text-[red]', () => {
36+
const classGenerator = new ClassGenerator()
37+
const runtimeSet = new Set<string>()
38+
39+
// eslint-disable-next-line no-template-curly-in-string
40+
const testCase = ''
41+
testCase.split(' ').forEach((x) => {
42+
runtimeSet.add(x)
43+
})
44+
const code = jsHandler(testCase, {
45+
classGenerator,
46+
runtimeSet
47+
}).code
48+
expect(code).toMatchSnapshot()
49+
})
50+
3551
it('z-10 not transform', () => {
3652
const classGenerator = new ClassGenerator()
3753
const runtimeSet = new Set<string>()

0 commit comments

Comments
 (0)