-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: babel, error diagnostics, typings
- Loading branch information
Guillaume Chau
committed
Nov 15, 2018
1 parent
2f76cfb
commit 12093c6
Showing
40 changed files
with
3,230 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# nodepack-plugin-babel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.