-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.cjs
252 lines (220 loc) · 7.95 KB
/
index.cjs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var postcssBrowserComments = require('postcss-browser-comments');
var path = require('node:path');
var node_module = require('node:module');
var path$1 = require('path');
var fs = require('fs');
var postcss = require('postcss');
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
const assign = (...objects) => Object.assign(...objects);
const create = (...objects) => assign(Object.create(null), ...objects);
const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
// get resolved filenames for normalize.css
const normalizeCSS = require$1.resolve('@csstools/normalize.css');
const normalizeDir = path.dirname(normalizeCSS);
const normalizeOpinionatedCSS = path.join(normalizeDir, 'opinionated.css');
// get resolved filenames for sanitize.css
const sanitizeCSS = require$1.resolve('sanitize.css');
const sanitizeDir = path.dirname(sanitizeCSS);
const sanitizeAssetsCSS = path.join(sanitizeDir, 'assets.css');
const sanitizeFormsCSS = path.join(sanitizeDir, 'forms.css');
const sanitizeReduceMotionCSS = path.join(sanitizeDir, 'reduce-motion.css');
const sanitizeTypographyCSS = path.join(sanitizeDir, 'typography.css');
const sanitizeSystemUiCSS = path.join(sanitizeDir, 'system-ui.css');
const sanitizeUiMonospace = path.join(sanitizeDir, 'ui-monospace.css');
// export a hashmap of css library filenames
const parsableFilenames = create({
[normalizeCSS]: true,
[normalizeOpinionatedCSS]: true,
[sanitizeCSS]: true,
[sanitizeAssetsCSS]: true,
[sanitizeFormsCSS]: true,
[sanitizeReduceMotionCSS]: true,
[sanitizeTypographyCSS]: true,
[sanitizeSystemUiCSS]: true,
[sanitizeUiMonospace]: true,
});
// export a hashmap of css library filenames by id
const resolvedFilenamesById = create({
'normalize': [normalizeCSS],
'normalize/opinionated': [normalizeOpinionatedCSS],
'normalize/*': [normalizeOpinionatedCSS],
'sanitize': [sanitizeCSS],
'sanitize/assets': [sanitizeAssetsCSS],
'sanitize/forms': [sanitizeCSS, sanitizeFormsCSS],
'sanitize/page': [sanitizeAssetsCSS], // deprecated; remaining for v10.0.0 compatibility
'sanitize/reduce-motion': [sanitizeCSS, sanitizeReduceMotionCSS],
'sanitize/system-ui': [sanitizeCSS, sanitizeSystemUiCSS],
'sanitize/typography': [sanitizeCSS, sanitizeTypographyCSS],
'sanitize/ui-monospace': [sanitizeCSS, sanitizeUiMonospace],
'sanitize/*': [sanitizeCSS, sanitizeFormsCSS],
});
const cache$1 = create();
async function readFile (filename) {
filename = path$1.resolve(filename);
cache$1[filename] = cache$1[filename] || create();
return new Promise(
(resolve, reject) => fs.stat(
filename,
(statsError, { mtime }) => statsError
? reject(statsError)
: mtime === cache$1[filename].mtime
? resolve(cache$1[filename].data)
: fs.readFile(
filename,
'utf8',
(readFileError, data) => readFileError
? reject(readFileError)
: resolve(
(cache$1[filename] = { data, mtime }).data
)
)
)
)
}
const cache = create(null);
var parse = (filename, transformer) => readFile(filename).then(
// cache the parsed css root
css => (cache[css] = cache[css] || postcss.parse(css, { from: filename }))
).then(
// clone the cached root
root => root.clone()
).then(
// transform the cloned root
clone => Promise.resolve(
transformer(clone)
).then(
// resolve the cloned root
() => clone
)
);
var postcssImportNormalize = commentsTransformer => opts => {
opts = create(opts);
// return an postcss-import configuration
return create({
load (filename, importOptions) {
return filename in parsableFilenames
// parse the file (the file and css are conservatively cached)
? parse(filename, commentsTransformer).then(
root => root.toResult({ to: filename, map: true }).css
)
: typeof opts.load === 'function'
// otherwise, use the override loader
? opts.load.call(null, filename, importOptions)
// otherwise, return the (conservatively cached) contents of the file
: readFile(filename)
},
resolve (id, basedir, importOptions) {
// get the css id by removing css extensions
const cssId = id.replace(cssExtRegExp$1, '');
return cssId in resolvedFilenamesById
// return the known resolved path for the css id
? resolvedFilenamesById[cssId]
: typeof opts.resolve === 'function'
// otherwise, use the override resolver
? opts.resolve.call(null, id, basedir, importOptions)
// otherwise, return the id to be resolved by postcss-import
: id
}
})
};
const cssExtRegExp$1 = /\.css\b/g;
const postcssPlugin = (commentsTransformer, opts) => root => {
const promises = [];
const insertedFilenames = {};
// use @import insertion point
root.walkAtRules(
importRegExp,
atrule => {
// get name as a fallback value for the library (e.g. @import-normalize is like @import "normalize.css")
const name = atrule.name.match(importRegExp)[1];
// get url from "library", 'library', url("library"), url('library'), or the fallback value
const url = (atrule.params.match(paramsRegExp) || []).slice(1).find(part => part) || name;
if (url) {
// get the css id by removing css extensions
const cssId = url.replace(cssExtRegExp, '');
if (cssId in resolvedFilenamesById) {
// promise the library import is replaced with its contents
promises.push(
Promise.all(
resolvedFilenamesById[cssId].filter(
// ignore filenames that have already been inserted
filename => insertedFilenames[filename] = opts.allowDuplicates || !(filename in insertedFilenames)
).map(
// parse the file (the file and css are conservatively cached)
filename => parse(filename, commentsTransformer)
)
).then(roots => {
if (roots.length) {
// combine all the library nodes returned by the parsed files
const nodes = roots.reduce(
(all, root) => all.concat(root.nodes),
[]
);
// replace the import with all the library nodes
atrule.replaceWith(...nodes);
}
})
);
}
}
}
);
return Promise.all([].concat(
// promise the library imports are replaced with their contents
promises,
// promise certain libraries are prepended
Promise.all(
[].concat(
opts.forceImport || []
).reduce(
// filter the id to be a known id or boolean true
(all, id) => {
if (id === true) {
all.push(...resolvedFilenamesById.normalize);
} else if (typeof id === 'string') {
const cssId = id.replace(cssExtRegExp, '');
if (cssId in resolvedFilenamesById) {
all.push(...resolvedFilenamesById[cssId]);
}
}
return all
},
[]
).filter(
// ignore filenames that have already been inserted
filename => insertedFilenames[filename] = opts.allowDuplicates || !(filename in insertedFilenames)
).map(
// parse the file (the file and css are conservatively cached)
filename => parse(filename, commentsTransformer)
)
).then(roots => {
if (roots.length) {
// combine all the library nodes returned by the parsed files
const nodes = roots.reduce(
(all, root) => all.concat(root.nodes),
[]
);
// prepend the stylesheet with all the library nodes
root.prepend(...nodes);
}
})
))
};
const cssExtRegExp = /\.css\b/g;
const importRegExp = /^import(?:-(normalize|sanitize))?$/;
const paramsRegExp = /^\s*(?:url\((?:"(.+)"|'(.+)')\)|"(.+)"|'(.+)')[\W\w]*$/;
const plugin = opts => {
opts = create(opts);
const commentsTransformer = postcssBrowserComments(opts).Once;
const normalizeTransformer = postcssPlugin(commentsTransformer, opts);
const postcssImportConfig = postcssImportNormalize(commentsTransformer);
return {
postcssPlugin: 'postcss-normalize',
Once(root) {
return normalizeTransformer(root)
},
postcssImport: postcssImportConfig
}
};
plugin.postcss = true;
module.exports = plugin;