|
1 | | -import type { ChildNode, Plugin, PluginCreator } from 'postcss'; |
| 1 | +import type { ChildNode, Plugin, PluginCreator, Root } from 'postcss'; |
2 | 2 | import { parseImport } from './parse-import'; |
3 | 3 |
|
4 | 4 | /** postcss-global-data plugin options */ |
5 | 5 | export type pluginOptions = { |
6 | 6 | /** List of files to be used as context */ |
7 | 7 | files?: Array<string>, |
| 8 | + /** Remove nodes in a separate plugin object, this object can be added later in your list of plugins */ |
| 9 | + lateRemover?: boolean, |
| 10 | + /** Add global CSS to the start of files, defaults to `false` */ |
| 11 | + prepend?: boolean, |
8 | 12 | }; |
9 | 13 |
|
| 14 | +const pluginName = 'postcss-global-data'; |
| 15 | + |
10 | 16 | const creator: PluginCreator<pluginOptions> = (opts?: pluginOptions) => { |
11 | 17 | const options = Object.assign( |
12 | 18 | // Default options |
13 | 19 | { |
14 | 20 | files: [], |
| 21 | + lateRemover: false, |
| 22 | + prepend: false, |
15 | 23 | }, |
16 | 24 | // Provided options |
17 | 25 | opts, |
18 | 26 | ); |
19 | 27 |
|
20 | | - return { |
21 | | - postcssPlugin: 'postcss-global-data', |
22 | | - prepare(): Plugin { |
23 | | - let importedFiles = new Set<string>(); |
24 | | - let importedCSS = new Set<ChildNode>(); |
25 | | - |
26 | | - return { |
27 | | - postcssPlugin: 'postcss-global-data', |
28 | | - Once(root, postcssHelpers): void { |
29 | | - options.files.forEach((file) => { |
30 | | - if (importedFiles.has(file)) { |
31 | | - return; |
32 | | - } |
| 28 | + function insert(destination: Root, source: Root, set: { add: (_: ChildNode) => void }): void { |
| 29 | + if (!options.prepend) { |
| 30 | + source.each((node) => { |
| 31 | + destination.append(node); |
| 32 | + set.add(node); |
| 33 | + }); |
33 | 34 |
|
34 | | - const newCSS = parseImport(root, postcssHelpers, file, importedFiles); |
35 | | - if (!newCSS) { |
36 | | - return; |
37 | | - } |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const nodes = Array.from(source.nodes); |
| 39 | + nodes.reverse(); |
38 | 40 |
|
39 | | - newCSS.each((node) => { |
40 | | - root.append(node); |
41 | | - importedCSS.add(node); |
| 41 | + nodes.forEach((node) => { |
| 42 | + destination.prepend(node); |
| 43 | + set.add(node); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + if (!options.lateRemover) { |
| 48 | + return { |
| 49 | + postcssPlugin: pluginName, |
| 50 | + prepare(): Plugin { |
| 51 | + const importedCSS = new Set<ChildNode>(); |
| 52 | + const importedFiles = new Set<string>(); |
| 53 | + |
| 54 | + return { |
| 55 | + postcssPlugin: pluginName, |
| 56 | + Once(root, postcssHelpers): void { |
| 57 | + options.files.forEach((file) => { |
| 58 | + const newCSS = parseImport(root, postcssHelpers, file, importedFiles); |
| 59 | + if (!newCSS) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + insert(root, newCSS, importedCSS); |
42 | 64 | }); |
43 | | - }); |
| 65 | + }, |
| 66 | + OnceExit(): void { |
| 67 | + importedCSS.forEach((node) => { |
| 68 | + node.remove(); |
| 69 | + }); |
| 70 | + |
| 71 | + importedFiles.clear(); |
| 72 | + importedCSS.clear(); |
| 73 | + }, |
| 74 | + }; |
| 75 | + }, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const importedCSS = new WeakSet<ChildNode>(); |
| 80 | + |
| 81 | + return { |
| 82 | + postcssPlugin: pluginName, |
| 83 | + plugins: [ |
| 84 | + { |
| 85 | + postcssPlugin: pluginName, |
| 86 | + prepare(): Plugin { |
| 87 | + const importedFiles = new Set<string>(); |
| 88 | + |
| 89 | + return { |
| 90 | + postcssPlugin: pluginName, |
| 91 | + Once(root, postcssHelpers): void { |
| 92 | + options.files.forEach((file) => { |
| 93 | + const newCSS = parseImport(root, postcssHelpers, file, importedFiles); |
| 94 | + if (!newCSS) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + insert(root, newCSS, importedCSS); |
| 99 | + }); |
| 100 | + }, |
| 101 | + OnceExit(): void { |
| 102 | + importedFiles.clear(); |
| 103 | + }, |
| 104 | + }; |
44 | 105 | }, |
45 | | - OnceExit(): void { |
46 | | - importedCSS.forEach((node) => { |
47 | | - node.remove(); |
| 106 | + }, |
| 107 | + { |
| 108 | + postcssPlugin: pluginName + '/late-remover', |
| 109 | + OnceExit(root): void { |
| 110 | + root.each((node) => { |
| 111 | + if (importedCSS.has(node)) { |
| 112 | + node.remove(); |
| 113 | + } |
48 | 114 | }); |
49 | | - importedCSS = new Set<ChildNode>(); |
50 | | - importedFiles = new Set<string>(); |
51 | 115 | }, |
52 | | - }; |
53 | | - }, |
54 | | - }; |
| 116 | + } |
| 117 | + ] |
| 118 | + } |
55 | 119 | }; |
56 | 120 |
|
57 | 121 | creator.postcss = true; |
|
0 commit comments