mirror of
https://github.com/danog/inline-critical.git
synced 2024-11-26 20:14:41 +01:00
Try fixing query string stylesheets
This commit is contained in:
parent
b12a0dad33
commit
972f0d2ace
11
index.js
11
index.js
@ -148,8 +148,10 @@ function inline(html, styles, options) {
|
|||||||
links.map((link) => {
|
links.map((link) => {
|
||||||
if (o.extract) {
|
if (o.extract) {
|
||||||
const href = link.getAttribute('href');
|
const href = link.getAttribute('href');
|
||||||
const file = path.resolve(path.join(o.basePath || process.cwd, href));
|
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)) {
|
if (fs.existsSync(file)) {
|
||||||
const orig = fs.readFileSync(file);
|
const orig = fs.readFileSync(file);
|
||||||
const diff = extractCss(orig, inlined, o.minify);
|
const diff = extractCss(orig, inlined, o.minify);
|
||||||
@ -157,7 +159,12 @@ function inline(html, styles, options) {
|
|||||||
|
|
||||||
fs.writeFileSync(filename, diff);
|
fs.writeFileSync(filename, diff);
|
||||||
link.setAttribute('href', normalizePath(reaver.rev(href, diff)));
|
link.setAttribute('href', normalizePath(reaver.rev(href, diff)));
|
||||||
} else if (!/\/\//.test(href)) {
|
// eslint-disable-next-line array-callback-return
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!/\/\//.test(href)) {
|
||||||
throw new Error(`Error: file "${href}" not found in "${o.basePath || process.cwd}". Specify base path.`);
|
throw new Error(`Error: file "${href}" not found in "${o.basePath || process.cwd}". Specify base path.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,14 @@ test('Inline css', async () => {
|
|||||||
expect(strip(out.toString())).toBe(strip(expected));
|
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 () => {
|
test('Inline css with media=print', async () => {
|
||||||
const html = await read('fixtures/index.html');
|
const html = await read('fixtures/index.html');
|
||||||
const css = await read('fixtures/critical.css');
|
const css = await read('fixtures/critical.css');
|
||||||
|
Loading…
Reference in New Issue
Block a user