Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enabling sideEffects on style loaders #1423

Merged
merged 7 commits into from
Mar 9, 2021

Conversation

rschristian
Copy link
Member

@rschristian rschristian commented Sep 14, 2020

What kind of change does this PR introduce?

Bugfix / feature

Did you add tests for your changes?

Yes. Added a test to ensure that non-module CSS imported within a package that has "sideEffects": false would result in CSS being inlined in the head of the document. If the change to the style loader is reverted, the CSS import is ignored (skipped? not sure what it technically does) and the CSS will not end up being inlined.

Summary

Fixes #1411

I noticed this issue while building a component library. This library uses the Preact CLI to (mainly) run a development environment and Microbundle is used to the build/package the library itself. This is all well and fine.

The issue that this PR fixes arose when I had decided that my dev environment looked good enough to toss up as demo of the library. The sideEffects field stops the Preact CLI from importing non-module CSS when set to false in a production build. This should not happen as import 'x.css'; will always have a side effect. It's the very nature of that type of import.

Reading through various threads led me to CRA's solution, which was to enable sideEffects for their style loaders. Doing something similar with our setup seems to work just as well.

@developit had suggested something along the lines of the following which also would work with slight modification:

// in webpack.config.base.js
module: {
  rules: [
    {
      test: /\.m?[jt]sx?$/,
      exclude: nodeModules,
      sideEffects: true
    }
  ]
}

Changing this to target CSS works just as well as what I did in this PR. I figured it's simpler to just add a field to the existing loader rather than add a new one. There might be a distinction between the two methods, but I'm not sure what that would be.

Does this PR introduce a breaking change?

Shouldn't be breaking.

@changeset-bot
Copy link

changeset-bot bot commented Sep 14, 2020

🦋 Changeset detected

Latest commit: 4dc6675

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
preact-cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rschristian rschristian changed the title fix: Enabling sideEffects for style loaders fix: Enabling sideEffects on style loaders Sep 15, 2020
@rschristian rschristian marked this pull request as ready for review September 15, 2020 05:16
@rschristian rschristian requested a review from a team as a code owner September 15, 2020 05:16
Comment on lines +272 to +275
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment taken right from CRA, I felt that having an explanation in the source is useful.

@rschristian
Copy link
Member Author

rschristian commented Sep 15, 2020

Something that may be awkward is how to handle users who want to disable CSS modules. Besides disabling the modules option they might also need to know to add the side effects field.

Not sure what the best way to handle that is though. Maybe just keep it in mind if anyone runs into issues?

@ForsakenHarmony
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Mar 8, 2021
**What kind of change does this PR introduce?**

Bugfix / feature

**Did you add tests for your changes?**

Yes. Added a test to ensure that non-module CSS imported within a package that has `"sideEffects": false` would result in CSS being inlined in the head of the document. If the change to the style loader is reverted, the CSS import is ignored (skipped? not sure what it technically does) and the CSS will not end up being inlined.

**Summary**

Fixes #1411

I noticed this issue while building a component library. This library uses the Preact CLI to (mainly) run a development environment and Microbundle is used to the build/package the library itself. This is all well and fine. 

The issue that this PR fixes arose when I had decided that my dev environment looked good enough to toss up as demo of the library. The `sideEffects` field stops the Preact CLI from importing non-module CSS when set to `false` in a production build. This should not happen as `import 'x.css';` will always have a side effect. It's the very nature of that type of import.

Reading through various threads led me to CRA's solution, which was to [enable sideEffects for their style loaders](https://github.com/facebook/create-react-app/blob/65d8eb23f28475716e7e9497215cd574c1bf0b6c/packages/react-scripts/config/webpack.config.js#L502). Doing something similar with our setup seems to work just as well.

@developit had suggested something along the lines of the following which also would work with slight modification:

```
// in webpack.config.base.js
module: {
  rules: [
    {
      test: /\.m?[jt]sx?$/,
      exclude: nodeModules,
      sideEffects: true
    }
  ]
}
```

Changing this to target CSS works just as well as what I did in this PR. I figured it's simpler to just add a field to the existing loader rather than add a new one. There might be a distinction between the two methods, but I'm not sure what that would be.

**Does this PR introduce a breaking change?**

Shouldn't be breaking.

Co-authored-by: Ryan Christian <[email protected]>
Co-authored-by: Ryan Christian <[email protected]>
@ForsakenHarmony
Copy link
Member

bors retry

bors bot pushed a commit that referenced this pull request Mar 9, 2021
**What kind of change does this PR introduce?**

Bugfix / feature

**Did you add tests for your changes?**

Yes. Added a test to ensure that non-module CSS imported within a package that has `"sideEffects": false` would result in CSS being inlined in the head of the document. If the change to the style loader is reverted, the CSS import is ignored (skipped? not sure what it technically does) and the CSS will not end up being inlined.

**Summary**

Fixes #1411

I noticed this issue while building a component library. This library uses the Preact CLI to (mainly) run a development environment and Microbundle is used to the build/package the library itself. This is all well and fine. 

The issue that this PR fixes arose when I had decided that my dev environment looked good enough to toss up as demo of the library. The `sideEffects` field stops the Preact CLI from importing non-module CSS when set to `false` in a production build. This should not happen as `import 'x.css';` will always have a side effect. It's the very nature of that type of import.

Reading through various threads led me to CRA's solution, which was to [enable sideEffects for their style loaders](https://github.com/facebook/create-react-app/blob/65d8eb23f28475716e7e9497215cd574c1bf0b6c/packages/react-scripts/config/webpack.config.js#L502). Doing something similar with our setup seems to work just as well.

@developit had suggested something along the lines of the following which also would work with slight modification:

```
// in webpack.config.base.js
module: {
  rules: [
    {
      test: /\.m?[jt]sx?$/,
      exclude: nodeModules,
      sideEffects: true
    }
  ]
}
```

Changing this to target CSS works just as well as what I did in this PR. I figured it's simpler to just add a field to the existing loader rather than add a new one. There might be a distinction between the two methods, but I'm not sure what that would be.

**Does this PR introduce a breaking change?**

Shouldn't be breaking.

Co-authored-by: Ryan Christian <[email protected]>
Co-authored-by: Ryan Christian <[email protected]>
@ForsakenHarmony ForsakenHarmony merged commit cc2f3e7 into preactjs:master Mar 9, 2021
@preact-bot preact-bot mentioned this pull request Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI takes "sideEffects" field of package into account while building application
2 participants