-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathpluginFactory.js
More file actions
152 lines (135 loc) · 4.8 KB
/
Copy pathpluginFactory.js
File metadata and controls
152 lines (135 loc) · 4.8 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import postcss from "postcss";
import unquote from "./unquote";
import Parser from "./Parser";
import saveJSON from "./saveJSON";
import { makeLocalsConventionReducer } from "./localsConvention";
import FileSystemLoader from "./FileSystemLoader";
import {
getDefaultPlugins,
getDefaultScopeBehaviour,
behaviours,
getScopedNameGenerator,
} from "./scoping";
const PLUGIN_NAME = "postcss-modules";
function isGlobalModule(globalModules, inputFile) {
return globalModules.some((regex) => inputFile.match(regex));
}
function getDefaultPluginsList(opts, inputFile) {
const globalModulesList = opts.globalModulePaths || null;
const exportGlobals = opts.exportGlobals || false;
const defaultBehaviour = getDefaultScopeBehaviour(opts.scopeBehaviour);
const generateScopedName = getScopedNameGenerator(opts.generateScopedName, opts.hashPrefix);
if (globalModulesList && isGlobalModule(globalModulesList, inputFile)) {
return getDefaultPlugins({
behaviour: behaviours.GLOBAL,
generateScopedName,
exportGlobals,
});
}
return getDefaultPlugins({
behaviour: defaultBehaviour,
generateScopedName,
exportGlobals,
});
}
function getLoader(opts, plugins) {
const root = typeof opts.root === "undefined" ? "/" : opts.root;
return typeof opts.Loader === "function"
? new opts.Loader(root, plugins, opts.resolve)
: new FileSystemLoader(root, plugins, opts.resolve);
}
function isOurPlugin(plugin) {
return plugin.postcssPlugin === PLUGIN_NAME;
}
export function makePlugin(opts) {
return {
postcssPlugin: PLUGIN_NAME,
async OnceExit(css, { result }) {
const getJSON = opts.getJSON || saveJSON;
const inputFile = css.source.input.file;
const pluginList = getDefaultPluginsList(opts, inputFile);
const resultPluginIndex = result.processor.plugins.findIndex((plugin) =>
isOurPlugin(plugin),
);
if (resultPluginIndex === -1) {
throw new Error("Plugin missing from options.");
}
const earlierPlugins = result.processor.plugins.slice(0, resultPluginIndex);
const loaderPlugins = [...earlierPlugins, ...pluginList];
const loader = getLoader(opts, loaderPlugins);
const fetcher = async (file, relativeTo, depTrace) => {
const unquoteFile = unquote(file);
return loader.fetch.call(loader, unquoteFile, relativeTo, depTrace);
};
const parser = new Parser(fetcher);
await postcss([...pluginList, parser.plugin()]).process(css, {
from: inputFile,
});
// Remove rules whose selector was reduced to whitespace after
// `:global { ... }` unwrapping — see #136.
css.walkRules((rule) => {
if (rule.selector.trim() === "") rule.remove();
});
// Prepend the traced dependencies exactly as before, then re-parse
// each dependency with its own `from` and graft those sources onto
// the prepended nodes, pairing them by position. The pairing holds
// because each dependency is complete CSS, so parsing the
// concatenation yields the same node sequence as parsing each part.
// If malformed CSS merges nodes across a file boundary the counts
// differ and the graft is skipped, leaving the nodes unattributed
// as before. Plugins that run after this one (e.g. url rewriters)
// need `source.input.file` to resolve relative paths against the
// file the CSS actually came from — see #149.
const sources = Array.isArray(loader.finalSources) ? loader.finalSources : null;
if (sources) {
const joined = sources.map((entry) => entry.source).join("");
if (joined) {
const countBefore = css.nodes.length;
css.prepend(joined);
const prepended = css.nodes.slice(0, css.nodes.length - countBefore);
const sourceSeq = [];
for (const { file, source } of sources) {
postcss.parse(source, { from: file }).walk((node) => {
sourceSeq.push(node.source);
});
}
let count = 0;
for (const node of prepended) {
count += 1;
if (typeof node.walk === "function") {
node.walk(() => {
count += 1;
});
}
}
if (count === sourceSeq.length) {
let i = 0;
for (const node of prepended) {
node.source = sourceSeq[i++];
if (typeof node.walk === "function") {
node.walk((child) => {
child.source = sourceSeq[i++];
});
}
}
}
}
} else {
// Custom loaders may not implement `finalSources`.
const out = loader.finalSource;
if (out) css.prepend(out);
}
if (opts.localsConvention) {
const reducer = makeLocalsConventionReducer(opts.localsConvention, inputFile);
parser.exportTokens = Object.entries(parser.exportTokens).reduce(reducer, {});
}
result.messages.push({
type: "export",
plugin: "postcss-modules",
exportTokens: parser.exportTokens,
});
// getJSON may return a promise
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
},
};
}