-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (119 loc) · 3.55 KB
/
index.js
File metadata and controls
133 lines (119 loc) · 3.55 KB
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
import get from 'lodash-es/get.js'
import { defu as merge } from 'defu'
// PostHTML
import posthtml from 'posthtml'
import posthtmlFetch from 'posthtml-fetch'
import envTags from './plugins/envTags.js'
import components from 'posthtml-component'
import posthtmlPostcss from 'posthtml-postcss'
import expandLinkTag from './plugins/expandLinkTag.js'
import envAttributes from './plugins/envAttributes.js'
import { getPosthtmlOptions } from './defaultConfig.js'
// PostCSS
import tailwindcss from 'tailwindcss'
import postcssCalc from 'postcss-calc'
import postcssImport from 'postcss-import'
import cssVariables from 'postcss-css-variables'
import postcssSafeParser from 'postcss-safe-parser'
import postcssColorFunctionalNotation from 'postcss-color-functional-notation'
import defaultComponentsConfig from './defaultComponentsConfig.js'
export async function process(html = '', config = {}) {
/**
* Configure PostCSS pipeline. Plugins defined and added here
* will apply to all `<style>` tags in the HTML.
*/
const resolveCSSProps = get(config, 'css.resolveProps')
const resolveCalc = get(config, 'css.resolveCalc') !== false
? get(config, 'css.resolveCalc', { precision: 2 }) // it's true by default, use default precision 2
: false
const postcssPlugin = posthtmlPostcss(
[
postcssImport(),
tailwindcss(get(config, 'css.tailwind', {})),
resolveCSSProps !== false && cssVariables(resolveCSSProps),
resolveCalc !== false && postcssCalc(resolveCalc),
postcssColorFunctionalNotation(),
...get(config, 'postcss.plugins', []),
],
merge(
get(config, 'postcss.options', {}),
{
from: config.cwd || './',
parser: postcssSafeParser
}
)
)
/**
* Define PostHTML options by merging user-provided ones
* on top of a default configuration.
*/
const posthtmlOptions = getPosthtmlOptions(get(config, 'posthtml.options', {}))
const componentsUserOptions = get(config, 'components', {})
const expressionsOptions = merge(
get(config, 'expressions', get(config, 'posthtml.expressions', {})),
get(componentsUserOptions, 'expressions', {}),
)
const locals = merge(
get(config, 'locals', {}),
get(expressionsOptions, 'locals', {}),
{ page: config },
)
const fetchPlugin = posthtmlFetch(
merge(
{
expressions: merge(
{ locals },
expressionsOptions,
{
missingLocal: '{local}',
strictMode: false,
},
),
},
get(config, 'fetch', {})
)
)
const componentsConfig = merge(
{
expressions: merge(
{ locals },
expressionsOptions,
)
},
componentsUserOptions,
defaultComponentsConfig
)
// Ensure `fileExtension` is array and has no duplicates
componentsConfig.fileExtension = Array.from(new Set(
[].concat(componentsConfig.fileExtension)
))
const beforePlugins = get(config, 'posthtml.plugins.before', [])
return posthtml([
...beforePlugins,
envTags(config.env),
envAttributes(config.env),
expandLinkTag,
postcssPlugin,
fetchPlugin,
components(componentsConfig),
expandLinkTag,
postcssPlugin,
envTags(config.env),
envAttributes(config.env),
...get(
config,
'posthtml.plugins.after',
beforePlugins.length > 0
? []
: get(config, 'posthtml.plugins', [])
),
])
.process(html, posthtmlOptions)
.then(result => ({
config: merge(config, { page: config }),
html: result.html,
}))
.catch(error => {
throw error
})
}