Skip to content

Commit 82d252a

Browse files
committed
feat: support webpack/nextjs
1 parent 6af0887 commit 82d252a

File tree

12 files changed

+84
-52
lines changed

12 files changed

+84
-52
lines changed

apps/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"name": "apps",
33
"version": "1.0.0",
44
"description": "",
5+
"author": "",
6+
"license": "ISC",
7+
"keywords": [],
58
"main": "index.js",
69
"scripts": {
710
"build": "pnpm run -r build",
811
"extract": "pnpm run -r tw-extract",
912
"preview": "pnpm run -r preview"
1013
},
11-
"keywords": [],
12-
"author": "",
13-
"license": "ISC",
1414
"dependencies": {
1515
"@tailwindcss-mangle/config": "link:../packages/config",
1616
"@tailwindcss-mangle/core": "link:../packages/core",
@@ -19,4 +19,4 @@
1919
"tailwindcss-patch": "link:../packages/tailwindcss-patch",
2020
"unplugin-tailwindcss-mangle": "link:../packages/unplugin-tailwindcss-mangle"
2121
}
22-
}
22+
}

packages/config/package.json

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
"utils"
2020
],
2121
"exports": {
22-
".": "./src/index.ts"
22+
".": {
23+
"types": "./dist/index.d.ts",
24+
"import": "./dist/index.mjs",
25+
"require": "./dist/index.cjs"
26+
}
2327
},
28+
"main": "./dist/index.cjs",
29+
"module": "./dist/index.mjs",
30+
"types": "./dist/index.d.ts",
2431
"typesVersions": {
2532
"*": {
2633
"*": [
@@ -40,17 +47,7 @@
4047
},
4148
"publishConfig": {
4249
"access": "public",
43-
"registry": "https://registry.npmjs.org/",
44-
"exports": {
45-
".": {
46-
"types": "./dist/index.d.ts",
47-
"import": "./dist/index.mjs",
48-
"require": "./dist/index.cjs"
49-
}
50-
},
51-
"main": "./dist/index.cjs",
52-
"module": "./dist/index.mjs",
53-
"types": "./dist/index.d.ts"
50+
"registry": "https://registry.npmjs.org/"
5451
},
5552
"dependencies": {
5653
"@tailwindcss-mangle/shared": "workspace:^",

packages/core/package.json

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
"mangle"
2020
],
2121
"exports": {
22-
".": "./src/index.ts"
22+
".": {
23+
"types": "./dist/index.d.ts",
24+
"import": "./dist/index.mjs",
25+
"require": "./dist/index.cjs"
26+
}
2327
},
28+
"main": "./dist/index.cjs",
29+
"module": "./dist/index.mjs",
30+
"types": "./dist/index.d.ts",
2431
"typesVersions": {
2532
"*": {
2633
"*": [
@@ -41,17 +48,7 @@
4148
},
4249
"publishConfig": {
4350
"access": "public",
44-
"registry": "https://registry.npmjs.org/",
45-
"exports": {
46-
".": {
47-
"types": "./dist/index.d.ts",
48-
"import": "./dist/index.mjs",
49-
"require": "./dist/index.cjs"
50-
}
51-
},
52-
"main": "./dist/index.cjs",
53-
"module": "./dist/index.mjs",
54-
"types": "./dist/index.d.ts"
51+
"registry": "https://registry.npmjs.org/"
5552
},
5653
"dependencies": {
5754
"@ast-core/escape": "^1.0.1",

packages/shared/package.json

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818
"utils"
1919
],
2020
"exports": {
21-
".": "./src/index.ts"
21+
".": {
22+
"types": "./dist/index.d.ts",
23+
"import": "./dist/index.mjs",
24+
"require": "./dist/index.cjs"
25+
}
2226
},
27+
"main": "./dist/index.cjs",
28+
"module": "./dist/index.mjs",
29+
"types": "./dist/index.d.ts",
2330
"typesVersions": {
2431
"*": {
2532
"*": [
@@ -39,17 +46,7 @@
3946
},
4047
"publishConfig": {
4148
"access": "public",
42-
"registry": "https://registry.npmjs.org/",
43-
"exports": {
44-
".": {
45-
"types": "./dist/index.d.ts",
46-
"import": "./dist/index.mjs",
47-
"require": "./dist/index.cjs"
48-
}
49-
},
50-
"main": "./dist/index.cjs",
51-
"module": "./dist/index.mjs",
52-
"types": "./dist/index.d.ts"
49+
"registry": "https://registry.npmjs.org/"
5350
},
5451
"dependencies": {},
5552
"devDependencies": {}

packages/unplugin-tailwindcss-mangle/.prettierignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/unplugin-tailwindcss-mangle/build.config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import path from 'node:path'
22
import { defineBuildConfig } from 'unbuild'
33

44
export default defineBuildConfig({
5-
// entries: ['./src/index', './src/cli'],
5+
entries: ['src/index.ts', 'src/vite.ts', 'src/esbuild.ts', 'src/rollup.ts', 'src/webpack.ts', 'src/utils.ts', 'src/core/loader.ts'],
66
rollup: {
7+
replace: {
8+
values: {
9+
__DEV__: false,
10+
},
11+
},
712
inlineDependencies: true,
813
emitCJS: true,
914
cjsBridge: true,
@@ -16,4 +21,5 @@ export default defineBuildConfig({
1621
'@': path.resolve(__dirname, './src'),
1722
},
1823
declaration: true,
24+
1925
})

packages/unplugin-tailwindcss-mangle/package.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@
4343
"import": "./dist/esbuild.mjs",
4444
"require": "./dist/esbuild.cjs"
4545
},
46-
"./nuxt": {
47-
"types": "./dist/nuxt.d.ts",
48-
"import": "./dist/nuxt.mjs",
49-
"require": "./dist/nuxt.cjs"
50-
},
51-
"./utils": {
52-
"types": "./dist/utils.d.ts",
53-
"import": "./dist/utils.mjs",
54-
"require": "./dist/utils.cjs"
55-
},
5646
"./*": "./*"
5747
},
5848
"main": "./dist/index.cjs",

packages/unplugin-tailwindcss-mangle/src/core/factory.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path'
12
import type { UnpluginFactory } from 'unplugin'
23
import { Context, cssHandler, htmlHandler, jsHandler } from '@tailwindcss-mangle/core'
34
import type { MangleUserConfig } from '@tailwindcss-mangle/config'
@@ -7,6 +8,8 @@ import type { OutputAsset } from 'rollup'
78
import { pluginName } from '@/constants'
89
import { getGroupedEntries } from '@/utils'
910

11+
const WEBPACK_LOADER = path.resolve(__dirname, __DEV__ ? '../../dist/core/loader.cjs' : 'core/loader.cjs')
12+
1013
const factory: UnpluginFactory<MangleUserConfig | undefined> = (options, { framework }) => {
1114
const ctx = new Context()
1215
let filter = (_id: string) => true
@@ -49,6 +52,28 @@ const factory: UnpluginFactory<MangleUserConfig | undefined> = (options, { frame
4952
return htmlHandler(code, opts)
5053
}
5154
},
55+
webpack(compiler) {
56+
const { NormalModule } = compiler.webpack
57+
const isExisted = true
58+
compiler.hooks.compilation.tap(pluginName, (compilation) => {
59+
NormalModule.getCompilationHooks(compilation).loader.tap(pluginName, (_loaderContext, module) => {
60+
if (isExisted) {
61+
const idx = module.loaders.findIndex(x => x.loader.includes('postcss-loader'))
62+
63+
if (idx > -1) {
64+
module.loaders.splice(idx, 0, {
65+
loader: WEBPACK_LOADER,
66+
ident: null,
67+
options: {
68+
ctx,
69+
},
70+
type: null,
71+
})
72+
}
73+
}
74+
})
75+
})
76+
},
5277
},
5378
{
5479
name: `${pluginName}:post`,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { LoaderContext } from 'webpack'
2+
import { type Context, cssHandler } from '@tailwindcss-mangle/core'
3+
4+
const TailwindcssMangleWebpackLoader = async function (this: LoaderContext<{ ctx: Context }>, source: string) {
5+
const callback = this.async()
6+
const { ctx } = this.getOptions()
7+
8+
const { code } = await cssHandler(source, {
9+
ctx,
10+
id: this.resource,
11+
})
12+
13+
callback(null, code)
14+
}
15+
16+
export default TailwindcssMangleWebpackLoader
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const __DEV__: boolean

0 commit comments

Comments
 (0)