Skip to content

Commit 366ddc3

Browse files
authoredJul 25, 2022
fix(module): Nuxt 2 support (#508)
1 parent e857b4b commit 366ddc3

14 files changed

+8287
-199
lines changed
 

‎nuxt2-playground/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# codesandbox-nuxt-tailwindcss
2+
3+
> Nuxt starter for CodeSandBox with TailwindCSS
4+
5+
## Build Setup
6+
7+
```bash
8+
# install dependencies
9+
$ yarn install
10+
11+
# serve with hot reload at localhost:3000
12+
$ yarn dev
13+
14+
# build for production and launch server
15+
$ yarn build
16+
$ yarn start
17+
18+
# generate static project
19+
$ yarn run generate
20+
```
21+
22+
For detailed explanation on how things work, checkout [Nuxt.js docs](https://nuxtjs.org).
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<button class="btn btn-blue">
3+
<slot>Button</slot>
4+
</button>
5+
</template>
6+
7+
<style lang="postcss" scoped>
8+
.btn {
9+
@apply font-bold py-2 px-4 rounded;
10+
&.btn-blue {
11+
@apply bg-blue-500 text-white;
12+
&:hover {
13+
@apply bg-blue-700;
14+
}
15+
}
16+
}
17+
</style>

‎nuxt2-playground/layouts/default.vue

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<nuxt class="mx-auto p-4" />
3+
</template>
4+
5+
<script>
6+
export default {
7+
head: {
8+
title: 'Nuxt.js with TailwindCSS',
9+
meta: [
10+
{ charset: 'utf-8' },
11+
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
12+
{
13+
hid: 'description',
14+
name: 'description',
15+
content: 'Nuxt.js with TailwindCSS example'
16+
}
17+
],
18+
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
19+
}
20+
}
21+
</script>

‎nuxt2-playground/nuxt.config.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import localModule from '../src/module'
2+
3+
export default {
4+
/*
5+
** Nuxt.js modules
6+
*/
7+
buildModules: [
8+
// Doc: https://github.com/nuxt-community/tailwindcss-module
9+
localModule
10+
]
11+
}

‎nuxt2-playground/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "nuxt-dark-tailwindcss",
3+
"version": "1.0.0",
4+
"description": "Nuxt.js example with @nuxtjs/color-mode and @nuxtjs/tailwindcss module.",
5+
"author": "Atinux",
6+
"private": true,
7+
"scripts": {
8+
"dev": "nuxt",
9+
"build": "nuxt build",
10+
"start": "nuxt start",
11+
"generate": "nuxt generate"
12+
},
13+
"dependencies": {
14+
"nuxt": "2.15.8",
15+
"postcss": "8.4.14"
16+
},
17+
"keywords": []
18+
}

‎nuxt2-playground/pages/index.vue

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div>
3+
<div class="max-w-sm rounded overflow-hidden shadow-lg mb-4">
4+
<div class="px-6 py-4 pb-2">
5+
<div class="text-customcolor">
6+
this text is a custom red color
7+
</div>
8+
</div>
9+
<div class="px-6 py-4">
10+
<span class="badge mr-2">this is red in css using apply</span>
11+
</div>
12+
</div>
13+
</div>
14+
</template>
15+
16+
<style scoped>
17+
.badge {
18+
@apply text-customcolor;
19+
}
20+
</style>

‎nuxt2-playground/sandbox.config.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"infiniteLoopProtection": true,
3+
"hardReloadOnChange": false,
4+
"view": "browser",
5+
"container": {
6+
"node": "14"
7+
}
8+
}

‎nuxt2-playground/tailwind.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
** TailwindCSS Configuration File
3+
**
4+
** Docs: https://tailwindcss.com/docs/configuration
5+
** Default: https://github.com/tailwindcss/tailwindcss/blob/master/stubs/defaultConfig.stub.js
6+
*/
7+
module.exports = {
8+
theme: { extend: { colors: { customcolor: '#FF0000' } } }
9+
}

‎nuxt2-playground/yarn.lock

+8,002
Large diffs are not rendered by default.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"build": "nuxt-module-build",
2020
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
2121
"dev": "nuxt dev playground",
22+
"dev:nuxt2": "nuxt dev nuxt2-playground",
2223
"lint": "eslint --ext .js,.ts,.vue",
2324
"prepack": "yarn build",
2425
"release": "yarn test && standard-version && git push --follow-tags && npm publish",

‎playground/nuxt.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineNuxtConfig } from 'nuxt'
22
import tailwindModule from '../src/module'
33

44
export default defineNuxtConfig({
5-
extends: ['./theme'],
5+
// extends: ['./theme'],
66
modules: [
77
'@nuxt/content',
88
tailwindModule
File renamed without changes.

‎src/module.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
createResolver,
1313
resolvePath,
1414
addVitePlugin,
15-
tryRequireModule
15+
tryRequireModule,
16+
isNuxt3
1617
} from '@nuxt/kit'
1718
import { Config } from 'tailwindcss'
1819
import { name, version } from '../package.json'
@@ -77,14 +78,14 @@ export default defineNuxtModule<ModuleOptions>({
7778
*/
7879
const addConfigPath = async (path: string | string[]) => {
7980
if (typeof path === 'string') {
80-
path = (await resolvePath(path)).split('.').slice(0, -1).join('.')
81+
path = (await resolvePath(path, { extensions: ['.js', '.ts'] })).split('.').slice(0, -1).join('.')
8182
configPaths.push(path)
8283
return
8384
}
8485

8586
if (Array.isArray(path)) {
8687
for (let _path of path) {
87-
_path = (await resolvePath(_path)).split('.').slice(0, -1).join('.')
88+
_path = (await resolvePath(_path, { extensions: ['.js', '.ts'] })).split('.').slice(0, -1).join('.')
8889
configPaths.push()
8990
}
9091
}
@@ -220,6 +221,7 @@ export default defineNuxtModule<ModuleOptions>({
220221
*/
221222

222223
// Add _tailwind config viewer endpoint
224+
// TODO: Fix `addServerHandler` on Nuxt 2 w/o Bridge
223225
if (nuxt.options.dev && moduleOptions.viewer) {
224226
const route = '/_tailwind'
225227
const createServer = await import('tailwind-config-viewer/server/index.js').then(r => r.default || r) as any
@@ -232,7 +234,8 @@ export default defineNuxtModule<ModuleOptions>({
232234
}
233235
_viewerDevMiddleware(req, res)
234236
}
235-
addDevServerHandler({ route, handler: viewerDevMiddleware })
237+
if (isNuxt3()) { addDevServerHandler({ route, handler: viewerDevMiddleware }) }
238+
if (isNuxt2()) { nuxt.options.serverMiddleware.push({ route, handler: viewerDevMiddleware }) }
236239
nuxt.hook('listen', (_, listener) => {
237240
const viewerUrl = `${withoutTrailingSlash(listener.url)}${route}`
238241
logger.info(`Tailwind Viewer: ${chalk.underline.yellow(withTrailingSlash(viewerUrl))}`)

‎yarn.lock

+150-194
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.