Skip to content

Commit

Permalink
feat: babel, error diagnostics, typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Nov 15, 2018
1 parent 2f76cfb commit 12093c6
Show file tree
Hide file tree
Showing 40 changed files with 3,230 additions and 128 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
"packages/test/*"
],
"scripts": {
"test:types": "tsc -p jsconfig.json"
"test": "yarn run test:types",
"test:types": "tsc"
},
"devDependencies": {
"@types/events": "^1.2.0",
"@types/fs-extra": "^5.0.4",
"@types/node": "^10.12.5",
"@types/semver": "^5.5.0",
"@types/webpack-chain": "^4.8.1",
"@types/webpack-env": "^1.13.6",
"eslint": "^5.9.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-vue-libs": "^3.0.0",
Expand Down
23 changes: 23 additions & 0 deletions packages/@moonreach/babel-preset-nodepack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# babel-preset-backpack

This package includes the [Babel](https://babeljs.io) preset used by [nodepack](https://github.com/moonreach/nodepack)

## Usage in Nodepack Projects

The easiest way to use this configuration is with Nodepack, which includes it by default. **You don’t need to install it separately in Nodepack projects.**

## Usage Outside of Nodepack

If you want to use this Babel preset in a project not built with Nodepack, you can install it with following steps.

First, [install Babel](https://babeljs.io/docs/setup/).

Then create a file named `.babelrc` with following contents in the root folder of your project:

```js
{
"presets": ["@moonreach/nodepack"]
}
```

This preset uses the `useBuiltIns` option with [transform-object-rest-spread](http://babeljs.io/docs/plugins/transform-object-rest-spread/), which assumes that `Object.assign` is available or polyfilled.
36 changes: 36 additions & 0 deletions packages/@moonreach/babel-preset-nodepack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@moonreach/babel-preset-nodepack",
"version": "0.0.1",
"description": "Babel preset for nodepack",
"author": "Guillaume Chau <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/moonreach/nodepack.git"
},
"bugs": {
"url": "https://github.com/moonreach/nodepack/issues"
},
"homepage": "https://github.com/moonreach/nodepack#readme",
"publishConfig": {
"access": "public"
},
"main": "src/index.js",
"scripts": {
"test": "test:lint",
"test:lint": "eslint src"
},
"dependencies": {
"@babel/core": "^7.1.5",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
"@babel/plugin-transform-parameters": "^7.1.0",
"@babel/plugin-transform-regenerator": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/runtime": "^7.0.0"
}
}
92 changes: 92 additions & 0 deletions packages/@moonreach/babel-preset-nodepack/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Original: https://github.com/jaredpalmer/backpack/blob/master/packages/babel-preset-backpack/index.js

module.exports = function () {
const path = require('path')

const preset = {
presets: [
[
require.resolve('@babel/preset-env'),
{
targets: {
node: 'current',
},
// Webpack takes care of modules, so babel doesn't have to.
modules: false,
},
],
],
plugins: [
// class { handleThing = () => { } }
require.resolve('@babel/plugin-proposal-class-properties'),

// class decorators
[
require.resolve('@babel/plugin-proposal-decorators'),
{
decoratorsBeforeExport: true,
},
],

// The following two plugins use Object.assign directly, instead of Babel's
// extends helper. Note that this assumes `Object.assign` is available.
// { ...todo, completed: true }
[
require.resolve('@babel/plugin-proposal-object-rest-spread'),
{
useBuiltIns: true,
},
],

[
require.resolve('@babel/plugin-transform-regenerator'),
{
// Async functions are converted to generators by babel-preset-env (which
// is based on babel-preset-latest)
async: false,
},
],

// This is so we don't need to add `babel-polyfill` to our webpack `entry`.
// Unlike `babel-polyfill`, `babel-runtime` + the transform do not pollute
// the global namespace. Yay.
// @see https://medium.com/@jcse/clearing-up-the-babel-6-ecosystem-c7678a314bf3#.7j10g8yn0
[
require.resolve('@babel/plugin-transform-runtime'),
{
helpers: false,
regenerator: true,
// Resolve the Babel runtime relative to the config.
absoluteRuntime: path.dirname(
require.resolve('@babel/runtime/package')
),
},
],
],
}

const v = process.versions.node.split('.').map(v => parseInt(v))
if ((v[0] >= 7 && v[1] >= 6) || v[0] >= 8) {
// @ts-ignore
preset.presets[0].exclude = [
'@babel/plugin-transform-regenerator',
'@babel/transform-async-to-generator',
]
}
if (process.env.NODE_ENV === 'test' || process.env.BABEL_ENV === 'test') {
preset.plugins.push(
// We always include this plugin regardless of environment
// because of a Babel bug that breaks object rest/spread without it:
// https://github.com/babel/babel/issues/4851
require.resolve('@babel/plugin-transform-parameters'),
// Jest needs this to work properly with import/export syntax
[
require.resolve('@babel/plugin-transform-modules-commonjs'),
// @ts-ignore
{ loose: true },
],
)
}

return preset
}
2 changes: 2 additions & 0 deletions packages/@moonreach/env-check/src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ exports.checkNode = function (id, wanted, exit = true) {
' requires Node ' + wanted + '.\nPlease upgrade your Node version.'
))
if (exit) process.exit(1)
return false
}
return true
}
1 change: 1 addition & 0 deletions packages/@moonreach/nodepack-plugin-babel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# nodepack-plugin-babel
28 changes: 28 additions & 0 deletions packages/@moonreach/nodepack-plugin-babel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@moonreach/nodepack-plugin-babel",
"version": "0.0.1",
"description": "Babel plugin for nodepack",
"author": "Guillaume Chau <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/moonreach/nodepack.git"
},
"bugs": {
"url": "https://github.com/moonreach/nodepack/issues"
},
"homepage": "https://github.com/moonreach/nodepack#readme",
"publishConfig": {
"access": "public"
},
"main": "src/index.js",
"scripts": {
"test": "test:lint",
"test:lint": "eslint src"
},
"dependencies": {
"@babel/core": "^7.0.0",
"@moonreach/babel-preset-nodepack": "^0.0.1",
"babel-loader": "^8.0.4"
}
}
77 changes: 77 additions & 0 deletions packages/@moonreach/nodepack-plugin-babel/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/** @typedef {import('@moonreach/nodepack/src/lib/PackPlugin.js').PackPluginApply} PackPluginApply */

/** @type {PackPluginApply} */
module.exports = (api, options) => {
const path = require('path')
const fs = require('fs')
const useThreads = process.env.NODE_ENV === 'production' && options.parallel

let nodepackPath
try {
nodepackPath = path.dirname(require.resolve('@moonreach/nodepack'))
} catch (e) {}

api.chainWebpack(webpackConfig => {
const babelRcPath = path.resolve('babel.config.js')
const hasBabelRc = fs.existsSync(babelRcPath)
const babelOptions = {
babelrc: true,
cacheDirectory: true,
/** @type {string []} */
presets: [],
}

if (hasBabelRc) {
console.log('> Using .babelrc defined in your app root')
} else {
babelOptions.presets.push('@moonreach/nodepack')
}

webpackConfig.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))

const jsRule = webpackConfig.module
.rule('js')
.test(/\.jsx?$/)
.exclude
.add(filepath => {
// exclude dynamic entries from nodepack
if (nodepackPath && filepath.startsWith(nodepackPath)) {
return true
}
// check if this is something the user explicitly wants to transpile
if (options.transpileDependencies && options.transpileDependencies.some(dep => {
if (typeof dep === 'string') {
return filepath.includes(path.normalize(dep))
} else {
return !!filepath.match(dep)
}
})) {
return false
}
// Don't transpile node_modules
return /node_modules/.test(filepath)
})
.end()
.use('cache-loader')
.loader('cache-loader')
.options(api.genCacheConfig('babel-loader', {
'@babel/core': require('@babel/core/package.json').version,
'@moonreach/babel-preset-app': require('@moonreach/babel-preset-nodepack/package.json').version,
'babel-loader': require('babel-loader/package.json').version,
}, [
'babel.config.js',
]))
.end()

if (useThreads) {
jsRule
.use('thread-loader')
.loader('thread-loader')
}

jsRule
.use('babel-loader')
.loader('babel-loader')
.options(babelOptions)
})
}
7 changes: 7 additions & 0 deletions packages/@moonreach/nodepack-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@
"dependencies": {
"chalk": "^2.4.1",
"execa": "^1.0.0",
"find-up": "^3.0.0",
"fs-extra": "^7.0.1",
"inquirer": "^6.2.0",
"joi": "^14.0.6",
"listr": "^0.14.2",
"lodash.clonedeep": "^4.5.0",
"opn": "^5.4.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.5",
"semver": "^5.6.0",
"string.prototype.padstart": "^3.0.0"
}
Expand Down
Loading

0 comments on commit 12093c6

Please sign in to comment.