From 6ed0ad4ea2e389c0cd2f80a0b187cbe323ef4f74 Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 15:34:15 -0500 Subject: [PATCH 1/9] lint the tests --- package.json | 2 +- test/index.js | 94 ++++++++++++++++++++++++------------------------- test/preTest.js | 11 +++--- 3 files changed, 52 insertions(+), 55 deletions(-) diff --git a/package.json b/package.json index 79fe1e5..cdf2f51 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "build": "eslint src/** && npm run flow && babel src --out-dir lib", "example": "./node_modules/.bin/babel-node example/example.js", "flow": "flow; test $? -eq 0 -o $? -eq 2", - "eslint": "eslint src/**", + "eslint": "eslint test/**/*.js && eslint src/**", "start": "eslint src/** && npm run flow && babel src/index.js --out-file index.js --watch", "pretest": "./node_modules/.bin/babel-node test/preTest.js", "test": "npm run pretest && tape test" diff --git a/test/index.js b/test/index.js index ab58bdb..f63dd9e 100644 --- a/test/index.js +++ b/test/index.js @@ -1,13 +1,11 @@ #!/usr/bin/env node -var fs = require('fs') -var test = require('tape') -var postcss = require('postcss') -var postcssCriticalCSS = require('..'); -const basePath = `${process.cwd()}/test/fixtures`; -const chalk = require('chalk'); +const fs = require('fs') +const test = require('tape') +const basePath = `${process.cwd()}/test/fixtures` +const chalk = require('chalk') -function compareCritical(t, name, testNonCritical) { +function compareCritical (t, name, testNonCritical) { t.equal( fs.readFileSync( `${basePath}/${name}.${testNonCritical ? 'non-critical.actual' : 'critical.actual'}.css`, 'utf8' @@ -16,55 +14,55 @@ function compareCritical(t, name, testNonCritical) { `${basePath}/${name}.${testNonCritical ? 'non-critical.expected' : 'critical.expected'}.css`, 'utf8' ).trim(), `processed fixture ${chalk.bold(name)} should be equal to expected output` - ); + ) } -test('Testing "this" critical result', function(t) { - compareCritical(t, 'this'); - t.end(); -}); +test('Testing "this" critical result', function (t) { + compareCritical(t, 'this') + t.end() +}) -test('Testing "this" non-critical result', function(t) { - compareCritical(t, 'this', true); - t.end(); -}); +test('Testing "this" non-critical result', function (t) { + compareCritical(t, 'this', true) + t.end() +}) -test('Testing "atRule" critical result', function(t) { - compareCritical(t, 'atRule'); - t.end(); -}); +test('Testing "atRule" critical result', function (t) { + compareCritical(t, 'atRule') + t.end() +}) -test('Testing "atRule" non-critical result', function(t) { - compareCritical(t, 'atRule', true); - t.end(); -}); +test('Testing "atRule" non-critical result', function (t) { + compareCritical(t, 'atRule', true) + t.end() +}) -test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} critical result`), function(t) { - compareCritical(t, 'atRule-wrapping'); - t.end(); -}); +test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} critical result`), function (t) { + compareCritical(t, 'atRule-wrapping') + t.end() +}) -test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} non-critical result`), function(t) { - compareCritical(t, 'atRule-wrapping', true); - t.end(); -}); +test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} non-critical result`), function (t) { + compareCritical(t, 'atRule-wrapping', true) + t.end() +}) -test('Testing "media" critical result', function(t) { - compareCritical(t, 'media'); - t.end(); -}); +test('Testing "media" critical result', function (t) { + compareCritical(t, 'media') + t.end() +}) -test('Testing "media" non-critical result', function(t) { - compareCritical(t, 'media', true); - t.end(); -}); +test('Testing "media" non-critical result', function (t) { + compareCritical(t, 'media', true) + t.end() +}) -test(chalk.yellow(`Testing ${chalk.bold('scope')} critical result`), function(t) { - compareCritical(t, 'scope'); - t.end(); -}); +test(chalk.yellow(`Testing ${chalk.bold('scope')} critical result`), function (t) { + compareCritical(t, 'scope') + t.end() +}) -test(chalk.yellow(`Testing ${chalk.bold('scope')} non-critical result`), function(t) { - compareCritical(t, 'scope', true); - t.end(); -}); +test(chalk.yellow(`Testing ${chalk.bold('scope')} non-critical result`), function (t) { + compareCritical(t, 'scope', true) + t.end() +}) diff --git a/test/preTest.js b/test/preTest.js index e79e602..faea064 100644 --- a/test/preTest.js +++ b/test/preTest.js @@ -1,17 +1,16 @@ #!/usr/bin/env node -var fs = require('fs') -var test = require('tape') -var postcss = require('postcss') -var postcssCriticalCSS = require('..'); +const fs = require('fs') +const postcss = require('postcss') +const postcssCriticalCSS = require('..') +const basePath = `${process.cwd()}/test/fixtures` -const basePath = `${process.cwd()}/test/fixtures`; function cb (files) { function useFileData (data, file) { postcss([postcssCriticalCSS({outputPath: basePath})]) .process(data) .then(result => fs.writeFile(`${basePath}/${file.split('.')[0]}.non-critical.actual.css`, result.css)) } - files.forEach(function(file) { + files.forEach(function (file) { if (file.indexOf('.actual') !== -1) { fs.unlink(`${basePath}/${file}`) } From 7d93c471afcb93b72a03a2b1e1541d2b46858c5e Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 15:50:37 -0500 Subject: [PATCH 2/9] remove unnecessary eslint airbnb config dep --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index cdf2f51..8d783f0 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "chalk": "^1.1.3", "cssnano": "^3.5.2", "eslint": "^3.3.1", - "eslint-config-airbnb": "^10.0.1", "eslint-config-standard": "^6.2.1", "eslint-plugin-flowtype": "^2.7.1", "eslint-plugin-import": "^1.13.0", From 5631a4c26bb16e68c9ecc493755e40e745266bb1 Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 15:53:40 -0500 Subject: [PATCH 3/9] use arrow functions in test index.js --- test/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/index.js b/test/index.js index f63dd9e..df358f7 100644 --- a/test/index.js +++ b/test/index.js @@ -17,52 +17,52 @@ function compareCritical (t, name, testNonCritical) { ) } -test('Testing "this" critical result', function (t) { +test('Testing "this" critical result', (t) => { compareCritical(t, 'this') t.end() }) -test('Testing "this" non-critical result', function (t) { +test('Testing "this" non-critical result', (t) => { compareCritical(t, 'this', true) t.end() }) -test('Testing "atRule" critical result', function (t) { +test('Testing "atRule" critical result', (t) => { compareCritical(t, 'atRule') t.end() }) -test('Testing "atRule" non-critical result', function (t) { +test('Testing "atRule" non-critical result', (t) => { compareCritical(t, 'atRule', true) t.end() }) -test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} critical result`), function (t) { +test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} critical result`), (t) => { compareCritical(t, 'atRule-wrapping') t.end() }) -test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} non-critical result`), function (t) { +test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} non-critical result`), (t) => { compareCritical(t, 'atRule-wrapping', true) t.end() }) -test('Testing "media" critical result', function (t) { +test('Testing "media" critical result', (t) => { compareCritical(t, 'media') t.end() }) -test('Testing "media" non-critical result', function (t) { +test('Testing "media" non-critical result', (t) => { compareCritical(t, 'media', true) t.end() }) -test(chalk.yellow(`Testing ${chalk.bold('scope')} critical result`), function (t) { +test(chalk.yellow(`Testing ${chalk.bold('scope')} critical result`), (t) => { compareCritical(t, 'scope') t.end() }) -test(chalk.yellow(`Testing ${chalk.bold('scope')} non-critical result`), function (t) { +test(chalk.yellow(`Testing ${chalk.bold('scope')} non-critical result`), (t) => { compareCritical(t, 'scope', true) t.end() }) From e7f8396c6a9267cab2edc98ed2d98a26e27e4dbe Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 16:07:18 -0500 Subject: [PATCH 4/9] fix some test node deprecation warnings --- test/preTest.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/preTest.js b/test/preTest.js index faea064..c3ee6c4 100644 --- a/test/preTest.js +++ b/test/preTest.js @@ -8,11 +8,21 @@ function cb (files) { function useFileData (data, file) { postcss([postcssCriticalCSS({outputPath: basePath})]) .process(data) - .then(result => fs.writeFile(`${basePath}/${file.split('.')[0]}.non-critical.actual.css`, result.css)) + .then(result => fs.writeFile( + `${basePath}/${file.split('.')[0]}.non-critical.actual.css`, + result.css, + 'utf8', + (err) => { + if (err) { + throw new Error(err) + } + })) } files.forEach(function (file) { if (file.indexOf('.actual') !== -1) { - fs.unlink(`${basePath}/${file}`) + fs.unlink(`${basePath}/${file}`, (err) => { + if (err) { throw new Error(err) } + }) } if (file.indexOf('.expected') === -1 && file.indexOf('.actual') === -1) { fs.readFile(`${basePath}/${file}`, 'utf8', (err, data) => { From 64afda5798d3a125e678b4af67197ac50edb26d6 Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 18:59:15 -0500 Subject: [PATCH 5/9] add err callback when writing file --- lib/index.js | 6 +++++- src/index.js | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index ff81dbe..3c2b849 100644 --- a/lib/index.js +++ b/lib/index.js @@ -47,7 +47,11 @@ function buildCritical(options) { criticalCSS.append(criticalOutput[cur]); (0, _postcss2.default)(args.minify ? [(0, _cssnano2.default)()] : []).process(criticalCSS).then(function (result) { if (!args.dryRun) { - _fs2.default.writeFile(_path2.default.join(args.outputPath, cur), result.css); + _fs2.default.writeFile(_path2.default.join(args.outputPath, cur), result.css, function (err) { + if (err) { + throw new Error(err); + } + }); } else { console.log( // eslint-disable-line no-console _chalk2.default.green('\nCritical CSS result is:\n' + _chalk2.default.yellow(result.css))); diff --git a/src/index.js b/src/index.js index edf4a03..7042db0 100644 --- a/src/index.js +++ b/src/index.js @@ -40,7 +40,12 @@ function buildCritical (options: ArgsType): Function { if (!args.dryRun) { fs.writeFile( path.join(args.outputPath, cur), - result.css + result.css, + (err) => { + if (err) { + throw new Error(err) + } + } ) } else { console.log( // eslint-disable-line no-console From f118e0932bfcd7b8c21bd3b3f145844c0430fec1 Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 18:59:35 -0500 Subject: [PATCH 6/9] fix flow type typos --- src/getCriticalDestination.js | 2 +- src/getCriticalRules.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/getCriticalDestination.js b/src/getCriticalDestination.js index 504894b..870ba83 100644 --- a/src/getCriticalDestination.js +++ b/src/getCriticalDestination.js @@ -7,7 +7,7 @@ * @param {string} Default output CSS file name. * @return {string} String corresponding to output destination. */ -export function getCriticalDestination (rule: Object, dest: String): string { +export function getCriticalDestination (rule: Object, dest: string): string { rule.walkDecls('critical-filename', (decl: Object) => { dest = decl.value.replace(/['"]*/g, '') decl.remove() diff --git a/src/getCriticalRules.js b/src/getCriticalRules.js index 2711eaf..de11206 100644 --- a/src/getCriticalRules.js +++ b/src/getCriticalRules.js @@ -12,7 +12,7 @@ import { getCriticalDestination } from './getCriticalDestination' * @param {string} Default output CSS file name. * @return {object} Object containing critical rules, organized by output destination */ -export function getCriticalRules (css: Object, shouldPreserve: boolean, defaultDest: String): Object { +export function getCriticalRules (css: Object, shouldPreserve: boolean, defaultDest: string): Object { const critical = getCriticalFromAtRule({ css }) css.walkDecls('critical-selector', (decl: Object) => { const dest = getCriticalDestination(decl.parent, defaultDest) From 4cc4b83b64597e6e6ccfa9ab06257df329b3522d Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 20:25:45 -0500 Subject: [PATCH 7/9] add default test --- test/fixtures/critical.css | 1 + test/fixtures/default.critical.expected.css | 1 + test/fixtures/default.css | 8 ++++++ test/fixtures/default.non-critical.actual.css | 6 +++++ .../default.non-critical.expected.css | 6 +++++ test/index.js | 15 ++++++++++- test/preTest.js | 27 ++++++++++--------- 7 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 test/fixtures/critical.css create mode 100644 test/fixtures/default.critical.expected.css create mode 100644 test/fixtures/default.css create mode 100644 test/fixtures/default.non-critical.actual.css create mode 100644 test/fixtures/default.non-critical.expected.css diff --git a/test/fixtures/critical.css b/test/fixtures/critical.css new file mode 100644 index 0000000..24dc7a1 --- /dev/null +++ b/test/fixtures/critical.css @@ -0,0 +1 @@ +.default{color:orange;float:left;margin-right:1em;width:100%} \ No newline at end of file diff --git a/test/fixtures/default.critical.expected.css b/test/fixtures/default.critical.expected.css new file mode 100644 index 0000000..51c6065 --- /dev/null +++ b/test/fixtures/default.critical.expected.css @@ -0,0 +1 @@ +.default{color:orange;float:left;margin-right:1em;width:100%} diff --git a/test/fixtures/default.css b/test/fixtures/default.css new file mode 100644 index 0000000..a755513 --- /dev/null +++ b/test/fixtures/default.css @@ -0,0 +1,8 @@ +@critical; + +.default { + color: orange; + float: left; + margin-right: 1em; + width: 100%; +} diff --git a/test/fixtures/default.non-critical.actual.css b/test/fixtures/default.non-critical.actual.css new file mode 100644 index 0000000..351a862 --- /dev/null +++ b/test/fixtures/default.non-critical.actual.css @@ -0,0 +1,6 @@ +.default { + color: orange; + float: left; + margin-right: 1em; + width: 100%; +} diff --git a/test/fixtures/default.non-critical.expected.css b/test/fixtures/default.non-critical.expected.css new file mode 100644 index 0000000..351a862 --- /dev/null +++ b/test/fixtures/default.non-critical.expected.css @@ -0,0 +1,6 @@ +.default { + color: orange; + float: left; + margin-right: 1em; + width: 100%; +} diff --git a/test/index.js b/test/index.js index df358f7..ff55e9e 100644 --- a/test/index.js +++ b/test/index.js @@ -6,9 +6,12 @@ const basePath = `${process.cwd()}/test/fixtures` const chalk = require('chalk') function compareCritical (t, name, testNonCritical) { + const actualFilename = -1 !== name.indexOf('default') && !testNonCritical + ? 'critical' + : `${name}.${testNonCritical ? 'non-critical.actual' : 'critical.actual'}` t.equal( fs.readFileSync( -`${basePath}/${name}.${testNonCritical ? 'non-critical.actual' : 'critical.actual'}.css`, 'utf8' +`${basePath}/${actualFilename}.css`, 'utf8' ).trim(), fs.readFileSync( `${basePath}/${name}.${testNonCritical ? 'non-critical.expected' : 'critical.expected'}.css`, 'utf8' @@ -17,6 +20,16 @@ function compareCritical (t, name, testNonCritical) { ) } +test('Testing default critical result', (t) => { + compareCritical(t, 'default') + t.end() +}) + +test('Testing default non-critical result', (t) => { + compareCritical(t, 'default', true) + t.end() +}) + test('Testing "this" critical result', (t) => { compareCritical(t, 'this') t.end() diff --git a/test/preTest.js b/test/preTest.js index c3ee6c4..e2f11f6 100644 --- a/test/preTest.js +++ b/test/preTest.js @@ -19,18 +19,21 @@ function cb (files) { })) } files.forEach(function (file) { - if (file.indexOf('.actual') !== -1) { - fs.unlink(`${basePath}/${file}`, (err) => { - if (err) { throw new Error(err) } - }) - } - if (file.indexOf('.expected') === -1 && file.indexOf('.actual') === -1) { - fs.readFile(`${basePath}/${file}`, 'utf8', (err, data) => { - if (err) { - throw new Error(err) - } - useFileData(data, file) - }) + // Ignore any critical.css file(s) already written + if (file !== 'critical.css') { + if (file.indexOf('.actual') !== -1) { + fs.unlink(`${basePath}/${file}`, (err) => { + if (err) { throw new Error(err) } + }) + } + if (file.indexOf('.expected') === -1 && file.indexOf('.actual') === -1) { + fs.readFile(`${basePath}/${file}`, 'utf8', (err, data) => { + if (err) { + throw new Error(err) + } + useFileData(data, file) + }) + } } }) } From b1f166059866b840b2bebf2accc866e46d2b9dcf Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 20:29:02 -0500 Subject: [PATCH 8/9] fix a yoda --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index ff55e9e..60416ec 100644 --- a/test/index.js +++ b/test/index.js @@ -6,7 +6,7 @@ const basePath = `${process.cwd()}/test/fixtures` const chalk = require('chalk') function compareCritical (t, name, testNonCritical) { - const actualFilename = -1 !== name.indexOf('default') && !testNonCritical + const actualFilename = name.indexOf('default') !== -1 && !testNonCritical ? 'critical' : `${name}.${testNonCritical ? 'non-critical.actual' : 'critical.actual'}` t.equal( From cb9643704df9071ea058cd6d5ade276f0958ac97 Mon Sep 17 00:00:00 2001 From: Zach Green Date: Thu, 29 Dec 2016 20:39:08 -0500 Subject: [PATCH 9/9] 2.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8d783f0..dc34145 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postcss-critical-css", - "version": "2.1.0", + "version": "2.1.1", "description": "Generate critical CSS using PostCSS", "main": "index.js", "repository": {