Skip to content

Commit 5477591

Browse files
committedJun 9, 2023
feat: switched js to ts, created a new package-utils package
BREAKING CHANGE: Changed the package to support cjs and mjs, so the type module was removed
1 parent 91d79b0 commit 5477591

26 files changed

+1020
-352
lines changed
 

‎package.json

+4-10
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"packages/*"
5555
],
5656
"scripts": {
57-
"build:packages": "nx run-many --target=build --parallel --projects=browserslist-config-anolilab",
58-
"build:prod:packages": "nx run-many --target=build:prod --parallel --projects=browserslist-config-anolilab",
57+
"build:packages": "nx run-many --target=build --parallel --projects=browserslist-config-anolilab,package-json-utils,@anolilab/babel-preset",
58+
"build:prod:packages": "nx run-many --target=build:prod --parallel --projects=browserslist-config-anolilab,package-json-utils,@anolilab/babel-preset",
5959
"preinstall": "node verify-node-version.cjs",
6060
"postinstall": "is-ci || husky install",
6161
"lint:eslint": "cross-env NO_LOGS=true eslint -c ./.eslintrc.cjs --ext .js,.cjs,.mjs,.ts,.tsx ./packages --cache --cache-strategy content .",
@@ -68,7 +68,7 @@
6868
"sort-package-json": "sort-package-json ./packages/**/package.json ./package.json",
6969
"test": "vitest --config ./vitest.config.ts",
7070
"test:all": "pnpm test:coverage && pnpm test:stylelint && pnpm test:babel",
71-
"test:babel": "babel --config-file=./packages/babel-preset/index.cjs ./packages/babel-preset/fixture/test.ts --out-file=./packages/babel-preset/fixture/test.js",
71+
"test:babel": "babel --config-file=./packages/babel-preset/dist/index.js ./packages/babel-preset/fixture/test.ts --out-file=./packages/babel-preset/fixture/test.js",
7272
"test:coverage": "vitest --config ./vitest.config.ts run --coverage",
7373
"test:stylelint": "node scripts/isNotWindows || stylelint --formatter verbose --config ./packages/stylelint-config/index.cjs ./packages/stylelint-config/__tests__/css/**/*.css"
7474
},
@@ -85,12 +85,6 @@
8585
"@nrwl/tao": "16.3.2",
8686
"@nrwl/workspace": "16.3.2",
8787
"@qiwi/multi-semantic-release": "^6.7.0",
88-
"@semantic-release/changelog": "^6.0.3",
89-
"@semantic-release/commit-analyzer": "^10.0.1",
90-
"@semantic-release/exec": "^6.0.3",
91-
"@semantic-release/git": "^10.0.1",
92-
"@semantic-release/github": "^9.0.3",
93-
"@semantic-release/release-notes-generator": "^11.0.3",
9488
"@tsconfig/node16": "^1.0.4",
9589
"@tsconfig/strictest": "^2.0.1",
9690
"@types/node": "16.18.34",
@@ -113,7 +107,7 @@
113107
"postcss": "^8.4.24",
114108
"prettier": "^2.8.8",
115109
"read-pkg": "^8.0.0",
116-
"semantic-release": "^21.0.3",
110+
"semantic-release": "^21.0.4",
117111
"sort-package-json": "^2.4.1",
118112
"stylelint": "^15.7.0",
119113
"textlint": "^13.3.2",

‎packages/babel-preset/LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Anolilab
3+
Copyright (c) 2023 Anolilab
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

‎packages/babel-preset/index.cjs

-228
This file was deleted.

‎packages/babel-preset/lib/is-module-available.cjs

-8
This file was deleted.

‎packages/babel-preset/lib/missing.cjs

-33
This file was deleted.

‎packages/babel-preset/package.json

+25-23
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"anolilab",
1111
"javascript"
1212
],
13-
"homepage": "https://anolilab.com/nodejs/packages/browserslist-config",
13+
"homepage": "https://anolilab.com/nodejs/packages/babel-preset",
1414
"repository": {
1515
"type": "git",
1616
"url": "https://github.com/anolilab/javascript-style-guide.git",
@@ -31,19 +31,32 @@
3131
"name": "Daniel Bannert",
3232
"email": "d.bannert@anolilab.de"
3333
},
34-
"type": "module",
35-
"main": "index.cjs",
34+
"sideEffects": false,
35+
"exports": {
36+
".": {
37+
"types": "./dist/index.d.ts",
38+
"require": "./dist/index.js",
39+
"import": "./dist/index.mjs"
40+
},
41+
"./package.json": "./package.json"
42+
},
43+
"main": "dist/index.js",
44+
"module": "dist/index.mjs",
45+
"types": "dist/index.d.ts",
3646
"files": [
37-
"lib",
38-
"index.cjs",
47+
"dist",
3948
"README.md",
4049
"CHANGELOG.md",
4150
"LICENSE.md"
4251
],
4352
"scripts": {
44-
"postinstall": "node lib/postinstall.cjs"
53+
"build": "cross-env NODE_ENV=development tsup",
54+
"build:prod": "cross-env NODE_ENV=production tsup",
55+
"clean": "rimraf node_modules dist",
56+
"postinstall": "node dist/postinstall.js"
4557
},
4658
"dependencies": {
59+
"@anolilab/package-json-utils": "workspace:*",
4760
"@babel/helper-plugin-utils": "^7.22.5",
4861
"@babel/plugin-proposal-class-properties": "^7.18.6",
4962
"@babel/plugin-proposal-class-static-block": "^7.21.0",
@@ -91,6 +104,8 @@
91104
"babel-plugin-polyfill-corejs3": "^0.8.1",
92105
"babel-plugin-transform-react-remove-prop-types": "0.4.24",
93106
"core-js": "^3.30.2",
107+
"cross-env": "^7.0.3",
108+
"rimraf": "^5.0.1",
94109
"tsup": "^6.7.0"
95110
},
96111
"peerDependencies": {
@@ -112,21 +127,8 @@
112127
"access": "public",
113128
"provenance": true
114129
},
115-
"moduleName": "babel-preset",
116-
"pnpm": {
117-
"overrides": {
118-
"glob-parent@<5.1.2": ">=5.1.2",
119-
"minimist@<1.2.6": ">=1.2.6",
120-
"chrono-node@<2.2.4": ">=2.2.4",
121-
"yargs-parser@>=6.0.0 <13.1.2": ">=13.1.2",
122-
"trim@<0.0.3": ">=0.0.3",
123-
"trim-newlines@<3.0.1": ">=3.0.1",
124-
"moment@<2.29.2": ">=2.29.2",
125-
"semver-regex@<3.1.4": ">=3.1.4",
126-
"got@<11.8.5": ">=11.8.5",
127-
"moment@>=2.18.0 <2.29.4": ">=2.29.4",
128-
"ansi-regex@>=3.0.0 <3.0.1": ">=3.0.1",
129-
"ansi-regex@>=4.0.0 <4.1.1": ">=4.1.1"
130-
}
131-
}
130+
"sources": [
131+
"src/index.ts",
132+
"src/postinstall.ts"
133+
]
132134
}

‎packages/babel-preset/src/index.ts

+251
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
import { isPackageAvailable, showMissingPackages } from "@anolilab/package-json-utils";
2+
// @ts-expect-error TS2305: Module '"@babel/helper-plugin-utils"' has no exported member 'declare'.
3+
import { declare } from "@babel/helper-plugin-utils";
4+
5+
type BabelAPI = {
6+
assertVersion: (version: string) => void;
7+
env: (env: string) => boolean;
8+
cache: {
9+
using: (fn: () => boolean) => boolean;
10+
};
11+
};
12+
13+
type Options = {
14+
modules?: "auto" | true | false;
15+
targets?: any;
16+
removePropTypes?: boolean | object;
17+
loose?: boolean;
18+
looseClasses?: boolean;
19+
looseObjectRestSpread?: boolean;
20+
looseComputedProperties?: boolean;
21+
looseParameters?: boolean;
22+
looseTemplateLiterals?: boolean;
23+
typescript?: boolean;
24+
react?: boolean | object;
25+
polyfillRegenerator?: boolean;
26+
useBuiltIns?: boolean;
27+
corejs?: boolean | { method?: string; version?: number };
28+
debug?: boolean;
29+
development?: boolean;
30+
};
31+
32+
const babelPresetTypescript = "@babel/preset-typescript";
33+
const babelPluginTransformTypescript = "@babel/plugin-transform-typescript";
34+
const babelPluginSyntaxJSX = "@babel/plugin-syntax-jsx";
35+
const babelPresetReact = "@babel/preset-react";
36+
const babelPluginTransformReactRemovePropTypes = "babel-plugin-transform-react-remove-prop-types";
37+
38+
const preset = declare((api: BabelAPI, options: Options) => {
39+
// see docs about api at https://babeljs.io/docs/en/config-files#apicache
40+
api.assertVersion("^7.13");
41+
42+
const {
43+
modules = "auto",
44+
targets,
45+
removePropTypes: removePropertyTypes = false,
46+
loose = true,
47+
looseClasses = true,
48+
looseObjectRestSpread = true,
49+
looseComputedProperties = true,
50+
looseParameters = true,
51+
looseTemplateLiterals = true,
52+
typescript = false,
53+
react = false,
54+
polyfillRegenerator = false,
55+
useBuiltIns = false,
56+
corejs = false,
57+
} = options;
58+
59+
if (modules !== undefined && modules !== "auto") {
60+
throw new TypeError(
61+
'@anolilab/babel-preset only accepts `true`, `false`, or `"auto"` as the value of the "modules" option',
62+
);
63+
}
64+
65+
if (removePropertyTypes && !react) {
66+
throw new Error("removePropTypes can't be enabled if react is disabled.");
67+
}
68+
69+
const install: string[] = [];
70+
71+
if (typescript) {
72+
if (!isPackageAvailable(babelPresetTypescript)) {
73+
install.push(babelPresetTypescript);
74+
}
75+
76+
if (!isPackageAvailable(babelPluginTransformTypescript)) {
77+
install.push(babelPluginTransformTypescript);
78+
}
79+
80+
if (!isPackageAvailable(babelPluginSyntaxJSX)) {
81+
install.push(babelPluginSyntaxJSX);
82+
}
83+
}
84+
85+
if (react) {
86+
if (!isPackageAvailable(babelPresetReact)) {
87+
install.push(babelPresetReact);
88+
}
89+
90+
if (removePropertyTypes && !isPackageAvailable(babelPluginTransformReactRemovePropTypes)) {
91+
install.push(babelPluginTransformReactRemovePropTypes);
92+
}
93+
}
94+
95+
if (install.length > 0) {
96+
showMissingPackages("@anolilab/babel-preset", install);
97+
}
98+
99+
const debug = typeof options.debug === "boolean" ? options.debug : false;
100+
const development =
101+
typeof options.development === "boolean"
102+
? options.development
103+
: api.cache.using(() => process.env?.["NODE_ENV"] === "development");
104+
105+
const presets = [
106+
[
107+
"@babel/preset-env",
108+
{
109+
debug,
110+
bugfixes: true,
111+
useBuiltIns,
112+
exclude: ["transform-async-to-generator", "transform-regenerator"],
113+
// @ts-expect-error TS2367: This comparison appears to be unintentional because the types 'string' and 'boolean' have no overlap.
114+
modules: modules === false ? false : "auto",
115+
targets,
116+
shippedProposals: api.env("modern"),
117+
loose,
118+
},
119+
],
120+
typescript
121+
? [
122+
"@babel/preset-typescript",
123+
{
124+
allExtensions: true,
125+
isTSX: true,
126+
},
127+
]
128+
: undefined,
129+
react ? ["@babel/preset-react", { development, ...(typeof react === "object" ? react : {}) }] : undefined,
130+
].filter(Boolean) as [string, object?][];
131+
132+
const plugins = [
133+
"@babel/plugin-transform-shorthand-properties",
134+
"@babel/plugin-transform-block-scoping",
135+
"babel-plugin-annotate-pure-calls",
136+
"babel-plugin-dev-expression",
137+
"@babel/plugin-proposal-class-static-block",
138+
[
139+
"@babel/plugin-proposal-class-properties",
140+
{
141+
loose: looseClasses,
142+
},
143+
],
144+
[
145+
"@babel/plugin-proposal-private-methods",
146+
{
147+
loose: looseClasses,
148+
},
149+
],
150+
[
151+
"@babel/plugin-proposal-private-property-in-object",
152+
{
153+
loose: looseClasses,
154+
},
155+
],
156+
[
157+
"@babel/plugin-transform-classes",
158+
{
159+
loose: looseClasses,
160+
},
161+
],
162+
[
163+
"@babel/plugin-proposal-decorators",
164+
{
165+
legacy: true,
166+
},
167+
],
168+
[
169+
"@babel/plugin-transform-computed-properties",
170+
{
171+
loose: looseComputedProperties,
172+
},
173+
],
174+
[
175+
"@babel/plugin-transform-parameters",
176+
{
177+
loose: looseParameters,
178+
},
179+
],
180+
[
181+
"@babel/plugin-transform-template-literals",
182+
{
183+
loose: looseTemplateLiterals,
184+
},
185+
],
186+
[
187+
"@babel/plugin-proposal-object-rest-spread",
188+
{
189+
loose: looseObjectRestSpread,
190+
useBuiltIns,
191+
},
192+
],
193+
react && removePropertyTypes
194+
? [
195+
"babel-plugin-transform-react-remove-prop-types",
196+
{
197+
mode: "unsafe-wrap",
198+
ignoreFilenames: ["node_modules"],
199+
...((removePropertyTypes as object) || {}),
200+
},
201+
]
202+
: undefined,
203+
"@babel/plugin-proposal-export-namespace-from",
204+
typescript ? "@babel/plugin-transform-typescript" : undefined,
205+
typescript ? "@babel/plugin-syntax-jsx" : undefined,
206+
"@babel/plugin-transform-property-mutators",
207+
"@babel/plugin-transform-member-expression-literals",
208+
"@babel/plugin-transform-property-literals",
209+
"@babel/plugin-transform-for-of",
210+
"@babel/plugin-transform-arrow-functions",
211+
"@babel/plugin-transform-destructuring",
212+
"@babel/plugin-transform-spread",
213+
"@babel/plugin-proposal-nullish-coalescing-operator",
214+
"@babel/plugin-proposal-numeric-separator",
215+
"@babel/plugin-proposal-optional-catch-binding",
216+
"@babel/plugin-proposal-optional-chaining",
217+
"@babel/plugin-proposal-export-default-from",
218+
"@babel/plugin-syntax-bigint",
219+
"@babel/plugin-syntax-async-generators",
220+
"babel-plugin-macros",
221+
polyfillRegenerator
222+
? [
223+
"babel-plugin-polyfill-regenerator",
224+
{
225+
method: "usage-pure",
226+
},
227+
]
228+
: undefined,
229+
typeof corejs === "object"
230+
? [
231+
"babel-plugin-polyfill-corejs3",
232+
{
233+
method: corejs.method || "usage-global",
234+
absoluteImports: "core-js",
235+
version: corejs.version,
236+
...corejs,
237+
},
238+
]
239+
: undefined,
240+
].filter(Boolean) as [string, object?][];
241+
242+
return {
243+
assumptions: {
244+
noDocumentAll: true,
245+
},
246+
presets,
247+
plugins,
248+
};
249+
});
250+
251+
export default preset;

‎packages/babel-preset/lib/postinstall.cjs ‎packages/babel-preset/src/postinstall.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/usr/bin/env node
22

3-
// eslint-disable-next-line no-undef
4-
if (process.env.CI) {
3+
if (process.env?.["CI"]) {
54
// eslint-disable-next-line no-undef
65
process.exit(0);
76
}
87

9-
const { writeFile, existsSync } = require("node:fs");
8+
import { writeFile, existsSync } from "node:fs";
109
// eslint-disable-next-line unicorn/import-style
11-
const { resolve, join } = require("node:path");
12-
const { promisify } = require("node:util");
10+
import { resolve, join } from "node:path";
11+
import { promisify } from "node:util";
12+
// @ts-ignore TS7016: Could not find a declaration file for module '@anolilab/package-json-utils'.
13+
import { pkgIsTypeModule } from "@anolilab/package-json-utils";
1314

1415
const writeFileAsync = promisify(writeFile);
1516

@@ -20,36 +21,36 @@ const projectPath = resolve(process.cwd(), "..", "..", "..");
2021
console.log("Configuring @anolilab/babel-preset", projectPath, "\n");
2122

2223
/**
23-
* Writes babel.config.cjs if it doesn't exist. Warns if it exists.
24+
* Writes babel.config.{c|m}js if it doesn't exist. Warns if it exists.
2425
*/
2526
const writeBabelRc = () => {
26-
const eslintPath = join(projectPath, "babel.config.cjs");
27-
const content = `module.exports = {
27+
const babelPath = join(projectPath, `babel.config.${pkgIsTypeModule ? "m" : "c"}js`);
28+
const content = `${pkgIsTypeModule ? "export default" : "module.exports ="} {
2829
presets: ["@anolilab/babel-preset"]
2930
};
3031
`;
3132

32-
if (existsSync(eslintPath)) {
33-
console.warn(`⚠️ babel.config.cjs already exists;
33+
if (existsSync(babelPath)) {
34+
console.warn(`⚠️ babel.config.${pkgIsTypeModule ? "m" : "c"}js already exists;
3435
Make sure that it includes the following for @anolilab/babel-preset'
3536
to work as it should: { presets: ["@anolilab/babel-preset"] }.`);
3637

3738
return Promise.resolve();
3839
}
3940

40-
return writeFileAsync(eslintPath, content, "utf-8");
41+
return writeFileAsync(babelPath, content, "utf-8");
4142
};
4243

4344
// eslint-disable-next-line unicorn/prefer-top-level-await
44-
(async () => {
45+
(async (): Promise<void> => {
4546
try {
4647
await writeBabelRc();
4748

4849
console.log("😎 Everything went well, have fun!");
4950

5051
// eslint-disable-next-line no-undef
5152
process.exit(0);
52-
} catch (error) {
53+
} catch (error: any) {
5354
console.log("😬 something went wrong:");
5455
console.error(error.message);
5556

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.json"
3+
}

‎packages/babel-preset/tsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"display": "Node 16",
4+
"extends": "../../tsconfig.json",
5+
"baseUrl": "./",
6+
"include": ["src/**/*", "*.d.ts"]
7+
}

‎packages/babel-preset/tsup.config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createConfig } from "../../tsup.config";
2+
3+
const config = createConfig();
4+
5+
export default config;
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package-lock.json
2+
3+
src
4+
fixture
5+
__tests__
6+
__stories__
7+
.rpt2_cache
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"branches": [
3+
"+([0-9])?(.{+([0-9]),x}).x",
4+
"main",
5+
"next",
6+
"next-major",
7+
{
8+
"name": "beta",
9+
"prerelease": true
10+
},
11+
{
12+
"name": "alpha",
13+
"prerelease": true
14+
}
15+
],
16+
"plugins": [
17+
[
18+
"@semantic-release/commit-analyzer",
19+
{
20+
"preset": "conventionalcommits"
21+
}
22+
],
23+
[
24+
"@semantic-release/release-notes-generator",
25+
{
26+
"preset": "conventionalcommits"
27+
}
28+
],
29+
"@semantic-release/changelog",
30+
"@semantic-release/npm",
31+
[
32+
"@semantic-release/git",
33+
{
34+
"message": "chore(release): ${nextRelease.gitTag} [skip ci]\\n\\n${nextRelease.notes}"
35+
}
36+
],
37+
[
38+
"@semantic-release/github",
39+
{
40+
"successComment": false,
41+
"failComment": false
42+
}
43+
]
44+
]
45+
}

‎packages/package-json-utils/CHANGELOG.md

Whitespace-only changes.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Anolilab
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎packages/package-json-utils/README.md

+348
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
# Javascript Style Guide Utils
2+
3+
Some utils functions, like find the root pkg json, has dependencies etc.
4+
5+
---
6+
7+
<div align="center">
8+
<p>
9+
<sup>
10+
Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
11+
</sup>
12+
</p>
13+
</div>
14+
15+
---
16+
17+
## Install
18+
19+
```sh
20+
$ npm install --save-dev @babel/core @babel/runtime @anolilab/babel-preset
21+
```
22+
23+
## Usage
24+
25+
### Via `.babelrc` (Recommended)
26+
27+
**.babelrc**
28+
29+
```json
30+
{
31+
"presets": ["@anolilab/babel-preset"]
32+
}
33+
```
34+
35+
### Via CLI
36+
37+
```sh
38+
$ babel script.js --presets @anolilab/babel-preset
39+
```
40+
41+
### Via Node API
42+
43+
```javascript
44+
require("@babel/core").transform("code", {
45+
presets: ["@anolilab/babel-preset"],
46+
});
47+
```
48+
49+
### Targeting Environments
50+
51+
This module uses `@babel/preset-env` to target specific environments.
52+
53+
Please refer to [@babel/preset-env#targets](https://babeljs.io/docs/en/babel-preset-env#targets) for a list of available options.
54+
55+
For a list of browsers please see [browserlist](https://github.com/browserslist/browserslist).
56+
57+
You may override our default list of targets by providing your own `targets` key.
58+
59+
[List of our supported browsers](https://browserl.ist/?q=last+1+Android+versions%2C+last+1+ChromeAndroid+versions%2C+last+2+Chrome+versions%2C+last+2+Firefox+versions%2C+last+2+Safari+versions%2C+last+2+iOS+versions%2C+last+2+Edge+versions%2C+last+2+Opera+versions%2C+%3E+1%25%2C+ie+%3E%3D+11)
60+
61+
The following transpiles only for Node v6.
62+
63+
```json
64+
{
65+
"presets": [
66+
[
67+
"@anolilab/babel-preset",
68+
{
69+
"targets": {
70+
"node": 6
71+
}
72+
}
73+
]
74+
]
75+
}
76+
```
77+
78+
If you wish, you can also inherit our default list of browsers and extend them.
79+
80+
```javascript
81+
const browserlist = require("browserslist-config-anolilab");
82+
83+
module.exports = {
84+
presets: [
85+
[
86+
"@anolilab/babel-preset",
87+
{
88+
targets: browserlist['production'],
89+
},
90+
],
91+
],
92+
};
93+
```
94+
95+
You may override our default debug option by providing your own `debug` key.
96+
97+
```json
98+
{
99+
"presets": [
100+
[
101+
"@anolilab/babel-preset",
102+
{
103+
"debug": true
104+
}
105+
]
106+
]
107+
}
108+
```
109+
110+
## Typescript Mode
111+
112+
To use this preset please install
113+
114+
```sh
115+
$ npm install --save-dev @babel/preset-typescript @babel/plugin-syntax-jsx
116+
```
117+
118+
This preset can be configured to support `typescript`, using `"typescript": true` in our preset.
119+
120+
```json
121+
{
122+
"presets": [
123+
[
124+
"@anolilab/babel-preset",
125+
{
126+
"typescript": true
127+
}
128+
]
129+
]
130+
}
131+
```
132+
133+
## React Mode
134+
135+
To use this preset please install
136+
137+
```sh
138+
$ npm install --save-dev @babel/preset-react
139+
```
140+
141+
This preset can be configured to support `react`, using `"react": true` or `"react": {...}` in our preset.
142+
143+
```json
144+
{
145+
"presets": [
146+
[
147+
"@anolilab/babel-preset",
148+
{
149+
"react": true
150+
}
151+
]
152+
]
153+
}
154+
```
155+
156+
Or
157+
158+
```json
159+
{
160+
"presets": [
161+
[
162+
"@anolilab/babel-preset",
163+
{
164+
"react": {
165+
"pragma": "dom", // default pragma is React.createElement (only in classic runtime)
166+
"pragmaFrag": "DomFrag", // default is React.Fragment (only in classic runtime)
167+
"throwIfNamespace": false, // defaults to true
168+
"runtime": "classic" // defaults to classic
169+
// "importSource": "custom-jsx-library" // defaults to react (only in automatic runtime)
170+
}
171+
}
172+
]
173+
]
174+
}
175+
```
176+
177+
## React Development Mode
178+
179+
When `process.env.NODE_ENV` is `'development'`, [the `development` mode will be set for `@babel/preset-react`](https://babeljs.io/docs/en/babel-preset-react#development).
180+
181+
You may override our default development option by providing your own boolean `development` key.
182+
183+
```json
184+
{
185+
"presets": [
186+
[
187+
"@anolilab/babel-preset",
188+
{
189+
"react": true,
190+
"development": false
191+
}
192+
]
193+
]
194+
}
195+
```
196+
197+
## React PropTypes removal
198+
199+
This preset can be configured to remove propTypes using [babel-plugin-transform-react-remove-prop-types](https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types) with the following default options:
200+
201+
To enable this transformation with the default options, set the `removePropTypes` option to `true`:
202+
203+
```json
204+
{
205+
"presets": [
206+
[
207+
"@anolilab/babel-preset",
208+
{
209+
"react": true,
210+
"removePropTypes": true
211+
}
212+
]
213+
]
214+
}
215+
```
216+
217+
The default options that will be used are:
218+
219+
```json
220+
{
221+
"mode": "wrap",
222+
"ignoreFilenames": ["node_modules"]
223+
}
224+
```
225+
226+
Default options can be overridden using the `removePropTypes` option. These options will be shallow-merged with the defaults:
227+
228+
```json
229+
{
230+
"presets": [
231+
[
232+
"@anolilab/babel-preset",
233+
{
234+
"removePropTypes": {
235+
"mode": "remove"
236+
}
237+
}
238+
]
239+
]
240+
}
241+
```
242+
243+
For example, if you are using this plugin in a deployable app, you want to use the remove mode for your production build (and disable this transform entirely in development for optimal build speeds).
244+
245+
## Selective loose modes
246+
247+
By default, this preset will compile everything in lose mode. We have options to selectively opt out to loose mode for features. These options are:
248+
249+
- [env](https://babeljs.io/docs/en/babel-preset-env#loose) `loose`
250+
- [classes](https://babeljs.io/docs/en/babel-plugin-transform-classes#loose): `looseClasses`
251+
- [computed properties](https://babeljs.io/docs/en/babel-plugin-transform-computed-properties#loose): `looseComputedProperties`
252+
- [parameters](https://babeljs.io/docs/en/babel-plugin-transform-parameters#loose): `looseParameters`
253+
- [template literals](https://babeljs.io/docs/en/babel-plugin-transform-template-literals#loose): `looseTemplateLiterals`
254+
- [object rest spread](https://babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread#loose): `looseObjectRestSpread`
255+
256+
Example:
257+
258+
```json
259+
{
260+
"presets": [
261+
[
262+
"@anolilab/babel-preset",
263+
{
264+
"loose": false,
265+
"looseClasses": false,
266+
"looseComputedProperties": false,
267+
"looseParameters": false,
268+
"looseTemplateLiterals": false,
269+
"looseObjectRestSpread": false
270+
}
271+
]
272+
]
273+
}
274+
```
275+
276+
The [risks of enabling loose classes are outlined in the Babel docs](https://babeljs.io/docs/en/babel-plugin-transform-classes#loose).
277+
278+
## Specifying module transforms
279+
280+
You can use the `modules` option to enable transformation of modules given to this preset:
281+
282+
```json
283+
{
284+
"presets": [
285+
[
286+
"@anolilab/babel-preset",
287+
{
288+
"modules": "auto"
289+
}
290+
]
291+
]
292+
}
293+
```
294+
295+
Both `true` and the option default `auto` will not transform modules if ES6 module syntax is already supported by the environment, or `"commonjs"` otherwise. `false` will not transform modules.
296+
297+
## Specifying use of core-js v3
298+
299+
You can use the `corejs` option to enable the use of the core-js v3 polyfills:
300+
301+
```json
302+
{
303+
"presets": [
304+
[
305+
"@anolilab/babel-preset",
306+
{
307+
"corejs": {
308+
"version": 3
309+
}
310+
}
311+
]
312+
]
313+
}
314+
```
315+
316+
## Optimizations
317+
318+
- [`babel-plugin-annotate-pure-calls`](https://github.com/Andarist/babel-plugin-annotate-pure-calls): Injects for `#__PURE` annotations to enable tree-shaking
319+
- [`babel-plugin-dev-expressions`](https://github.com/4Catalyzer/babel-plugin-dev-expression): A mirror of Facebook’s dev-expression Babel plugin. It reduces or eliminates development checks from production code
320+
321+
### Development-only Expressions + Treeshaking
322+
323+
`babel-plugin-annotate-pure-calls` + `babel-plugin-dev-expressions` work together to fully eliminate dead code (aka treeshake) development checks from your production code. Let’s look at an example to see how it works.
324+
325+
326+
## Supported Node.js Versions
327+
328+
Libraries in this ecosystem make the best effort to track
329+
[Node.js’ release schedule](https://nodejs.org/en/about/releases/). Here’s [a
330+
post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
331+
332+
Contributing
333+
------------
334+
335+
If you would like to help take a look at the [list of issues](https://github.com/anolilab/javascript-style-guide/issues) and check our [Contributing](.github/CONTRIBUTING.md) guild.
336+
337+
> **Note:** please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
338+
339+
Credits
340+
-------------
341+
342+
- [Daniel Bannert](https://github.com/prisis)
343+
- [All Contributors](https://github.com/anolilab/javascript-style-guide/graphs/contributors)
344+
345+
License
346+
-------------
347+
348+
The anolilab javascript-style-guide is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT)
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"name": "@anolilab/package-json-utils",
3+
"version": "1.0.0",
4+
"description": "Shared utilities for parsing information from a package.json.",
5+
"keywords": [
6+
"package.json",
7+
"utils",
8+
"anolilab",
9+
"javascript"
10+
],
11+
"homepage": "https://anolilab.com/nodejs/packages/package-json-utils",
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/anolilab/javascript-style-guide.git",
15+
"directory": "packages/package-json-utils"
16+
},
17+
"funding": [
18+
{
19+
"type": "github",
20+
"url": "https://github.com/sponsors/prisis"
21+
},
22+
{
23+
"type": "consulting",
24+
"url": "https://anolilab.com/support"
25+
}
26+
],
27+
"license": "MIT",
28+
"author": {
29+
"name": "Daniel Bannert",
30+
"email": "d.bannert@anolilab.de"
31+
},
32+
"sideEffects": false,
33+
"exports": {
34+
".": {
35+
"types": "./dist/index.d.ts",
36+
"require": "./dist/index.js",
37+
"import": "./dist/index.mjs"
38+
},
39+
"./package.json": "./package.json"
40+
},
41+
"main": "dist/index.js",
42+
"module": "dist/index.mjs",
43+
"source": "src/index.ts",
44+
"types": "dist/index.d.ts",
45+
"files": [
46+
"dist",
47+
"README.md",
48+
"CHANGELOG.md",
49+
"LICENSE.md"
50+
],
51+
"scripts": {
52+
"build": "cross-env NODE_ENV=development tsup",
53+
"build:prod": "cross-env NODE_ENV=production tsup",
54+
"clean": "rimraf node_modules dist"
55+
},
56+
"dependencies": {
57+
"read-pkg-up": "^7.0.1"
58+
},
59+
"devDependencies": {
60+
"@anolilab/semantic-release-preset": "2.2.1",
61+
"cross-env": "^7.0.3",
62+
"rimraf": "^5.0.1",
63+
"tsup": "^6.7.0"
64+
},
65+
"engines": {
66+
"node": ">=16"
67+
},
68+
"publishConfig": {
69+
"access": "public",
70+
"provenance": true
71+
}
72+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
3+
"sourceRoot": "packages/package-json-utils/src",
4+
"projectType": "library",
5+
"tags": ["package-json-utils"],
6+
"implicitDependencies": []
7+
}
+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { existsSync, realpathSync } from "node:fs";
2+
import { dirname, join } from "node:path";
3+
import readPkgUp from "read-pkg-up";
4+
import module from "module";
5+
6+
export const arrify = (value: any): any[] => {
7+
if (value === null || value === undefined) {
8+
return [];
9+
}
10+
11+
if (Array.isArray(value)) {
12+
return value;
13+
}
14+
15+
if (typeof value === "string") {
16+
return [value];
17+
}
18+
19+
if (typeof value[Symbol.iterator] === "function") {
20+
return [...value];
21+
}
22+
23+
return [value];
24+
};
25+
26+
const { packageJson: package_, path: packagePath } = readPkgUp.sync({
27+
cwd: realpathSync(process.cwd()),
28+
}) || { packageJson: null, path: null };
29+
30+
export const hasPackageProperty = (properties: any): boolean =>
31+
arrify(properties).some((property: any) => package_ !== null && Object.hasOwnProperty.call(package_, property));
32+
33+
export const hasPackageSubProperty =
34+
(packageProperty: string) =>
35+
(properties: any): boolean =>
36+
hasPackageProperty(arrify(properties).map((p: string) => `${packageProperty}.${p}`));
37+
38+
export const environmentIsSet = (name: string): boolean =>
39+
Boolean(process.env.hasOwnProperty(name) && process.env[name] && process.env[name] !== "undefined");
40+
41+
export const parseEnvironment = (name: string, def: any): any => {
42+
if (environmentIsSet(name)) {
43+
try {
44+
return JSON.parse(process.env[name] || "");
45+
} catch {
46+
return process.env[name];
47+
}
48+
}
49+
50+
return def;
51+
};
52+
53+
export const appDirectory: string = packagePath ? dirname(packagePath) : "";
54+
export const fromRoot = (...p: string[]): string => join(appDirectory, ...p);
55+
export const hasFile = (...p: string[]): boolean => existsSync(fromRoot(...p));
56+
57+
export const hasScript = hasPackageSubProperty("scripts");
58+
export const hasPeerDep = hasPackageSubProperty("peerDependencies");
59+
export const hasDep = hasPackageSubProperty("dependencies");
60+
export const hasDevelopmentDep = hasPackageSubProperty("devDependencies");
61+
export const hasAnyDep = (arguments_: any): boolean =>
62+
[hasDep, hasDevelopmentDep, hasPeerDep].some((function_: any) => function_(arguments_));
63+
64+
export const hasTypescript: boolean = hasAnyDep("typescript") && hasFile("tsconfig.json");
65+
66+
export const pkgIsTypeModule = hasPackageSubProperty("type") && package_?.["type"] === "module";
67+
68+
export const isPackageAvailable = (moduleName: string) => {
69+
const targetModule = import.meta.url;
70+
// See https://yarnpkg.com/advanced/pnpapi
71+
if (process.versions?.["pnp"]) {
72+
// @ts-expect-error TS2339: Property 'findPnpApi' does not exist on type 'typeof Module'.
73+
const targetPnp = module.findPnpApi(targetModule);
74+
75+
if (targetPnp.resolveRequest(moduleName, targetModule)) {
76+
return true;
77+
}
78+
} else if (pkgIsTypeModule) {
79+
// See https://nodejs.org/api/esm.html#esm_resolver_algorithm
80+
try {
81+
module.createRequire(targetModule).resolve(moduleName);
82+
83+
return true;
84+
} catch (error) {
85+
return false;
86+
}
87+
}
88+
89+
try {
90+
require.resolve(moduleName);
91+
92+
return true;
93+
} catch (error) {
94+
return false;
95+
}
96+
};
97+
98+
export const showMissingPackages = (packageName: string, packages: string[]) => {
99+
const atLatest = (package_: string) => {
100+
if (!package_.split("@").includes("@")) {
101+
return `${package_}@latest`;
102+
}
103+
104+
return package_;
105+
};
106+
107+
const s = packages.length === 1 ? "" : "s";
108+
109+
console.warn(`\nOops! Something went wrong! :(
110+
111+
${packageName} could not find the following package${s}
112+
113+
${packages.join("\n ")}
114+
115+
To install the missing package${s}, please run the following command:
116+
117+
npm install ${packages.map((element) => atLatest(element)).join(" ")} --save-dev
118+
119+
or
120+
121+
yarn add ${packages.map((element) => atLatest(element)).join(" ")} --dev
122+
123+
or
124+
125+
pnpm add ${packages.map((element) => atLatest(element)).join(" ")} -D
126+
`);
127+
128+
// eslint-disable-next-line no-undef
129+
process.exit(1); // eslint-disable-line unicorn/no-process-exit
130+
};
131+
132+
export const unique = (array: any[]): any[] => [...new Set(array)];
133+
134+
export const pkg = package_ as readPkgUp.NormalizedReadResult | null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.json"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"display": "Node 16",
4+
"extends": "../../tsconfig.json",
5+
"baseUrl": "./",
6+
"include": ["src/**/*", "*.d.ts"]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createConfig } from "../../tsup.config";
2+
3+
const config = createConfig();
4+
5+
export default config;

‎packages/semantic-release-preset/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@
5656
"@semantic-release/exec": "^6.0.3",
5757
"@semantic-release/git": "^10.0.1",
5858
"@semantic-release/github": "^9.0.3",
59-
"@semantic-release/npm": "^10.0.3",
59+
"@semantic-release/npm": "^10.0.4",
6060
"@semantic-release/release-notes-generator": "^11.0.3",
6161
"commitizen": "^4.3.0",
6262
"conventional-changelog-conventionalcommits": "^6.0.0",
6363
"cz-conventional-changelog": "^3.3.0",
6464
"semantic-release-conventional-commits": "^3.0.0"
6565
},
6666
"devDependencies": {
67-
"semantic-release": "^21.0.3"
67+
"semantic-release": "^21.0.4"
6868
},
6969
"peerDependencies": {
7070
"semantic-release": "^21.0.0"

‎packages/stylelint-config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"postinstall": "node lib/postinstall.js"
4848
},
4949
"dependencies": {
50-
"browserslist-config-anolilab": "3.2.1",
50+
"browserslist-config-anolilab": "4.0.0",
5151
"stylelint-a11y": "^1.2.3",
5252
"stylelint-config-rational-order": "^0.1.2",
5353
"stylelint-config-standard": "^33.0.0",

‎packages/textlint-config/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ Signed-off-by: prisis <d.bannert@anolilab.de>
323323

324324
### Bug Fixes
325325

326-
- removed index.cjs from text-lint ([e5ab6d6](https://github.com/anolilab/javascript-style-guide/commit/e5ab6d6a9214b288646d471e19afff163ac5cf57))
326+
- removed index.ts from text-lint ([e5ab6d6](https://github.com/anolilab/javascript-style-guide/commit/e5ab6d6a9214b288646d471e19afff163ac5cf57))
327327

328328
### @anolilab/textlint-config [2.0.2](https://github.com/anolilab/javascript-style-guide/compare/@anolilab/textlint-config@2.0.1...@anolilab/textlint-config@2.0.2) (2021-05-04)
329329

‎pnpm-lock.yaml

+57-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.