Skip to content

Commit

Permalink
Revert "Revert "Packages: Use npm workspaces (#65681)" (#66270)"
Browse files Browse the repository at this point in the history
This reverts commit 91272a2.
  • Loading branch information
sirreal committed Oct 22, 2024
1 parent e43a1c5 commit bfb1f15
Show file tree
Hide file tree
Showing 182 changed files with 797 additions and 866 deletions.
69 changes: 69 additions & 0 deletions bin/check-licenses.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env node

/**
* External dependencies
*/
import { spawnSync } from 'node:child_process';

/**
* Internal dependencies
*/
import { checkDepsInTree } from '../packages/scripts/utils/license.js';

const ignored = [ '@ampproject/remapping' ];

/*
* `wp-scripts check-licenses` uses prod and dev dependencies of the package to scan for dependencies. With npm workspaces, workspace packages (the @wordpress/* packages) are not listed in the main package json and this approach does not work.
*
* Instead, work from an npm query that uses some custom information in package.json files to declare packages that are shipped with WordPress (and must be GPLv2 compatible) or other files that may use more permissive licenses.
*/

/**
* @typedef PackageInfo
* @property {string} name Package name.
*/

/** @type {ReadonlyArray<PackageInfo>} */
const workspacePackages = JSON.parse(
spawnSync(
'npm',
[
'query',
'.workspace:attr([wpScript]), .workspace:attr([wpScriptModuleExports])',
],
/*
* Set the max buffer to ~157MB, since the output size for
* prod is ~21 MB and dev is ~110 MB
*/
{ maxBuffer: 1024 * 1024 * 150 }
).stdout
);

const packageNames = workspacePackages.map( ( { name } ) => name );

const dependenciesToProcess = JSON.parse(
spawnSync(
'npm',
[
'ls',
'--json',
'--long',
'--all',
'--lockfile-only',
'--omit=dev',
...packageNames.map(
( packageName ) => `--workspace=${ packageName }`
),
],
/*
* Set the max buffer to ~157MB, since the output size for
* prod is ~21 MB and dev is ~110 MB
*/
{ maxBuffer: 1024 * 1024 * 150 }
).stdout
).dependencies;

checkDepsInTree( dependenciesToProcess, {
ignored,
gpl2: true,
} );
6 changes: 1 addition & 5 deletions docs/contributors/code/managing-packages.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# Managing Packages

This repository uses [monorepo] to manage WordPress modules and publish them with [lerna] as packages to [npm]. This enforces certain steps in the workflow which are described in details in [packages](https://github.com/WordPress/gutenberg/blob/HEAD/packages/README.md) documentation.
This repository uses [npm workspaces](https://docs.npmjs.com/cli/v10/using-npm/workspaces) to manage WordPress packages and [lerna](https://lerna.js.org/) to publish them to [npm](https://www.npmjs.com/). This enforces certain steps in the workflow which are described in details in [packages](https://github.com/WordPress/gutenberg/blob/HEAD/packages/README.md) documentation.

Maintaining dozens of npm packages is difficult—it can be tough to keep track of changes. That's why we use `CHANGELOG.md` files for each package to simplify the release process. As a contributor, you should add an entry to the aforementioned file each time you contribute adding production code as described in [Maintaining Changelogs](https://github.com/WordPress/gutenberg/blob/HEAD/packages/README.md#maintaining-changelogs) section.

Publishing WordPress packages to npm is automated by synchronizing it with the bi-weekly Gutenberg plugin RC1 release. You can learn more about this process and other ways to publish new versions of npm packages in the [Gutenberg Release Process document](/docs/contributors/code/release.md#packages-releases-to-npm-and-wordpress-core-updates).

[lerna]: https://lerna.js.org/
[monorepo]: https://monorepo.tools
[npm]: https://www.npmjs.com/
111 changes: 6 additions & 105 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,6 @@
"config": {
"IS_GUTENBERG_PLUGIN": true
},
"dependencies": {
"@wordpress/a11y": "file:packages/a11y",
"@wordpress/annotations": "file:packages/annotations",
"@wordpress/api-fetch": "file:packages/api-fetch",
"@wordpress/autop": "file:packages/autop",
"@wordpress/blob": "file:packages/blob",
"@wordpress/block-directory": "file:packages/block-directory",
"@wordpress/block-editor": "file:packages/block-editor",
"@wordpress/block-library": "file:packages/block-library",
"@wordpress/block-serialization-default-parser": "file:packages/block-serialization-default-parser",
"@wordpress/block-serialization-spec-parser": "file:packages/block-serialization-spec-parser",
"@wordpress/blocks": "file:packages/blocks",
"@wordpress/commands": "file:packages/commands",
"@wordpress/components": "file:packages/components",
"@wordpress/compose": "file:packages/compose",
"@wordpress/core-commands": "file:packages/core-commands",
"@wordpress/core-data": "file:packages/core-data",
"@wordpress/customize-widgets": "file:packages/customize-widgets",
"@wordpress/data": "file:packages/data",
"@wordpress/data-controls": "file:packages/data-controls",
"@wordpress/dataviews": "file:packages/dataviews",
"@wordpress/date": "file:packages/date",
"@wordpress/deprecated": "file:packages/deprecated",
"@wordpress/dom": "file:packages/dom",
"@wordpress/dom-ready": "file:packages/dom-ready",
"@wordpress/edit-post": "file:packages/edit-post",
"@wordpress/edit-site": "file:packages/edit-site",
"@wordpress/edit-widgets": "file:packages/edit-widgets",
"@wordpress/editor": "file:packages/editor",
"@wordpress/element": "file:packages/element",
"@wordpress/escape-html": "file:packages/escape-html",
"@wordpress/fields": "file:packages/fields",
"@wordpress/format-library": "file:packages/format-library",
"@wordpress/hooks": "file:packages/hooks",
"@wordpress/html-entities": "file:packages/html-entities",
"@wordpress/i18n": "file:packages/i18n",
"@wordpress/icons": "file:packages/icons",
"@wordpress/interactivity": "file:packages/interactivity",
"@wordpress/interactivity-router": "file:packages/interactivity-router",
"@wordpress/interface": "file:packages/interface",
"@wordpress/is-shallow-equal": "file:packages/is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:packages/keyboard-shortcuts",
"@wordpress/keycodes": "file:packages/keycodes",
"@wordpress/list-reusable-blocks": "file:packages/list-reusable-blocks",
"@wordpress/media-utils": "file:packages/media-utils",
"@wordpress/notices": "file:packages/notices",
"@wordpress/nux": "file:packages/nux",
"@wordpress/patterns": "file:packages/patterns",
"@wordpress/plugins": "file:packages/plugins",
"@wordpress/preferences": "file:packages/preferences",
"@wordpress/preferences-persistence": "file:packages/preferences-persistence",
"@wordpress/primitives": "file:packages/primitives",
"@wordpress/priority-queue": "file:packages/priority-queue",
"@wordpress/private-apis": "file:packages/private-apis",
"@wordpress/react-i18n": "file:packages/react-i18n",
"@wordpress/react-native-aztec": "file:packages/react-native-aztec",
"@wordpress/react-native-bridge": "file:packages/react-native-bridge",
"@wordpress/react-native-editor": "file:packages/react-native-editor",
"@wordpress/redux-routine": "file:packages/redux-routine",
"@wordpress/reusable-blocks": "file:packages/reusable-blocks",
"@wordpress/rich-text": "file:packages/rich-text",
"@wordpress/router": "file:packages/router",
"@wordpress/server-side-render": "file:packages/server-side-render",
"@wordpress/shortcode": "file:packages/shortcode",
"@wordpress/style-engine": "file:packages/style-engine",
"@wordpress/sync": "file:packages/sync",
"@wordpress/token-list": "file:packages/token-list",
"@wordpress/undo-manager": "file:packages/undo-manager",
"@wordpress/url": "file:packages/url",
"@wordpress/viewport": "file:packages/viewport",
"@wordpress/vips": "file:packages/vips",
"@wordpress/warning": "file:packages/warning",
"@wordpress/widgets": "file:packages/widgets",
"@wordpress/wordcount": "file:packages/wordcount"
},
"devDependencies": {
"@actions/core": "1.9.1",
"@actions/github": "5.0.0",
Expand Down Expand Up @@ -145,33 +70,6 @@
"@types/semver": "7.3.8",
"@types/sprintf-js": "1.1.2",
"@types/uuid": "8.3.1",
"@wordpress/babel-plugin-import-jsx-pragma": "file:packages/babel-plugin-import-jsx-pragma",
"@wordpress/babel-plugin-makepot": "file:packages/babel-plugin-makepot",
"@wordpress/babel-preset-default": "file:packages/babel-preset-default",
"@wordpress/base-styles": "file:packages/base-styles",
"@wordpress/browserslist-config": "file:packages/browserslist-config",
"@wordpress/create-block": "file:packages/create-block",
"@wordpress/create-block-tutorial-template": "file:packages/create-block-tutorial-template",
"@wordpress/dependency-extraction-webpack-plugin": "file:packages/dependency-extraction-webpack-plugin",
"@wordpress/docgen": "file:packages/docgen",
"@wordpress/e2e-test-utils": "file:packages/e2e-test-utils",
"@wordpress/e2e-test-utils-playwright": "file:packages/e2e-test-utils-playwright",
"@wordpress/e2e-tests": "file:packages/e2e-tests",
"@wordpress/env": "file:packages/env",
"@wordpress/eslint-plugin": "file:packages/eslint-plugin",
"@wordpress/jest-console": "file:packages/jest-console",
"@wordpress/jest-preset-default": "file:packages/jest-preset-default",
"@wordpress/jest-puppeteer-axe": "file:packages/jest-puppeteer-axe",
"@wordpress/lazy-import": "file:packages/lazy-import",
"@wordpress/npm-package-json-lint-config": "file:packages/npm-package-json-lint-config",
"@wordpress/postcss-plugins-preset": "file:packages/postcss-plugins-preset",
"@wordpress/postcss-themes": "file:packages/postcss-themes",
"@wordpress/prettier-config": "file:packages/prettier-config",
"@wordpress/project-management-automation": "file:packages/project-management-automation",
"@wordpress/readable-js-assets-webpack-plugin": "file:packages/readable-js-assets-webpack-plugin",
"@wordpress/report-flaky-tests": "file:packages/report-flaky-tests",
"@wordpress/scripts": "file:packages/scripts",
"@wordpress/stylelint-config": "file:packages/stylelint-config",
"ajv": "8.7.1",
"appium": "2.0.0",
"babel-jest": "29.7.0",
Expand Down Expand Up @@ -276,7 +174,7 @@
"build": "npm run build:packages && wp-scripts build",
"build:analyze-bundles": "npm run build -- --webpack-bundle-analyzer",
"build:package-types": "node ./bin/packages/validate-typescript-version.js && ( tsc --build || ( echo 'tsc failed. Try cleaning up first: `npm run clean:package-types`'; exit 1 ) ) && node ./bin/packages/check-build-type-declaration-files.js",
"prebuild:packages": "npm run clean:packages && lerna run build",
"prebuild:packages": "npm run clean:packages && npm run --if-present --workspaces build",
"build:packages": "npm run --silent build:package-types && node ./bin/packages/build.js",
"build:plugin-zip": "bash ./bin/build-plugin-zip.sh",
"clean:package-types": "tsc --build --clean && rimraf \"./packages/*/build-types\"",
Expand Down Expand Up @@ -310,7 +208,7 @@
"lint:pkg-json": "wp-scripts lint-pkg-json . 'packages/*/package.json'",
"native": "npm run --prefix packages/react-native-editor",
"other:changelog": "node ./bin/plugin/cli.js changelog",
"other:check-licenses": "concurrently \"wp-scripts check-licenses --prod --gpl2 --ignore=@react-native-community/cli,@react-native-community/cli-platform-ios,@ampproject/remapping,human-signals,fb-watchman,walker,chrome-launcher,lighthouse-logger,chromium-edge-launcher\" \"wp-scripts check-licenses --dev\"",
"other:check-licenses": "concurrently \"node ./bin/check-licenses.mjs\" \"wp-scripts check-licenses --dev\"",
"preother:check-local-changes": "npm run docs:build",
"other:check-local-changes": "node ./bin/check-local-changes.js",
"other:cherry-pick": "node ./bin/cherry-pick.mjs",
Expand Down Expand Up @@ -389,5 +287,8 @@
"**/tsconfig.json": [
"npm run lint:tsconfig"
]
}
},
"workspaces": [
"packages/*"
]
}
41 changes: 15 additions & 26 deletions packages/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Managing Packages

This repository uses [monorepo] to manage WordPress modules and publish them with [lerna] as packages to [npm].
This repository uses [npm workspaces](https://docs.npmjs.com/cli/v10/using-npm/workspaces) to manage WordPress packages and [lerna](https://lerna.js.org/) to publish them with to [npm](https://www.npmjs.com/).

## Creating a New Package

Expand Down Expand Up @@ -36,19 +36,15 @@ When creating a new package, you need to provide at least the following:
}
```
This assumes that your code is located in the `src` folder and will be transpiled with `Babel`.
2. `.npmrc` file which disables creating `package-lock.json` file for the package:
```
package-lock=false
```
3. `README.md` file containing at least:
1. `README.md` file containing at least:
- Package name
- Package description
- Installation details
- Usage example
- API documentation, if applicable ([more info](#maintaining-api-documentation))
- A link to the contributing guidelines ([here's an example](https://github.com/WordPress/gutenberg/tree/HEAD/packages/a11y/README.md#contributing-to-this-package) from the a11y package)
- `Code is Poetry` logo (`<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>`)
4. `CHANGELOG.md` file containing at least:
1. `CHANGELOG.md` file containing at least:

```
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->
Expand All @@ -70,35 +66,32 @@ Production dependencies are stored in the `dependencies` section of the package

#### Adding New Dependencies

The simplest way to add a production dependency to one of the packages is to run a very convenient [lerna add](https://github.com/lerna/lerna/tree/HEAD/commands/add#readme) command from the root of the project.
The simplest way to add a production dependency to one of the packages is to run a command like the following from the root of the project.

_Example:_

```bash
lerna add change-case packages/a11y
npm install change-case -w packages/a11y
```

This command adds the latest version of `change-case` as a dependency to the `@wordpress/a11y` package, which is located in `packages/a11y` folder.
This command adds the `change-case` as a dependency to the `@wordpress/a11y` package, which is located in `packages/a11y` folder. If there was the same dependency installed then the version specified in the `package-lock.json` file is going to be reused. If you want to enforce a different version, you can do so by adding the `@` suffix to the package name.

_Example:_

```bash
npm install change-case@latest -w packages/a11y
```

#### Removing Existing Dependencies

Removing a dependency from one of the WordPress packages requires some manual work. You need to remove the line in the corresponding `dependencies` section of the `package.json` file.
Removing a dependency from one of the WordPress packages is similar to installation. You need to run a command like the following from the root of the project.

_Example:_

```diff
+++ b/packages/scripts/package.json
@@ -43,7 +43,6 @@
"check-node-version": "^4.1.0",
"cross-spawn": "^5.1.0",
"eslint": "^7.1.0",
- "jest": "^29.6.2",
"minimist": "^1.2.0",
"npm-package-json-lint": "^6.4.0",
```bash
npm uninstall change-case -w packages/a11y
```

Next, you need to run `npm install` in the root of the project to ensure that `package-lock.json` file gets properly regenerated.

#### Updating Existing Dependencies

This is the most confusing part of working with [monorepo] which causes a lot of hassles for contributors. The most successful strategy so far is to do the following:
Expand Down Expand Up @@ -239,10 +232,6 @@ For consumers to use the published type declarations, we'll set the `types` fiel

Ensure that the `build-types` directory will be included in the published package, for example if a `files` field is declared.

[lerna]: https://lerna.js.org/
[monorepo]: https://monorepo.tools
[npm]: https://www.npmjs.com/

## Optimizing for bundlers

In order for bundlers to tree-shake packages effectively, they often need to know whether a package includes side effects in its code. This is done through the `sideEffects` field in the package's `package.json`.
Expand Down
1 change: 0 additions & 1 deletion packages/a11y/.npmrc

This file was deleted.

7 changes: 4 additions & 3 deletions packages/a11y/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
"wpScriptModuleExports": "./build-module/module/index.js",
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/dom-ready": "file:../dom-ready",
"@wordpress/i18n": "file:../i18n"
"@wordpress/dom-ready": "*",
"@wordpress/i18n": "*"
},
"publishConfig": {
"access": "public"
}
},
"wpScript": true
}
1 change: 0 additions & 1 deletion packages/annotations/.npmrc

This file was deleted.

11 changes: 6 additions & 5 deletions packages/annotations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@
"react-native": "src/index",
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/data": "file:../data",
"@wordpress/hooks": "file:../hooks",
"@wordpress/i18n": "file:../i18n",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/data": "*",
"@wordpress/hooks": "*",
"@wordpress/i18n": "*",
"@wordpress/rich-text": "*",
"uuid": "^9.0.1"
},
"peerDependencies": {
"react": "^18.0.0"
},
"publishConfig": {
"access": "public"
}
},
"wpScript": true
}
1 change: 0 additions & 1 deletion packages/api-fetch/.npmrc

This file was deleted.

7 changes: 4 additions & 3 deletions packages/api-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
"types": "build-types",
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/i18n": "file:../i18n",
"@wordpress/url": "file:../url"
"@wordpress/i18n": "*",
"@wordpress/url": "*"
},
"publishConfig": {
"access": "public"
}
},
"wpScript": true
}
1 change: 0 additions & 1 deletion packages/autop/.npmrc

This file was deleted.

3 changes: 2 additions & 1 deletion packages/autop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
},
"publishConfig": {
"access": "public"
}
},
"wpScript": true
}
1 change: 0 additions & 1 deletion packages/babel-plugin-import-jsx-pragma/.npmrc

This file was deleted.

1 change: 0 additions & 1 deletion packages/babel-plugin-makepot/.npmrc

This file was deleted.

1 change: 0 additions & 1 deletion packages/babel-preset-default/.npmrc

This file was deleted.

Loading

0 comments on commit bfb1f15

Please sign in to comment.