Skip to content

feat: integrate tailwind-config-viewer #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,6 @@ module.exports = async function (moduleOptions) {
postcss.plugins.tailwindcss = tailwindConfig
}

/*
** Add /_tailwind UI
*/
/* istanbul ignore if */
if (nuxt.options.dev) {
// add layout
const layout = resolve(__dirname, 'ui', 'layouts', 'tw.vue')
this.addLayout(layout, 'tw')
// add page
const page = resolve(__dirname, 'ui', 'pages', 'index.vue')

// register page
this.extendRoutes((routes) => {
routes.unshift({
name: '_tailwind',
path: '/_tailwind',
component: page
})
})
const url = nuxt.server.listeners && nuxt.server.listeners[0] ? nuxt.server.listeners[0].url : '/'
logger.info(`Tailwind Colors available on ${url}_tailwind`)
}
/*
** Expose resolved tailwind config as an alias
** https://tailwindcss.com/docs/configuration/#referencing-in-javascript
Expand Down Expand Up @@ -123,6 +101,18 @@ module.exports = async function (moduleOptions) {
}
}
})

/*
** Add /_tailwind UI
*/
const tailwindConfigViewer = require('./serverMiddleware/tailwindConfigViewer')
tailwindConfigViewer({
path: '_tailwind',
tailwindConfig,
addServerMiddleware: this.addServerMiddleware
})
const url = nuxt.server.listeners && nuxt.server.listeners[0] ? nuxt.server.listeners[0].url : '/'
logger.info(`Tailwind Config Viewer available on ${url}_tailwind/`)
}

module.exports.meta = require('../package.json')
30 changes: 30 additions & 0 deletions lib/serverMiddleware/tailwindConfigViewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const createServer = require('tailwind-config-viewer/server')

module.exports = ({ path, tailwindConfig, addServerMiddleware }) => {
// strip initial and trailing slash from path
path = path.replace(/(^\/|\/$)/g, '').replace(/^\//)

/**
* Force redirect if requested path does not have trailing slash.
* This is required for relative asset paths in the Tailwind
* Config Viewer to load correctly.
*/
addServerMiddleware({
path,
handler: (req, res, next) => {
if (req.originalUrl === `/${path}`) {
res.writeHead(301, { Location: `${path}/` })
res.end()
} else {
next()
}
}
})

addServerMiddleware({
path,
handler: createServer({
tailwindConfigProvider: () => tailwindConfig
}).asMiddleware()
})
}
3 changes: 0 additions & 3 deletions lib/ui/layouts/tw.vue

This file was deleted.

199 changes: 0 additions & 199 deletions lib/ui/pages/index.vue

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"consola": "^2.15.0",
"defu": "^3.2.2",
"fs-extra": "^9.0.1",
"tailwind-config-viewer": "^1.3.1",
"tailwindcss": "^1.9.6"
},
"devDependencies": {
Expand Down
Loading