Mark import as external#880
Conversation
node/test/bundle.test.mjs
Outdated
| resolver: { | ||
| resolve(specifier, originatingFile) { | ||
| if (specifier === './does_not_exist.css' || specifier.startsWith('https:')) { | ||
| return true; |
There was a problem hiding this comment.
For now, I made resolve to accept string | true to focus on the rough design. I think string | { specifier: string, external?: boolean } would be nice here. What do you think?
There was a problem hiding this comment.
yeah or maybe just string | {external: string}?
| @import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap'); | ||
| @import './does_not_exist.css'; | ||
| @import './b.css'; |
There was a problem hiding this comment.
This case can be bundled without any semantics change, but for the following case, it's difficult to keep the semantics.
@import './b.css'; // bundled
@import "https://fonts.googleapis.com/css2?family=Roboto&display=swap"; // externalizedrelated: postcss/postcss-import#536, parcel-bundler/parcel#5840 (comment), evanw/esbuild#465 (comment)
Maybe it's fine to simply change the semantics in this case? (both postcss-import and esbuild seems to do that)
| /// Resolves the given import specifier to a file path given the file | ||
| /// which the import originated from. | ||
| fn resolve(&self, specifier: &str, originating_file: &Path) -> Result<PathBuf, Self::Error>; | ||
| fn resolve(&self, specifier: &str, originating_file: &Path) -> Result<ResolveResult, Self::Error>; |
There was a problem hiding this comment.
I'm not familiar with the rust libraries' semver compat, but I guess changing the return value of a public trait is a breaking change. Is it fine to introduce a breaking change? If not, I'll try to add resolve_advanced method with a default implementation so that it won't be a breaking change.
(On the JS side, it's not a breaking change)
There was a problem hiding this comment.
it's ok, the crate is still under pre-release
|
@devongovett Would you take this one a look? This is blocking Vite stabilize the lightningcss support. |
…ngcss into sapphi-red-feat/resolve-external
|
As you mentioned, one issue is that it will change the behavior of the CSS if external and internal imports are mixed. @import "other.css";
@import "http://example.com/external.css";
@import "another.css";If |
|
I think it's too restrictive to error on it. I assume it won't matter in most cases. But I think it'd be nice to have a warning. |
…sapphi-red-feat/resolve-external
|
Currently the bundler produces invalid css when externals are after bundled imports: However when layers are involved this gets complicated. @layer a;
@import 'a.css';
@layer b;
@import 'external.css';Here the user has declared layers a and b before @layer a;
@layer b;
@import 'external.css';
/* a.css */But what if It would be much simpler to emit an error if externals are found after bundled imports. I am tempted to try this and see what issues people run into. We could potentially try implementing that later (or even a simpler approach where all externals are hoisted to the top regardless of layers). But perhaps that should be behind an unsafe flag or something. What do you think? |
|
Starting with that sounds good to me. Maybe we can tell users to add /* # input */
/* ## index.css */
@import 'a.css'
@import 'external1.css' layer(b);
@import 'c.css'
/* ## a.css */
@layer a;
.a {}
/* ## c.css */
@layer c;
.c {}
/* ---------------- */
/* # output */
@layer a, b;
@import 'external1.css' layer(b);
.a {}
@layer c;
.c {}Just in case, this example:
is not a valid code as /* ## index.css */
@import 'a.css'
@import 'b.css'
/* ## a.css */
@layer a;
@import 'external1.css';
/* ## b.css */
@layer b;
@import 'external2.css';which requires layer "b" to come after |
|
Oh right, I forgot about the consecutive requirement. That makes it slightly simpler. And good idea about the layers. Unfortunately that affects the behavior with unlayered styles too (e.g. inline styles on the page that we can't see) since unlayered styles always take precedence over layered styles. I'd still like to start with an error and see how common of an issue it is. It has a relatively simple fix (moving the external css above the bundled styles), but if that's common in libraries that users can't edit then perhaps we need to add an option. |
|
Thank you for getting this over the line! |
This PR adds a way to mark an
importas external withresolver.closes #479
closes #555