Skip to content

Commit 394bc65

Browse files
Merge pull request #157 from conveyal/dev
v3.7.0
2 parents df75f0e + 32447b9 commit 394bc65

File tree

11 files changed

+102
-64
lines changed

11 files changed

+102
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`build development should transform css 1`] = `345`;
4+
5+
exports[`build development should transform js 1`] = `157929`;
6+
7+
exports[`build production should transform and minify js and css 1`] = `156127`;
8+
9+
exports[`build production should transform and minify js and css 2`] = `345`;

__tests__/lib/__snapshots__/jest.js.snap

+22-24
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@
22

33
exports[`test.js generateTestConfig should generate proper config 1`] = `
44
Array [
5-
"-u",
5+
"--forceExit",
6+
"--updateSnapshot",
67
"--no-cache",
7-
"-i",
8+
"--runInBand",
89
"--config",
10+
Object {
11+
"cacheDirectory": "tmp",
12+
"collectCoverageFrom": Array [
13+
"lib/**/*.js",
14+
"bin",
15+
"src",
16+
"another-folder",
17+
],
18+
"coverageDirectory": "coverage",
19+
"notify": true,
20+
"setupFiles": Array [
21+
"beforeTestsSetup.js",
22+
],
23+
"testEnvironment": "node",
24+
"testPathIgnorePatterns": Array [
25+
"<rootDir>/node_modules/",
26+
"<rootDir>/__tests__/test-utils",
27+
],
28+
},
929
"these",
1030
"files",
1131
"only",
1232
]
1333
`;
14-
15-
exports[`test.js generateTestConfig should generate proper config 2`] = `
16-
Object {
17-
"cacheDirectory": "tmp",
18-
"collectCoverageFrom": Array [
19-
"lib/**/*.js",
20-
"bin",
21-
"src",
22-
"another-folder",
23-
],
24-
"coverageDirectory": "coverage",
25-
"notify": true,
26-
"setupFiles": Array [
27-
"beforeTestsSetup.js",
28-
],
29-
"testEnvironment": "node",
30-
"testPathIgnorePatterns": Array [
31-
"<rootDir>/node_modules/",
32-
"<rootDir>/__tests__/test-utils",
33-
],
34-
}
35-
`;

__tests__/lib/build.js

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* globals afterEach, beforeEach, describe, it, expect, jasmine */
22

33
const build = require('../../lib/build')
4+
const loadConfig = require('../../lib/load-config')
45
const util = require('../test-utils/util.js')
56

67
const originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
@@ -16,48 +17,45 @@ describe('build', () => {
1617
})
1718

1819
describe('development', () => {
19-
it('should transform js', (done) => {
20-
const results = build({
21-
config: {},
20+
it('should transform js', async () => {
21+
const [result] = await build({
22+
config: loadConfig(process.cwd(), null, 'development'),
2223
files: [[`${mockDir}/index.js`]]
2324
})
2425

25-
results[0]
26-
.then((buf) => {
27-
expect(buf.toString().indexOf('MockTestComponentUniqueName')).to.not.equal(-1)
28-
done()
29-
})
30-
.catch(done)
26+
const transpiledString = result.toString()
27+
expect(transpiledString.indexOf('MockTestComponentUniqueName')).not.toBe(-1)
28+
expect(transpiledString.length).toMatchSnapshot()
3129
})
3230

3331
it('should transform css', async () => {
34-
const results = build({
35-
config: {},
32+
const [result] = await build({
33+
config: loadConfig(process.cwd(), null, 'development'),
3634
files: [[`${mockDir}/index.css`]]
3735
})
3836

39-
const result = await results[0]
4037
const css = result.css
4138
expect(css.indexOf('criticalClass')).toBeGreaterThan(-1)
39+
expect(css.length).toMatchSnapshot()
4240
})
4341
})
4442

4543
describe('production', () => {
46-
it('should transform and minify js', async () => {
47-
const results = build({
48-
config: {},
44+
it('should transform and minify js and css', async () => {
45+
const [jsResult, cssResult] = await build({
46+
config: loadConfig(process.cwd(), null, 'production'),
4947
env: 'production',
5048
files: [
5149
[`${mockDir}/index.js`],
5250
[`${mockDir}/index.css`]
5351
],
5452
minify: true
5553
})
56-
57-
const output = await Promise.all(results)
58-
59-
expect(output[0].toString().indexOf('MockTestComponentUniqueName')).not.toBe(-1)
60-
expect(output[1].css.indexOf('criticalClass')).not.toBe(-1)
54+
const transpiledString = jsResult.toString()
55+
expect(transpiledString.indexOf('MockTestComponentUniqueName')).not.toBe(-1)
56+
expect(cssResult.css.indexOf('criticalClass')).not.toBe(-1)
57+
expect(transpiledString.length).toMatchSnapshot()
58+
expect(cssResult.css.length).toMatchSnapshot()
6159
})
6260
})
6361
})

__tests__/lib/jest.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const jestUtils = require('../../lib/jest')
44

5+
const JEST_CONFIG_INDEX = 5
6+
57
describe('test.js', () => {
68
it('generateTestConfig should generate proper config', () => {
79
const cfg = jestUtils.generateTestConfig(['these', 'files', 'only'], {
@@ -12,12 +14,9 @@ describe('test.js', () => {
1214
testEnvironment: 'node',
1315
updateSnapshots: true
1416
})
15-
expect(cfg).toBeTruthy()
16-
expect(cfg.length).toEqual(8)
17-
const jestCfg = JSON.parse(cfg.splice(4, 1))
17+
cfg[JEST_CONFIG_INDEX] = JSON.parse(cfg[JEST_CONFIG_INDEX])
18+
expect(cfg[JEST_CONFIG_INDEX].transform['.*']).toContain('lib/jest-preprocessor.js')
19+
delete cfg[JEST_CONFIG_INDEX].transform
1820
expect(cfg).toMatchSnapshot()
19-
expect(jestCfg.transform['.*']).toContain('lib/jest-preprocessor.js')
20-
delete jestCfg.transform
21-
expect(jestCfg).toMatchSnapshot()
2221
})
2322
})

__tests__/lib/load-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const loadConfig = require('../../lib/load-config')
44

55
describe('load-config', () => {
66
it('should work without any config files present', () => {
7-
const config = loadConfig(process.cwd(), '~/mastarm/configurations/default', 'test')
7+
const config = loadConfig(process.cwd(), 'configurations/default', 'test')
88
expect(config.path).toContain('mastarm/configurations/default')
99
delete config.path
1010
expect(config).toMatchSnapshot()

__tests__/lib/push-to-s3.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ describe('lib > push to s3', () => {
99
const createLogger = require('../../lib/logger')
1010

1111
it('should compile JavaScript and CSS and send to s3 via aws-sdk', () => {
12-
const config = loadConfig(process.cwd(), '~/mastarm/configurations/default', 'test')
12+
const config = loadConfig(process.cwd(), 'configurations/default', 'development')
1313
const push = configPush({
14-
env: 'test',
14+
env: 'development',
1515
config,
1616
log: createLogger(),
1717
minify: false,

__tests__/test-utils/mocks/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uuid from 'uuid'
2+
import png from '../../../mastarm.png'
23

34
export default class MockTestComponentUniqueName {
45
static defaultProps = {
@@ -11,3 +12,4 @@ export default class MockTestComponentUniqueName {
1112
}
1213

1314
console.log(uuid.v4())
15+
console.log(png.length)

bin/mastarm

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,30 @@ commander
2525
const get = util.makeGetFn([options, commander, config.settings])
2626
const files = util.parseEntries([...entries, ...(get('entries') || [])], get('outdir'))
2727
util.assertEntriesExist(files)
28+
const watch = get('watch')
2829
const opts = {
2930
config,
3031
env: get('env'),
3132
files,
3233
flyle: get('flyle'),
3334
minify: get('minify'),
3435
proxy: get('proxy'),
35-
watch: get('watch')
36+
watch
3637
}
3738
if (get('serve')) {
3839
const budo = require('../lib/budo')
3940
budo(opts)
4041
} else {
4142
const build = require('../lib/build')
4243
build(opts)
44+
.then((results) => {
45+
console.log('done building...')
46+
if (!watch) process.exit(0)
47+
})
48+
.catch((err) => {
49+
console.error(err)
50+
if (!watch) process.exit(1)
51+
})
4352
}
4453
})
4554

lib/build.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ module.exports = function ({
1919
minify,
2020
watch
2121
}) {
22-
return files.map(([entry, outfile]) =>
22+
return Promise.all(files.map(([entry, outfile]) =>
2323
path.extname(entry) === '.css'
2424
? buildCss({config, entry, outfile, watch})
25-
: buildJs({config, entry, env, minify, outfile, watch}))
25+
: buildJs({config, entry, env, minify, outfile, watch})))
2626
}
2727

2828
/**
@@ -34,11 +34,10 @@ function buildJs ({config, entry, env, minify, outfile, watch}) {
3434
const pipeline = browserify({config, entry, env, minify})
3535
const bundle = () => {
3636
return new Promise((resolve, reject) => {
37-
const stream = pipeline
38-
.bundle((err, buf) => {
39-
if (err) reject(err)
40-
else resolve(buf)
41-
})
37+
const stream = pipeline.bundle((err, buf) => {
38+
if (err) reject(err)
39+
else resolve(buf)
40+
})
4241

4342
if (outfile) {
4443
mkdirp.sync(path.dirname(outfile))

lib/jest.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ module.exports.generateTestConfig = (patterns, options) => {
3232
})
3333

3434
// jest cli params
35-
let jestArguments = []
35+
let jestArguments = ['--forceExit']
3636
if (options.updateSnapshots) {
37-
jestArguments.push('-u')
37+
jestArguments.push('--updateSnapshot')
3838
}
3939

4040
if (options.cache === false) {
4141
jestArguments.push('--no-cache')
4242
}
4343

4444
if (options.runInBand || process.env.CI) {
45-
jestArguments.push('-i')
45+
jestArguments.push('--runInBand')
4646
}
4747

4848
jestArguments.push('--config', JSON.stringify(jestConfig))

lib/js-transform.js

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module.exports = function transform ({
1414
util.configureEnvironment({config, env})
1515

1616
return [
17+
svgToString,
18+
imagesToBase64String,
1719
htmlTransform,
1820
markdown({
1921
html: true
@@ -24,6 +26,28 @@ module.exports = function transform ({
2426
]
2527
}
2628

29+
function svgToString (filename) {
30+
if (!/\.svg$/i.test(filename)) {
31+
return through()
32+
}
33+
34+
return through(function (buf, enc, next) {
35+
this.push('module.exports=' + JSON.stringify(buf.toString('utf8')))
36+
next()
37+
})
38+
}
39+
40+
function imagesToBase64String (filename) {
41+
if (!/\.png|\.jpg|\.jpeg|\.gif$/i.test(filename)) {
42+
return through()
43+
}
44+
45+
return through(function (buf, enc, next) {
46+
this.push('module.exports=' + JSON.stringify(buf.toString('base64')))
47+
next()
48+
})
49+
}
50+
2751
function htmlTransform (filename) {
2852
if (!/\.html$/i.test(filename)) {
2953
return through()

0 commit comments

Comments
 (0)