Skip to content

Commit 3023317

Browse files
committed
Add docs for external resolvers
1 parent 7f8a861 commit 3023317

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

website/pages/bundling.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ body { background: green }
157157

158158
The `bundleAsync` API is an asynchronous version of `bundle`, which also accepts a custom `resolver` object. This allows you to provide custom JavaScript functions for resolving `@import` specifiers to file paths, and reading files from the file system (or another source). The `read` and `resolve` functions are both optional, and may either return a string synchronously, or a Promise for asynchronous resolution.
159159

160+
`resolve` may also return a `{external: string}` object to mark an `@import` as external. This will preserve the `@import` in the output instead of bundling it. The string provided to the `external` property represents the target URL to import, which may be the original specifier or a different value.
161+
162+
Note that using a custom resolver can slow down bundling significantly, especially when reading files asynchronously. Use `readFileSync` rather than `readFile` if possible for better performance, or omit either of the methods if you don't need to override the default behavior.
163+
160164
```js
161165
import { bundleAsync } from 'lightningcss';
162166

@@ -168,10 +172,27 @@ let { code, map } = await bundleAsync({
168172
return fs.readFileSync(filePath, 'utf8');
169173
},
170174
resolve(specifier, from) {
175+
if (/^https?:/.test(specifier)) {
176+
return {external: specifier};
177+
}
171178
return path.resolve(path.dirname(from), specifier);
172179
}
173180
}
174181
});
175182
```
176183

177-
Note that using a custom resolver can slow down bundling significantly, especially when reading files asynchronously. Use `readFileSync` rather than `readFile` if possible for better performance, or omit either of the methods if you don't need to override the default behavior.
184+
<div class="warning">
185+
186+
**Note:** External imports must be placed before all bundled imports in the source code. CSS does not support interleaving `@import` rules with other rules, so this is required to preserve the behavior of the source code.
187+
188+
```css
189+
@import "bundled.css";
190+
@import "https://example.com/external.css"; /**/
191+
```
192+
193+
```css
194+
@import "https://example.com/external.css"; /**/
195+
@import "bundled.css";
196+
```
197+
198+
</div>

0 commit comments

Comments
 (0)