Skip to content

Commit

Permalink
fix: test and warning for already processed files
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Apr 6, 2020
1 parent 9cc8c88 commit 19fd942
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 13 deletions.
72 changes: 72 additions & 0 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,77 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`already-processed: js 1`] = `
"/* eslint-env browser */
/** @type {HTMLElement[]} */
const containers = [];
/** @type {{prepend:HTMLStyleElement,append:HTMLStyleElement}[]} */
const styleTags = [];
/**
* @param {string|undefined} css
* @param {object} [options={}]
* @param {boolean} [options.prepend]
* @param {boolean} [options.singleTag]
* @param {HTMLElement} [options.container]
* @returns {void}
*/
var injector_14187a73 = (css, options = {}) => {
if (!css || typeof document === \\"undefined\\") return;
const singleTag = typeof options.singleTag !== \\"undefined\\" ? options.singleTag : false;
const container = typeof options.container !== \\"undefined\\" ? options.container : document.head;
const position = options.prepend === true ? \\"prepend\\" : \\"append\\";
const createStyleTag = () => {
const styleTag = document.createElement(\\"style\\");
styleTag.type = \\"text/css\\";
if (position === \\"prepend\\" && container.firstChild) {
container.insertBefore(styleTag, container.firstChild);
} else {
container.append(styleTag);
}
return styleTag;
};
/** @type {HTMLStyleElement} */
let styleTag;
if (singleTag) {
let id = containers.indexOf(container);
if (id === -1) {
id = containers.push(container) - 1;
styleTags[id] = {};
}
if (styleTags[id] && styleTags[id][position]) {
styleTag = styleTags[id][position];
} else {
styleTag = styleTags[id][position] = createStyleTag();
}
} else {
styleTag = createStyleTag();
}
// strip potential UTF-8 BOM if css was read from a file
if (css.charCodeAt(0) === 0xfeff) css = css.slice(1);
if (styleTag.styleSheet) {
styleTag.styleSheet.cssText += css;
} else {
styleTag.textContent += css;
}
};
var css_2f84417a = \\".foo {\\\\n color: red;\\\\n}\\\\n\\";
const stylesheet = css_2f84417a;
injector_14187a73(css_2f84417a);
export default css_2f84417a;
export { stylesheet };
"
`;

exports[`basic postcss-config: js 1`] = `
"'use strict';
Expand Down
12 changes: 6 additions & 6 deletions __tests__/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ export function validate({ title, input, outDir, options = {} }: Test): void {
throw error;
}

expect(await res.js()).toMatchSnapshot("js");
await expect(res.js()).resolves.toMatchSnapshot("js");

if (options.extract) {
expect(await res.isCss()).toBeTruthy();
expect(await res.css()).toMatchSnapshot("css");
await expect(res.isCss()).resolves.toBeTruthy();
await expect(res.css()).resolves.toMatchSnapshot("css");
}

const sourceMap = options && options.sourceMap;
if (sourceMap === "inline") {
expect(await res.isMap()).toBeFalsy();
await expect(res.isMap()).resolves.toBeFalsy();
} else if (sourceMap === true) {
expect(await res.isMap()).toBe(Boolean(options.extract));
if (options.extract) expect(await res.map()).toMatchSnapshot("map");
await expect(res.isMap()).resolves.toBe(Boolean(options.extract));
if (options.extract) await expect(res.map()).resolves.toMatchSnapshot("map");
}
},
30000,
Expand Down
20 changes: 17 additions & 3 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ test("on-extract-fn", async () => {
},
},
});
expect(await res.js()).toMatchSnapshot();
expect(await res.isCss()).toBeFalsy();
expect(await res.isMap()).toBeFalsy();
await expect(res.js()).resolves.toMatchSnapshot();
await expect(res.isCss()).resolves.toBeFalsy();
await expect(res.isMap()).resolves.toBeFalsy();
});

test("augment-chunk-hash", async () => {
Expand Down Expand Up @@ -312,6 +312,20 @@ test("augment-chunk-hash", async () => {
expect(barHash).not.toEqual(fooTwoHash);
});

test("already-processed", async () => {
const bundle = await rollup({
input: fixture("simple/foo.css"),
plugins: [styles(), styles()],
});

const outDir = fixture("dist", "already-processed");
const { output } = await bundle.write({ dir: outDir });
const outfile = path.join(outDir, output[0].fileName);

await expect(fs.pathExists(outfile)).resolves.toBeTruthy();
await expect(fs.readFile(outfile, "utf8")).resolves.toMatchSnapshot("js");
});

test("multiple-instances", async () => {
const bundle = await rollup({
input: fixture("multiple-instances/index.js"),
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import cssnano from "cssnano";

import { Options, PostCSSLoaderOptions, LoaderContext, Payload, ExtractedData } from "./types";
import Loaders from "./loaders";
import { relativePath } from "./utils/path-utils";
import { relativePath, humanlizePath } from "./utils/path-utils";
import { MapModifier } from "./utils/sourcemap-utils";
import {
inferOption,
Expand Down Expand Up @@ -63,8 +63,9 @@ export default (options: Options = {}): Plugin => {
// Check if file was already processed into JS
// by other instance(s) of this or other plugin(s)
try {
this.parse(code, {});
return null; // Was already processed, skipping
this.parse(code, undefined); // If it doesn't throw...
this.warn(`Skipping processed file ${humanlizePath(id)}`);
return null;
} catch {
// Was not already processed, continuing
}
Expand Down
2 changes: 1 addition & 1 deletion src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class Loaders {
throw new TypeError("The rule in `use` option must be string or array!");
});

this.test = (filepath: string): boolean =>
this.test = (filepath): boolean =>
options.extensions.some(ext => filepath.toLowerCase().endsWith(ext));

this.listLoader(postcssLoader);
Expand Down

0 comments on commit 19fd942

Please sign in to comment.