Skip to content

Commit c77377f

Browse files
committed
feat(dt): support in <style> tags
1 parent 8542da6 commit c77377f

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@nuxt/kit": "^3.0.0-rc.6",
4747
"browser-style-dictionary": "^3.1.1-browser.1",
4848
"chroma-js": "^2.4.2",
49+
"magic-regexp": "^0.4.1",
4950
"untyped": "^0.4.4"
5051
},
5152
"devDependencies": {

playground/pages/index.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@
4242
const { fetch: fetchTokens } = useTokens()
4343
4444
const { data } = await useAsyncData(fetchTokens)
45-
46-
// eslint-disable-next-line no-console
47-
console.log(data.value)
4845
</script>
4946

5047
<style scoped lang="postcss">
5148
.app {
5249
height: 100vh;
5350
width: 100vw;
51+
background-color: $dt('colors.primary.100');
5452
}
5553
</style>

src/module.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { Nitro } from 'nitropack'
1313
import { join } from 'pathe'
1414
import { generateTokens } from './generate'
1515
import { logger, name, version, NuxtLayer, resolveTokens, MODULE_DEFAULTS, createTokensDir } from './utils'
16+
import transformPlugin from './transform'
1617
import type { NuxtDesignTokens, ModuleOptions } from './index'
1718

1819
export default defineNuxtModule<ModuleOptions>({
@@ -212,5 +213,21 @@ export default defineNuxtModule<ModuleOptions>({
212213
tailwindConfig.content = tailwindConfig.content ?? []
213214
tailwindConfig.content.push(join(tokensDir, 'index.ts'))
214215
})
216+
217+
/**
218+
* Transform plugin for `$dt()` usage in <style> tags
219+
*/
220+
221+
// Webpack plugin
222+
nuxt.hook('webpack:config', (config: any) => {
223+
config.plugins = config.plugins || []
224+
config.plugins.unshift(transformPlugin.webpack())
225+
})
226+
227+
// Vite plugin
228+
nuxt.hook('vite:extend', (vite: any) => {
229+
vite.config.plugins = vite.config.plugins || []
230+
vite.config.plugins.push(transformPlugin.vite())
231+
})
215232
}
216233
})

src/transform.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { regexLiteral } from '@babel/types'
2+
import { kebabCase } from 'scule'
3+
import { createUnplugin } from 'unplugin'
4+
5+
const resolveToken = (path: string): string => `var(--${path.split('.').map(key => kebabCase(key)).join('-')})`
6+
7+
export default createUnplugin(() => ({
8+
name: 'unplugin-starter',
9+
transformInclude (id) {
10+
// Check if the target is a Vue <style> attribute.
11+
const regex = /\?vue&type=style/
12+
return regex.test(id)
13+
},
14+
transform (code) {
15+
const regex = /\$dt\('(.*)'\)/
16+
17+
if (regex.test(code)) {
18+
// Find any usage of `$dt('...')` in code and replace it by `var(--...)`
19+
return code.replace(regex, (_, path) => resolveToken(path))
20+
}
21+
22+
return code
23+
}
24+
}))

yarn.lock

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,11 @@ acorn@^8.5.0, acorn@^8.6.0, acorn@^8.7.0, acorn@^8.7.1:
13921392
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.1.tgz#0197122c843d1bf6d0a5e83220a788f278f63c30"
13931393
integrity sha512-Xx54uLJQZ19lKygFXOWsscKUbsBZW0CPykPhVQdhIeIwrbPmJzqeASDInc8nKBnp/JT6igTs82qPXz069H8I/A==
13941394

1395+
acorn@^8.8.0:
1396+
version "8.8.0"
1397+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.0.tgz#88c0187620435c7f6015803f5539dae05a9dbea8"
1398+
integrity sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==
1399+
13951400
agent-base@6:
13961401
version "6.0.2"
13971402
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
@@ -4712,6 +4717,17 @@ lru-cache@^6.0.0:
47124717
dependencies:
47134718
yallist "^4.0.0"
47144719

4720+
magic-regexp@^0.4.1:
4721+
version "0.4.1"
4722+
resolved "https://registry.yarnpkg.com/magic-regexp/-/magic-regexp-0.4.1.tgz#9805c77984fc2302c333e1cdeb221a9523331594"
4723+
integrity sha512-JNwUo8+mV7yYUz3z6YDac4qhKMKVYOiEMU1DkW+EwG+BADaXCZpdh0wNFk1+7B2wrMGF4V6jrARWBitFZW2gGw==
4724+
dependencies:
4725+
estree-walker "^3.0.1"
4726+
magic-string "^0.26.2"
4727+
mlly "^0.5.7"
4728+
ufo "^0.8.5"
4729+
unplugin "^0.8.1"
4730+
47154731
magic-string@^0.25.7:
47164732
version "0.25.9"
47174733
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c"
@@ -5341,6 +5357,16 @@ mlly@^0.5.2, mlly@^0.5.3, mlly@^0.5.4:
53415357
pathe "^0.3.1"
53425358
pkg-types "^0.3.3"
53435359

5360+
mlly@^0.5.7:
5361+
version "0.5.10"
5362+
resolved "https://registry.yarnpkg.com/mlly/-/mlly-0.5.10.tgz#4d5bdce6f9531392846cfde6257e1a45bc196458"
5363+
integrity sha512-mY6i+bwcgn0XAdZTiiBt6kyoUjLsm3Cuv0T4CchQJcq/UCSUcGPapSxc4g7whtIsUfcsJ2kGqZAdmqCF/VNC/Q==
5364+
dependencies:
5365+
acorn "^8.8.0"
5366+
pathe "^0.3.4"
5367+
pkg-types "^0.3.3"
5368+
ufo "^0.8.5"
5369+
53445370
mri@^1.1.0, mri@^1.2.0:
53455371
version "1.2.0"
53465372
resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b"
@@ -5971,6 +5997,11 @@ pathe@^0.3.0, pathe@^0.3.1, pathe@^0.3.2:
59715997
resolved "https://registry.yarnpkg.com/pathe/-/pathe-0.3.2.tgz#016345ed643027404d7a9ed8d1454ad997a1483a"
59725998
integrity sha512-qhnmX0TOqlCvdWWTkoM83wh5J8fZ2yhbDEc9MlsnAEtEc+JCwxUKEwmd6pkY9hRe6JR1Uecbc14VcAKX2yFSTA==
59735999

6000+
pathe@^0.3.4:
6001+
version "0.3.4"
6002+
resolved "https://registry.yarnpkg.com/pathe/-/pathe-0.3.4.tgz#35bfb1e92373f98d9711cc74e7d6475a0654deae"
6003+
integrity sha512-YWgqEdxf36R6vcsyj0A+yT/rDRPe0wui4J9gRR7T4whjU5Lx/jZOr75ckEgTNaLVQABAwsrlzHRpIKcCdXAQ5A==
6004+
59746005
pathval@^1.1.1:
59756006
version "1.1.1"
59766007
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.1.tgz#8534e77a77ce7ac5a2512ea21e0fdb8fcf6c3d8d"
@@ -7808,6 +7839,16 @@ unplugin@^0.7.2:
78087839
webpack-sources "^3.2.3"
78097840
webpack-virtual-modules "^0.4.4"
78107841

7842+
unplugin@^0.8.1:
7843+
version "0.8.1"
7844+
resolved "https://registry.yarnpkg.com/unplugin/-/unplugin-0.8.1.tgz#4517b6a8ec3d944e838f9c346921d9777cd159e1"
7845+
integrity sha512-o7rUZoPLG1fH4LKinWgb77gDtTE6mw/iry0Pq0Z5UPvZ9+HZ1/4+7fic7t58s8/CGkPrDpGq+RltO+DmswcR4g==
7846+
dependencies:
7847+
acorn "^8.8.0"
7848+
chokidar "^3.5.3"
7849+
webpack-sources "^3.2.3"
7850+
webpack-virtual-modules "^0.4.4"
7851+
78117852
unstorage@^0.4.1:
78127853
version "0.4.1"
78137854
resolved "https://registry.yarnpkg.com/unstorage/-/unstorage-0.4.1.tgz#66d174886f017a5ceef567b8f007c5966b4690f4"

0 commit comments

Comments
 (0)