mirror of
https://github.com/danog/inline-critical.git
synced 2024-11-26 12:04:38 +01:00
commit
438253c03b
4
cli.js
4
cli.js
@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
const os = require('os');
|
||||
const fs = require('fs');
|
||||
const meow = require('meow');
|
||||
@ -111,7 +113,7 @@ cli.flags = _.reduce(
|
||||
);
|
||||
|
||||
function processError(err) {
|
||||
process.stderr.write(chalk.red(indentString('Error: ' + (err.message || err), 2)));
|
||||
process.stderr.write(chalk.red(indentString(`Error: ${err.message || err}`, 2)));
|
||||
process.stderr.write(os.EOL);
|
||||
process.stderr.write(indentString(help, 2));
|
||||
process.exit(1);
|
||||
|
4
index.js
4
index.js
@ -8,7 +8,9 @@
|
||||
* http://bezoerb.mit-license.org/
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const isString = require('lodash/isString');
|
||||
@ -103,6 +105,7 @@ function inline(html, styles, options) {
|
||||
// eslint-disable-next-line array-callback-return
|
||||
o.replaceStylesheets.map(href => {
|
||||
const link = document.createElement('link');
|
||||
|
||||
link.setAttribute('rel', 'stylesheet');
|
||||
link.setAttribute('href', href);
|
||||
document.addNoscript(link);
|
||||
@ -130,6 +133,7 @@ function inline(html, styles, options) {
|
||||
if (o.extract) {
|
||||
const href = link.getAttribute('href');
|
||||
const file = path.resolve(path.join(o.basePath || process.cwd, href));
|
||||
|
||||
if (fs.existsSync(file)) {
|
||||
const orig = fs.readFileSync(file);
|
||||
const diff = extractCss(orig, inlined, o.minify);
|
||||
|
@ -1,3 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const prettier = require('prettier');
|
||||
const CleanCSS = require('clean-css');
|
||||
const postcss = require('postcss');
|
||||
|
16
src/dom.js
16
src/dom.js
@ -1,3 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const {JSDOM} = require('jsdom');
|
||||
@ -14,8 +16,8 @@ const UglifyJS = require('uglify-js');
|
||||
function getScript() {
|
||||
const loadCssMain = resolve.sync('fg-loadcss');
|
||||
const loadCssBase = path.dirname(loadCssMain);
|
||||
|
||||
const loadCSS = fs.readFileSync(path.join(loadCssBase, 'cssrelpreload.js'), 'utf8');
|
||||
|
||||
return UglifyJS.minify(loadCSS).code.trim();
|
||||
}
|
||||
|
||||
@ -117,7 +119,7 @@ class Dom {
|
||||
.join(`\n${textIndent}`);
|
||||
|
||||
const styles = this.document.createElement('style');
|
||||
styles.append(this.document.createTextNode('\n' + textIndent + text + '\n' + referenceIndent));
|
||||
styles.append(this.document.createTextNode(`\n${textIndent}${text}\n${referenceIndent}`));
|
||||
return styles;
|
||||
}
|
||||
|
||||
@ -162,14 +164,14 @@ class Dom {
|
||||
insertStylesBefore(css, referenceNode) {
|
||||
const styles = this.createStyleNode(css);
|
||||
referenceNode.before(styles);
|
||||
styles.after(this.document.createTextNode('\n' + this.headIndent.indent));
|
||||
styles.after(this.document.createTextNode(`\n${this.headIndent.indent}`));
|
||||
}
|
||||
|
||||
appendStyles(css, referenceNode) {
|
||||
const styles = this.createStyleNode(css);
|
||||
referenceNode.append(styles);
|
||||
styles.before(this.document.createTextNode(this.headIndent.indent));
|
||||
styles.after(this.document.createTextNode('\n' + this.headIndent.indent));
|
||||
styles.after(this.document.createTextNode(`\n${this.headIndent.indent}`));
|
||||
}
|
||||
|
||||
addNoscript(link) {
|
||||
@ -180,12 +182,12 @@ class Dom {
|
||||
|
||||
insertBefore(node, referenceNode) {
|
||||
referenceNode.before(node);
|
||||
node.after(this.document.createTextNode('\n' + this.headIndent.indent));
|
||||
node.after(this.document.createTextNode(`\n${this.headIndent.indent}`));
|
||||
}
|
||||
|
||||
insertAfter(node, referenceNode) {
|
||||
referenceNode.after(node);
|
||||
referenceNode.after(this.document.createTextNode('\n' + this.headIndent.indent));
|
||||
referenceNode.after(this.document.createTextNode(`\n${this.headIndent.indent}`));
|
||||
}
|
||||
|
||||
remove(node) {
|
||||
@ -220,7 +222,7 @@ class Dom {
|
||||
|
||||
if (scriptAnchor) {
|
||||
scriptAnchor.after(script);
|
||||
scriptAnchor.after(this.document.createTextNode('\n' + this.headIndent.indent));
|
||||
scriptAnchor.after(this.document.createTextNode(`\n${this.headIndent.indent}`));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const readPkgUp = require('read-pkg-up');
|
||||
const {read, strip, run, getArgs, pipe} = require('./helper');
|
||||
|
@ -1,4 +1,7 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const readPkgUp = require('read-pkg-up');
|
||||
|
@ -1,4 +1,7 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const reaver = require('reaver');
|
||||
const {extractCss} = require('../src/css.js');
|
||||
|
Loading…
Reference in New Issue
Block a user