Skip to content

Commit

Permalink
Remove support for non-standard module field in package.json
Browse files Browse the repository at this point in the history
This commit removes support for non-standard `"module"` field in `package.json`. Purpose of this field was to implement support for ES modules in its early days. This used to be a common way how to do it in bundlers, see:

- Webpack: https://webpack.js.org/guides/author-libraries/#final-steps
- Rollup: https://github.com/rollup/rollup/wiki/pkg.module
- https://stackoverflow.com/questions/42708484/what-is-the-module-package-json-field-for

However, seems like the whole system around ES modules evolved a bit differently (top level field `"type": "module"` + field `"exports"`), see:

- https://nodejs.org/api/packages.html
- https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
- https://babeljs.io/blog/2021/02/22/7.13.0#automatic-es-modules-in-babelruntime-12632httpsgithubcombabelbabelpull12632

This change is essentially a cleanup of our quirky and non-standard solution in order to be able to implement this modern approach.
  • Loading branch information
mrtnzlml authored and kodiakhq[bot] committed May 5, 2021
1 parent 7e3a466 commit 4644aba
Show file tree
Hide file tree
Showing 43 changed files with 51 additions and 593 deletions.
3 changes: 1 addition & 2 deletions src/babel-plugin-transform-sx-tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"license": "MIT",
"private": false,
"version": "0.18.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"module": false,
"dependencies": {
"@adeira/murmur-hash": "^2.0.0",
"@adeira/sx-tailwind": "^0.12.0",
Expand Down
2 changes: 1 addition & 1 deletion src/babel-preset-adeira/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": false,
"license": "MIT",
"version": "3.0.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.12.13",
Expand Down
2 changes: 1 addition & 1 deletion src/css-colors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "2.0.0",
"sideEffects": false,
"private": false,
"main": "src/index",
"main": "./src/index.js",
"license": "MIT",
"dependencies": {
"@adeira/js": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/eslint-config-adeira/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": false,
"version": "6.0.0",
"main": "index",
"main": "./index.js",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.14.0",
Expand Down
2 changes: 1 addition & 1 deletion src/eslint-fixtures-tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Eslint fixtures tester utility.",
"homepage": "https://github.com/adeira/universe/tree/master/src/eslint-fixtures-tester",
"version": "0.1.0",
"main": "src/index",
"main": "./src/index.js",
"license": "MIT",
"private": false,
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/eslint-plugin-adeira/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": false,
"version": "0.13.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.14.0",
Expand Down
3 changes: 1 addition & 2 deletions src/eslint-plugin-sx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
"version": "0.11.0",
"license": "MIT",
"private": false,
"module": false,
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin"
],
"main": "src/index",
"main": "./src/index.js",
"dependencies": {
"@babel/runtime": "^7.14.0"
},
Expand Down
3 changes: 1 addition & 2 deletions src/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"private": false,
"license": "MIT",
"version": "2.0.2",
"main": "src/fetchWithRetries",
"module": false,
"main": "./src/fetchWithRetries.js",
"sideEffects": false,
"homepage": "https://github.com/adeira/universe/tree/master/src/fetch",
"description": "Production ready fetch function with advanced capabilities like retries with delay and request cancellation after timeout.",
Expand Down
2 changes: 1 addition & 1 deletion src/fixtures-tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": false,
"version": "1.0.1",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@adeira/js": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/flow-config-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"private": false,
"license": "MIT",
"main": "src/index",
"main": "./src/index.js",
"dependencies": {
"@adeira/fixtures-tester": "^1.0.1",
"@adeira/js": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/flow-types-eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Flow types for building Eslint plugins and rules",
"version": "0.0.0",
"private": true,
"main": "src/index",
"main": "./src/index.js",
"license": "MIT",
"dependencies": {}
}
2 changes: 1 addition & 1 deletion src/graphql-bc-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": false,
"version": "0.3.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@adeira/signed-source": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/graphql-global-id/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": false,
"version": "2.0.1",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@adeira/js": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/graphql-relay-fauna/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@adeira/graphql-relay-fauna",
"private": false,
"version": "0.3.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"homepage": "https://github.com/adeira/universe/tree/master/src/graphql-relay-fauna",
"description": "Helpers for Relay compliant GraphQL server with FaunaDB backend",
Expand Down
2 changes: 1 addition & 1 deletion src/graphql-relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "git",
"url": "http://github.com/adeira/universe.git"
},
"main": "src/index",
"main": "./src/index.js",
"peerDependencies": {
"graphql": "^15.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/graphql-resolve-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": false,
"version": "0.3.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.14.0"
Expand Down
2 changes: 1 addition & 1 deletion src/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": false,
"version": "2.1.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.14.0"
Expand Down
2 changes: 2 additions & 0 deletions src/monorepo-npm-publisher/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Note for all Flow users: all projects in [`adeira/universe`](https://github.com/adeira/universe) now use implicit exact Flow types (`{}` for strict objects and `{ ... }` for open objects, syntax `{||}` is deprecated). We do not expect any issues as long as you are using `exact_by_default=true` Flow option.

- Support for non-standard `"module"` top field in `package.json` has been removed. The plan is to replace it with [more standardized packages approach](https://nodejs.org/api/packages.html) without our custom quirks.

# 2.0.0

- Support for Node.js 12 has been removed. This package might continue working on older Node.js versions, however, it's highly recommended upgrading to Node.js version 14 or newer. For more details, see: https://nodejs.org/en/about/releases/, or discuss here: https://github.com/adeira/universe/discussions/1588
Expand Down
89 changes: 14 additions & 75 deletions src/monorepo-npm-publisher/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
This package prepares our public NPM packages to be published. It can automatically find these packages, transpile them based on our Babel configuration, copy Flow versions of the files and automatically publish it to NPM (in CI). It publishes only packages with new version and it ignores old or current versions.
This package prepares our public NPM packages to be published. It can automatically find these packages, transpile them based on our Babel configuration, copy Flow versions of the files and automatically publish it to NPM (only when running on CI server). It publishes only packages with new version and it ignores old or current versions.

This publisher uses [@adeira/babel-preset-adeira](https://www.npmjs.com/package/@adeira/babel-preset-adeira) behind the scenes to transpile JS (and JS-ESM) and Flow files.
This publisher uses [@adeira/babel-preset-adeira](https://www.npmjs.com/package/@adeira/babel-preset-adeira) behind the scenes to transpile JS and Flow files.

Please note: changelogs are not responsibility of this package. You should write them manually for your users.
_Please note: changelogs are not responsibility of this package. You should write them manually for your users._

# Installation

Expand All @@ -27,23 +27,27 @@ import publish from '@adeira/monorepo-npm-publisher';
// Where to store transpiled code before it's being published.
buildCache: path.join(
os.tmpdir(),
'com.adeira.TODO_YOUR_PROJECT.npm', // change please
'com.adeira.TODO_YOUR_PROJECT.npm', // please change
'.build',
),

// Workspaces to publish on NPM. It takes into account only
// packages with public visibility set in `package.json`.
workspaces: new Set(['@adeira/js', '@adeira/fetch', '@adeira/relay', '@adeira/eslint-config']),

npmAuthToken: '*** TODO ***', // see: https://www.npmjs.com/settings/<USERNAME>/tokens
reactRuntime: 'classic', // or `automatic` if you want to use the new JSX transform. 'automatic' is the default option. see https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
// See: https://www.npmjs.com/settings/<USERNAME>/tokens
npmAuthToken: '*** TODO ***',

// React runtime `classic` or `automatic` if you want to use the new JSX transform.
// Runtime `automatic` is the default option. See: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
reactRuntime: 'classic',
});
})();
```

This NPM publisher automatically takes `.npmignore` (or `.gitignore`) files into account. Read this info for more details: https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

# BEGIN-ADEIRA-UNIVERSE-INTERNAL, END-ADEIRA-UNIVERSE-INTERNAL
# Comments `(BEGIN|END)-ADEIRA-UNIVERSE-INTERNAL`,

In rare scenarios, you might need to skip some part of the source code when publishing to NPM. It can be done like so:

Expand Down Expand Up @@ -92,24 +96,19 @@ com.adeira.universe.npm/.build/js
└── src
├── index.js
├── index.js.flow
├── index.mjs
├── invariant.js
├── invariant.js.flow
├── invariant.mjs
├── isObject.js
├── isObject.js.flow
├── isObject.mjs
├── sprintf.js
├── sprintf.js.flow
├── sprintf.mjs
├── warning.js
├── warning.js.flow
└── warning.mjs
1 directory, 18 files
1 directory, 13 files
```

As you can see all the important files are still in the final bundle but tests are missing. It's because they are excluded in `.npmignore` file. Every JS file is distributed in multiple variants. JS files are transpiled so they can be used basically everywhere:
As you can see all the important files are still in the final bundle but tests are missing. It's because they are excluded in `.npmignore` file. JS files are transpiled so they can be used basically everywhere:

```js
'use strict';
Expand Down Expand Up @@ -138,64 +137,4 @@ export default function isObject(value: mixed): boolean %checks {
}
```

You can read more about [Flow Declaration Files here](https://flow.org/en/docs/declarations/). And MJS files contain JS version for modern environments (essentially JS version but with ES6 modules support):

```js
import _typeof from '@babel/runtime/helpers/esm/typeof';
export default function isObject(value) {
return _typeof(value) === 'object' && value !== null && !Array.isArray(value);
}
```

One last change is happening: NPM publisher modifies `package.json` file so it contains correct `module` field pointing to MJS file variants. Please note: ES6 modules can be disabled completely when you specify `"module": false` in your package.json:

```json
{
"name": "withDisabledES6",
"version": "0.0.0",
"main": "src/index.js",
"module": false,
"dependencies": {
"//": "none"
}
}
```

This is handy when your code is not ready for ES6 modules yet.

# Note on ES6 modules

_The following text describes the current situation up to this date. Seems like some parts of the JS ecosystem are not well documented and they may change in future._

Usually tools use CJS requires but some of them also support ES6 imports. This is how it works in Next.js for example:

```js
const webpackResolveConfig = {
// Disable .mjs for node_modules bundling
extensions: isServer
? ['.js', '.mjs', '.jsx', '.json', '.wasm']
: ['.mjs', '.js', '.jsx', '.json', '.wasm'],
mainFields: isServer ? ['main', 'module'] : ['browser', 'module', 'main'],
// ...
};
```

Notice the ordering of supported extensions. Therefore Next.js actually uses different systems for FE and BE. We distribute both versions: JS and MJS files. Different situation is when package uses MJS and this package has dependency on different package which also uses MJS. This subpackage must be configured properly in `package.json`. The only possible solution is currently this:

```json
{
"name": "@adeira/js",
"module": "src/index.mjs",
"main": "src/index"
}
```

Notice that field `main` **cannot** have file extension! There is currently not many resources explaining why but it's further explained in [this Babel issue](https://github.com/babel/babel/issues/7294). This extension cannot be present even when you don't have `module` field but you are distributing MJS files anyway.

More resources:

- https://github.com/graphql/graphql-js/issues/1217
- https://nodejs.org/api/esm.html#esm_package_entry_points
- https://nodejs.org/api/modules.html#modules_addenda_the_mjs_extension
- https://webpack.js.org/concepts/module-resolution/
- https://github.com/webpack/webpack/issues/7482
You can read more about [Flow Declaration Files here](https://flow.org/en/docs/declarations/).
3 changes: 1 addition & 2 deletions src/monorepo-npm-publisher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
"license": "MIT",
"private": false,
"version": "2.0.0",
"main": "src/index",
"main": "./src/index.js",
"sideEffects": false,
"dependencies": {
"@adeira/babel-preset-adeira": "^3.0.0",
"@adeira/fixtures-tester": "^1.0.1",
"@adeira/monorepo-utils": "^0.11.0",
"@babel/runtime": "^7.14.0",
"chalk": "^4.1.1",
Expand Down
Loading

0 comments on commit 4644aba

Please sign in to comment.