Skip to content

Commit

Permalink
Merge pull request #157 from conveyal/dev
Browse files Browse the repository at this point in the history
v3.7.0
  • Loading branch information
trevorgerhardt authored Mar 30, 2017
2 parents df75f0e + 32447b9 commit 394bc65
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 64 deletions.
9 changes: 9 additions & 0 deletions __tests__/lib/__snapshots__/build.js.snap
Original file line number Diff line number Diff line change
@@ -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`;
46 changes: 22 additions & 24 deletions __tests__/lib/__snapshots__/jest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
"<rootDir>/node_modules/",
"<rootDir>/__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 [
"<rootDir>/node_modules/",
"<rootDir>/__tests__/test-utils",
],
}
`;
38 changes: 18 additions & 20 deletions __tests__/lib/build.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,48 +17,45 @@ 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`],
[`${mockDir}/index.css`]
],
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()
})
})
})
11 changes: 5 additions & 6 deletions __tests__/lib/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'], {
Expand All @@ -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()
})
})
2 changes: 1 addition & 1 deletion __tests__/lib/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions __tests__/lib/push-to-s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions __tests__/test-utils/mocks/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import uuid from 'uuid'
import png from '../../../mastarm.png'

export default class MockTestComponentUniqueName {
static defaultProps = {
Expand All @@ -11,3 +12,4 @@ export default class MockTestComponentUniqueName {
}

console.log(uuid.v4())
console.log(png.length)
11 changes: 10 additions & 1 deletion bin/mastarm
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,30 @@ 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'),
files,
flyle: get('flyle'),
minify: get('minify'),
proxy: get('proxy'),
watch: get('watch')
watch
}
if (get('serve')) {
const budo = require('../lib/budo')
budo(opts)
} 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)
})
}
})

Expand Down
13 changes: 6 additions & 7 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})))
}

/**
Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions lib/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ 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) {
jestArguments.push('--no-cache')
}

if (options.runInBand || process.env.CI) {
jestArguments.push('-i')
jestArguments.push('--runInBand')
}

jestArguments.push('--config', JSON.stringify(jestConfig))
Expand Down
24 changes: 24 additions & 0 deletions lib/js-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = function transform ({
util.configureEnvironment({config, env})

return [
svgToString,
imagesToBase64String,
htmlTransform,
markdown({
html: true
Expand All @@ -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()
Expand Down

0 comments on commit 394bc65

Please sign in to comment.