tailwindcss/src/lib/handleImportAtRules.js
Robin Malfait f49b054b7d
[Oxide] Add built-in @import processing support (#11239)
* run PostCSS relative to test file

The PostCSS `from` was always set to the `tests/util/run` file with the
current test name as a query param to make it unique. However, that
results in issues with relative file paths because they are now relative
to this file instead of the actual test file.

Luckily, `expect.getState()` exposes the `testPath` value that points to
the current test file itself.

* add built in `@import` processing

+ additional features such that you can use `@tailwind` and `@config`
  directives before the `@import` at rule which isn't normally allowed.
  However "@tailwind" in a way is an `@import` and `@config` in a way is
  a `@charset` which can be used before the `@import` at rule.

* drop partial normalizeTailwindDirectives when in the Oxide engine

This will now be handled by Tailwind from a higher level, so the core
doesn't have to worry about normalizing this anymore since it will
always be normalized right now.

* add `handleImportAtRules` to CLI

* update changelog

* scope `import processing` to the oxide engine

* update Changelog

* use direct comparison

We don't need the `startsWith` check anymore because the contents of the
comment will be the actual value we expect.
2023-05-15 19:29:57 +02:00

33 lines
791 B
JavaScript

import postcss from 'postcss'
const TAILWIND = Symbol()
export function handleImportAtRules(postcssImport = require('postcss-import')) {
let RESTORE_ATRULE_COMMENT = '__TAILWIND_RESTORE__'
let atRulesToRestore = ['tailwind', 'config']
return [
(root) => {
root.walkAtRules((rule) => {
if (!atRulesToRestore.includes(rule.name)) return rule
rule.after(
postcss.comment({
text: RESTORE_ATRULE_COMMENT,
raws: { [TAILWIND]: { rule } },
})
)
rule.remove()
})
},
postcssImport(),
(root) => {
root.walkComments((rule) => {
if (rule.text === RESTORE_ATRULE_COMMENT) {
rule.after(rule.raws[TAILWIND].rule)
rule.remove()
}
})
},
]
}