Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(shaker): keep side effects from modules (#1190)
- Loading branch information
1 parent
a6ab7e9
commit cf1d661
Showing
6 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@linaria/shaker': patch | ||
--- | ||
|
||
fix: keep side effects from modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/shaker/src/plugins/__tests__/utils/shouldKeepSideEffect.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import shouldKeepSideEffect from '../../utils/shouldKeepSideEffect'; | ||
|
||
describe('shouldKeepSideEffect', () => { | ||
it('allows modules', () => { | ||
expect(shouldKeepSideEffect('@babel/runtime')).toBeTruthy(); | ||
expect(shouldKeepSideEffect('regenerator-runtime')).toBeTruthy(); | ||
|
||
expect(shouldKeepSideEffect('./side-effect')).toBeTruthy(); | ||
}); | ||
|
||
it('allows extensions', () => { | ||
expect(shouldKeepSideEffect('./side-effect.js')).toBeTruthy(); | ||
expect(shouldKeepSideEffect('./side-effect.cjs')).toBeTruthy(); | ||
expect(shouldKeepSideEffect('./side-effect.mjs')).toBeTruthy(); | ||
|
||
expect(shouldKeepSideEffect('regenerator-runtime/runtime.js')).toBeTruthy(); | ||
}); | ||
|
||
it('skips assets', () => { | ||
expect(shouldKeepSideEffect('./asset.css')).toBeFalsy(); | ||
expect(shouldKeepSideEffect('./asset.scss')).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const EXT_REGEX = /\.[0-9a-z]+$/; | ||
const ALLOWED_EXTENSIONS = ['.js', '.cjs', '.mjs']; | ||
|
||
export default function shouldKeepSideEffect(importPath: string) { | ||
const [ext] = importPath.match(EXT_REGEX) || ['']; | ||
|
||
return ext === '' || ALLOWED_EXTENSIONS.includes(ext); | ||
} |