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