Skip to content

Commit

Permalink
fix: πŸ› Fixes importing of CSS from external named imports
Browse files Browse the repository at this point in the history
βœ… Closes: 86
  • Loading branch information
Nick Reese committed Nov 3, 2020
1 parent da7c346 commit a18a6ba
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elderjs/elderjs",
"version": "1.1.7",
"version": "1.1.8",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"engineStrict": true,
Expand Down
68 changes: 68 additions & 0 deletions src/utils/__tests__/rollupPluginHandleCss.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const cssClock = `svg.svelte-74ebks{width:100%;height:100%}.clock-face.svelte-74
const cssHookDetail = `.list.svelte-obuov5.svelte-obuov5{display:block;margin-bottom:0.5rem;font-size:13px}.list.svelte-obuov5 .code.svelte-obuov5{cursor:help}.hook.svelte-obuov5.svelte-obuov5{max-width:100%;text-overflow:wrap;padding:1rem;border:1px solid #ddd;border-collapse:collapse;margin-bottom:1rem;border-radius:1rem;position:relative;background:white}.hook-number.svelte-obuov5.svelte-obuov5{position:absolute;top:0;right:0px;width:2rem;height:1.75rem;border-top-right-radius:1rem;border-bottom-left-radius:1rem;text-align:center;padding-top:3px;background:#ddd;font-size:14px}.overview.svelte-obuov5.svelte-obuov5{margin-right:1rem}@media(min-width: 768px){.hook.svelte-obuov5.svelte-obuov5:nth-child(even){margin-left:0.5rem}.hook.svelte-obuov5.svelte-obuov5:nth-child(odd){margin-right:0.5rem}}.use.svelte-obuov5.svelte-obuov5{font-size:14px}.use ul{padding-left:1rem}.overview.svelte-obuov5.svelte-obuov5{margin-bottom:0.75rem;padding-bottom:0.75rem;border-bottom:1px solid #ddd}
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiSG9va0RldGFpbC5zdmVsdGUiLCJzb3VyY2VzIjpb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LENBQUMsQUFBQyxDQUFDLEFBQ3BCLFlBQVksQ0FBRSxNQUFNLEFBQ3RCLENBQUMsQUFDSCxDQUFDLEFBRUQsSUFBSSw0QkFBQyxDQUFDLEFBQ0osU0FBUyxDQUFFLElBQUksQUFDakIsQ0FBQyxBQUNPLE9BQU8sQUFBRSxDQUFDLEFBQ2hCLFlBQVksQ0FBRSxJQUFJLEFBQ3BCLENBQUMsQUFFRCxTQUFTLDRCQUFDLENBQUMsQUFDVCxhQUFhLENBQUUsT0FBTyxDQUN0QixjQUFjLENBQUUsT0FBTyxDQUN2QixhQUFhLENBQUUsR0FBRyxDQUFDLEtBQUssQ0FBQyxJQUFJLEFBQy9CLENBQUMifQ== */`;

const externalNamedImport = `.button.svelte-zlh48j{padding:10px 20px;background-color:#f50;color:#fff;font-weight:bold}
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQnV0dG9uLnN2ZWx0ZSIsInNvdXJjZXMiOlsiQnV0dG9uLnN2ZWx0ZSJdLCJzb3VyY2VzQ29udGVudCI6WyI8c3R5bGU+XG4gICAgLmJ1dHRvbiB7XG4gICAgICAgIHBhZGRpbmc6IDEwcHggMjBweDtcbiAgICAgICAgYmFja2dyb3VuZC1jb2xvcjogI2Y1MDtcbiAgICAgICAgY29sb3I6ICNmZmY7XG4gICAgICAgIGZvbnQtd2VpZ2h0OiBib2xkO1xuICAgIH1cbi8qIyBzb3VyY2VNYXBwaW5nVVJMPWRhdGE6YXBwbGljYXRpb24vanNvbjtiYXNlNjQsZXlKMlpYSnphVzl1SWpvekxDSnpiM1Z5WTJWeklqcGJJbTV2WkdWZmJXOWtkV3hsY3k5MFpYTjBMV1Y0ZEdWeWJtRnNMWE4yWld4MFpTMXNhV0p5WVhKNUwzTnlZeTlqYjIxd2IyNWxiblJ6TDI1dlpHVmZiVzlrZFd4bGN5OTBaWE4wTFdWNGRHVnlibUZzTFhOMlpXeDBaUzFzYVdKeVlYSjVMM055WXk5amIyMXdiMjVsYm5SekwwSjFkSFJ2Ymk1emRtVnNkR1VpWFN3aWJtRnRaWE1pT2x0ZExDSnRZWEJ3YVc1bmN5STZJanRKUVVOSk8xRkJRMGtzYTBKQlFXdENPMUZCUTJ4Q0xITkNRVUZ6UWp0UlFVTjBRaXhYUVVGWE8xRkJRMWdzYVVKQlFXbENPMGxCUTNKQ0lpd2labWxzWlNJNkltNXZaR1ZmYlc5a2RXeGxjeTkwWlhOMExXVjRkR1Z5Ym1Gc0xYTjJaV3gwWlMxc2FXSnlZWEo1TDNOeVl5OWpiMjF3YjI1bGJuUnpMMEoxZEhSdmJpNXpkbVZzZEdVaUxDSnpiM1Z5WTJWelEyOXVkR1Z1ZENJNld5SmNiaUFnSUNBdVluVjBkRzl1SUh0Y2JpQWdJQ0FnSUNBZ2NHRmtaR2x1WnpvZ01UQndlQ0F5TUhCNE8xeHVJQ0FnSUNBZ0lDQmlZV05yWjNKdmRXNWtMV052Ykc5eU9pQWpaalV3TzF4dUlDQWdJQ0FnSUNCamIyeHZjam9nSTJabVpqdGNiaUFnSUNBZ0lDQWdabTl1ZEMxM1pXbG5hSFE2SUdKdmJHUTdYRzRnSUNBZ2ZWeHVJbDE5ICovPC9zdHlsZT5cblxuPGJ1dHRvbiBjbGFzcz1cImJ1dHRvblwiPjxzbG90IC8+PC9idXR0b24+Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNJLE9BQU8sY0FBQyxDQUFDLEFBQ0wsT0FBTyxDQUFFLElBQUksQ0FBQyxJQUFJLENBQ2xCLGdCQUFnQixDQUFFLElBQUksQ0FDdEIsS0FBSyxDQUFFLElBQUksQ0FDWCxXQUFXLENBQUUsSUFBSSxBQUNyQixDQUFDIn0= */`;

const simpleSvelte = {
exports: ['default'],
facadeModuleId: '/Users/nick/repos/elderjs/template/src/routes/simple/Simple.svelte',
Expand Down Expand Up @@ -82,6 +85,50 @@ const homeSvelte = {
referencedFiles: [],
};

const externalNamedImportHome = {
exports: ['default'],
facadeModuleId: '/Users/nick/repos/elderjs/template/src/routes/home/Home.svelte',
isDynamicEntry: false,
isEntry: true,
isImplicitEntry: false,
modules: {
'/Users/nick/repos/elderjs/template/src/routes/home/Home.css': {
originalLength: 13105,
removedExports: [],
renderedExports: [],
renderedLength: 0,
},
'/Users/nick/repos/elderjs/template/src/routes/home/Home.svelte': {
originalLength: 5811,
removedExports: [],
renderedExports: [],
renderedLength: 15947,
},
},
name: [],
type: 'chunk',
code: undefined,
dynamicImports: [],
fileName: 'routes/Home.js',
implicitlyLoadedBefore: [],
importedBindings: {
'index-f52204f9.js': ['create_ssr_component', 'escape', 'each', 'validate_component', 'add_attribute'],
'components/HookDetail.js': ['default'],
'components/BlogTeaser.js': ['default'],
'components/Clock.js': [],
'index-585c9439.js': ['Button'],
},
imports: [
'index-f52204f9.js',
'components/HookDetail.js',
'components/BlogTeaser.js',
'components/Clock.js',
'index-585c9439.js',
],
map: undefined,
referencedFiles: [],
};

beforeEach(() => {
jest.resetModules();
});
Expand Down Expand Up @@ -146,5 +193,26 @@ describe('#rollupPluginHandleCss', () => {
);
expect(output).toContain(`module.exports._cssMap = "\\u002F*# sourceMappingURL=data:application\\u002Fjson;`);
});

it('Finds named export of "Button" by looking for named Imports and checking if css emitted ends with that name {"index-585c9439.js": ["Button"]} finds plugins/components/Button.css ', () => {
const { transform, renderChunk } = rollupPluginHandleCss({ rootDir: '/Users/nick/repos/elderjs/template' });
transform(cssHookDetail, '/Users/nick/repos/elderjs/template/src/components/HookDetail.css');
transform(cssClock, '/Users/nick/repos/elderjs/template/src/components/Clock.css');
transform(cssHome, '/Users/nick/repos/elderjs/template/src/routes/home/Home.css');
transform(
externalNamedImport,
'/Users/nick/repos/elderjs/template/node_modules/test-external-svelte-library/src/components/Button.css',
);
const output = renderChunk('', externalNamedImportHome);
expect(output).toContain('button.svelte-zlh48j');
expect(output).toContain(
`module.exports._css = ".banner.svelte-au0a7c{padding:1rem 2rem;background:#eee;border-radius:2rem;margin-bottom:1rem}.entries.svelte-au0a7c{display:grid;grid-template-columns:1fr;margin:3rem 0}@media(min-width: 768px){.entries.svelte-au0a7c{display:grid;grid-template-columns:1fr 1fr 1fr;margin:3rem 0}.entries .entry{margin-right:1rem}}.entry{padding:1rem;border:1px solid #ddd;border-radius:1rem;margin-bottom:1rem;background:white}.about.svelte-au0a7c{margin-bottom:2rem}@media(min-width: 768px){.hydrate.svelte-au0a7c{display:grid;grid-template-columns:80% 20%}}.hooks.svelte-au0a7c{display:grid;grid-template-columns:100%}`,
);
// eslint-disable-next-line no-useless-escape
expect(output).toContain(
`module.exports._cssIncluded = ["routes/Home.css","components/HookDetail.css","components/Clock.css","plugins/components/Button.css"]`,
);
expect(output).toContain(`module.exports._cssMap = "\\u002F*# sourceMappingURL=data:application\\u002Fjson;`);
});
});
});
29 changes: 29 additions & 0 deletions src/utils/rollupPluginHandleCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ export default function elderjsHandleCss({ rootDir }) {

chunk.imports.forEach((i) => requiredCss.add(i.replace('.js', '.css')));

// check the cssMap to see if each of the requiredCss exists, if not, then look at importBindings

[...requiredCss.keys()].forEach((key) => {
if (!cssMap.has(key)) {
const arrayOfNamedImports = Object.keys(chunk.importedBindings)
.reduce((out: string[], cv: string): string[] => {
if (Array.isArray(chunk.importedBindings[cv])) {
return [...out, ...chunk.importedBindings[cv]];
}
return out;
}, [])
.filter((v) => v !== 'default');
const cssFileNames = [...cssMap.keys()];
const importNameMatch: string | false = arrayOfNamedImports.reduce((out, namedImport) => {
if (!out) {
out = cssFileNames.find(
(cssFile) => typeof cssFile === 'string' && cssFile.endsWith(`${namedImport}.css`),
);
}
return out;
}, false);

if (importNameMatch) {
requiredCss.delete(key);
requiredCss.add(importNameMatch);
}
}
});

const { cssChunks, matches } = [...requiredCss].reduce(
(out, key) => {
if (cssMap.has(key)) {
Expand Down

0 comments on commit a18a6ba

Please sign in to comment.