Skip to content

Commit

Permalink
feat: babel preset (#11)
Browse files Browse the repository at this point in the history
* feat: add package.json

* chore: add deps

* feat: add deps

* feat: use babel-preset to build mjolnir-cli

* WIP: add demo

* WIP: update eslint-config
  • Loading branch information
leohxj authored Aug 13, 2019
1 parent d534ebe commit 1f6c3c5
Show file tree
Hide file tree
Showing 19 changed files with 1,775 additions and 97 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ jspm_packages/

.changelog/
lerna-debug.log

# build assets
dist
18 changes: 18 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = (api) => {
api.cache(false);

return {
babelrcRoots: ['.', 'packages/*'],
presets: [
[
'@mjolnir/babel-preset',
{
react: false,
typescript: false
}
]
]
};
};
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
"packages/*"
],
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@mjolnir/babel-preset": "link:packages/babel-preset",
"@mjolnir/eslint-config": "link:packages/eslint-config",
"cross-env": "^5.2.0",
"eslint": "^6.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-prettier": "^3.1.0",
"lerna": "^3.15.0",
"lerna-changelog": "^0.8.2",
"prettier": "^1.17.1"
"prettier": "^1.17.1",
"typescript": "^3.5.3"
},
"scripts": {
"changelog": "lerna-changelog",
"build": "lerna run build",
"publish": "lerna publish",
"lint": "eslint --ext .js ./",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-preset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @mjolnir/babel-preset
A babel preset for es/ts/react project with stage-2.

## Introduction


## install
`yarn add @babel/core @mjolnir/babel-preset -D`

## useage

```json
```
72 changes: 72 additions & 0 deletions packages/babel-preset/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const { declare } = require('@babel/helper-plugin-utils');
const env = process.env.NODE_ENV;

module.exports = declare((api, opts) => {
api.assertVersion(7);

const { react, typescript, modules = false } = opts;

if (!env) {
throw new Error(`require specify 'NODE_ENV' environment variables.`);
}
if (typeof react !== 'boolean') {
throw new Error("options 'react' must be a boolean, or undefined");
}
if (typeof typescript !== 'boolean') {
throw new Error("option 'typescript' must be a boolean, or undefined");
}

// @babel/plugin-proposal-decorators should comes before @babel/plugin-proposal-class-properties.
// When using the legacy: true option of @babel/plugin-proposal-decorators, @babel/plugin-proposal-class-properties must be used in loose: true mode.
const plugins = [
// Stage 2
[require('@babel/plugin-proposal-decorators'), { legacy: true }],
require('@babel/plugin-proposal-function-sent'),
require('@babel/plugin-proposal-export-namespace-from'),
require('@babel/plugin-proposal-numeric-separator'),
require('@babel/plugin-proposal-throw-expressions'),
// Stage 3
require('@babel/plugin-syntax-dynamic-import'),
require('@babel/plugin-syntax-import-meta'),
[require('@babel/plugin-proposal-class-properties'), { loose: true }],
require('@babel/plugin-proposal-json-strings')
];

const presets = [
// default preset is @babel/preset-env
[
require('@babel/preset-env'),
{
//TODO: how about other opts
modules // default to false, webpack takes care of modules
}
]
];

if (typescript) presets.push(require('@babel/preset-typescript'));

if (react) {
presets.push([
require('@babel/preset-react'),
{
development: env
}
]);
if (env === 'production') {
// Remove PropTypes from production build
plugins.push([
require('babel-plugin-transform-react-remove-prop-types'),
{
removeImport: true
}
]);
}
}

return {
presets,
plugins
};
});
49 changes: 49 additions & 0 deletions packages/babel-preset/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@mjolnir/babel-preset",
"version": "0.0.1",
"description": "",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/mjolnirjs/mjolnir/tree/master/packages/babel-preset"
},
"homepage": "https://github.com/mjolnirjs/mjolnir/tree/master/packages/babel-preset",
"bugs": {
"url": "https://github.com/mjolnirjs/mjolnir/issues"
},
"scripts": {},
"keywords": [
"babel",
"config",
"javascript"
],
"license": "MIT",
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=8.0.0"
},
"peerDependencies": {
"@babel/core": "^7.5.5"
},
"devDependencies": {
"@babel/core": "^7.5.5"
},
"dependencies": {
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-decorators": "^7.4.4",
"@babel/plugin-proposal-export-namespace-from": "^7.5.2",
"@babel/plugin-proposal-function-sent": "^7.5.0",
"@babel/plugin-proposal-json-strings": "^7.2.0",
"@babel/plugin-proposal-numeric-separator": "^7.2.0",
"@babel/plugin-proposal-throw-expressions": "^7.2.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-syntax-import-meta": "^7.2.0",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24"
}
}
16 changes: 0 additions & 16 deletions packages/eslint-config/.editorconfig

This file was deleted.

23 changes: 0 additions & 23 deletions packages/eslint-config/.gitignore

This file was deleted.

51 changes: 25 additions & 26 deletions packages/eslint-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@

适用于 node/es/ts/react 环境下的 eslint 配置集合。

## 使用说明
按环境区分使用
## 说明
### requirement
- eslint@6

### es6 (默认)
`npm i -D @mjolnir/eslint-config eslint eslint-plugin-import`
### 包含的插件
- "@typescript-eslint/eslint-plugin": "^1.13.0"
- "@typescript-eslint/parser": "^1.13.0"
- "eslint-plugin-babel": "^5.3.0"
- "eslint-plugin-import": "^2.18.2"
- "eslint-plugin-node": "^9.1.0"
- "eslint-plugin-react": "^7.14.3"
- "eslint-plugin-react-hooks": "^1.7.0"

## 安装
`yarn add eslint @mjolnir/eslint-config -D`

or

#### 配置
`npm i eslint @mjolnir/eslint-config -D`

## 配置
### es6 (默认)
```json
{
"extends": [
Expand All @@ -19,9 +34,6 @@


### Node.js
`npm i -D @mjolnir/eslint-config eslint eslint-plugin-node`

#### 配置
```json
{
"extends": [
Expand All @@ -31,9 +43,6 @@
```

### Babel (ES)
`npm i -D @mjolnir/eslint-config eslint eslint-plugin-import babel-eslint eslint-plugin-babel`

#### 配置
```json
{
"extends": [
Expand All @@ -43,9 +52,6 @@
```

### TypeScript
`npm i -D @mjolnir/eslint-config eslint eslint-plugin-import @typescript-eslint/parser @typescript-eslint/eslint-plugin`

#### 配置
```json
{
"extends": [
Expand All @@ -55,9 +61,6 @@
```

### React
`npm i -D @mjolnir/eslint-config eslint eslint-plugin-import babel-eslint eslint-plugin-babel eslint-plugin-react eslint-plugin-react-hook`

#### 配置
```json
{
"extends": [
Expand All @@ -67,12 +70,7 @@
}
```



### React with TypeScript
`npm i -D @mjolnir/eslint-config eslint eslint-plugin-import @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react eslint-plugin-react-hook`

#### 配置
```json
{
"extends": [
Expand All @@ -83,9 +81,10 @@
```

### jest 环境
`npm i -D @mjolnir/eslint-config eslint eslint-plugin-jest`

#### 配置
```json

{
"extends": [
"@mjolnir/eslint-config/jest"
]
}
```
12 changes: 11 additions & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@
"keywords": [
"eslint",
"config",
"airbnb",
"javascript"
],
"license": "MIT",
"peerDependencies": {
"eslint": "^6.0.0"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"babel-eslint": "^10.0.2",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^22.15.1",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^1.7.0"
},
"publishConfig": {
"access": "public"
},
Expand Down
13 changes: 13 additions & 0 deletions packages/mjolnir-cli/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// .babelrc can merge with babel.config.js
module.exports = {
presets: [
[
'@mjolnir/babel-preset',
{
react: false,
typescript: true,
modules: "commonjs"
}
]
]
};
4 changes: 4 additions & 0 deletions packages/mjolnir-cli/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: ['@mjolnir/eslint-config/typescript', 'plugin:prettier/recommended'],
rules: {}
};
Empty file added packages/mjolnir-cli/.gitkeep
Empty file.
Loading

0 comments on commit 1f6c3c5

Please sign in to comment.