Skip to content

Commit

Permalink
fix: Enabling sideEffects on style loaders (#1423)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Christian <[email protected]>
  • Loading branch information
rschristian and rschristian authored Mar 9, 2021
1 parent c33f020 commit cc2f3e7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-pumas-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-cli': patch
---

Fixes bug with style loader that would strip non-module CSS files if 'sideEffects' was set to false for the package.
5 changes: 5 additions & 0 deletions packages/cli/lib/lib/webpack/webpack-base-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ module.exports = function (env) {
},
},
],
// 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
sideEffects: true,
},
{
test: /\.(xml|html|txt|md)$/,
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ describe('preact build', () => {
expect(() => build(dir)).not.toThrow();
});

it('should import non-modules CSS even when side effects are false', async () => {
let dir = await subject('side-effect-css');
await build(dir);

let head = await getHead(dir);
expect(head).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.sideEffectCss))
);
});

it('should copy resources from static to build directory', async () => {
let dir = await subject('static-root');
await build(dir);
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/tests/images/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ exports.sass = `
</body>
`;

exports.sideEffectCss = `
<head>
<meta charset="utf-8">
<title>side-effect-css<\\/title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="apple-touch-icon" href=\\"\\/assets\\/icons\\/apple-touch-icon\\.png\\">
<link rel="manifest" href="\\/manifest\\.json">
<style>h1{background:#673ab8}<\\/style>
<link href=\\"/bundle.\\w{5}.css\\" rel=\\"stylesheet\\" media=\\"only x\\" onload=\\"this.media='all'\\">
<noscript>
<link rel=\\"stylesheet\\" href=\\"\\/bundle.\\w{5}.css\\">
</noscript>
<\\/head>
`;

exports.prerender = {};

exports.prerender.heads = {};
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/tests/subjects/side-effect-css/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { h } from 'preact';
import './style.css';

export default () => {
return <h1>SideEffect CSS test</h1>;
};
5 changes: 5 additions & 0 deletions packages/cli/tests/subjects/side-effect-css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"private": true,
"name": "side-effect-css",
"sideEffects": false
}
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/side-effect-css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
background: #673ab8;
}

0 comments on commit cc2f3e7

Please sign in to comment.