-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpostcss.config.js
61 lines (57 loc) · 2.09 KB
/
postcss.config.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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const platformPostcssConfig = require('@hashicorp/platform-postcss-config')
// Next.js allows for plugins to be specified as an array of arrays, but this
// isn't supported by Vitest, so use a helper function to convert the array of
// arrays to an object.
function standardSyntax(plugins) {
const _plugins = {}
plugins.forEach((p) => {
if (Array.isArray(p)) {
_plugins[p[0]] = p[1]
} else {
_plugins[p] = {}
}
})
return _plugins
}
/**
* TODO: this is a spike to show that this configuration may resolve
* the issues we're having in dev-dot when using @custom-media.
* We likely want to incorporate this into `@hashicorp/platform-postcss-config`.
* Eg, maybe it should accept an optional "options" object, where we can
* pass additional files to `importFrom`?
*/
function alsoImportDevDotCustomMedia(postcssConfig) {
const newPlugins = postcssConfig.plugins.map((plugin) => {
// we only want to modify postcss-preset-env
const isPresetEnv =
Array.isArray(plugin) &&
plugin.length == 2 &&
plugin[0] == 'postcss-preset-env'
if (!isPresetEnv) {
return plugin
}
// we want to modify the postcss-preset-env settings object,
// which we expect as a second part of a tuple in the plugin array entry.
// specifically, we want to add to the "importFrom" setting.
const [presetEnvName, presetEnvOptions] = plugin
const existingImportFrom =
presetEnvOptions.features['custom-media-queries'].importFrom
const alsoImportFrom = require.resolve('./src/styles/custom-media.css')
const newImportFrom = [alsoImportFrom].concat(existingImportFrom)
// tack the newImportFrom on to the existing settings,
// while retaining the rest of the settings
const newPresetEnvSettings = { ...presetEnvOptions }
newPresetEnvSettings.features['custom-media-queries'].importFrom =
newImportFrom
return [presetEnvName, newPresetEnvSettings]
})
// return the modified config
return { ...postcssConfig, plugins: standardSyntax(newPlugins) }
}
module.exports = {
...alsoImportDevDotCustomMedia(platformPostcssConfig),
}