summaryrefslogtreecommitdiff
path: root/@linaria/packages/rollup/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to '@linaria/packages/rollup/src/index.ts')
-rw-r--r--@linaria/packages/rollup/src/index.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/@linaria/packages/rollup/src/index.ts b/@linaria/packages/rollup/src/index.ts
new file mode 100644
index 0000000..64ba9fc
--- /dev/null
+++ b/@linaria/packages/rollup/src/index.ts
@@ -0,0 +1,70 @@
+/**
+ * This file contains a Rollup loader for Linaria.
+ * It uses the transform.ts function to generate class names from source code,
+ * returns transformed code without template literals and attaches generated source maps
+ */
+
+import { createFilter } from '@rollup/pluginutils';
+import { transform, slugify, Result } from '@linaria/babel-preset';
+import type { PluginOptions, Preprocessor } from '@linaria/babel-preset';
+
+type RollupPluginOptions = {
+ include?: string | string[];
+ exclude?: string | string[];
+ sourceMap?: boolean;
+ preprocessor?: Preprocessor;
+} & Partial<PluginOptions>;
+
+export default function linaria({
+ include,
+ exclude,
+ sourceMap,
+ preprocessor,
+ ...rest
+}: RollupPluginOptions = {}) {
+ const filter = createFilter(include, exclude);
+ const cssLookup: { [key: string]: string } = {};
+
+ return {
+ name: 'linaria',
+ load(id: string) {
+ return cssLookup[id];
+ },
+ /* eslint-disable-next-line consistent-return */
+ resolveId(importee: string) {
+ if (importee in cssLookup) return importee;
+ },
+ transform(
+ code: string,
+ id: string
+ ): { code: string; map: Result['sourceMap'] } | undefined {
+ // Do not transform ignored and generated files
+ if (!filter(id) || id in cssLookup) return;
+
+ const result = transform(code, {
+ filename: id,
+ preprocessor,
+ pluginOptions: rest,
+ });
+
+ if (!result.cssText) return;
+
+ let { cssText } = result;
+
+ const slug = slugify(cssText);
+ const filename = `@linaria:${id.replace(/\.js$/, '')}_${slug}.css`;
+
+ if (sourceMap && result.cssSourceMapText) {
+ const map = Buffer.from(result.cssSourceMapText).toString('base64');
+ cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
+ }
+
+ cssLookup[filename] = cssText;
+
+ result.code += `\nimport ${JSON.stringify(filename)};\n`;
+
+ /* eslint-disable-next-line consistent-return */
+ return { code: result.code, map: result.sourceMap };
+ },
+ };
+}