Skip to content

Commit 573d5dd

Browse files
committed
added a modules option
1 parent a2395ae commit 573d5dd

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ export default {
1919
};
2020
```
2121

22-
This will make all imported css files be bundled into a single css file as well as make css files accessible as a default export.
23-
24-
This plugins supports two forms of importing css, when a css file is imported without an assigned variable, the css is bundled into a single css file.
22+
This plugins supports three forms of importing css.
2523
```js
26-
import "./styles.css";
27-
import styles from "./styles.css";
24+
import "./styles.css"; /* extract the styles to a external css bundle */
25+
import styles from "./styles.css"; /* import the styles as a string */
26+
import styles from "./styles.css"; assert { type: "css" }; /* import the styles as a CSSStyleSheet */
2827
```
2928

3029
If your build uses code splitting via dynamic imports or multiple entry points, this plugin will combine all css imports into a single file.
@@ -70,6 +69,13 @@ Default: `false`
7069

7170
Minifies the css being imported.
7271

72+
### modules
73+
74+
Type: `boolean`
75+
Default: `false`
76+
77+
All css files being imported with a variable will use native CSS Modules.
78+
7379
### alwaysOutput
7480

7581
Type: `boolean`
@@ -81,7 +87,7 @@ Always output a css bundle even if the css output is empty.
8187

8288
## Why
8389

84-
With WebComponent frameworks, its useful to be able to import the css for a component. Other solutions for rollup either lack features or are large and bloated with extra features that some users may not need such as SASS or LESS support. This plugin is small and by default only supports plain css with the option to process it further. Unlike most other css plugins this plugin maintains the order of your imports in order to avoid overwritting css unexpectedly.
90+
With WebComponent frameworks, its useful to be able to import the css for components in a variety of ways. Other solutions for Rollup either lack features or are large and bloated with extra features that some users may not need such as SASS or LESS support. This plugin is small and by default only supports plain css with the option to process it further. Unlike most other css plugins this plugin maintains the order of your imports in order to avoid overwritting css unexpectedly.
8591

8692
---
8793

src/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default (options = {}) => {
3838
}
3939

4040
const moduleInfo = this.getModuleInfo(id);
41-
if (moduleInfo.assertions?.type == "css") {
41+
if (options.modules || moduleInfo.assertions?.type == "css") {
4242
return {
4343
code: `export default new CSSStyleSheet().replaceSync(${JSON.stringify(transformedCode)});`,
4444
map: { mappings: "" }
@@ -68,8 +68,9 @@ export default (options = {}) => {
6868
/* remove css that was imported as a string */
6969
const css = Object.entries(styles)
7070
.sort((a, b) => moduleIds.indexOf(a[0]) - moduleIds.indexOf(b[0]))
71-
.filter((module) => !modules[module.id])
72-
.map((module) => module.code)
71+
.map(([id, code]) => {
72+
if (!modules[id]) return code;
73+
})
7374
.join("\n");
7475

7576
if (css.trim().length <= 0 && !alwaysOutput) return;

types/plugin.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ declare interface Options {
66
output?: string;
77
transform?: Function;
88
minify?: boolean;
9+
modules?: boolean;
910
alwaysOutput?: boolean;
1011
}
1112

0 commit comments

Comments
 (0)