@nera-static/stan 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,159 @@
1
+ import { existsSync } from 'node:fs'
2
+ import path from 'node:path'
3
+
4
+ import comments from '@eslint-community/eslint-plugin-eslint-comments'
5
+ import js from '@eslint/js'
6
+ import { isPlainObject } from '@nera-static/plugin-utils/values'
7
+ import stylistic from '@stylistic/eslint-plugin'
8
+ import { includeIgnoreFile } from 'eslint/config'
9
+ import n from 'eslint-plugin-n'
10
+ import globals from 'globals'
11
+
12
+ import { HELPER_REPOS, helperNames } from '../helper-names.js'
13
+ import { kindOf, readPackage } from '../kind.js'
14
+ import { entryFile, publishedPatterns } from '../repo-files.js'
15
+ import nera from '../rules/index.js'
16
+ import { TEST_UTILS, testHelperNames } from '../test-helper-names.js'
17
+
18
+ /** The package.json key that holds a repo's exceptions (D4). */
19
+ export const CONFIG_KEY = 'nera-stan'
20
+
21
+ /** Paths nera-stan never checks, in any repo (D4). */
22
+ export const ALWAYS_IGNORED = Object.freeze([
23
+ '**/node_modules/',
24
+ 'public/',
25
+ 'dist/',
26
+ 'coverage/',
27
+ '**/.nera-cache/',
28
+ '**/test/site/public/',
29
+ ])
30
+
31
+ const SCRIPTS = '**/*.{js,mjs,cjs}'
32
+ const BROWSER_SCRIPTS = '**/assets/**/*.{js,mjs}'
33
+ const VIEW_SCRIPTS = ['views/**/*.{js,mjs}']
34
+ const TESTS = ['test/**/*.{js,mjs,cjs}', '**/*.test.{js,mjs,cjs}']
35
+
36
+ /**
37
+ * Build the flat ESLint config nera-stan checks a repo with (D3, D4).
38
+ *
39
+ * @param {object} [options]
40
+ * @param {string} [options.root=process.cwd()] - The checked repo's root directory.
41
+ * @param {object} [options.pkg] - The repo's parsed `package.json`; read from `root` when absent.
42
+ * @param {Record<string, string>} [options.helpers] - The plugin-utils export names and their import
43
+ * specifiers, for `nera/no-local-helper`; collected from nera-stan's and the repo's copies when absent.
44
+ * @param {Record<string, string>} [options.testHelpers] - The test-utils helper names and their import
45
+ * specifiers, for `nera/test-helpers`; the shipped list and the repo's installed copy when absent.
46
+ * @returns {object[]} The flat config array.
47
+ * @throws {Error} When `package.json` is missing or invalid, or its `nera-stan` key is not
48
+ * an object whose `ignores` is a list of globs.
49
+ */
50
+ export function createConfig({
51
+ root = process.cwd(),
52
+ pkg = readPackage(root),
53
+ helpers = helperNames({ root }),
54
+ testHelpers = testHelperNames({ root }),
55
+ } = {}) {
56
+ const gitignore = path.join(root, '.gitignore')
57
+
58
+ return [
59
+ { name: 'nera-stan/ignores', ignores: [...ALWAYS_IGNORED] },
60
+ ...(existsSync(gitignore)
61
+ ? [includeIgnoreFile(gitignore, { name: 'nera-stan/gitignore', gitignoreResolution: true })]
62
+ : []),
63
+ ...repoIgnores(pkg, root),
64
+ { name: 'nera-stan/recommended', files: [SCRIPTS], ...js.configs.recommended },
65
+ {
66
+ name: 'nera-stan/base',
67
+ files: [SCRIPTS],
68
+ languageOptions: {
69
+ ecmaVersion: 'latest',
70
+ sourceType: 'module',
71
+ globals: globals.node,
72
+ },
73
+ linterOptions: { reportUnusedDisableDirectives: 'error' },
74
+ plugins: { '@stylistic': stylistic, '@eslint-community/eslint-comments': comments, n, nera },
75
+ settings: {
76
+ nera: {
77
+ kind: kindOf(pkg),
78
+ name: pkg.name,
79
+ root,
80
+ entry: entryFile(pkg),
81
+ published: publishedPatterns(pkg),
82
+ helpers,
83
+ testHelpers,
84
+ },
85
+ },
86
+ rules: {
87
+ '@stylistic/indent': ['error', 4],
88
+ '@stylistic/semi': ['error', 'never'],
89
+ '@stylistic/quotes': ['error', 'single', { avoidEscape: true }],
90
+ '@stylistic/comma-dangle': ['error', 'always-multiline'],
91
+ '@stylistic/arrow-parens': ['error', 'always'],
92
+ '@stylistic/eol-last': ['error', 'always'],
93
+ '@stylistic/no-trailing-spaces': 'error',
94
+ '@stylistic/max-len': ['error', {
95
+ code: 120,
96
+ ignoreStrings: true,
97
+ ignoreUrls: true,
98
+ ignoreComments: true,
99
+ }],
100
+ // ESLint's own copy: @stylistic 6 deprecated its fork for `list-style`, which also enforces
101
+ // line breaks inside brackets, and warns on stderr on every run.
102
+ 'object-curly-spacing': ['error', 'always'],
103
+ 'prefer-const': 'error',
104
+ 'no-var': 'error',
105
+ 'no-unused-vars': 'error',
106
+ 'n/prefer-node-protocol': 'error',
107
+ // An inline exception says why: `// eslint-disable-next-line no-var -- <reason>` (D4).
108
+ '@eslint-community/eslint-comments/require-description': 'error',
109
+ 'nera/no-local-helper': HELPER_REPOS.includes(pkg.name) ? 'off' : 'error',
110
+ 'nera/shared-logger': 'error',
111
+ 'nera/no-copied-from': 'error',
112
+ 'nera/hook-shape': 'error',
113
+ 'nera/test-no-chdir': 'error',
114
+ 'nera/test-no-cwd-path': 'error',
115
+ 'nera/test-name': 'error',
116
+ 'nera/test-helpers': pkg.name === TEST_UTILS ? 'off' : 'error',
117
+ },
118
+ },
119
+ {
120
+ // Browser scripts cannot import plugin-utils without a bundler.
121
+ name: 'nera-stan/browser-helpers',
122
+ files: [BROWSER_SCRIPTS, ...VIEW_SCRIPTS],
123
+ rules: { 'nera/no-local-helper': 'off' },
124
+ },
125
+ {
126
+ name: 'nera-stan/commonjs',
127
+ files: ['**/*.cjs'],
128
+ languageOptions: { sourceType: 'commonjs' },
129
+ },
130
+ {
131
+ // A plugin's `views/*.js` is shipped to the site and runs in the visitor's browser.
132
+ name: 'nera-stan/browser',
133
+ files: [BROWSER_SCRIPTS, ...VIEW_SCRIPTS],
134
+ languageOptions: { globals: globals.browser },
135
+ },
136
+ {
137
+ name: 'nera-stan/tests',
138
+ files: TESTS,
139
+ languageOptions: { globals: { ...globals.node, ...globals.vitest } },
140
+ },
141
+ ]
142
+ }
143
+
144
+ function repoIgnores(pkg, root) {
145
+ const own = pkg[CONFIG_KEY]
146
+ if (own === undefined) {
147
+ return []
148
+ }
149
+
150
+ const ignores = isPlainObject(own) ? own.ignores ?? [] : undefined
151
+ const valid = Array.isArray(ignores) && ignores.every((glob) => typeof glob === 'string' && glob !== '')
152
+ if (!valid) {
153
+ throw new Error(
154
+ `${path.join(root, 'package.json')}: "${CONFIG_KEY}" must be an object whose "ignores" is a list ` +
155
+ `of globs relative to the repo root, e.g. { "${CONFIG_KEY}": { "ignores": ["test/markup/"] } }`,
156
+ )
157
+ }
158
+ return ignores.length > 0 ? [{ name: 'nera-stan/package-ignores', ignores: [...ignores] }] : []
159
+ }
@@ -0,0 +1,159 @@
1
+ import { existsSync, readFileSync } from 'node:fs'
2
+ import { createRequire } from 'node:module'
3
+ import path from 'node:path'
4
+
5
+ import { isPlainObject } from '@nera-static/plugin-utils/values'
6
+ import * as espree from 'espree'
7
+
8
+ /** The package whose exports `nera/no-local-helper` protects (D6). */
9
+ export const PLUGIN_UTILS = '@nera-static/plugin-utils'
10
+
11
+ /** The repos that define the helpers themselves, where `nera/no-local-helper` is off (D6). */
12
+ export const HELPER_REPOS = Object.freeze([PLUGIN_UTILS, '@nera-static/test-utils'])
13
+
14
+ const SCRIPT = /\.(?:js|mjs|cjs)$/
15
+ const PARSE_OPTIONS = Object.freeze({ ecmaVersion: 'latest', sourceType: 'module' })
16
+
17
+ /**
18
+ * The helper names `nera/no-local-helper` knows: the union of the exports of
19
+ * nera-stan's own `@nera-static/plugin-utils` and of the copy installed in the
20
+ * checked repo's `node_modules` (D6). Files are parsed, never imported.
21
+ *
22
+ * @param {object} options
23
+ * @param {string} options.root - The checked repo's root directory.
24
+ * @param {string|null} [options.own] - The directory of nera-stan's own plugin-utils copy;
25
+ * `null` leaves it out.
26
+ * @returns {Record<string, string>} Each export name mapped to the import specifier that
27
+ * provides it; the checked repo's copy wins where the two disagree.
28
+ */
29
+ export function helperNames({ root, own = ownPluginUtils() }) {
30
+ const copies = [own, path.join(root, 'node_modules', ...PLUGIN_UTILS.split('/'))]
31
+ return Object.assign({}, ...copies
32
+ .filter((copy) => copy && existsSync(path.join(copy, 'package.json')))
33
+ .map(packageExports))
34
+ }
35
+
36
+ /**
37
+ * The directory of the `@nera-static/plugin-utils` nera-stan itself resolves.
38
+ *
39
+ * @returns {string|null} The directory, or `null` when it cannot be resolved.
40
+ */
41
+ export function ownPluginUtils() {
42
+ let entry
43
+ try {
44
+ entry = createRequire(import.meta.url).resolve(PLUGIN_UTILS)
45
+ } catch {
46
+ return null
47
+ }
48
+ for (let dir = path.dirname(entry); dir !== path.dirname(dir); dir = path.dirname(dir)) {
49
+ if (readManifest(dir)?.name === PLUGIN_UTILS) {
50
+ return dir
51
+ }
52
+ }
53
+ return null
54
+ }
55
+
56
+ /**
57
+ * The named exports of the package at `dir`, by walking its `exports` map (or `main`)
58
+ * and parsing each target file with espree.
59
+ *
60
+ * @param {string} dir - The package directory.
61
+ * @returns {Record<string, string>} Each export name mapped to its import specifier; a name
62
+ * a subpath exports wins over the root's.
63
+ */
64
+ export function packageExports(dir) {
65
+ const pkg = readManifest(dir) ?? {}
66
+ const name = typeof pkg.name === 'string' ? pkg.name : PLUGIN_UTILS
67
+ const exports = {}
68
+ for (const [subpath, target] of entryPoints(pkg)) {
69
+ const specifier = subpath === '.' ? name : `${name}/${subpath.slice(2)}`
70
+ for (const exported of exportedNames(path.resolve(dir, target), new Set())) {
71
+ if (!exports[exported] || subpath !== '.') {
72
+ exports[exported] = specifier
73
+ }
74
+ }
75
+ }
76
+ return exports
77
+ }
78
+
79
+ function readManifest(dir) {
80
+ try {
81
+ return JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'))
82
+ } catch {
83
+ return null
84
+ }
85
+ }
86
+
87
+ /** `[subpath, file]` pairs of the `exports` map (or `main`), wildcard subpaths skipped. */
88
+ function entryPoints(pkg) {
89
+ const { exports } = pkg
90
+ if (exports === undefined || exports === null) {
91
+ return [['.', typeof pkg.main === 'string' ? pkg.main : 'index.js']]
92
+ }
93
+ const map = typeof exports === 'string' || Array.isArray(exports) || !hasSubpaths(exports)
94
+ ? { '.': exports }
95
+ : exports
96
+ return Object.entries(map)
97
+ .filter(([subpath]) => !subpath.includes('*'))
98
+ .flatMap(([subpath, target]) => targets(target).map((file) => [subpath, file]))
99
+ }
100
+
101
+ function hasSubpaths(exports) {
102
+ return Object.keys(exports).some((key) => key.startsWith('.'))
103
+ }
104
+
105
+ /** The script files a target names: a string, a fallback array or a (nested) conditions object. */
106
+ function targets(target) {
107
+ if (typeof target === 'string') {
108
+ return SCRIPT.test(target) ? [target] : []
109
+ }
110
+ if (Array.isArray(target)) {
111
+ return target.flatMap(targets)
112
+ }
113
+ if (isPlainObject(target)) {
114
+ return Object.entries(target)
115
+ .filter(([condition]) => condition !== 'types')
116
+ .flatMap(([, nested]) => targets(nested))
117
+ }
118
+ return []
119
+ }
120
+
121
+ /** The names `file` exports, following `export * from` into relative files. */
122
+ function exportedNames(file, seen) {
123
+ if (seen.has(file)) {
124
+ return []
125
+ }
126
+ seen.add(file)
127
+
128
+ let ast
129
+ try {
130
+ ast = espree.parse(readFileSync(file, 'utf8'), PARSE_OPTIONS)
131
+ } catch {
132
+ return []
133
+ }
134
+
135
+ return ast.body.flatMap((node) => {
136
+ if (node.type === 'ExportNamedDeclaration') {
137
+ return node.declaration ? declaredNames(node.declaration) : node.specifiers.map((s) => nameOf(s.exported))
138
+ }
139
+ if (node.type === 'ExportAllDeclaration') {
140
+ if (node.exported) {
141
+ return [nameOf(node.exported)]
142
+ }
143
+ const source = node.source.value
144
+ return source.startsWith('.') ? exportedNames(path.resolve(path.dirname(file), source), seen) : []
145
+ }
146
+ return []
147
+ }).filter((exported) => exported !== 'default')
148
+ }
149
+
150
+ function declaredNames(declaration) {
151
+ if (declaration.type === 'VariableDeclaration') {
152
+ return declaration.declarations.filter((d) => d.id.type === 'Identifier').map((d) => d.id.name)
153
+ }
154
+ return declaration.id ? [declaration.id.name] : []
155
+ }
156
+
157
+ function nameOf(node) {
158
+ return node.type === 'Identifier' ? node.name : String(node.value)
159
+ }
package/src/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import { createConfig } from './eslint/config.js'
2
+
3
+ export { ALWAYS_IGNORED, CONFIG_KEY, createConfig } from './eslint/config.js'
4
+ export { KINDS, detectKind, kindOf } from './kind.js'
5
+
6
+ /**
7
+ * The nera-stan ESLint config for the repo in the current directory, for
8
+ * editors: `export default [...nera]` in the repo's `eslint.config.js`.
9
+ */
10
+ export default createConfig()
package/src/kind.js ADDED
@@ -0,0 +1,102 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import path from 'node:path'
3
+
4
+ import { isPluginPackage } from '@nera-static/plugin-utils/packages'
5
+ import { isPlainObject } from '@nera-static/plugin-utils/values'
6
+
7
+ const SCOPE = '@nera-static/'
8
+ const CORE = `${SCOPE}core`
9
+ const CLI = `${SCOPE}nera`
10
+ const THEME_PREFIX = `${SCOPE}theme-`
11
+
12
+ /**
13
+ * The repo kinds nera-stan knows, in detection order (D1). Rules declare which
14
+ * of them they apply to.
15
+ */
16
+ export const KINDS = Object.freeze(['core', 'starter', 'theme', 'plugin', 'site', 'tool'])
17
+
18
+ /**
19
+ * Read and parse `<root>/package.json`.
20
+ *
21
+ * @param {string} root - The repo's root directory.
22
+ * @returns {object} The parsed manifest.
23
+ * @throws {Error} When the file is missing, unreadable, not JSON or not an object.
24
+ */
25
+ export function readPackage(root) {
26
+ const file = path.join(root, 'package.json')
27
+ let text
28
+ try {
29
+ text = readFileSync(file, 'utf8')
30
+ } catch (error) {
31
+ const reason = error.code === 'ENOENT' ? 'not found' : `cannot be read (${error.code || error.message})`
32
+ throw new Error(
33
+ `${file} ${reason}: run nera-stan in a repo's root or pass the repo's directory`,
34
+ { cause: error },
35
+ )
36
+ }
37
+
38
+ let pkg
39
+ try {
40
+ pkg = JSON.parse(text)
41
+ } catch (error) {
42
+ throw new Error(
43
+ `${file} is not valid JSON (${error.message}): fix the file, then run nera-stan again`,
44
+ { cause: error },
45
+ )
46
+ }
47
+
48
+ if (!isPlainObject(pkg)) {
49
+ throw new Error(`${file} must hold a JSON object: fix the file, then run nera-stan again`)
50
+ }
51
+
52
+ return pkg
53
+ }
54
+
55
+ /**
56
+ * Classify a parsed `package.json` as one of `KINDS`. The first match wins:
57
+ * core, starter (`nera.starter`), theme (`theme-` name or `nera.generator`),
58
+ * plugin (`plugin-` name except plugin-utils), site (unscoped name with a
59
+ * dependency on `@nera-static/nera`), tool (everything else).
60
+ *
61
+ * @param {object} pkg - A parsed `package.json`.
62
+ * @returns {string} The repo kind.
63
+ */
64
+ export function kindOf(pkg) {
65
+ const name = typeof pkg.name === 'string' ? pkg.name : ''
66
+ const nera = isPlainObject(pkg.nera) ? pkg.nera : {}
67
+
68
+ if (name === CORE) {
69
+ return 'core'
70
+ }
71
+ if (nera.starter !== undefined) {
72
+ return 'starter'
73
+ }
74
+ if (name.startsWith(THEME_PREFIX) || nera.generator !== undefined) {
75
+ return 'theme'
76
+ }
77
+ if (isPluginPackage(name)) {
78
+ return 'plugin'
79
+ }
80
+ if (!name.startsWith(SCOPE) && dependsOn(pkg, CLI)) {
81
+ return 'site'
82
+ }
83
+ return 'tool'
84
+ }
85
+
86
+ /**
87
+ * Detect the kind of the repo at `root` from its `package.json`.
88
+ *
89
+ * @param {string} root - The repo's root directory.
90
+ * @returns {string} One of `KINDS`.
91
+ * @throws {Error} When `package.json` is missing or invalid.
92
+ */
93
+ export function detectKind(root) {
94
+ return kindOf(readPackage(root))
95
+ }
96
+
97
+ function dependsOn(pkg, name) {
98
+ return ['dependencies', 'devDependencies'].some((field) => {
99
+ const deps = pkg[field]
100
+ return isPlainObject(deps) && Object.hasOwn(deps, name)
101
+ })
102
+ }
@@ -0,0 +1,140 @@
1
+ import path from 'node:path'
2
+
3
+ import { escapeRegExp } from '@nera-static/plugin-utils/html'
4
+ import { isPlainObject } from '@nera-static/plugin-utils/values'
5
+
6
+ /** Top-level folders that hold no plugin runtime code (D8). */
7
+ const NON_RUNTIME = Object.freeze(['test', 'bin', 'scripts', 'views', 'assets'])
8
+ const TEST_FILE = /\.test\.[cm]?js$/
9
+ const GLOB = /[*?]/
10
+ const GLOB_PARTS = Object.freeze({ '**/': '(?:.*/)?', '**': '.*', '*': '[^/]*', '?': '[^/]' })
11
+ const CONDITIONS = Object.freeze(['import', 'node', 'default'])
12
+
13
+ /**
14
+ * The path of `filename` relative to the repo root, with `/` separators.
15
+ *
16
+ * @param {string} root - The repo's root directory.
17
+ * @param {string} filename - An absolute file path, as ESLint's `context.filename`.
18
+ * @returns {string}
19
+ */
20
+ export function repoFile(root, filename) {
21
+ return path.relative(root, filename).split(path.sep).join('/')
22
+ }
23
+
24
+ /**
25
+ * Whether a repo file is a test: under `test/` or named `*.test.js` (D8).
26
+ *
27
+ * @param {string} file - A repo-relative path.
28
+ * @returns {boolean}
29
+ */
30
+ export function isTestFile(file) {
31
+ return file.startsWith('test/') || TEST_FILE.test(file)
32
+ }
33
+
34
+ /**
35
+ * Whether a repo file is runtime code: not a test and not under `bin/`,
36
+ * `scripts/`, `views/` or `assets/` (D8).
37
+ *
38
+ * @param {string} file - A repo-relative path.
39
+ * @returns {boolean}
40
+ */
41
+ export function isRuntimeFile(file) {
42
+ return !isTestFile(file) && !NON_RUNTIME.includes(file.split('/')[0])
43
+ }
44
+
45
+ /**
46
+ * The module core imports a plugin's hooks from: `main`, else the `exports`
47
+ * root, else `index.js`.
48
+ *
49
+ * @param {object} pkg - A parsed `package.json`.
50
+ * @returns {string} A repo-relative path.
51
+ */
52
+ export function entryFile(pkg) {
53
+ const target = typeof pkg.main === 'string' ? pkg.main : exportTarget(rootExport(pkg.exports))
54
+ return target ? normalize(target) : 'index.js'
55
+ }
56
+
57
+ /**
58
+ * The patterns of the files `npm publish` ships: `files` plus `main`, every `bin`
59
+ * and every `exports` target; `null` when `files` is absent, because npm then
60
+ * ships everything.
61
+ *
62
+ * @param {object} pkg - A parsed `package.json`.
63
+ * @returns {string[] | null}
64
+ */
65
+ export function publishedPatterns(pkg) {
66
+ if (!Array.isArray(pkg.files)) {
67
+ return null
68
+ }
69
+ const bins = typeof pkg.bin === 'string' ? [pkg.bin] : Object.values(pkg.bin ?? {})
70
+ return [pkg.main, ...pkg.files, ...bins, ...exportTargets(pkg.exports)]
71
+ .filter((entry) => typeof entry === 'string' && entry !== '')
72
+ }
73
+
74
+ /**
75
+ * Whether `npm publish` ships a repo file. Without `files` (`patterns` is `null`)
76
+ * that is every file but the tests (D8).
77
+ *
78
+ * @param {string[] | null} patterns - From `publishedPatterns`.
79
+ * @param {string} file - A repo-relative path.
80
+ * @returns {boolean}
81
+ */
82
+ export function isPublished(patterns, file) {
83
+ if (patterns === null) {
84
+ return !isTestFile(file)
85
+ }
86
+ const excluded = patterns.filter((entry) => entry.startsWith('!')).map((entry) => entry.slice(1))
87
+ const included = patterns.filter((entry) => !entry.startsWith('!'))
88
+ return included.some((entry) => matches(entry, file)) && !excluded.some((entry) => matches(entry, file))
89
+ }
90
+
91
+ /** A `files` entry matches the file itself or, as a folder, everything inside it. */
92
+ function matches(entry, file) {
93
+ const pattern = normalize(entry).replace(/\/$/, '')
94
+ if (!GLOB.test(pattern)) {
95
+ return file === pattern || file.startsWith(`${pattern}/`)
96
+ }
97
+ return globToRegExp(pattern).test(file)
98
+ }
99
+
100
+ /** `**` spans folders, `*` and `?` stay inside one; a match may be a folder, so its contents match too. */
101
+ function globToRegExp(pattern) {
102
+ const source = pattern.split(/(\*\*\/|\*\*|\*|\?)/)
103
+ .map((part) => GLOB_PARTS[part] ?? escapeRegExp(part)).join('')
104
+ return new RegExp(`^${source}(?:/.*)?$`)
105
+ }
106
+
107
+ function normalize(entry) {
108
+ return entry.replace(/^\.\//, '')
109
+ }
110
+
111
+ function rootExport(exports) {
112
+ if (isPlainObject(exports) && Object.keys(exports).some((key) => key.startsWith('.'))) {
113
+ return exports['.']
114
+ }
115
+ return exports
116
+ }
117
+
118
+ /** The file a conditional export resolves to for an ESM import. */
119
+ function exportTarget(target) {
120
+ if (typeof target === 'string') {
121
+ return target
122
+ }
123
+ if (Array.isArray(target)) {
124
+ return target.map(exportTarget).find(Boolean)
125
+ }
126
+ if (isPlainObject(target)) {
127
+ return CONDITIONS.map((condition) => exportTarget(target[condition])).find(Boolean)
128
+ }
129
+ return undefined
130
+ }
131
+
132
+ function exportTargets(target) {
133
+ if (typeof target === 'string') {
134
+ return [target]
135
+ }
136
+ if (target && typeof target === 'object') {
137
+ return Object.values(target).flatMap(exportTargets)
138
+ }
139
+ return []
140
+ }