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

Try fixing query string stylesheets #274

Open
wants to merge 1 commit into
base: main
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
25 changes: 16 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,23 @@ function inline(html, styles, options) {
links.map((link) => {
if (o.extract) {
const href = link.getAttribute('href');
const file = path.resolve(path.join(o.basePath || process.cwd, href));

if (fs.existsSync(file)) {
const orig = fs.readFileSync(file);
const diff = extractCss(orig, inlined, o.minify);
const filename = reaver.rev(file, diff);
const fileOrig = path.resolve(path.join(o.basePath || process.cwd, href));
const files = [fileOrig, fileOrig.replace(/\?.*/, '')];

for (const file of files) {
if (fs.existsSync(file)) {
const orig = fs.readFileSync(file);
const diff = extractCss(orig, inlined, o.minify);
const filename = reaver.rev(file, diff);

fs.writeFileSync(filename, diff);
link.setAttribute('href', normalizePath(reaver.rev(href, diff)));
// eslint-disable-next-line array-callback-return
return;
}
}

fs.writeFileSync(filename, diff);
link.setAttribute('href', normalizePath(reaver.rev(href, diff)));
} else if (!/\/\//.test(href)) {
if (!/\/\//.test(href)) {
throw new Error(`Error: file "${href}" not found in "${o.basePath || process.cwd}". Specify base path.`);
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ test('Inline css', async () => {
expect(strip(out.toString())).toBe(strip(expected));
});

test('Inline css with query string link', async () => {
const html = await read('fixtures/index-query.html');
const css = await read('fixtures/critical.css');
const expected = await read('expected/index-inlined-async-query.html');
const out = inline(html, css, {minify: false, polyfill: true});
expect(strip(out.toString())).toBe(strip(expected));
});

test('Inline css with media=print', async () => {
const html = await read('fixtures/index.html');
const css = await read('fixtures/critical.css');
Expand Down