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

bug/issue 1338 handle bundling of bare CSS @import specifiers #1342

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@shoelace-style/shoelace": "^2.18.0",
"@spectrum-css/card": "^9.3.0",
"@spectrum-web-components/action-menu": "^1.0.1",
"@spectrum-web-components/styles": "^1.0.1",
"@uswds/web-components": "^0.0.1-alpha",
"geist": "^1.2.0",
"lit": "^3.1.0",
Expand Down
24 changes: 17 additions & 7 deletions packages/cli/src/plugins/resource/plugin-standard-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { parse, walk } from 'css-tree';
import { ResourceInterface } from '../../lib/resource-interface.js';
import { hashString } from '../../lib/hashing-utils.js';

function bundleCss(body, url, compilation) {
function bundleCss(body, sourceUrl, compilation, workingUrl) {
const { projectDirectory, outputDir, userWorkspace } = compilation.context;
const ast = parse(body, {
onParseError(error) {
Expand All @@ -26,13 +26,23 @@ function bundleCss(body, url, compilation) {
if ((type === 'String' || type === 'Url') && this.atrulePrelude && this.atrule.name === 'import') {
const { value } = node;

if (value.indexOf('.') === 0 || value.indexOf('/node_modules') === 0) {
const resolvedUrl = value.startsWith('/node_modules')
? new URL(`.${value}`, projectDirectory)
: new URL(value, url);
const importContents = fs.readFileSync(resolvedUrl, 'utf-8');
if (!value.startsWith('http')) {
if (value.indexOf('.') === 0 || value.indexOf('/node_modules') === 0) {
const resolvedUrl = value.startsWith('/node_modules')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? new URL(`.${value}`, projectDirectory)
: new URL(value, sourceUrl);

optimizedCss += bundleCss(importContents, url, compilation);
const importContents = fs.readFileSync(resolvedUrl, 'utf-8');

optimizedCss += bundleCss(importContents, sourceUrl, compilation, resolvedUrl);
} else if (workingUrl) {
const resolvedUrl = new URL(`./${value}`, workingUrl);
const importContents = fs.readFileSync(resolvedUrl, 'utf-8');

optimizedCss += bundleCss(importContents, workingUrl, compilation);
} else {
console.warn(`Unable to resolve ${value} from file => ${sourceUrl}`);
}
} else {
optimizedCss += `@import url('${value}');`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
* index.html
* scripts/
* main.js
* styles/
* theme.css
*/
import chai from 'chai';
import fs from 'fs';
import glob from 'glob-promise';
import { JSDOM } from 'jsdom';
import path from 'path';
Expand Down Expand Up @@ -172,6 +175,10 @@ describe('Build Greenwood With: ', function() {
`${process.cwd()}/node_modules/prismjs/package.json`,
`${outputPath}/node_modules/prismjs/`
);
const spectrumStyles = await getDependencyFiles(
`${process.cwd()}/node_modules/@spectrum-web-components/styles/*.css`,
`${outputPath}/node_modules/@spectrum-web-components/styles/`
);

runner.setup(outputPath, [
...getSetupFiles(outputPath),
Expand Down Expand Up @@ -205,7 +212,8 @@ describe('Build Greenwood With: ', function() {
...prismCss,
...prismPackageJson,
...simpleCss,
...simpleCssPackageJson
...simpleCssPackageJson,
...spectrumStyles
]);
runner.runCommand(cliPath, 'build');

Expand Down Expand Up @@ -279,6 +287,25 @@ describe('Build Greenwood With: ', function() {
expect(await glob.promise(path.join(this.context.publicDir, 'prism-tomorrow.*.css'))).to.have.lengthOf(1);
});
});

describe('<link rel="stylesheet" href="..."> with reference to node_modules with bare @import paths in the <head> tag', function() {
it('should have one <link href="..."> tag in the <head> tag', function() {
const linkTags = dom.window.document.querySelectorAll('head > link[rel="stylesheet"]');
const themeLinkTag = Array.prototype.slice.call(linkTags).filter(link => {
return (/theme.*.css/).test(link.href);
});

expect(themeLinkTag.length).to.be.equal(1);
});

it('should have the expected theme.css file in the output directory with the expected content', async function() {
const themeFile = await glob.promise(path.join(this.context.publicDir, 'styles/theme.*.css'));
const contents = fs.readFileSync(themeFile[0], 'utf-8');

expect(themeFile).to.have.lengthOf(1);
expect(contents.indexOf(':root,:host{--spectrum-global-animation-linear:cubic-bezier(0, 0, 1, 1);')).to.equal(0);
});
});
});

after(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</script>
<script type="module" src="/node_modules/lit-html/lit-html.js"></script>
<link rel="stylesheet" href="/node_modules/prismjs/themes/prism-tomorrow.css"></link>
<link rel="stylesheet" href="../styles/theme.css"></link>
</head>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import url('../../node_modules/@spectrum-web-components/styles/all-large-dark.css');
1 change: 1 addition & 0 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"@evergreen-wc/eve-button": "^0.1.1",
"@evergreen-wc/eve-container": "^0.1.1",
"@spectrum-web-components/styles": "^1.0.1",
"lit": "^3.1.0",
"prismjs": "^1.21.0"
},
Expand Down
1 change: 1 addition & 0 deletions www/styles/theme.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import url('../../node_modules/@spectrum-web-components/styles/all-large-dark.css');
@import url('../assets/fonts/source-sans-pro.css');

* {
Expand Down
Loading