diff --git a/index.js b/index.js index 7465be9..5459231 100644 --- a/index.js +++ b/index.js @@ -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)); + const fileOrig = path.resolve(path.join(o.basePath || process.cwd, href)); + const files = [fileOrig, fileOrig.replace(/\?.*/, '')]; - if (fs.existsSync(file)) { - const orig = fs.readFileSync(file); - const diff = extractCss(orig, inlined, o.minify); - const filename = reaver.rev(file, diff); + 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))); - } else if (!/\/\//.test(href)) { + fs.writeFileSync(filename, diff); + link.setAttribute('href', normalizePath(reaver.rev(href, diff))); + // 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.`); } } diff --git a/test/index.test.js b/test/index.test.js index 8628ca7..31519d5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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');