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 652232a..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 @@ -16,36 +17,33 @@ describe('build', () => { }) describe('development', () => { - it('should transform js', (done) => { - const results = build({ - config: {}, + it('should transform js', async () => { + const [result] = await build({ + config: loadConfig(process.cwd(), null, 'development'), files: [[`${mockDir}/index.js`]] }) - results[0] - .then((buf) => { - expect(buf.toString().indexOf('MockTestComponentUniqueName')).to.not.equal(-1) - done() - }) - .catch(done) + const transpiledString = result.toString() + expect(transpiledString.indexOf('MockTestComponentUniqueName')).not.toBe(-1) + expect(transpiledString.length).toMatchSnapshot() }) it('should transform css', async () => { - const results = build({ - config: {}, + const [result] = await build({ + config: loadConfig(process.cwd(), null, 'development'), files: [[`${mockDir}/index.css`]] }) - const result = await results[0] const css = result.css expect(css.indexOf('criticalClass')).toBeGreaterThan(-1) + expect(css.length).toMatchSnapshot() }) }) describe('production', () => { - it('should transform and minify js', async () => { - const results = 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`], @@ -53,11 +51,11 @@ 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) + 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/bin/mastarm b/bin/mastarm index cb6a752..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') @@ -40,6 +41,14 @@ commander } else { const build = require('../lib/build') build(opts) + .then((results) => { + console.log('done building...') + if (!watch) process.exit(0) + }) + .catch((err) => { + console.error(err) + if (!watch) process.exit(1) + }) } }) diff --git a/lib/build.js b/lib/build.js index 413d76e..ed4f82f 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}))) } /** @@ -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)) diff --git a/lib/js-transform.js b/lib/js-transform.js index 846c2b8..22dcc9d 100644 --- a/lib/js-transform.js +++ b/lib/js-transform.js @@ -14,6 +14,8 @@ module.exports = function transform ({ util.configureEnvironment({config, env}) return [ + svgToString, + imagesToBase64String, htmlTransform, markdown({ html: true @@ -24,6 +26,28 @@ module.exports = function transform ({ ] } +function svgToString (filename) { + if (!/\.svg$/i.test(filename)) { + return through() + } + + return through(function (buf, enc, next) { + 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() + }) +} + function htmlTransform (filename) { if (!/\.html$/i.test(filename)) { return through()