@anolilab/package-json-utils
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -0,1 +1,8 @@ | ||
## @anolilab/package-json-utils [1.0.1](https://github.com/anolilab/javascript-style-guide/compare/@anolilab/package-json-utils@1.0.0...@anolilab/package-json-utils@1.0.1) (2023-06-10) | ||
### Bug Fixes | ||
* updated the README.md ([447ffb7](https://github.com/anolilab/javascript-style-guide/commit/447ffb73e5852588102e5868ef4e5f76c94aa8f3)) | ||
## @anolilab/package-json-utils 1.0.0 (2023-06-10) | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "@anolilab/package-json-utils", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Shared utilities for parsing information from a package.json.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
322
README.md
# Javascript Style Guide Utils | ||
Some utils functions, like find the root pkg json, has dependencies etc. | ||
Shared utilities for parsing information from a package.json file. | ||
@@ -20,308 +20,80 @@ --- | ||
```sh | ||
$ npm install --save-dev @babel/core @babel/runtime @anolilab/babel-preset | ||
$ npm install @anolilab/package-json-utils | ||
``` | ||
## Usage | ||
### Via `.babelrc` (Recommended) | ||
**.babelrc** | ||
```json | ||
{ | ||
"presets": ["@anolilab/babel-preset"] | ||
} | ||
``` | ||
### Via CLI | ||
```sh | ||
$ babel script.js --presets @anolilab/babel-preset | ||
$ yarn add @anolilab/package-json-utils | ||
``` | ||
### Via Node API | ||
```javascript | ||
require("@babel/core").transform("code", { | ||
presets: ["@anolilab/babel-preset"], | ||
}); | ||
``` | ||
### Targeting Environments | ||
This module uses `@babel/preset-env` to target specific environments. | ||
Please refer to [@babel/preset-env#targets](https://babeljs.io/docs/en/babel-preset-env#targets) for a list of available options. | ||
For a list of browsers please see [browserlist](https://github.com/browserslist/browserslist). | ||
You may override our default list of targets by providing your own `targets` key. | ||
[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) | ||
The following transpiles only for Node v6. | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"targets": { | ||
"node": 6 | ||
} | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
If you wish, you can also inherit our default list of browsers and extend them. | ||
```javascript | ||
const browserlist = require("browserslist-config-anolilab"); | ||
module.exports = { | ||
presets: [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
targets: browserlist['production'], | ||
}, | ||
], | ||
], | ||
}; | ||
``` | ||
You may override our default debug option by providing your own `debug` key. | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"debug": true | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
## Typescript Mode | ||
To use this preset please install | ||
```sh | ||
$ npm install --save-dev @babel/preset-typescript @babel/plugin-syntax-jsx | ||
$ pnpm add @anolilab/package-json-utils | ||
``` | ||
This preset can be configured to support `typescript`, using `"typescript": true` in our preset. | ||
## Usage | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"typescript": true | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
```js | ||
const { pkg } = require('@anolilab/package-json-utils'); | ||
## React Mode | ||
To use this preset please install | ||
```sh | ||
$ npm install --save-dev @babel/preset-react | ||
console.log(pkg.name); // will print the name of the "process.cwd()" package.json | ||
``` | ||
This preset can be configured to support `react`, using `"react": true` or `"react": {...}` in our preset. | ||
List of available helpers: | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"react": true | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
- hasPackageProperties | ||
> This function takes an array of property names as input and checks if the "process.cwd()" package.json object has any of those properties. | ||
Or | ||
- hasPackageSubProperties | ||
> This function takes a package property name as input and returns a new function that checks if the "process.cwd()" package.json object has sub-properties under the specified package property. | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"react": { | ||
"pragma": "dom", // default pragma is React.createElement (only in classic runtime) | ||
"pragmaFrag": "DomFrag", // default is React.Fragment (only in classic runtime) | ||
"throwIfNamespace": false, // defaults to true | ||
"runtime": "classic" // defaults to classic | ||
// "importSource": "custom-jsx-library" // defaults to react (only in automatic runtime) | ||
} | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
- environmentIsSet | ||
> This function takes an environment variable name as input and checks if the environment variable is set and not equal to "undefined". | ||
## React Development Mode | ||
- parseEnvironment | ||
> This function takes an environment variable name and a default value as input. It attempts to parse the value of the environment variable as JSON and returns the parsed value. If parsing fails, it returns the original value as a string. | ||
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). | ||
- appDirectory | ||
> This variable is assigned the value of the directory name of the package path or an empty string if the package path is undefined. | ||
You may override our default development option by providing your own boolean `development` key. | ||
- fromRoot | ||
> This function takes a variable number of path segments as input and joins them with the appDirectory to create a complete path from the root directory. | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"react": true, | ||
"development": false | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
- hasFile | ||
> This function takes a variable number of path segments as input and checks if a file exists at the specified path relative to the appDirectory. | ||
## React PropTypes removal | ||
- hasScripts | ||
> This variable is assigned the result of calling hasPackageSubProperties with the argument "scripts". It checks if the "process.cwd()" package.json object has any scripts defined. | ||
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: | ||
- hasPeerDep | ||
> This variable is assigned the result of calling hasPackageSubProperties with the argument "peerDependencies". It checks if the "process.cwd()" package.json object has any peer dependencies defined. | ||
To enable this transformation with the default options, set the `removePropTypes` option to `true`: | ||
- hasDep | ||
> This variable is assigned the result of calling hasPackageSubProperties with the argument "dependencies". It checks if the "process.cwd()" package.json object has any regular dependencies defined. | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"react": true, | ||
"removePropTypes": true | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
- hasDevelopmentDep | ||
> This variable is assigned the result of calling hasPackageSubProperties with the argument "devDependencies". It checks if the "process.cwd()" package.json object has any development dependencies defined. | ||
The default options that will be used are: | ||
- hasAnyDep | ||
> This function takes an array of dependency names as input and checks if any of the dependency checks (hasDep, hasDevelopmentDep, hasPeerDep) return true for any of the dependencies. | ||
```json | ||
{ | ||
"mode": "wrap", | ||
"ignoreFilenames": ["node_modules"] | ||
} | ||
``` | ||
- hasTypescript | ||
> This variable is assigned the result of checking if "typescript" is a dependency and if "tsconfig.json" file exists using hasAnyDep and hasFile functions. | ||
Default options can be overridden using the `removePropTypes` option. These options will be shallow-merged with the defaults: | ||
- packageIsTypeModule | ||
> This variable is assigned the result of checking if the "process.cwd()" package.json object has a property "type" and its value is "module". | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"removePropTypes": { | ||
"mode": "remove" | ||
} | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
- isPackageAvailable | ||
> This function takes a module name as input and checks if the module is available for import. It uses different methods depending on the environment and package type (module or commonjs). | ||
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). | ||
- showMissingPackages | ||
> This function takes a package name and an array of missing package names as input and logs a warning message with instructions on how to install the missing packages using npm, yarn, or pnpm. | ||
## Selective loose modes | ||
- unique | ||
> This function takes an array as input and returns a new array with only unique values. | ||
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: | ||
- pkg | ||
> This variable returns the "process.cwd()" package.json object or undefined. | ||
- [env](https://babeljs.io/docs/en/babel-preset-env#loose) `loose` | ||
- [classes](https://babeljs.io/docs/en/babel-plugin-transform-classes#loose): `looseClasses` | ||
- [computed properties](https://babeljs.io/docs/en/babel-plugin-transform-computed-properties#loose): `looseComputedProperties` | ||
- [parameters](https://babeljs.io/docs/en/babel-plugin-transform-parameters#loose): `looseParameters` | ||
- [template literals](https://babeljs.io/docs/en/babel-plugin-transform-template-literals#loose): `looseTemplateLiterals` | ||
- [object rest spread](https://babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread#loose): `looseObjectRestSpread` | ||
> **Note:** The process.cwd() method is an inbuilt application programming interface of the process module | ||
which is used to get the current working directory of the node.js process. | ||
Example: | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"loose": false, | ||
"looseClasses": false, | ||
"looseComputedProperties": false, | ||
"looseParameters": false, | ||
"looseTemplateLiterals": false, | ||
"looseObjectRestSpread": false | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
The [risks of enabling loose classes are outlined in the Babel docs](https://babeljs.io/docs/en/babel-plugin-transform-classes#loose). | ||
## Specifying module transforms | ||
You can use the `modules` option to enable transformation of modules given to this preset: | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"modules": "auto" | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
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. | ||
## Specifying use of core-js v3 | ||
You can use the `corejs` option to enable the use of the core-js v3 polyfills: | ||
```json | ||
{ | ||
"presets": [ | ||
[ | ||
"@anolilab/babel-preset", | ||
{ | ||
"corejs": { | ||
"version": 3 | ||
} | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
## Optimizations | ||
- [`babel-plugin-annotate-pure-calls`](https://github.com/Andarist/babel-plugin-annotate-pure-calls): Injects for `#__PURE` annotations to enable tree-shaking | ||
- [`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 | ||
### Development-only Expressions + Treeshaking | ||
`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. | ||
## Supported Node.js Versions | ||
@@ -328,0 +100,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29598
121