Skip to content

Commit 3a61eed

Browse files
authored
feat: exposeConfig to reference resolved config in javascript runtime (#69)
* feat: exposeConfig to access resolved config in runtime fixes #62 * chore: update docs * chore: update docs * chore: improve module comments * perf: force chunk creation for long term caching * chore: import config in example index page * chore: update docs * chore: fix test
1 parent f711f42 commit 3a61eed

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ If you want to set a different path for the configuration file or css file, you
5555
tailwindcss: {
5656
configPath: '~/config/tailwind.config.js',
5757
cssPath: '~/assets/css/tailwind.css',
58-
purgeCSSInDev: false
58+
purgeCSSInDev: false,
59+
exposeConfig: false
5960
}
6061
}
6162
```
@@ -83,6 +84,33 @@ You can also use CSS comments to tell purgeCSS to ignore some blocks:
8384
/* purgecss end ignore */
8485
```
8586

87+
## Referencing in JavaScript
88+
89+
It can often be useful to reference tailwind configuration values in runtime. For example to access some of your theme values when dynamically applying inline styles in a component.
90+
91+
If you need resolved tailwind config in runtime, you can enable `exposeConfig` option in `nuxt.config`:
92+
93+
```js
94+
// nuxt.config.js
95+
{
96+
tailwindcss: {
97+
exposeConfig: true
98+
},
99+
}
100+
```
101+
102+
Then import where needed from `~tailwind.config`:
103+
104+
```js
105+
// Import fully resolved config
106+
import tailwindConfig from '~tailwind.config'
107+
108+
// Import only part which is required to allow tree-shaking
109+
import { theme } from '~tailwind.config'
110+
```
111+
112+
**NOTE:** Please be aware this adds **~19.5KB (~3.5KB)** to the client bundle size.
113+
86114
## Development
87115

88116
1. Clone this repository

example/nuxt.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module.exports = {
55
buildDir: resolve(__dirname, '.nuxt'),
66
srcDir: __dirname,
77
modules: [
8-
{ handler: require('../') }
8+
[require('../'), { exposeConfig: true }]
99
]
1010
}

example/pages/index.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
</div>
99
</template>
1010

11+
<script>
12+
import tailwindConfig from '~tailwind.config'
13+
14+
global.tailwindConfig = tailwindConfig
15+
16+
export default {}
17+
</script>
18+
1119
<style scoped>
1220
.banner {
1321
@apply bg-indigo-900 text-center py-4;

lib/module.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const { join } = require('path')
1+
const { join, resolve } = require('path')
22
const { ensureTemplateFile } = require('./utils')
33

44
module.exports = async function (moduleOptions) {
55
const options = {
66
configPath: 'tailwind.config.js',
77
cssPath: join(this.options.dir.assets, 'css', 'tailwind.css'),
88
purgeCSSInDev: false,
9+
exposeConfig: false,
910
...this.options.tailwindcss,
1011
...moduleOptions
1112
}
@@ -55,6 +56,37 @@ module.exports = async function (moduleOptions) {
5556
}
5657
await this.requireModule(['nuxt-purgecss', purgeCSS])
5758
}
59+
60+
/*
61+
** Expose resolved tailwind config as an alias
62+
** https://tailwindcss.com/docs/configuration/#referencing-in-javascript
63+
*/
64+
if (options.exposeConfig) {
65+
// Resolve config
66+
const tailwindConfig = require(configPath)
67+
const resolveConfig = require('tailwindcss/resolveConfig')
68+
const resolvedConfig = resolveConfig(tailwindConfig)
69+
70+
// Render as a json file in buildDir
71+
this.addTemplate({
72+
src: resolve(__dirname, 'templates/tailwind.config.json'),
73+
fileName: 'tailwind.config.json',
74+
options: { config: resolvedConfig }
75+
})
76+
77+
// Alias to ~tailwind.config
78+
this.options.alias['~tailwind.config'] =
79+
resolve(this.options.buildDir, 'tailwind.config.json')
80+
81+
// Force chunk creation for long term caching
82+
const { cacheGroups } = this.options.build.optimization.splitChunks
83+
cacheGroups.tailwindConfig = {
84+
test: /tailwind\.config/,
85+
chunks: 'all',
86+
priority: 10,
87+
name: true
88+
}
89+
}
5890
})
5991
}
6092

lib/templates/tailwind.config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= JSON.stringify(options.config, null, 2) %>

0 commit comments

Comments
 (0)