Skip to content

Support for specifying plugins in Array #17

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
roots: [
'<rootDir>'
],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/?(*.)+(spec|test).+(ts|tsx|js)'
],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest'
}
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"build": "siroc build",
"prepublishOnly": "yarn build",
"dev": "nuxt dev test/fixture",
"dev:legacy": "nuxt dev -c nuxt.config.legacy.ts test/fixture",
"lint": "eslint --ext .ts .",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint"
"test": "yarn build && jest"
},
"dependencies": {
"autoprefixer": "^10.2.5",
Expand All @@ -27,9 +28,13 @@
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "^5.0.0",
"@types/jest": "^26.0.23",
"eslint": "^7.22.0",
"jest": "^27.0.5",
"nuxt": "^2.15.3",
"siroc": "^0.8.0",
"standard-version": "^9.1.1"
"standard-version": "^9.1.1",
"ts-jest": "^27.0.3",
"ts-node": "^10.0.0"
}
}
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ function postcss8Module () {
return [item].concat(arr.filter(el => el !== item))
}
nuxt.options.build.postcss = defu(nuxt.options.build.postcss, {
plugins: {
autoprefixer: {}
},
plugins: Array.isArray(nuxt.options.build.postcss.plugins)
? [
['autoprefixer', {}]
]
: {
autoprefixer: {}
},
order (names) {
names = moveToFirst(names, 'postcss-url')
names = moveToFirst(names, 'postcss-import')
Expand Down
52 changes: 52 additions & 0 deletions test/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const postcss8Module = require('../../dist')

describe('@nuxt/postcss8', () => {
const mockNuxt: any = (() => {
const nuxt: any = {}
nuxt.constructor.version = '2.15.3'
nuxt.options = {
build: {
postcss: {}
}
}

return { nuxt }
})()

describe.each([
[
'plugins are specified by object',
{
pluginA: {},
pluginB: {}
},
{
pluginA: {},
pluginB: {},
autoprefixer: {}
}
],
[
'plugins are specified by array',
[
['pluginA', {}],
['pluginB', {}]
],
[
['autoprefixer', {}],
['pluginA', {}],
['pluginB', {}]
]
]
])('when %s', (_case: string, pluginsInDefaults: object, pluginsExpected: object) => {
describe(`defaults are ${JSON.stringify(pluginsInDefaults)}`, () => {
it(`should overwrite build.postcss.plugins to ${JSON.stringify(pluginsExpected)}`, () => {
mockNuxt.nuxt.options.build.postcss = { plugins: pluginsInDefaults }
mockNuxt.module = postcss8Module
mockNuxt.module()

expect(mockNuxt.nuxt.options.build.postcss.plugins).toStrictEqual(pluginsExpected)
})
})
})
})
7 changes: 7 additions & 0 deletions test/fixture/mock-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

module.exports = () => {
return {
postcssPlugin: 'mock-plugin'
}
}
module.exports.postcss = true
17 changes: 17 additions & 0 deletions test/fixture/nuxt.config.legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import nuxtpostcss8 from '../../src'

export default {
buildModules: [
nuxtpostcss8
],
css: [
'~/css/app.css'
],
build: {
postcss: {
plugins: {
'mock-plugin': require('./mock-plugin')
}
}
}
}
9 changes: 8 additions & 1 deletion test/fixture/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ export default {
],
css: [
'~/css/app.css'
]
],
build: {
postcss: {
plugins: [
require('./mock-plugin')
]
}
}
}
Loading