Skip to content

Commit

Permalink
Remove resolveAsset option from inline transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Dec 12, 2022
1 parent 2f98065 commit d910619
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-dodos-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ultrahtml": minor
---

Remove `resolveAsset` option from `inline` transformer, making it synchronous again.
19 changes: 2 additions & 17 deletions src/transformers/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,14 @@ import { walkSync, ELEMENT_NODE, TEXT_NODE, Node, ElementNode } from "../index.j
import { querySelectorAll, specificity } from "../selector.js";
import { compile } from "stylis";

export interface InlineOptions {
resolveAsset: (node: ElementNode) => void|string|Promise<string|void>;
}
export interface InlineOptions {}
export default function inline(opts?: InlineOptions) {
return async (doc: Node): Promise<Node> => {
return (doc: Node): Promise<Node> => {
const style: string[] = [];
const actions: (() => void)[] = [];
const promises: Promise<void>[] = [];
walkSync(doc, (node: Node, parent?: Node) => {
if (node.type === ELEMENT_NODE) {
if (typeof opts?.resolveAsset === 'function' && node.name === 'link' && node.attributes.rel === 'stylesheet') {
// ensure order is maintained
const i = style.push('');
promises.push(Promise.resolve(opts.resolveAsset(node)).then(css => {
if (css) {
style[i] = css;
actions.push(() => {
parent!.children = parent!.children.filter((c: Node) => c !== node);
});
}
}))
}
if (node.name === "style") {
style.push(
node.children
Expand All @@ -36,7 +22,6 @@ export default function inline(opts?: InlineOptions) {
}
}
});
await Promise.all(promises);
for (const action of actions) {
action();
}
Expand Down
11 changes: 11 additions & 0 deletions test/transformers/inline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ describe("inline", () => {
const output = await transform(input, [inline()]);
expect(output).toEqual(`<div style="color:blue;">Hello world</div>`);
});

it("inline styles with class", async () => {
const input = `<div class="cool">Hello world</div>
<style>
.cool {
color: red;
}
</style>`;
const output = await transform(input, [inline()]).trim();
expect(output).toEqual(`<div class="cool" style="color:red;">Hello world</div>`);
});
});

describe("inline jsx", () => {
Expand Down

0 comments on commit d910619

Please sign in to comment.