1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const dom5 = require("dom5/lib/index-next");
|
13 | const parse5 = require("parse5");
|
14 | const shadyCSS = require("shady-css-parser");
|
15 | const stream = require("stream");
|
16 | class NoCommentStringifier extends shadyCSS.Stringifier {
|
17 | comment(node) {
|
18 | const value = node.value;
|
19 | if (value.indexOf('@license') >= 0) {
|
20 | return value;
|
21 | }
|
22 | return '';
|
23 | }
|
24 | }
|
25 | const parser = new shadyCSS.Parser();
|
26 | const stringifier = new NoCommentStringifier();
|
27 | const pred = dom5.predicates;
|
28 | const isInlineStyle = pred.AND(pred.hasTagName('style'), pred.OR(pred.NOT(pred.hasAttr('type')), pred.hasAttrValue('type', 'text/css')));
|
29 |
|
30 |
|
31 |
|
32 | function html(text) {
|
33 | const ast = parse5.parse(text);
|
34 | const styleNodes = dom5.queryAll(ast, isInlineStyle, dom5.childNodesIncludeTemplate);
|
35 | for (const styleNode of styleNodes) {
|
36 | const text = dom5.getTextContent(styleNode);
|
37 | dom5.setTextContent(styleNode, css(text));
|
38 | }
|
39 | return parse5.serialize(ast);
|
40 | }
|
41 | exports.html = html;
|
42 | const APPLY_NO_SEMI = /@apply\s*\(?--[\w-]+\)?[^;]/g;
|
43 | function addSemi(match) {
|
44 | return match + ';';
|
45 | }
|
46 | function css(text) {
|
47 | text = text.replace(APPLY_NO_SEMI, addSemi);
|
48 | return stringifier.stringify(parser.parse(text));
|
49 | }
|
50 | exports.css = css;
|
51 | class GulpTransform extends stream.Transform {
|
52 | constructor() {
|
53 | super({ objectMode: true });
|
54 | }
|
55 | _transform(file, _encoding, callback) {
|
56 | if (file.isStream()) {
|
57 | return callback(new Error('css-slam does not support streams'));
|
58 | }
|
59 | if (file.contents) {
|
60 | let contents;
|
61 | if (file.path.slice(-5) === '.html') {
|
62 | contents = file.contents.toString();
|
63 | file.contents = new Buffer(html(contents));
|
64 | }
|
65 | else if (file.path.slice(-4) === '.css') {
|
66 | contents = file.contents.toString();
|
67 | file.contents = new Buffer(css(contents));
|
68 | }
|
69 | }
|
70 | callback(undefined, file);
|
71 | }
|
72 | }
|
73 | exports.GulpTransform = GulpTransform;
|
74 | function gulp() {
|
75 | return new GulpTransform();
|
76 | }
|
77 | exports.gulp = gulp;
|
78 |
|
\ | No newline at end of file |