Skip to content

Commit 6f38a51

Browse files
authored
Merge pull request #12 from atom-community/typescript
2 parents 30f8bfa + 1a7cb1a commit 6f38a51

File tree

5 files changed

+164
-50
lines changed

5 files changed

+164
-50
lines changed

.prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
pnpm-lock.yaml
3+
package-lock.json
4+
CHANGELOG.md
5+
dist

README.md

+35-20
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,47 @@
33
This includes the babel configuration used for JavaScript packages in atom-ide-community.
44

55
## Installation
6+
67
```
78
npm install --save-dev babel-preset-atomic
89
```
910

1011
You should also install the peer dependencies:
12+
1113
```
1214
npm install -save-dev "@babel/core"
1315
npm install -save-dev "@babel/cli"
1416
```
1517

1618
## Usage
19+
1720
Create a `babel.config.js` file at the root of the project with the following content:
21+
1822
```js
19-
let presets = [
20-
"babel-preset-atomic",
21-
];
23+
let presets = ["babel-preset-atomic"]
2224

23-
let plugins = [];
25+
let plugins = []
2426

2527
module.exports = {
2628
presets: presets,
2729
plugins: plugins,
2830
exclude: "node_modules/**",
2931
sourceMap: "inline",
30-
};
32+
}
3133
```
3234

3335
## Options
3436

35-
1) `keepModules`
37+
1. `keepModules`
3638

3739
If you want to keep the ES modules as they are (not transforming `import` to `require`), set `BABEL_KEEP_MODULES` environment variable to `true`. This is useful with bundlers which need you to keep ES6 modules intact. By default the ES6 modules are transformed to ES5 (the value is `false`)
40+
3841
```
3942
cross-env BABEL_KEEP_MODULES=true
4043
```
4144

4245
To permanently set this option, you can add it to your babel config (which disables environment variable effectiveness):
46+
4347
```js
4448
let presets = [
4549
[
@@ -48,26 +52,27 @@ let presets = [
4852
keepModules: true,
4953
},
5054
],
51-
];
55+
]
5256
```
5357

54-
2) `targets`
58+
2. `targets`
5559

5660
To change the target of `preset-env` plugin. By default this is configured for Electron.
61+
5762
```js
5863
let presets = [
5964
[
6065
"babel-preset-atomic",
6166
{
6267
targets: {
6368
electron: 6,
64-
}
69+
},
6570
},
6671
],
67-
];
72+
]
6873
```
6974

70-
3) `addModuleExports`:
75+
3. `addModuleExports`:
7176

7277
Allows to `require` a ES6 module that has exported a single thing as `default`, in a ES5 fashion without `require().default`. This is `true` by default for backward compatibility with Atom packages.
7378

@@ -76,64 +81,74 @@ let presets = [
7681
[
7782
"babel-preset-atomic",
7883
{
79-
addModuleExports: false
84+
addModuleExports: false,
8085
},
8186
],
82-
];
87+
]
8388
```
8489

85-
4) `addModuleExportsDefaultProperty`:
90+
4. `addModuleExportsDefaultProperty`:
8691

8792
```js
8893
let presets = [
8994
[
9095
"babel-preset-atomic",
9196
{
9297
addModuleExports: true,
93-
addModuleExportsDefaultProperty: true
98+
addModuleExportsDefaultProperty: true,
9499
},
95100
],
96-
];
101+
]
97102
```
98103

99104
Adds `default` property to `module.exports` so the ES6 module can be required in the ES6 fashion as well (by `require().default`). This is `false` by default.
100105

101-
6) `react`
106+
6. `react`
102107

103108
Enable `"@babel/preset-react"`. `true` by default.
104109

105-
7) `flow`
110+
7. `flow`
106111

107112
Enable `"@babel/preset-flow"`. `true` by default.
108113

109-
8) `removeAllUseStrict`
114+
7. `typescript`
115+
116+
Enable `"@babel/preset-typescript"`. `true` by default.
117+
118+
9. `removeAllUseStrict`
110119

111120
Remove all `'use strict'` from all files. Passed to [`babel-plugin-transform-not-strict`](https://github.com/atom-ide-community/babel-plugin-transform-not-strict#usage-remove-all). This is `false` by default.
112121

113-
9) `notStrictDirectiveTriggers` and `notStrictCommentTriggers`
122+
10. `notStrictDirectiveTriggers` and `notStrictCommentTriggers`
114123

115124
These specify `"not strict"` triggers. Passed to [`babel-plugin-transform-not-strict`](https://github.com/atom-ide-community/babel-plugin-transform-not-strict#usage-extra-directive-or-comment-triggers).
116125

117126
## Behind the scenes
118127

119128
It includes the following presets:
129+
120130
- `"@babel/preset-env"` (configured for `electron`)
121131
- `"@babel/preset-react"`
122132
- `"@babel/preset-flow"`
133+
- `"@babel/preset-typescript"`
123134

124135
It also includes all the proposal plugins such as:
136+
125137
- `"@babel/plugin-proposal-optional-chaining"`
126138
- `"@babel/plugin-proposal-nullish-coalescing-operator"`
127139
- `"@babel/plugin-proposal-export-default-from"`
128140
- `"@babel/plugin-proposal-export-namespace-from"`
129141
- ...
130142

131143
It includes the plugins for compile time code generation:
144+
132145
- `"babel-plugin-codegen"`
133146
- `"babel-plugin-preval"`
134147

135148
It has the preset that automatically adds default export for older Node versions (so no `require().default` is needed).
149+
136150
- `"babel-plugin-add-module-exports"`
137151

138152
It has the plugin for removing `'use strict'`:
153+
139154
- `"babel-plugin-transform-not-strict"`

index.js

+69-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ if (process.env.BABEL_KEEP_MODULES === "true") {
55
}
66

77
function handleOptions(options) {
8-
let {targets, keepModules, addModuleExports, addModuleExportsDefaultProperty, react, flow, removeAllUseStrict, notStrictDirectiveTriggers, notStrictCommentTriggers } = options
8+
let {
9+
targets,
10+
keepModules,
11+
addModuleExports,
12+
addModuleExportsDefaultProperty,
13+
react,
14+
flow,
15+
typescript,
16+
removeAllUseStrict,
17+
notStrictDirectiveTriggers,
18+
notStrictCommentTriggers,
19+
} = options
920

1021
// use Electron 5 targets by default
1122
if (targets == null) {
@@ -37,43 +48,68 @@ function handleOptions(options) {
3748
flow = true
3849
}
3950

51+
if (typescript == null) {
52+
typescript = true
53+
}
54+
4055
if (removeAllUseStrict == null) {
4156
removeAllUseStrict = false
4257
}
4358
if (notStrictDirectiveTriggers == null) {
44-
notStrictDirectiveTriggers = ['use babel']
59+
notStrictDirectiveTriggers = ["use babel"]
4560
}
4661
if (notStrictCommentTriggers == null) {
47-
notStrictCommentTriggers = ['@babel', '@flow', '* @babel', '* @flow']
62+
notStrictCommentTriggers = ["@babel", "@flow", "* @babel", "* @flow"]
4863
}
4964

50-
return {targets, keepModules, addModuleExports, addModuleExportsDefaultProperty, react, flow, removeAllUseStrict, notStrictDirectiveTriggers, notStrictCommentTriggers }
65+
return {
66+
targets,
67+
keepModules,
68+
addModuleExports,
69+
addModuleExportsDefaultProperty,
70+
react,
71+
flow,
72+
typescript,
73+
removeAllUseStrict,
74+
notStrictDirectiveTriggers,
75+
notStrictCommentTriggers,
76+
}
5177
}
5278

5379
module.exports = (api, options, dirname) => {
54-
55-
const {targets, keepModules, addModuleExports, addModuleExportsDefaultProperty, react, flow, removeAllUseStrict, notStrictDirectiveTriggers, notStrictCommentTriggers } = handleOptions(options)
80+
const {
81+
targets,
82+
keepModules,
83+
addModuleExports,
84+
addModuleExportsDefaultProperty,
85+
react,
86+
flow,
87+
typescript,
88+
removeAllUseStrict,
89+
notStrictDirectiveTriggers,
90+
notStrictCommentTriggers,
91+
} = handleOptions(options)
5692

5793
let presets = [
5894
[
5995
require("@babel/preset-env"),
6096
{
6197
targets: targets,
62-
modules: keepModules ? false : "commonjs"
98+
modules: keepModules ? false : "commonjs",
6399
},
64100
],
65-
];
101+
]
66102

67103
if (react) {
68-
presets.push(...[
69-
require("@babel/preset-react"),
70-
]);
104+
presets.push(...[require("@babel/preset-react")])
71105
}
72106

73107
if (flow) {
74-
presets.push(...[
75-
require("@babel/preset-flow"),
76-
]);
108+
presets.push(...[require("@babel/preset-flow")])
109+
}
110+
111+
if (typescript) {
112+
presets.push(...[require("@babel/preset-typescript")])
77113
}
78114

79115
let plugins = [
@@ -102,29 +138,36 @@ module.exports = (api, options, dirname) => {
102138
require("babel-plugin-preval"),
103139

104140
// not strict
105-
[require("babel-plugin-transform-not-strict"), {removeAll: removeAllUseStrict, directiveTriggers: notStrictDirectiveTriggers, commentTriggers: notStrictCommentTriggers}],
141+
[
142+
require("babel-plugin-transform-not-strict"),
143+
{
144+
removeAll: removeAllUseStrict,
145+
directiveTriggers: notStrictDirectiveTriggers,
146+
commentTriggers: notStrictCommentTriggers,
147+
},
148+
],
106149

107150
// reserved keywords
108-
require("@babel/plugin-transform-reserved-words")
109-
];
151+
require("@babel/plugin-transform-reserved-words"),
152+
]
110153

111154
// transform modules (e.g when without Rollup)
112155
if (!keepModules) {
113-
plugins.push(...[
114-
require("@babel/plugin-transform-modules-commonjs"),
115-
require("@babel/plugin-syntax-dynamic-import"),
116-
]);
156+
plugins.push(
157+
...[require("@babel/plugin-transform-modules-commonjs"), require("@babel/plugin-syntax-dynamic-import")]
158+
)
117159

118160
if (addModuleExports) {
119-
plugins.push(...[
120-
[require("babel-plugin-add-module-exports"), {addDefaultProperty: addModuleExportsDefaultProperty}] // atom needs this
121-
]);
161+
plugins.push(
162+
...[
163+
[require("babel-plugin-add-module-exports"), { addDefaultProperty: addModuleExportsDefaultProperty }], // atom needs this
164+
]
165+
)
122166
}
123-
124167
}
125168

126169
return {
127170
presets: presets,
128-
plugins: plugins
171+
plugins: plugins,
129172
}
130173
}

package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"test.lint": "eslint .",
2121
"bump": "ncu -u"
2222
},
23+
"prettier": "prettier-config-atomic",
2324
"dependencies": {
2425
"@babel/plugin-proposal-class-properties": "^7.13.0",
25-
"@babel/plugin-proposal-private-methods": "7.13.0",
2626
"@babel/plugin-proposal-decorators": "^7.13.5",
2727
"@babel/plugin-proposal-do-expressions": "7.12.13",
2828
"@babel/plugin-proposal-export-default-from": "7.12.13",
@@ -35,17 +35,19 @@
3535
"@babel/plugin-proposal-numeric-separator": "7.12.13",
3636
"@babel/plugin-proposal-optional-chaining": "7.13.12",
3737
"@babel/plugin-proposal-pipeline-operator": "7.12.13",
38+
"@babel/plugin-proposal-private-methods": "7.13.0",
3839
"@babel/plugin-proposal-throw-expressions": "7.12.13",
3940
"@babel/plugin-syntax-dynamic-import": "7.8.3",
4041
"@babel/plugin-syntax-import-meta": "7.10.4",
42+
"@babel/plugin-transform-reserved-words": "^7.12.13",
4143
"@babel/preset-env": "7.13.12",
4244
"@babel/preset-flow": "7.13.13",
4345
"@babel/preset-react": "7.13.13",
46+
"@babel/preset-typescript": "^7.13.0",
4447
"babel-plugin-add-module-exports": "^1.0.4",
4548
"babel-plugin-codegen": "^4.1.4",
4649
"babel-plugin-preval": "^5.0.0",
47-
"babel-plugin-transform-not-strict": "^0.3.1",
48-
"@babel/plugin-transform-reserved-words": "^7.12.13"
50+
"babel-plugin-transform-not-strict": "^0.3.1"
4951
},
5052
"peerDependencies": {
5153
"@babel/cli": "^7",
@@ -54,6 +56,7 @@
5456
"devDependencies": {
5557
"@babel/cli": "7.11.6",
5658
"@babel/core": "7.11.6",
57-
"npm-check-updates": "11.3.0"
59+
"npm-check-updates": "11.3.0",
60+
"prettier-config-atomic": "^1.0.1"
5861
}
5962
}

0 commit comments

Comments
 (0)