Skip to content

Commit

Permalink
fix: upgrade tailwind and remove log
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Aug 8, 2019
1 parent 66b4360 commit c2a5798
Show file tree
Hide file tree
Showing 6 changed files with 1,152 additions and 2,315 deletions.
33 changes: 14 additions & 19 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { join } = require('path')
const { ensureTemplateFile } = require('./utils')
const logger = require('./logger')

module.exports = async function (moduleOptions) {
const options = {
Expand All @@ -10,7 +9,6 @@ module.exports = async function (moduleOptions) {
...moduleOptions
}

const isBuild = this.options._build
const configPath = this.nuxt.resolver.resolveAlias(options.configPath)
const cssPath = this.nuxt.resolver.resolveAlias(options.cssPath)

Expand All @@ -27,26 +25,23 @@ module.exports = async function (moduleOptions) {
this.options.css.unshift(cssPath)
}

/*
** Set PostCSS config
** It has to be set before `nuxt-purgecss`
** only for `nuxt dev` and `nuxt build` commands
*/
if (isBuild) {
logger.info('postcss-preset-env stage is set to 1 for supporting advanced css features')
// This hooks is called only for `nuxt dev` and `nuxt build` commands
this.nuxt.hook('build:before', async () => {
/*
** Set PostCSS config (before nuxt-purgecss)
*/
this.options.build.postcss.preset.stage = 1 // see https://tailwindcss.com/docs/using-with-preprocessors#future-css-features
this.options.build.postcss.plugins = this.options.build.postcss.plugins || {}
this.options.build.postcss.plugins.tailwindcss = this.options.build.postcss.plugins.tailwindcss || configPath
}

/*
** Add nuxt-purgecss module and set config
** only for `nuxt build` command
*/
if (!this.options.dev && isBuild) {
const purgeCSS = { mode: 'postcss', ...(this.options.purgeCSS || {}) }
await this.requireModule(['nuxt-purgecss', purgeCSS])
}
/*
** Add nuxt-purgecss module and set config
** only for `nuxt build` command
*/
if (!this.options.dev) {
const purgeCSS = { mode: 'postcss', ...(this.options.purgeCSS || {}) }
await this.requireModule(['nuxt-purgecss', purgeCSS])
}
})
}

module.exports.meta = require('../package.json')
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { resolve } = require('path')
const { pathExists, copy } = require('fs-extra')
const logger = require('./logger')

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

Expand Down
6 changes: 3 additions & 3 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const { remove } = require('fs-extra')
const { Nuxt, Builder } = require('nuxt-edge')
const request = require('request-promise-native')
const getPort = require('get-port')
const config = require('../example/nuxt.config')
const logger = require('@/logger')

const config = require('../example/nuxt.config')
logger.mockTypes(() => jest.fn())

config.dev = false
config.tailwindcss = {
configPath: 'custom/tailwind.js',
Expand All @@ -19,8 +21,6 @@ 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)
Expand Down
6 changes: 3 additions & 3 deletions test/fail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ require('fs-extra').pathExists = jest.fn().mockImplementation(() => Promise.reso
require('fs-extra').copy = jest.fn().mockImplementation(() => Promise.reject(new Error('Error when copy')))

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

const config = require('../example/nuxt.config')
logger.mockTypes(() => jest.fn())

config.dev = false

let nuxt

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

describe('fail', () => {
beforeAll(async () => {
nuxt = new Nuxt(config)
Expand Down
8 changes: 3 additions & 5 deletions test/ok.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ const { join } = require('path')
const { Nuxt, Builder } = require('nuxt-edge')
const request = require('request-promise-native')
const getPort = require('get-port')
const config = require('../example/nuxt.config')
const logger = require('@/logger')

const config = require('../example/nuxt.config')
logger.mockTypes(() => jest.fn())

config.dev = false
config._build = true // imitate `nuxt build` comman

let nuxt, port

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

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

describe('ok', () => {
beforeAll(async () => {
nuxt = new Nuxt(config)
Expand Down Expand Up @@ -46,7 +45,6 @@ describe('ok', () => {
})

test('build', () => {
expect(logger.info).toHaveBeenCalledWith('postcss-preset-env stage is set to 1 for supporting advanced css features')
expect(nuxt.options.build.postcss.preset.stage).toBe(1)
expect(nuxt.options.build.postcss.plugins).toBeDefined()
expect(nuxt.options.build.postcss.plugins.tailwindcss).toBe(join(nuxt.options.srcDir, 'tailwind.config.js'))
Expand Down
Loading

0 comments on commit c2a5798

Please sign in to comment.