/
postcss.js
288 lines (258 loc) · 9.38 KB
/
postcss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import fs from 'fs'
import path from 'path'
import consola from 'consola'
import { defaults, merge, cloneDeep } from 'lodash'
import createResolver from 'postcss-import-resolver'
import { isPureObject } from '@nuxt/utils'
export const orderPresets = {
cssnanoLast (names) {
const nanoIndex = names.indexOf('cssnano')
if (nanoIndex !== names.length - 1) {
names.push(names.splice(nanoIndex, 1)[0])
}
return names
},
presetEnvLast (names) {
const nanoIndex = names.indexOf('postcss-preset-env')
if (nanoIndex !== names.length - 1) {
names.push(names.splice(nanoIndex, 1)[0])
}
return names
},
presetEnvAndCssnanoLast (names) {
return orderPresets.cssnanoLast(orderPresets.presetEnvLast(names))
}
}
function postcssConfigFileWarning () {
if (postcssConfigFileWarning.executed) {
return
}
consola.warn('Please use `build.postcss` in your nuxt.config.js instead of an external config file. Support for such files will be removed in Nuxt 3 as they remove all defaults set by Nuxt and can cause severe problems with features like alias resolving inside your CSS.')
postcssConfigFileWarning.executed = true
}
export default class PostcssConfig {
constructor (buildContext) {
this.buildContext = buildContext
}
get cssSourceMap () {
return this.buildContext.buildOptions.cssSourceMap
}
get postcssLoaderOptions () {
return this.buildContext.buildOptions.postcss
}
get postcssOptions () {
return this.buildContext.buildOptions.postcss.postcssOptions
}
get postcssImportAlias () {
const alias = { ...this.buildContext.options.alias }
for (const key in alias) {
if (key.startsWith('~')) {
continue
}
const newKey = '~' + key
if (!alias[newKey]) {
alias[newKey] = alias[key]
}
}
return alias
}
/**
* Returns the default PostCSS options used by Nuxt.
* @returns {{ plugins: {"postcss-import": {resolve: Function}, "postcss-preset-env": {}, "postcss-url": {}, cssnano: (boolean|{preset: [string,{minifyFontValues: {removeQuotes: boolean}}]})}, order: string}}
*/
get defaultPostcssOptions () {
const { dev, srcDir, rootDir, modulesDir } = this.buildContext.options
return {
plugins: {
// https://github.com/postcss/postcss-import
'postcss-import': {
resolve: createResolver({
alias: this.postcssImportAlias,
modules: [srcDir, rootDir, ...modulesDir]
})
},
// https://github.com/postcss/postcss-url
'postcss-url': {},
// https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env
'postcss-preset-env': {},
cssnano: dev
? false
: {
preset: ['default', {
// Keep quotes in font values to prevent from HEX conversion
// https://github.com/nuxt/nuxt.js/issues/6306
minifyFontValues: { removeQuotes: false }
}]
}
},
// Array, String or Function
order: 'presetEnvAndCssnanoLast'
}
}
searchConfigFile () {
// Search for postCSS config file and use it if exists
// https://github.com/michael-ciniawsky/postcss-load-config
// TODO: Remove in Nuxt 3
const { srcDir, rootDir } = this.buildContext.options
for (const dir of [srcDir, rootDir]) {
for (const file of [
'postcss.config.js',
'.postcssrc.js',
'.postcssrc',
'.postcssrc.json',
'.postcssrc.yaml'
]) {
const configFile = path.resolve(dir, file)
if (fs.existsSync(configFile)) {
postcssConfigFileWarning()
return configFile
}
}
}
}
configFromFile () {
const loaderConfig = (this.postcssOptions && this.postcssOptions.config) || {}
if (loaderConfig.path) {
consola.warn('`postcss-loader` has been removed `config.path` option, please use `config` instead.')
return {
config: loaderConfig.path
}
}
const postcssConfigFile = this.searchConfigFile()
if (postcssConfigFile) {
return {
config: postcssConfigFile
}
}
}
/**
* Converts the old syntax to the one expected by Nuxt ^2.16.
* - `Array` to `{ plugins: [] }`
* - `Boolean` to `{ plugins: {} }` (default plugins) or `{ plugins: false }` (no plugins)
* - Moves the `preset`, `order` and `plugins` to the inner `postcssOptions` object
* - Does not convert an Array of plugins to an object
* @param postcssOptions
* @returns {{ postcssOptions: { plugins?: unknown, order?: string, preset?: any} }}
*/
normalize (postcssOptions, warnAboutTopLevelDeprecation = true) {
// TODO: Remove in Nuxt 3
if (Array.isArray(postcssOptions)) {
consola.warn('Using an Array as `build.postcss` will be deprecated in Nuxt 3. Please switch to the object' +
' declaration')
return { postcssOptions: { plugins: postcssOptions } }
} else if (typeof postcssOptions === 'boolean') {
consola.warn('Using a Boolean as `build.postcss` will be deprecated in Nuxt 3. Please switch to the object' +
' declaration')
return { postcssOptions: { plugins: postcssOptions ? {} : false } }
} else if (!isPureObject(postcssOptions)) {
return { postcssOptions: {} }
}
if (postcssOptions.postcssOptions && typeof postcssOptions.postcssOptions === 'function') {
const postcssOptionsFn = postcssOptions.postcssOptions
return {
postcssOptions: (loaderContext) => {
const result = this.normalize(postcssOptionsFn(loaderContext), false)
if (result) {
return result.postcssOptions
}
}
}
}
if (!('postcssOptions' in postcssOptions)) {
if (Object.keys(postcssOptions).length > 0 && warnAboutTopLevelDeprecation) {
consola.warn('Using the top-level properties in `build.postcss` will be deprecated in Nuxt 3. Please move ' +
'the settings to `postcss.postcssOptions`')
}
postcssOptions = { postcssOptions }
}
// The plugins and preset are merged, with priority to the inner `postcssOptions`
if (postcssOptions.plugins) {
postcssOptions.postcssOptions.plugins = merge(
postcssOptions.postcssOptions.plugins || {},
postcssOptions.plugins)
delete postcssOptions.plugins
}
// The preset is merged, with priority to the inner `postcssOptions`
if (postcssOptions.preset) {
postcssOptions.postcssOptions.preset = merge(
postcssOptions.preset,
postcssOptions.postcssOptions.preset || {}
)
delete postcssOptions.preset
}
// The `postcssOptions.order` is used only if there is no `postcssOptions.postcssOptions.order`
if (postcssOptions.order) {
postcssOptions.postcssOptions.order = postcssOptions.postcssOptions.order || postcssOptions.order
delete postcssOptions.order
}
return postcssOptions
}
sortPlugins ({ plugins, order }) {
const names = Object.keys(plugins)
if (typeof order === 'string') {
order = orderPresets[order]
}
return typeof order === 'function' ? order(names, orderPresets) : (order || names)
}
/**
* Load plugins from postcssOptions
* @param {{ postcssOptions: {plugins?: unknown, order?: string | function }}} postcssOptions
*/
loadPlugins (postcssOptions) {
const { plugins, order } = postcssOptions.postcssOptions
if (isPureObject(plugins)) {
// Map postcss plugins into instances on object mode once
postcssOptions.postcssOptions.plugins = this.sortPlugins({ plugins, order })
.map((p) => {
const plugin = this.buildContext.nuxt.resolver.requireModule(p, { paths: [__dirname] })
const opts = plugins[p]
if (opts === false) {
return false // Disabled
}
return plugin(opts)
})
.filter(Boolean)
}
}
config () {
/* istanbul ignore if */
if (!this.postcssLoaderOptions) {
return false
}
const postcssOptionsFromFile = this.configFromFile()
if (postcssOptionsFromFile) {
return {
postcssOptions: postcssOptionsFromFile,
sourceMap: this.cssSourceMap
}
}
/**
* Normalized postcss options
* @type {{postcssOptions: {plugins?: unknown, order?: string, preset?: unknown}}}
*/
const postcssOptions = this.normalize(cloneDeep(this.postcssLoaderOptions))
if (Array.isArray(postcssOptions.postcssOptions.plugins)) {
defaults(postcssOptions.postcssOptions.plugins, this.defaultPostcssOptions.plugins)
} else if (typeof postcssOptions.postcssOptions !== 'function') {
// Merge all plugins and use preset for setting up postcss-preset-env
if (postcssOptions.postcssOptions.preset) {
if (!postcssOptions.postcssOptions.plugins) {
postcssOptions.postcssOptions.plugins = {}
}
postcssOptions.postcssOptions.plugins['postcss-preset-env'] = defaults(
postcssOptions.postcssOptions.preset,
this.defaultPostcssOptions.plugins['postcss-preset-env']
)
delete postcssOptions.postcssOptions.preset
}
// Keep the order of default plugins
postcssOptions.postcssOptions = merge({}, this.defaultPostcssOptions, postcssOptions.postcssOptions)
this.loadPlugins(postcssOptions)
}
delete postcssOptions.postcssOptions.order
return {
sourceMap: this.cssSourceMap,
...postcssOptions
}
}
}