Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rollup preserveModules no js extension #822

Merged
merged 1 commit into from Sep 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/rollup/src/index.ts
Expand Up @@ -4,6 +4,7 @@
* returns transformed code without template literals and attaches generated source maps
*/

import path from 'path';
import { createFilter } from '@rollup/pluginutils';
import { transform, slugify, Result } from '@linaria/babel-preset';
import type { PluginOptions, Preprocessor } from '@linaria/babel-preset';
Expand All @@ -15,6 +16,11 @@ type RollupPluginOptions = {
preprocessor?: Preprocessor;
} & Partial<PluginOptions>;

type ViteConfig = {
root: string;
command: 'serve' | 'build';
};

export default function linaria({
include,
exclude,
Expand All @@ -24,9 +30,13 @@ export default function linaria({
}: RollupPluginOptions = {}) {
const filter = createFilter(include, exclude);
const cssLookup: { [key: string]: string } = {};
let config: ViteConfig;

return {
name: 'linaria',
configResolved(resolvedConfig: ViteConfig) {
config = resolvedConfig;
},
load(id: string) {
return cssLookup[id];
},
Expand All @@ -52,14 +62,17 @@ export default function linaria({
let { cssText } = result;

const slug = slugify(cssText);
const filename = `@linaria:${id.replace(/\.js$/, '')}_${slug}.css`;
const filename = `${id.replace(/\.[jt]sx?$/, '')}_${slug}.css`;

if (sourceMap && result.cssSourceMapText) {
const map = Buffer.from(result.cssSourceMapText).toString('base64');
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
}

cssLookup[filename] = cssText;
if (config?.command === 'serve' && config?.root) {
cssLookup['/' + path.posix.relative(config.root, filename)] = cssText;
}

result.code += `\nimport ${JSON.stringify(filename)};\n`;

Expand Down