Skip to content

Commit

Permalink
better detection for external CSS import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Dec 13, 2024
1 parent 93ef3db commit 1cab42f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/lib/resource-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async function trackResourcesForRoute(html, compilation, route) {
}

function isLocalLink(url = '') {
return url !== '' && (url.indexOf('http') !== 0 && url.indexOf('//') !== 0);
return url !== '' && !url.startsWith('http') && !url.startsWith('//');
}

// TODO handle full request

Check warning on line 176 in packages/cli/src/lib/resource-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO handle full request'

Check warning on line 176 in packages/cli/src/lib/resource-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO handle full request'

Check warning on line 176 in packages/cli/src/lib/resource-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO handle full request'

Check warning on line 176 in packages/cli/src/lib/resource-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO handle full request'
Expand Down Expand Up @@ -269,5 +269,6 @@ export {
requestAsObject,
resolveForRelativeUrl,
trackResourcesForRoute,
transformKoaRequestIntoStandardRequest
transformKoaRequestIntoStandardRequest,
isLocalLink
};
5 changes: 3 additions & 2 deletions packages/cli/src/plugins/resource/plugin-standard-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { parse, walk } from 'css-tree';
import { ResourceInterface } from '../../lib/resource-interface.js';
import { hashString } from '../../lib/hashing-utils.js';
import { getResolvedHrefFromPathnameShortcut } from '../../lib/node-modules-utils.js';
import { isLocalLink } from '../../lib/resource-utils.js';

function bundleCss(body, sourceUrl, compilation, workingUrl) {
const { projectDirectory, outputDir, userWorkspace } = compilation.context;
Expand All @@ -27,7 +28,7 @@ function bundleCss(body, sourceUrl, compilation, workingUrl) {
if ((type === 'String' || type === 'Url') && this.atrulePrelude && this.atrule.name === 'import') {
const { value } = node;

if (!value.startsWith('http')) {
if (isLocalLink(value)) {
if (value.indexOf('.') === 0 || value.indexOf('/node_modules') === 0) {
const resolvedUrl = value.startsWith('/node_modules')
? new URL(getResolvedHrefFromPathnameShortcut(value, projectDirectory))
Expand All @@ -48,7 +49,7 @@ function bundleCss(body, sourceUrl, compilation, workingUrl) {
optimizedCss += `@import url('${value}');`;
}
} else if (type === 'Url' && this.atrule?.name !== 'import') {
if (value.startsWith('http') || value.startsWith('//') || value.startsWith('data:')) {
if (!isLocalLink(value) || value.startsWith('data:')) {
optimizedCss += `url('${value}')`;
return;
}
Expand Down

0 comments on commit 1cab42f

Please sign in to comment.