From 1c0e502af209970d48b5760f96250b9600349fb4 Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Tue, 28 Mar 2017 15:22:14 +0200 Subject: [PATCH 1/6] refactor(build): Ensure the build finishes and print "done" when complete. --- bin/mastarm | 8 ++++++++ lib/build.js | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/bin/mastarm b/bin/mastarm index cb6a752..28f531c 100755 --- a/bin/mastarm +++ b/bin/mastarm @@ -40,6 +40,14 @@ commander } else { const build = require('../lib/build') build(opts) + .then((results) => { + console.log('done building...') + process.exit(0) + }) + .catch((err) => { + console.error(err) + process.exit(1) + }) } }) diff --git a/lib/build.js b/lib/build.js index 413d76e..d09117a 100644 --- a/lib/build.js +++ b/lib/build.js @@ -19,10 +19,10 @@ module.exports = function ({ minify, watch }) { - return files.map(([entry, outfile]) => + return Promise.all(files.map(([entry, outfile]) => path.extname(entry) === '.css' ? buildCss({config, entry, outfile, watch}) - : buildJs({config, entry, env, minify, outfile, watch})) + : buildJs({config, entry, env, minify, outfile, watch}))) } /** From 170e4f9effffdb420bdbcc8b07c136a7d70f8892 Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Tue, 28 Mar 2017 15:50:07 +0200 Subject: [PATCH 2/6] feat(images): require() SVGs as strings, PNG/JPG/GIFs as base64 encoded strings --- lib/js-transform.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/js-transform.js b/lib/js-transform.js index 846c2b8..42cfa4c 100644 --- a/lib/js-transform.js +++ b/lib/js-transform.js @@ -1,6 +1,7 @@ const babelify = require('babelify') const envify = require('envify/custom') const markdown = require('browserify-markdown') +const path = require('path') const through = require('through2') const YAML = require('yamljs') @@ -14,6 +15,7 @@ module.exports = function transform ({ util.configureEnvironment({config, env}) return [ + imageToString, htmlTransform, markdown({ html: true @@ -24,6 +26,21 @@ module.exports = function transform ({ ] } +function imageToString (filename) { + if (!/\.svg|\.png|\.jpg|\.jpeg|\.gif$/i.test(filename)) { + return through() + } + + return through(function (buf, enc, next) { + if (path.extname(filename) === '.svg') { + this.push('module.exports=' + JSON.stringify(buf.toString('utf8'))) + } else { + this.push('module.exports=' + JSON.stringify(buf.toString('base64'))) + } + next() + }) +} + function htmlTransform (filename) { if (!/\.html$/i.test(filename)) { return through() From 816aeafca3a234a73981495cc2a9d696f75228fb Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Tue, 28 Mar 2017 15:53:01 +0200 Subject: [PATCH 3/6] refactor(images): Split up SVG and other image transforms --- lib/js-transform.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/js-transform.js b/lib/js-transform.js index 42cfa4c..22dcc9d 100644 --- a/lib/js-transform.js +++ b/lib/js-transform.js @@ -1,7 +1,6 @@ const babelify = require('babelify') const envify = require('envify/custom') const markdown = require('browserify-markdown') -const path = require('path') const through = require('through2') const YAML = require('yamljs') @@ -15,7 +14,8 @@ module.exports = function transform ({ util.configureEnvironment({config, env}) return [ - imageToString, + svgToString, + imagesToBase64String, htmlTransform, markdown({ html: true @@ -26,17 +26,24 @@ module.exports = function transform ({ ] } -function imageToString (filename) { - if (!/\.svg|\.png|\.jpg|\.jpeg|\.gif$/i.test(filename)) { +function svgToString (filename) { + if (!/\.svg$/i.test(filename)) { return through() } return through(function (buf, enc, next) { - if (path.extname(filename) === '.svg') { - this.push('module.exports=' + JSON.stringify(buf.toString('utf8'))) - } else { - this.push('module.exports=' + JSON.stringify(buf.toString('base64'))) - } + this.push('module.exports=' + JSON.stringify(buf.toString('utf8'))) + next() + }) +} + +function imagesToBase64String (filename) { + if (!/\.png|\.jpg|\.jpeg|\.gif$/i.test(filename)) { + return through() + } + + return through(function (buf, enc, next) { + this.push('module.exports=' + JSON.stringify(buf.toString('base64'))) next() }) } From 1e82ee59ed03e439fcdb271f0419ccdd5fb5f7a0 Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Tue, 28 Mar 2017 16:39:37 +0200 Subject: [PATCH 4/6] fix(watch): Don't exit on watch. --- bin/mastarm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/mastarm b/bin/mastarm index 28f531c..4602f31 100755 --- a/bin/mastarm +++ b/bin/mastarm @@ -25,6 +25,7 @@ commander const get = util.makeGetFn([options, commander, config.settings]) const files = util.parseEntries([...entries, ...(get('entries') || [])], get('outdir')) util.assertEntriesExist(files) + const watch = get('watch') const opts = { config, env: get('env'), @@ -32,7 +33,7 @@ commander flyle: get('flyle'), minify: get('minify'), proxy: get('proxy'), - watch: get('watch') + watch } if (get('serve')) { const budo = require('../lib/budo') @@ -42,11 +43,11 @@ commander build(opts) .then((results) => { console.log('done building...') - process.exit(0) + if (!watch) process.exit(0) }) .catch((err) => { console.error(err) - process.exit(1) + if (!watch) process.exit(1) }) } }) From fce0fdbe0816f893c693a96e9ff56d8defe9c725 Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Tue, 28 Mar 2017 17:00:09 +0200 Subject: [PATCH 5/6] test(build): Update build tests to use new api --- __tests__/lib/build.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/__tests__/lib/build.js b/__tests__/lib/build.js index 652232a..f4b8a69 100644 --- a/__tests__/lib/build.js +++ b/__tests__/lib/build.js @@ -16,27 +16,23 @@ describe('build', () => { }) describe('development', () => { - it('should transform js', (done) => { - const results = build({ + it('should transform js', async () => { + const [result] = await build({ config: {}, files: [[`${mockDir}/index.js`]] }) - results[0] - .then((buf) => { - expect(buf.toString().indexOf('MockTestComponentUniqueName')).to.not.equal(-1) - done() - }) - .catch(done) + console.log(result) + + expect(result.toString().indexOf('MockTestComponentUniqueName')).not.toBe(-1) }) it('should transform css', async () => { - const results = build({ + const [result] = await build({ config: {}, files: [[`${mockDir}/index.css`]] }) - const result = await results[0] const css = result.css expect(css.indexOf('criticalClass')).toBeGreaterThan(-1) }) @@ -44,7 +40,7 @@ describe('build', () => { describe('production', () => { it('should transform and minify js', async () => { - const results = build({ + const output = await build({ config: {}, env: 'production', files: [ @@ -54,8 +50,6 @@ describe('build', () => { minify: true }) - const output = await Promise.all(results) - expect(output[0].toString().indexOf('MockTestComponentUniqueName')).not.toBe(-1) expect(output[1].css.indexOf('criticalClass')).not.toBe(-1) }) From 006b8886ab77756c2e3f28a6c709fe1539e5e87c Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Wed, 29 Mar 2017 11:14:04 +0200 Subject: [PATCH 6/6] test(images): Load images in the JS transpilation test --- __tests__/lib/__snapshots__/build.js.snap | 9 +++++ __tests__/lib/__snapshots__/jest.js.snap | 46 +++++++++++------------ __tests__/lib/build.js | 26 +++++++------ __tests__/lib/jest.js | 11 +++--- __tests__/lib/load-config.js | 2 +- __tests__/lib/push-to-s3.js | 4 +- __tests__/test-utils/mocks/index.js | 2 + lib/build.js | 9 ++--- lib/jest.js | 6 +-- 9 files changed, 63 insertions(+), 52 deletions(-) create mode 100644 __tests__/lib/__snapshots__/build.js.snap diff --git a/__tests__/lib/__snapshots__/build.js.snap b/__tests__/lib/__snapshots__/build.js.snap new file mode 100644 index 0000000..c8966eb --- /dev/null +++ b/__tests__/lib/__snapshots__/build.js.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`build development should transform css 1`] = `345`; + +exports[`build development should transform js 1`] = `157929`; + +exports[`build production should transform and minify js and css 1`] = `156127`; + +exports[`build production should transform and minify js and css 2`] = `345`; diff --git a/__tests__/lib/__snapshots__/jest.js.snap b/__tests__/lib/__snapshots__/jest.js.snap index 80064c9..a83fa8f 100644 --- a/__tests__/lib/__snapshots__/jest.js.snap +++ b/__tests__/lib/__snapshots__/jest.js.snap @@ -2,34 +2,32 @@ exports[`test.js generateTestConfig should generate proper config 1`] = ` Array [ - "-u", + "--forceExit", + "--updateSnapshot", "--no-cache", - "-i", + "--runInBand", "--config", + Object { + "cacheDirectory": "tmp", + "collectCoverageFrom": Array [ + "lib/**/*.js", + "bin", + "src", + "another-folder", + ], + "coverageDirectory": "coverage", + "notify": true, + "setupFiles": Array [ + "beforeTestsSetup.js", + ], + "testEnvironment": "node", + "testPathIgnorePatterns": Array [ + "/node_modules/", + "/__tests__/test-utils", + ], + }, "these", "files", "only", ] `; - -exports[`test.js generateTestConfig should generate proper config 2`] = ` -Object { - "cacheDirectory": "tmp", - "collectCoverageFrom": Array [ - "lib/**/*.js", - "bin", - "src", - "another-folder", - ], - "coverageDirectory": "coverage", - "notify": true, - "setupFiles": Array [ - "beforeTestsSetup.js", - ], - "testEnvironment": "node", - "testPathIgnorePatterns": Array [ - "/node_modules/", - "/__tests__/test-utils", - ], -} -`; diff --git a/__tests__/lib/build.js b/__tests__/lib/build.js index f4b8a69..e09e800 100644 --- a/__tests__/lib/build.js +++ b/__tests__/lib/build.js @@ -1,6 +1,7 @@ /* globals afterEach, beforeEach, describe, it, expect, jasmine */ const build = require('../../lib/build') +const loadConfig = require('../../lib/load-config') const util = require('../test-utils/util.js') const originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL @@ -18,30 +19,31 @@ describe('build', () => { describe('development', () => { it('should transform js', async () => { const [result] = await build({ - config: {}, + config: loadConfig(process.cwd(), null, 'development'), files: [[`${mockDir}/index.js`]] }) - console.log(result) - - expect(result.toString().indexOf('MockTestComponentUniqueName')).not.toBe(-1) + const transpiledString = result.toString() + expect(transpiledString.indexOf('MockTestComponentUniqueName')).not.toBe(-1) + expect(transpiledString.length).toMatchSnapshot() }) it('should transform css', async () => { const [result] = await build({ - config: {}, + config: loadConfig(process.cwd(), null, 'development'), files: [[`${mockDir}/index.css`]] }) const css = result.css expect(css.indexOf('criticalClass')).toBeGreaterThan(-1) + expect(css.length).toMatchSnapshot() }) }) describe('production', () => { - it('should transform and minify js', async () => { - const output = await build({ - config: {}, + it('should transform and minify js and css', async () => { + const [jsResult, cssResult] = await build({ + config: loadConfig(process.cwd(), null, 'production'), env: 'production', files: [ [`${mockDir}/index.js`], @@ -49,9 +51,11 @@ describe('build', () => { ], minify: true }) - - expect(output[0].toString().indexOf('MockTestComponentUniqueName')).not.toBe(-1) - expect(output[1].css.indexOf('criticalClass')).not.toBe(-1) + const transpiledString = jsResult.toString() + expect(transpiledString.indexOf('MockTestComponentUniqueName')).not.toBe(-1) + expect(cssResult.css.indexOf('criticalClass')).not.toBe(-1) + expect(transpiledString.length).toMatchSnapshot() + expect(cssResult.css.length).toMatchSnapshot() }) }) }) diff --git a/__tests__/lib/jest.js b/__tests__/lib/jest.js index 5e1af9f..21ecb69 100644 --- a/__tests__/lib/jest.js +++ b/__tests__/lib/jest.js @@ -2,6 +2,8 @@ const jestUtils = require('../../lib/jest') +const JEST_CONFIG_INDEX = 5 + describe('test.js', () => { it('generateTestConfig should generate proper config', () => { const cfg = jestUtils.generateTestConfig(['these', 'files', 'only'], { @@ -12,12 +14,9 @@ describe('test.js', () => { testEnvironment: 'node', updateSnapshots: true }) - expect(cfg).toBeTruthy() - expect(cfg.length).toEqual(8) - const jestCfg = JSON.parse(cfg.splice(4, 1)) + cfg[JEST_CONFIG_INDEX] = JSON.parse(cfg[JEST_CONFIG_INDEX]) + expect(cfg[JEST_CONFIG_INDEX].transform['.*']).toContain('lib/jest-preprocessor.js') + delete cfg[JEST_CONFIG_INDEX].transform expect(cfg).toMatchSnapshot() - expect(jestCfg.transform['.*']).toContain('lib/jest-preprocessor.js') - delete jestCfg.transform - expect(jestCfg).toMatchSnapshot() }) }) diff --git a/__tests__/lib/load-config.js b/__tests__/lib/load-config.js index 7095765..66b3ff1 100644 --- a/__tests__/lib/load-config.js +++ b/__tests__/lib/load-config.js @@ -4,7 +4,7 @@ const loadConfig = require('../../lib/load-config') describe('load-config', () => { it('should work without any config files present', () => { - const config = loadConfig(process.cwd(), '~/mastarm/configurations/default', 'test') + const config = loadConfig(process.cwd(), 'configurations/default', 'test') expect(config.path).toContain('mastarm/configurations/default') delete config.path expect(config).toMatchSnapshot() diff --git a/__tests__/lib/push-to-s3.js b/__tests__/lib/push-to-s3.js index f50f401..58a00ec 100644 --- a/__tests__/lib/push-to-s3.js +++ b/__tests__/lib/push-to-s3.js @@ -9,9 +9,9 @@ describe('lib > push to s3', () => { const createLogger = require('../../lib/logger') it('should compile JavaScript and CSS and send to s3 via aws-sdk', () => { - const config = loadConfig(process.cwd(), '~/mastarm/configurations/default', 'test') + const config = loadConfig(process.cwd(), 'configurations/default', 'development') const push = configPush({ - env: 'test', + env: 'development', config, log: createLogger(), minify: false, diff --git a/__tests__/test-utils/mocks/index.js b/__tests__/test-utils/mocks/index.js index 0639652..4b39b56 100644 --- a/__tests__/test-utils/mocks/index.js +++ b/__tests__/test-utils/mocks/index.js @@ -1,4 +1,5 @@ import uuid from 'uuid' +import png from '../../../mastarm.png' export default class MockTestComponentUniqueName { static defaultProps = { @@ -11,3 +12,4 @@ export default class MockTestComponentUniqueName { } console.log(uuid.v4()) +console.log(png.length) diff --git a/lib/build.js b/lib/build.js index d09117a..ed4f82f 100644 --- a/lib/build.js +++ b/lib/build.js @@ -34,11 +34,10 @@ function buildJs ({config, entry, env, minify, outfile, watch}) { const pipeline = browserify({config, entry, env, minify}) const bundle = () => { return new Promise((resolve, reject) => { - const stream = pipeline - .bundle((err, buf) => { - if (err) reject(err) - else resolve(buf) - }) + const stream = pipeline.bundle((err, buf) => { + if (err) reject(err) + else resolve(buf) + }) if (outfile) { mkdirp.sync(path.dirname(outfile)) diff --git a/lib/jest.js b/lib/jest.js index 84507d8..b2045d8 100644 --- a/lib/jest.js +++ b/lib/jest.js @@ -32,9 +32,9 @@ module.exports.generateTestConfig = (patterns, options) => { }) // jest cli params - let jestArguments = [] + let jestArguments = ['--forceExit'] if (options.updateSnapshots) { - jestArguments.push('-u') + jestArguments.push('--updateSnapshot') } if (options.cache === false) { @@ -42,7 +42,7 @@ module.exports.generateTestConfig = (patterns, options) => { } if (options.runInBand || process.env.CI) { - jestArguments.push('-i') + jestArguments.push('--runInBand') } jestArguments.push('--config', JSON.stringify(jestConfig))