Skip to content

Commit 92301c5

Browse files
authored
feat: mark scripts in libraries as being side effect free by default (#12372)
1 parent 6f273fb commit 92301c5

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

.changeset/clean-wolves-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-svelte": minor
3+
---
4+
5+
feat: mark scripts in libraries as being side effect free by default

documentation/docs/30-advanced/70-packaging.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ This is a legacy field that enabled tooling to recognise Svelte component librar
128128

129129
### sideEffects
130130

131-
The `sideEffects` field in `package.json` is used by bundlers to determine if a module may contain code that has side-effects. A module is considered to have side-effects if it makes changes that are observable from other scripts outside the module when it's imported. For example, side-effects include modifying global variables or the prototype of built-in JavaScript objects. Because a side-effect could potentially affect the behavior of other parts of the application, these files/modules will be included in the final bundle regardless of whether their exports are used in the application. It is a best practice to avoid side-effects in your code.
131+
The `sideEffects` field in `package.json` is used by bundlers to determine if a module may contain code that has side effects. A module is considered to have side effects if it makes changes that are observable from other scripts outside the module when it's imported. For example, side effects include modifying global variables or the prototype of built-in JavaScript objects. Because a side effect could potentially affect the behavior of other parts of the application, these files/modules will be included in the final bundle regardless of whether their exports are used in the application. It is a best practice to avoid side effects in your code.
132132

133-
Setting the `sideEffects` field in `package.json` can help the bundler to be more aggressive in eliminating unused exports from the final bundle, a process known as tree-shaking. This results in smaller and more efficient bundles. Different bundlers handle `sideEffects` in various manners. While not necessary for Vite, we recommend that libraries state that all CSS files have side-effects so that your library will be [compatible with webpack](https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free).
133+
Setting the `sideEffects` field in `package.json` can help the bundler to be more aggressive in eliminating unused exports from the final bundle, a process known as tree-shaking. This results in smaller and more efficient bundles. Different bundlers handle `sideEffects` in various manners. While not necessary for Vite, we recommend that libraries state that all CSS files have side effects so that your library will be [compatible with webpack](https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free). This is the configuration that comes with newly created projects:
134134

135135
```json
136136
/// file: package.json
@@ -139,12 +139,17 @@ Setting the `sideEffects` field in `package.json` can help the bundler to be mor
139139
}
140140
```
141141

142-
Make sure that `"sideEffects"` is correctly set. If a file with side effects is incorrectly marked as having no side effects, it can result in broken functionality. If your package has files with side effects, you can specify them in an array:
142+
> If the scripts in your library have side effects, ensure that you update the `sideEffects` field. All scripts are marked as side effect free by default in newly created projects. If a file with side effects is incorrectly marked as having no side effects, it can result in broken functionality.
143+
144+
If your package has files with side effects, you can specify them in an array:
143145

144146
```json
145147
/// file: package.json
146148
{
147-
"sideEffects": ["**/*.css", "./src/sideEffectfulFile.js"]
149+
"sideEffects": [
150+
"**/*.css",
151+
"./dist/sideEffectfulFile.js"
152+
]
148153
}
149154
```
150155

packages/create-svelte/templates/skeletonlib/package.template.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"svelte": "./dist/index.js"
1515
}
1616
},
17+
"sideEffects": ["**/*.css"],
1718
"files": ["dist", "!dist/**/*.test.*", "!dist/**/*.spec.*"],
1819
"peerDependencies": {
1920
"svelte": "^4.0.0"

0 commit comments

Comments
 (0)