From 325498e2fd462ec2d0d14c166b0541858af71a22 Mon Sep 17 00:00:00 2001
From: Jarda Snajdr <jsnajdr@gmail.com>
Date: Tue, 27 Oct 2020 13:20:09 +0100
Subject: [PATCH] Rewrite sideEffects flags to use only positive patterns
 (#26452)

* Rewrite sideEffects flags to use only positive patterns

* Add sideEffects declarations for src/**/*.scss files
---
 packages/README.md                    | 13 -------------
 packages/block-directory/package.json |  4 ----
 packages/block-editor/package.json    |  3 ++-
 packages/block-library/package.json   |  3 ++-
 packages/blocks/package.json          |  2 +-
 packages/components/package.json      |  3 ++-
 packages/core-data/package.json       |  2 +-
 packages/edit-navigation/package.json |  4 ----
 packages/edit-post/package.json       |  4 ----
 packages/edit-site/package.json       |  4 ----
 packages/edit-widgets/package.json    |  4 ----
 packages/editor/package.json          |  3 ++-
 packages/interface/package.json       |  3 ++-
 packages/nux/package.json             |  3 ++-
 packages/primitives/package.json      |  4 +++-
 packages/reusable-blocks/package.json |  3 +--
 packages/rich-text/package.json       |  3 ++-
 packages/side-effects.md              | 16 ----------------
 18 files changed, 20 insertions(+), 61 deletions(-)

diff --git a/packages/README.md b/packages/README.md
index 62609309bce59d..9d8c549ab96fd6 100644
--- a/packages/README.md
+++ b/packages/README.md
@@ -308,17 +308,4 @@ If your package includes a few files with side effects, you can list them instea
 }
 ```
 
-Many `@wordpress` UI-focused packages rely on side effects for registering blocks, plugins, and data stores. To reduce maintenance costs, it may be preferable to opt for an inverse glob strategy, where you instead list the paths where side effects are *not* present, leaving the bundler to assume that everything else might have them. This results in a glob with multiple roots (to match `@wordpress` package structure) and one or more excluded directories.
-
-Here is an example where we declare that the `components` and `utils` directories are side effect-free:
-
-```json
-{
-	"name": "package",
-	"sideEffects": [
-		"!((src|build|build-module)/(components|utils)/**)"
-	],
-}
-```
-
 Please consult the [side effects documentation](./side-effects.md) for more information on identifying and declaring side effects.
diff --git a/packages/block-directory/package.json b/packages/block-directory/package.json
index 5adcf7656c9a3b..7394385df5cfd8 100644
--- a/packages/block-directory/package.json
+++ b/packages/block-directory/package.json
@@ -21,10 +21,6 @@
 	"main": "build/index.js",
 	"module": "build-module/index.js",
 	"react-native": "src/index",
-	"sideEffects": [
-		"build-style/**",
-		"!((src|build|build-module)/components/**)"
-	],
 	"dependencies": {
 		"@wordpress/a11y": "file:../a11y",
 		"@wordpress/api-fetch": "file:../api-fetch",
diff --git a/packages/block-editor/package.json b/packages/block-editor/package.json
index 114b266f7d19b5..95460269e90037 100644
--- a/packages/block-editor/package.json
+++ b/packages/block-editor/package.json
@@ -24,7 +24,8 @@
 	"react-native": "src/index",
 	"sideEffects": [
 		"build-style/**",
-		"!((src|build|build-module)/(components|utils)/**)"
+		"src/**/*.scss",
+		"{src,build,build-module}/{index.js,store/index.js,hooks/**}"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/block-library/package.json b/packages/block-library/package.json
index b5ef3306280744..4fa1c4904debc4 100644
--- a/packages/block-library/package.json
+++ b/packages/block-library/package.json
@@ -22,7 +22,8 @@
 	"module": "build-module/index.js",
 	"react-native": "src/index",
 	"sideEffects": [
-		"build-style/**"
+		"build-style/**",
+		"src/**/*.scss"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/blocks/package.json b/packages/blocks/package.json
index b79f7d62bce405..8bb5d524e3d767 100644
--- a/packages/blocks/package.json
+++ b/packages/blocks/package.json
@@ -22,7 +22,7 @@
 	"module": "build-module/index.js",
 	"react-native": "src/index",
 	"sideEffects": [
-		"!((src|build|build-module)/api/**)"
+		"{src,build,build-module}/{index.js,store/index.js}"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/components/package.json b/packages/components/package.json
index ad6f16f9d8d263..ea8270de174f78 100644
--- a/packages/components/package.json
+++ b/packages/components/package.json
@@ -22,7 +22,8 @@
 	"module": "build-module/index.js",
 	"react-native": "src/index",
 	"sideEffects": [
-		"build-style/**"
+		"build-style/**",
+		"src/**/*.scss"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/core-data/package.json b/packages/core-data/package.json
index fd6534c182d9ab..fb16c6212c62a6 100644
--- a/packages/core-data/package.json
+++ b/packages/core-data/package.json
@@ -23,7 +23,7 @@
 	"module": "build-module/index.js",
 	"react-native": "src/index",
 	"sideEffects": [
-		"(src|build|build-module)/index.js"
+		"{src,build,build-module}/index.js"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/edit-navigation/package.json b/packages/edit-navigation/package.json
index e80ae12865edce..6324ea5f9ab6d8 100644
--- a/packages/edit-navigation/package.json
+++ b/packages/edit-navigation/package.json
@@ -23,10 +23,6 @@
 	"main": "build/index.js",
 	"module": "build-module/index.js",
 	"react-native": "src/index",
-	"sideEffects": [
-		"build-style/**",
-		"!((src|build|build-module)/components/**)"
-	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
 		"@wordpress/api-fetch": "file:../api-fetch",
diff --git a/packages/edit-post/package.json b/packages/edit-post/package.json
index 43c46552d1702b..6d0feda1ad045c 100644
--- a/packages/edit-post/package.json
+++ b/packages/edit-post/package.json
@@ -22,10 +22,6 @@
 	"main": "build/index.js",
 	"module": "build-module/index.js",
 	"react-native": "src/index",
-	"sideEffects": [
-		"build-style/**",
-		"!((src|build|build-module)/(components|utils)/**)"
-	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
 		"@wordpress/a11y": "file:../a11y",
diff --git a/packages/edit-site/package.json b/packages/edit-site/package.json
index e929666a6d3df3..9aebab7f34c81d 100644
--- a/packages/edit-site/package.json
+++ b/packages/edit-site/package.json
@@ -22,10 +22,6 @@
 	"main": "build/index.js",
 	"module": "build-module/index.js",
 	"react-native": "src/index",
-	"sideEffects": [
-		"build-style/**",
-		"!((src|build|build-module)/components/**)"
-	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
 		"@wordpress/a11y": "file:../a11y",
diff --git a/packages/edit-widgets/package.json b/packages/edit-widgets/package.json
index 4f720a96d7e0bd..6eca411e389b0b 100644
--- a/packages/edit-widgets/package.json
+++ b/packages/edit-widgets/package.json
@@ -22,10 +22,6 @@
 	"main": "build/index.js",
 	"module": "build-module/index.js",
 	"react-native": "src/index",
-	"sideEffects": [
-		"build-style/**",
-		"!((src|build|build-module)/components/**)"
-	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
 		"@wordpress/api-fetch": "file:../api-fetch",
diff --git a/packages/editor/package.json b/packages/editor/package.json
index 90dee0b3e7059b..74c04ad83aaa65 100644
--- a/packages/editor/package.json
+++ b/packages/editor/package.json
@@ -23,7 +23,8 @@
 	"react-native": "src/index",
 	"sideEffects": [
 		"build-style/**",
-		"!((src|build|build-module)/(components|utils)/**)"
+		"src/**/*.scss",
+		"{src,build,build-module}/{index.js,store/index.js,hooks/**}"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/interface/package.json b/packages/interface/package.json
index d03921c686dd3b..a3636aef2b3c2c 100644
--- a/packages/interface/package.json
+++ b/packages/interface/package.json
@@ -24,7 +24,8 @@
 	"react-native": "src/index",
 	"sideEffects": [
 		"build-style/**",
-		"!((src|build|build-module)/components/**)"
+		"src/**/*.scss",
+		"{src,build,build-module}/{index.js,store/index.js}"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/nux/package.json b/packages/nux/package.json
index 4eea1042328a5b..11131a75aef3f1 100644
--- a/packages/nux/package.json
+++ b/packages/nux/package.json
@@ -23,7 +23,8 @@
 	"react-native": "src/index",
 	"sideEffects": [
 		"build-style/**",
-		"!((src|build|build-module)/components/**)"
+		"src/**/*.scss",
+		"{src,build,build-module}/{index.js,store/index.js}"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/primitives/package.json b/packages/primitives/package.json
index 3ea6ec5087b8ee..7a378704d4d178 100644
--- a/packages/primitives/package.json
+++ b/packages/primitives/package.json
@@ -22,7 +22,9 @@
 	"main": "build/index.js",
 	"module": "build-module/index.js",
 	"react-native": "src/index",
-	"sideEffects": false,
+	"sideEffects": [
+		"src/**/*.scss"
+	],
 	"types": "build-types",
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/reusable-blocks/package.json b/packages/reusable-blocks/package.json
index ac393af19977ec..7887d38bb8a10e 100644
--- a/packages/reusable-blocks/package.json
+++ b/packages/reusable-blocks/package.json
@@ -22,8 +22,7 @@
 	"module": "build-module/index.js",
 	"react-native": "src/index",
 	"sideEffects": [
-		"build-style/**",
-		"!((src|build|build-module)/(components|utils)/**)"
+		"{src,build,build-module}/{index.js,store/index.js}"
 	],
 	"dependencies": {
 		"@wordpress/block-editor": "file:../block-editor",
diff --git a/packages/rich-text/package.json b/packages/rich-text/package.json
index 6e7df799ab95de..4ace95f8c2554c 100644
--- a/packages/rich-text/package.json
+++ b/packages/rich-text/package.json
@@ -22,7 +22,8 @@
 	"module": "build-module/index.js",
 	"react-native": "src/index",
 	"sideEffects": [
-		"!((src|build|build-module)/component/**)"
+		"src/**/*.scss",
+		"{src,build,build-module}/{index.js,store/index.js}"
 	],
 	"dependencies": {
 		"@babel/runtime": "^7.11.2",
diff --git a/packages/side-effects.md b/packages/side-effects.md
index d55742ff080878..c0e9c7ba07e52d 100644
--- a/packages/side-effects.md
+++ b/packages/side-effects.md
@@ -86,19 +86,3 @@ If it has a few files with side effects, it can list them:
 ```
 
 This allows the bundler to assume that only the modules that were declared have side effects, and *nothing else does*. Of course, this means that we need to be careful to include everything that *does* have side effects, or problems can arise in applications that make use of the package.
-
-## The approach in `@wordpress`
-
-In order to reduce maintenance cost and minimize the chance of breakage, we opted for using inverse globs for a number of `@wordpress` packages, where we list the paths that *do not* include side effects, leaving the bundler to assume that everything else does. Here's an example:
-
-```json
-{
-	"sideEffects": [
-		"!((src|build|build-module)/(components|utils)/**)"
-	],
-}
-```
-
-The above means that the bundler should assume that anything outside the `components` and `utils` directories contains side effects, and nothing in those directories does. These directories can be inside of a `src`, `build`, or `build-module` top-level directory in the package, due to the way `@wordpress` packages are built.
-
-This approach should guarantee that everything in `components` and `utils` can be tree-shaken. It will only potentially cause problems if one of the files in there uses side effects, which would be a bad practice for a component or utility file.