Skip to content

Commit

Permalink
refactor: module and increase coverage (#5)
Browse files Browse the repository at this point in the history
* chore: update deps

* style: formatting

* refactor: module refactor

* test: increase coverage
  • Loading branch information
ricardogobbosouza authored and atinux committed Jun 12, 2019
1 parent d807092 commit 43443c2
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 134 deletions.
2 changes: 1 addition & 1 deletion example/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="p-2 bg-indigo-800 items-center text-indigo-100 leading-none lg:rounded-full flex lg:inline-flex" role="alert">
<span class="flex rounded-full bg-indigo-500 uppercase px-2 py-1 text-xs font-bold mr-3">New</span>
<span class="font-semibold mr-2 text-left flex-auto">Get the coolest t-shirts from our brand new store</span>
<svg class="fill-current opacity-75 h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M12.95 10.707l.707-.707L8 4.343 6.586 5.757 10.828 10l-4.242 4.243L8 15.657l4.95-4.95z"/></svg>
<svg class="fill-current opacity-75 h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M12.95 10.707l.707-.707L8 4.343 6.586 5.757 10.828 10l-4.242 4.243L8 15.657l4.95-4.95z" /></svg>
</div>
</div>
</template>
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
collectCoverage: true,
collectCoverageFrom: [
'lib/**/*.js',
'!lib/plugin.js'
'!lib/templates/**'
],
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/lib/$1',
Expand Down
3 changes: 3 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const consola = require('consola')

module.exports = consola.withScope('nuxt:tailwindcss')
26 changes: 13 additions & 13 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
const { resolve } = require('path')
const logger = require('consola').withScope('@nuxtjs/tailwindcss')
const ModuleUtils = require('./utils')
const { join } = require('path')
const { ensureTemplateFile } = require('./utils')
const logger = require('./logger')

module.exports = async function (moduleOptions) {
// eslint-disable-next-line no-unused-vars
const options = {
configPath: 'tailwind.config.js',
cssPath: join(this.options.dir.assets, 'css', 'tailwind.css'),
...this.options.tailwindcss,
...moduleOptions
}

const isBuild = this.options._build
const { srcDir } = Object.assign({}, this.options)
const utils = new ModuleUtils(this.nuxt, __dirname)
const cssPath = this.nuxt.resolver.resolveAlias(options.cssPath || resolve(srcDir, this.options.dir.assets, 'css', 'tailwind.css'))
const configPath = this.nuxt.resolver.resolveAlias(options.configPath || resolve(srcDir, 'tailwind.config.js'))
const configPath = this.nuxt.resolver.resolveAlias(options.configPath)
const cssPath = this.nuxt.resolver.resolveAlias(options.cssPath)

/*
** Generates if not exists:
** - @/assets/css/tailwind.css
** - tailwind.config.js
** - @/assets/css/tailwind.css
*/
const tailwindCSSExists = await utils.ensureTemplateFile('./tailwind.css', cssPath)
await ensureTemplateFile(this.options.srcDir, 'tailwind.config.js', configPath)
const tailwindCSSExists = await ensureTemplateFile(this.options.srcDir, 'tailwind.css', cssPath)

// Include CSS file in project css
if (tailwindCSSExists) {
this.options.css.unshift(cssPath)
}
await utils.ensureTemplateFile('./tailwind.config.js', configPath)

/*
** Set PostCSS config
Expand All @@ -43,8 +44,7 @@ module.exports = async function (moduleOptions) {
** only for `nuxt build` command
*/
if (!this.options.dev && isBuild) {
const defaultPurgeCSS = { mode: 'postcss' }
const purgeCSS = Object.assign(defaultPurgeCSS, this.options.purgeCSS || {})
const purgeCSS = { mode: 'postcss', ...(this.options.purgeCSS || {}) }
await this.requireModule(['nuxt-purgecss', purgeCSS])
}
}
Expand Down
44 changes: 17 additions & 27 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
const { resolve } = require('path')
const { pathExists, copy } = require('fs-extra')
const logger = require('consola').withScope('@nuxtjs/tailwindcss')
const logger = require('./logger')

class ModuleUtils {
constructor(nuxt, cwd) {
this.nuxt = nuxt
this.srcDir = nuxt.options.srcDir
this.cwd = cwd
}
async function ensureTemplateFile(srcDir, from, to) {
const relativePath = to.replace(srcDir, '~')
const fileExists = await pathExists(to)

resolveTemplate(path) {
return resolve(this.cwd, 'templates', path)
if (fileExists) {
return true
}

async ensureTemplateFile(from, to) {
const destPath = resolve(this.srcDir, to)
const relativePath = destPath.replace(this.srcDir, '~')
const fileExists = await pathExists(destPath)

if (fileExists) {
return true
}
try {
// Copy docs: https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md
await copy(this.resolveTemplate(from), destPath)
logger.success(`${relativePath} created`)
return true
} catch (err) {
logger.warn(`Could not create ${relativePath}:`, err.message)
return false
}
try {
// Copy docs: https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md
await copy(resolve(__dirname, 'templates', from), to)
logger.success(`${relativePath} created`)
return true
} catch (err) {
logger.warn(`Could not create ${relativePath}:`, err.message)
return false
}
}

module.exports = ModuleUtils
module.exports = {
ensureTemplateFile
}
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,39 @@
}
],
"main": "lib/module.js",
"repository": "https://github.com/Atinux/nuxt-tailwindcss",
"repository": "https://github.com/nuxt-community/nuxt-tailwindcss",
"publishConfig": {
"access": "public"
},
"scripts": {
"dev": "nuxt example",
"lint": "eslint lib test",
"lint": "eslint --ext .js,.vue example lib test",
"test": "yarn lint && jest",
"release": "yarn test && standard-version && git push --follow-tags && npm publish"
},
"files": [
"lib"
],
"dependencies": {
"fs-extra": "^8.0.1",
"consola": "^2.7.1",
"fs-extra": "^7.0.1",
"nuxt-purgecss": "^0.2.1",
"tailwindcss": "^1.0.4"
},
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@commitlint/cli": "^8.0.0",
"@commitlint/config-conventional": "^8.0.0",
"@babel/core": "latest",
"@babel/preset-env": "latest",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@nuxtjs/eslint-config": "latest",
"babel-eslint": "latest",
"babel-jest": "latest",
"codecov": "^3.5.0",
"codecov": "latest",
"eslint": "latest",
"eslint-config-standard": "latest",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-jest": "^22.6.4",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-import": "latest",
"eslint-plugin-jest": "latest",
"eslint-plugin-node": "latest",
"eslint-plugin-promise": "latest",
"eslint-plugin-standard": "latest",
"eslint-plugin-vue": "latest",
Expand Down
57 changes: 57 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
jest.setTimeout(60000)

const { join } = require('path')
const { remove } = require('fs-extra')
const { Nuxt, Builder } = require('nuxt-edge')
const request = require('request-promise-native')
const getPort = require('get-port')
const logger = require('@/logger')

const config = require('../example/nuxt.config')
config.dev = false
config.tailwindcss = {
configPath: 'custom/tailwind.js',
cssPath: 'custom/tailwind.css'
}

let nuxt, port

const url = path => `http://localhost:${port}${path}`
const get = path => request(url(path))

logger.mockTypes(() => jest.fn())

describe('config', () => {
beforeAll(async () => {
nuxt = new Nuxt(config)
await nuxt.ready()
await new Builder(nuxt).build()
port = await getPort()
await nuxt.listen(port)
})

afterAll(async () => {
await nuxt.close()
await remove(join(nuxt.options.srcDir, 'custom'))
})

beforeEach(() => {
logger.clear()
})

test('render', async () => {
const html = await get('/')
expect(html).toContain('Get the coolest t-shirts from our brand new store')
expect(html).not.toContain('.bg-pink-700')
})

test('custom paths', () => {
expect(logger.success).toHaveBeenNthCalledWith(1, `~/${nuxt.options.tailwindcss.configPath} created`)
expect(logger.success).toHaveBeenNthCalledWith(2, `~/${nuxt.options.tailwindcss.cssPath} created`)
})

test('include custom tailwind.css file in project css', () => {
expect(nuxt.options.css).toHaveLength(1)
expect(nuxt.options.css).toContain(join(nuxt.options.srcDir, nuxt.options.tailwindcss.cssPath))
})
})
35 changes: 35 additions & 0 deletions test/fail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
jest.setTimeout(60000)
require('fs-extra').pathExists = jest.fn().mockImplementation(() => Promise.resolve(false))
require('fs-extra').copy = jest.fn().mockImplementation(() => Promise.reject(new Error('Error when copy')))

const { Nuxt, Builder } = require('nuxt-edge')
const logger = require('@/logger')

const config = require('../example/nuxt.config')
config.dev = false

let nuxt

logger.mockTypes(() => jest.fn())

describe('fail', () => {
beforeAll(async () => {
nuxt = new Nuxt(config)
await nuxt.ready()
await new Builder(nuxt).build()
})

afterAll(async () => {
await nuxt.close()
})

beforeEach(() => {
logger.clear()
})

test('when copy files', () => {
expect(logger.warn).toHaveBeenNthCalledWith(1, `Could not create ~/tailwind.config.js:`, 'Error when copy')
expect(logger.warn).toHaveBeenNthCalledWith(2, `Could not create ~/assets/css/tailwind.css:`, 'Error when copy')
expect(nuxt.options.css).toHaveLength(0)
})
})
39 changes: 0 additions & 39 deletions test/module.config.test.js

This file was deleted.

34 changes: 0 additions & 34 deletions test/module.test.js

This file was deleted.

Loading

0 comments on commit 43443c2

Please sign in to comment.