Skip to content

Commit f78d3ac

Browse files
johanholmerinzamotany
authored andcommitted
feat: add Rollup plugin (#253)
1 parent 631b884 commit f78d3ac

File tree

9 files changed

+140
-3
lines changed

9 files changed

+140
-3
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ yarn add linaria
4545

4646
## Setup
4747

48-
Linaria requires webpack to build your CSS. To set up the build, add the webpack loader to your `webpack.config.js` after `babel-loader` (if you use it):
48+
Linaria is compatible with webpack and Rollup.
49+
50+
### Webpack
51+
52+
To set up the build, add the webpack loader to your `webpack.config.js` after `babel-loader` (if you use it):
4953

5054
```js
5155
module: {
@@ -85,6 +89,29 @@ module: {
8589

8690
You also need `css-loader` and `mini-css-extract-plugin` in your pipeline. The usage is shown above.
8791

92+
### Rollup
93+
94+
To use linaria with Rollup you need to add it to your `rollup.config.js`, together with a plugin which handles CSS files, such as `rollup-plugin-css-only`.
95+
96+
```js
97+
import linaria from 'linaria/rollup';
98+
import css from 'rollup-plugin-css-only';
99+
100+
export default {
101+
/* rest of your config */
102+
plugins: [
103+
/* rest of your plugins */
104+
linaria({
105+
sourceMap: process.env.NODE_ENV !== 'production'
106+
}),
107+
css({
108+
output: 'styles.css'
109+
})
110+
]
111+
};
112+
```
113+
114+
88115
Now, the CSS you write with Linaria will be extracted at build time to the `styles.css` file. Linaria automatically vendor prefixes and strips whitespace from the CSS.
89116

90117
Linaria integrates with your CSS pipeline, so you can always perform additional operations on the CSS, for example, using [postcss](https://postcss.org/) plugins such as [clean-css](https://github.com/jakubpawlowicz/clean-css) to further minify your CSS.
@@ -171,6 +198,7 @@ Dynamic styles will be applied using CSS custom properties (aka CSS variables) a
171198
- [Critical CSS extraction](/docs/CRITICAL_CSS.md)
172199
- [Bundlers integration](/docs/BUNDLERS_INTEGRATION.md)
173200
- [Webpack](/docs/BUNDLERS_INTEGRATION.md#webpack)
201+
- [Rollup](/docs/BUNDLERS_INTEGRATION.md#rollup)
174202
- [Linting](/docs/LINTING.md)
175203
- [How it works](/docs/HOW_IT_WORKS.md)
176204
- [Example](/website)

docs/BABEL_PRESET.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `linaria/babel` preset
22

3-
The preset pre-processes and evaluates the CSS. The webpack loader uses this preset under the hood. You also might want to use this preset if you import the components outside webpack, such as on your server or in unit tests.
3+
The preset pre-processes and evaluates the CSS. The webpack loader and Rollup plugin use this preset under the hood. You also might want to use this preset if you import the components outside webpack or Rollup, such as on your server or in unit tests.
44

55
To use this preset, add `linaria/babel` to your Babel configuration at the end of the presets list:
66

docs/BUNDLERS_INTEGRATION.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,25 @@ module.exports = {
7272
```
7373

7474
This will extract the CSS from all files into a single `styles.css`. Then you need to include this file in your HTML file.
75+
76+
## Rollup
77+
78+
To use linaria with Rollup you need to add it to your `rollup.config.js`, together with a plugin which handles CSS files, such as `rollup-plugin-css-only`.
79+
80+
```js
81+
import linaria from 'linaria/rollup';
82+
import css from 'rollup-plugin-css-only';
83+
84+
export default {
85+
/* rest of your config */
86+
plugins: [
87+
/* rest of your plugins */
88+
linaria({
89+
sourceMap: process.env.NODE_ENV !== 'production'
90+
}),
91+
css({
92+
output: 'styles.css'
93+
})
94+
]
95+
};
96+
```

docs/HOW_IT_WORKS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Linaria consists of 2 parts:
44

55
1. Babel plugin
6-
2. Webpack loader
6+
2. Webpack loader or Rollup plugin
77

88
## Babel plugin
99

@@ -161,3 +161,7 @@ But keep in mind that if you're doing SSR for your app, this won't work with SSR
161161
## Webpack loader
162162

163163
The webpack loader uses the Babel plugin internally and writes the CSS text to a CSS file, which can be picked up by `css-loader` to generate the final CSS. It's also responsible for generating the sourcemap from the metadata from the Babel plugin.
164+
165+
## Rollup plugin
166+
167+
The Rollup plugin also uses the Babel plugin, and adds the CSS text to Rollup, to be picked up by a CSS plugin, which can generate the final CSS. It also generates a sourcemap.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module "rollup-pluginutils" {
2+
declare export function createFilter(
3+
include?: string | string[],
4+
exclude?: string | string[]
5+
): Function;
6+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"mkdirp": "^0.5.1",
7676
"postcss": "^7.0.2",
7777
"react-is": "^16.5.1",
78+
"rollup-pluginutils": "^2.3.3",
7879
"source-map": "^0.7.3",
7980
"stylis": "^3.5.3"
8081
},

rollup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import/no-unresolved */
2+
3+
module.exports = require('./lib/rollup');

src/rollup.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* @flow */
2+
3+
const { createFilter } = require('rollup-pluginutils');
4+
const transform = require('./transform');
5+
const slugify = require('./slugify');
6+
7+
/* ::
8+
type RollupPluginOptions = {
9+
include?: string | string[],
10+
exclude?: string | string[],
11+
sourceMap?: boolean,
12+
evaluate?: boolean,
13+
displayName?: boolean,
14+
}
15+
*/
16+
17+
module.exports = function linaria({
18+
include,
19+
exclude,
20+
sourceMap,
21+
...rest
22+
} /* :RollupPluginOptions */ = {}) {
23+
const filter = createFilter(include, exclude);
24+
const cssLookup = {};
25+
26+
return {
27+
name: 'linaria',
28+
load(id /* :string */) {
29+
return cssLookup[id];
30+
},
31+
/* eslint-disable-next-line consistent-return */
32+
resolveId(importee /* :string */) {
33+
if (importee in cssLookup) return importee;
34+
},
35+
transform(code /* :string */, id /* :string */) {
36+
if (!filter(id)) return;
37+
38+
const result = transform(id, code, rest);
39+
40+
if (!result.cssText) return;
41+
42+
let { cssText } = result;
43+
44+
const slug = slugify(id);
45+
const filename = `${id.replace(/\.js$/, '')}_${slug}.css`;
46+
47+
if (sourceMap && result.cssSourceMapText) {
48+
const map = Buffer.from(result.cssSourceMapText).toString('base64');
49+
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
50+
}
51+
52+
cssLookup[filename] = cssText;
53+
54+
result.code += `\nimport ${JSON.stringify(filename)};\n`;
55+
56+
/* eslint-disable-next-line consistent-return */
57+
return result.code;
58+
},
59+
};
60+
};

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,6 +3217,11 @@ estraverse@^4.0.0, estraverse@^4.1.0, estraverse@^4.1.1, estraverse@^4.2.0:
32173217
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13"
32183218
integrity sha1-De4/7TH81GlhjOc0IJn8GvoL2xM=
32193219

3220+
estree-walker@^0.5.2:
3221+
version "0.5.2"
3222+
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.5.2.tgz#d3850be7529c9580d815600b53126515e146dd39"
3223+
integrity sha512-XpCnW/AE10ws/kDAs37cngSkvgIR8aN3G0MS85m7dUpuK2EREo9VJ00uvw6Dg/hXEpfsE1I1TvJOJr+Z+TL+ig==
3224+
32203225
esutils@^2.0.2:
32213226
version "2.0.2"
32223227
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
@@ -7031,6 +7036,14 @@ rimraf@^2.5.4, rimraf@^2.6.2:
70317036
dependencies:
70327037
glob "^7.0.5"
70337038

7039+
rollup-pluginutils@^2.3.3:
7040+
version "2.3.3"
7041+
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.3.3.tgz#3aad9b1eb3e7fe8262820818840bf091e5ae6794"
7042+
integrity sha512-2XZwja7b6P5q4RZ5FhyX1+f46xi1Z3qBKigLRZ6VTZjwbN0K1IFGMlwm06Uu0Emcre2Z63l77nq/pzn+KxIEoA==
7043+
dependencies:
7044+
estree-walker "^0.5.2"
7045+
micromatch "^2.3.11"
7046+
70347047
run-async@^2.2.0:
70357048
version "2.3.0"
70367049
resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0"

0 commit comments

Comments
 (0)