Skip to content

Commit a8b393c

Browse files
committed
feat: add experimental browser build and karma setup
new babel config, new webpack config, and karma config all behind a experimental flag BREAKING CHANGE: webpack bumped to version 4+
1 parent 46ba716 commit a8b393c

21 files changed

+467
-195
lines changed

cli.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict'
44

55
const updateNotifier = require('update-notifier')
6+
const chalk = require('chalk')
67
const pkg = require('./package.json')
78

89
updateNotifier({
@@ -15,4 +16,20 @@ require('yargs') // eslint-disable-line
1516
.commandDir('cmds')
1617
.demandCommand()
1718
.help()
19+
.fail((msg, err, yargs) => {
20+
// errors from execa output the child_process stderr
21+
if (err && err.stderr) {
22+
console.error('Error running command: ', err.cmd, '\n')
23+
console.error(err.stderr)
24+
} else {
25+
if (msg) {
26+
console.error(chalk.red(msg))
27+
}
28+
if (err) {
29+
console.error(chalk.red(err.message))
30+
console.error(chalk.gray(err.stack))
31+
}
32+
}
33+
process.exit(1)
34+
})
1835
.argv

cmds/build.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@ module.exports = {
1313
alias: 'n',
1414
describe: 'Build for node usage',
1515
default: false
16+
},
17+
'enable-experimental-browser-builds': {
18+
alias: 'eebb',
19+
describe: 'Use experimental webpack config',
20+
default: false
21+
},
22+
env: {
23+
describe: 'Sets NODE_ENV in the childprocess (NODE_ENV=production aegir build also works)',
24+
default: 'production'
1625
}
1726
},
1827
handler (argv) {
1928
const build = require('../src/build')
20-
const onError = require('../src/error-handler')
21-
build.run(argv).catch(onError)
29+
if (argv.eebb) {
30+
return require('./../src/build/experimental-browser')(argv)
31+
}
32+
return build.run(argv)
2233
}
2334
}

cmds/karma.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict'
2+
const resolveBin = require('resolve-bin')
3+
const execa = require('execa')
4+
const path = require('path')
5+
const userConfig = require('./../src/config/user')()
6+
const {hook} = require('./../src/utils')
7+
const here = p => path.join(__dirname, p)
8+
9+
module.exports = {
10+
command: 'karma',
11+
desc: 'Run karma browser tests',
12+
builder: {
13+
watch: {
14+
alias: 'w',
15+
describe: 'Watch for file changes',
16+
default: false
17+
},
18+
env: {
19+
describe: 'Sets NODE_ENV in the childprocess (NODE_ENV=dev aegir karma also works)',
20+
default: 'development'
21+
},
22+
webworker: {
23+
describe: 'Webworker enviroment for Karma',
24+
default: false
25+
}
26+
27+
},
28+
handler (argv) {
29+
const bin = resolveBin.sync('karma')
30+
const input = argv._.slice(1)
31+
const watch = argv.watch ? ['--auto-watch', '--no-single-run'] : []
32+
return hook('browser', 'pre')(userConfig)
33+
.then(() => {
34+
return execa(bin, [
35+
'start',
36+
...watch,
37+
here('./../src/config/karma2.conf.js'),
38+
...input
39+
], {
40+
env: {
41+
NODE_ENV: argv.env,
42+
AEGIR_WEBWORKER: argv.webworker
43+
},
44+
stdio: 'inherit'
45+
})
46+
})
47+
.then(process => hook('browser', 'post')(userConfig))
48+
}
49+
}

cmds/test.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ module.exports = {
1717
default: false
1818
},
1919
watch: {
20+
alias: 'w',
2021
describe: 'Watch files for changes and rerun tests',
2122
default: false
2223
},
23-
updateSnapshot: {
24-
alias: 'u',
25-
describe: 'Use this flag to re-record every snapshot that fails during this test run. (node only)'
26-
},
2724
files: {
2825
alias: 'f',
2926
describe: 'Custom globs for files to test',
@@ -52,11 +49,19 @@ module.exports = {
5249
alias: 'b',
5350
describe: 'Mocha should bail once a test fails',
5451
default: false
52+
},
53+
env: {
54+
describe: 'Sets NODE_ENV in the childprocess (NODE_ENV=dev aegir karma also works)',
55+
default: 'development'
56+
},
57+
'enable-experimental-karma': {
58+
alias: 'eek',
59+
describe: 'Use the experimental karma config',
60+
default: false
5561
}
5662
},
5763
handler (argv) {
5864
const test = require('../src/test')
59-
const onError = require('../src/error-handler')
60-
test.run(argv).catch(onError)
65+
return test.run(argv)
6166
}
6267
}

package.json

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,51 @@
2929
],
3030
"license": "MIT",
3131
"dependencies": {
32+
"@babel/cli": "7.0.0-beta.47",
33+
"@babel/core": "7.0.0-beta.47",
34+
"@babel/plugin-transform-regenerator": "7.0.0-beta.47",
35+
"@babel/plugin-transform-runtime": "7.0.0-beta.47",
36+
"@babel/preset-env": "7.0.0-beta.47",
37+
"@babel/preset-flow": "7.0.0-beta.47",
38+
"@babel/runtime": "7.0.0-beta.47",
39+
"arrify": "^1.0.1",
3240
"async": "^2.6.0",
33-
"browserify-zlib": "~0.2.0",
41+
"babel-loader": "8.0.0-beta.2",
42+
"browserify-zlib": "^0.2.0",
3443
"chalk": "^2.3.0",
3544
"clean-documentation-theme": "~0.5.2",
3645
"codecov": "^3.0.2",
3746
"conventional-changelog": "^1.1.7",
3847
"conventional-github-releaser": "^2.0.0",
3948
"coveralls": "^3.0.0",
49+
"del": "^3.0.0",
4050
"detect-node": "^2.0.3",
4151
"documentation": "^5.3.3",
4252
"es6-promisify": "^5.0.0",
4353
"eslint": "^4.13.0",
4454
"eslint-config-aegir": "^1.0.1",
45-
"execa": "~0.8.0",
55+
"execa": "^0.10.0",
4656
"filesize": "^3.5.11",
4757
"findup-sync": "^2.0.0",
4858
"fs-extra": "^4.0.3",
4959
"gh-pages": "^1.1.0",
5060
"glob": "^7.1.2",
5161
"joi": "^13.0.2",
52-
"json-loader": "~0.5.7",
53-
"karma": "^1.7.1",
62+
"json-loader": "^0.5.7",
63+
"karma": "^2.0.2",
5464
"karma-chrome-launcher": "^2.2.0",
65+
"karma-cli": "^1.0.1",
66+
"karma-edge-launcher": "^0.4.2",
5567
"karma-firefox-launcher": "^1.1.0",
5668
"karma-junit-reporter": "^1.2.0",
5769
"karma-mocha": "^1.3.0",
5870
"karma-mocha-own-reporter": "git+https://github.com/dryajov/karma-mocha-own-reporter#d562a92a12d5c76469a05d67cee19bcb8db22b23",
5971
"karma-mocha-webworker": "^1.3.0",
60-
"karma-sourcemap-loader": "~0.3.7",
61-
"karma-webpack": "^2.0.6",
62-
"listr": "~0.13.0",
63-
"listr-update-renderer": "~0.4.0",
64-
"listr-verbose-renderer": "~0.4.1",
72+
"karma-sourcemap-loader": "^0.3.7",
73+
"karma-webpack": "v4.0.0-beta.0",
74+
"listr": "^0.13.0",
75+
"listr-update-renderer": "^0.4.0",
76+
"listr-verbose-renderer": "^0.4.1",
6577
"lodash": "^4.17.4",
6678
"mocha": "^4.0.1",
6779
"mocha-jenkins-reporter": "~0.3.10",
@@ -71,6 +83,8 @@
7183
"pify": "^3.0.0",
7284
"pre-push": "~0.1.1",
7385
"pretty-hrtime": "^1.0.3",
86+
"read-pkg-up": "^3.0.0",
87+
"resolve-bin": "^0.4.0",
7488
"rimraf": "^2.6.2",
7589
"safe-buffer": "^5.1.1",
7690
"semver": "^5.4.1",
@@ -80,9 +94,11 @@
8094
"transform-loader": "~0.2.4",
8195
"uglify-es": "^3.2.2",
8296
"update-notifier": "^2.3.0",
83-
"webpack": "^3.10.0",
97+
"webpack": "^4.6.0",
98+
"webpack-cli": "^2.1.2",
8499
"webpack-merge": "^4.1.1",
85-
"yargs": "^10.0.3"
100+
"yargs": "^10.0.3",
101+
"yargs-parser": "^10.0.0"
86102
},
87103
"repository": {
88104
"type": "git",
@@ -118,5 +134,18 @@
118134
"cross-env": "^5.1.1",
119135
"mock-require": "^2.0.2",
120136
"sinon": "^4.1.3"
137+
},
138+
"browserslist": {
139+
"development": [
140+
"last 2 chrome versions",
141+
"last 2 firefox versions",
142+
"last 2 edge versions"
143+
],
144+
"production": [
145+
">1%",
146+
"last 2 versions",
147+
"Firefox ESR",
148+
"not ie < 11"
149+
]
121150
}
122151
}

src/__mocks__/utils.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/build/experimental-browser.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
const resolveBin = require('resolve-bin')
3+
const execa = require('execa')
4+
const path = require('path')
5+
const here = p => path.join(__dirname, p)
6+
const bin = resolveBin.sync('webpack-cli')
7+
8+
module.exports = (argv) => {
9+
const input = argv._.slice(1)
10+
const useBuiltinConfig = !input.includes('--config')
11+
const config = useBuiltinConfig
12+
? ['--config', here('../config/webpack.config.js')]
13+
: []
14+
return execa(bin, [
15+
...config,
16+
'--env.' + argv.env,
17+
...input
18+
], {
19+
env: {NODE_ENV: argv.env},
20+
stdio: 'inherit'
21+
})
22+
}

src/config/babelrc.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict'
2+
const {hasPkgProp, browserslist} = require('./../utils')
3+
const useBuiltinBrowserslist = !hasPkgProp('browserslist')
4+
5+
const validateBoolOption = (name, value, defaultValue) => {
6+
if (typeof value === 'undefined') {
7+
value = defaultValue
8+
}
9+
10+
if (typeof value !== 'boolean') {
11+
throw new Error(`Preset aegir: '${name}' option must be a boolean.`)
12+
}
13+
14+
return value
15+
}
16+
module.exports = function (api, opts = {}) {
17+
const env = process.env.BABEL_ENV || process.env.NODE_ENV
18+
const isEnvDevelopment = env === 'development'
19+
const isEnvProduction = env === 'production'
20+
const isEnvTest = env === 'test'
21+
const isFlowEnabled = validateBoolOption('flow', opts.flow, true)
22+
const targets = useBuiltinBrowserslist ? {
23+
browsers: browserslist[env]
24+
} : {}
25+
26+
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
27+
throw new Error(
28+
'Using `babel-preset-aegir` requires that you specify `NODE_ENV` or ' +
29+
'`BABEL_ENV` environment variables. Valid values are "development", ' +
30+
'"test", and "production". Instead, received: ' +
31+
JSON.stringify(env) +
32+
'.'
33+
)
34+
}
35+
36+
return {
37+
presets: [
38+
isEnvTest && [
39+
// ES features necessary for user's Node version
40+
require('@babel/preset-env').default,
41+
{
42+
targets: {
43+
node: '6.12'
44+
}
45+
}
46+
],
47+
(isEnvProduction || isEnvDevelopment) && [
48+
// Latest stable ECMAScript features
49+
require('@babel/preset-env').default,
50+
{
51+
// `entry` transforms `@babel/polyfill` into individual requires for
52+
// the targeted browsers. This is safer than `usage` which performs
53+
// static code analysis to determine what's required.
54+
// This is probably a fine default to help trim down bundles when
55+
// end-users inevitably import '@babel/polyfill'.
56+
useBuiltIns: 'entry',
57+
// Do not transform modules to CJS
58+
modules: 'commonjs',
59+
targets
60+
}
61+
],
62+
isFlowEnabled && [require('@babel/preset-flow').default]
63+
].filter(Boolean),
64+
plugins: [
65+
[
66+
require('@babel/plugin-transform-runtime').default,
67+
{
68+
helpers: false,
69+
polyfill: false,
70+
regenerator: true
71+
}
72+
]
73+
]
74+
}
75+
}

0 commit comments

Comments
 (0)