Repository navigation
Expand file tree
/
Copy pathpostcss.config.js
More file actions
58 lines (54 loc) · 2.16 KB
/
Copy pathpostcss.config.js
File metadata and controls
58 lines (54 loc) · 2.16 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
// This is used to auto-prefix CSS, see: https://github.com/postcss/postcss-loader
const autoprefixer = require('autoprefixer');
const SCOPE_CLASS = '.bp-doc';
// Files whose selectors must be confined to the doc viewer DOM. Without this
// scoping, upstream selectors like `.sidebar` and `.toggleButton` collide with
// host-app selectors when the npm pdfjs path is used.
const SCOPE_FILE_RE = /pdfjs-dist[\\/](?:legacy[\\/])?web[\\/]pdf_viewer\.css$/;
// pdf.js appends its opaque text-measurement canvas to document.body and relies on
// this rule to hide it, so a scoped selector leaves a black box over the page.
const BODY_LEVEL_SELECTORS = new Set(['.hiddenCanvasElement']);
// Checking isNested to skip a second .bp-doc prefix on a rule already inside a prefixed selector.
const isNestedInRule = rule => {
let { parent } = rule;
while (parent) {
if (parent.type === 'rule') {
return true;
}
({ parent } = parent);
}
return false;
};
const scopePdfjsViewerCss = () => ({
postcssPlugin: 'scope-pdfjs-viewer-css',
Once(root, { result }) {
if (!SCOPE_FILE_RE.test(result.opts.from || '')) {
return;
}
root.walkRules(rule => {
// Skip rules nested in @keyframes — `from`/`to`/`50%` are not selectors.
if (rule.parent && rule.parent.type === 'atrule' && rule.parent.name === 'keyframes') {
return;
}
if (isNestedInRule(rule)) {
return;
}
rule.selectors = rule.selectors.map(selector => {
const trimmed = selector.trim();
// `:root` is the document root; rewrite to our scope so custom
// properties declared there only apply inside the viewer.
if (trimmed === ':root') {
return SCOPE_CLASS;
}
if (BODY_LEVEL_SELECTORS.has(trimmed)) {
return trimmed;
}
return `${SCOPE_CLASS} ${trimmed}`;
});
});
},
});
scopePdfjsViewerCss.postcss = true;
module.exports = {
plugins: [scopePdfjsViewerCss(), autoprefixer()],
};