Skip to content

Commit

Permalink
fix(rollup): rollup preserveModules no js extension (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmzy committed Sep 3, 2021
1 parent bbf35a7 commit ca5232a
Showing 1 changed file with 14 additions and 1 deletion.
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

0 comments on commit ca5232a

Please sign in to comment.