|
Last change
on this file since cdcff72 was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.3 KB
|
| Line | |
|---|
| 1 | import fs from 'fs'
|
|---|
| 2 | import path from 'path'
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Find the @config at-rule in the given CSS AST and return the relative path to the config file
|
|---|
| 6 | *
|
|---|
| 7 | * @param {import('postcss').Root} root
|
|---|
| 8 | * @param {import('postcss').Result} result
|
|---|
| 9 | */
|
|---|
| 10 | export function findAtConfigPath(root, result) {
|
|---|
| 11 | let configPath = null
|
|---|
| 12 | let relativeTo = null
|
|---|
| 13 |
|
|---|
| 14 | root.walkAtRules('config', (rule) => {
|
|---|
| 15 | relativeTo = rule.source?.input.file ?? result.opts.from ?? null
|
|---|
| 16 |
|
|---|
| 17 | if (relativeTo === null) {
|
|---|
| 18 | throw rule.error(
|
|---|
| 19 | 'The `@config` directive cannot be used without setting `from` in your PostCSS config.'
|
|---|
| 20 | )
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | if (configPath) {
|
|---|
| 24 | throw rule.error('Only one `@config` directive is allowed per file.')
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | let matches = rule.params.match(/(['"])(.*?)\1/)
|
|---|
| 28 | if (!matches) {
|
|---|
| 29 | throw rule.error('A path is required when using the `@config` directive.')
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | let inputPath = matches[2]
|
|---|
| 33 | if (path.isAbsolute(inputPath)) {
|
|---|
| 34 | throw rule.error('The `@config` directive cannot be used with an absolute path.')
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | configPath = path.resolve(path.dirname(relativeTo), inputPath)
|
|---|
| 38 | if (!fs.existsSync(configPath)) {
|
|---|
| 39 | throw rule.error(
|
|---|
| 40 | `The config file at "${inputPath}" does not exist. Make sure the path is correct and the file exists.`
|
|---|
| 41 | )
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | rule.remove()
|
|---|
| 45 | })
|
|---|
| 46 |
|
|---|
| 47 | return configPath ? configPath : null
|
|---|
| 48 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.