Skip to content

Commit

Permalink
fix: better lf placement for output
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Apr 6, 2020
1 parent 08e4bae commit 663c184
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 182 deletions.
158 changes: 82 additions & 76 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,77 +1,5 @@
// 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 Expand Up @@ -1458,8 +1386,86 @@ console.log(style$1);
"
`;

exports[`multiple-instances: js 1`] = `
"/* eslint-env browser */
exports[`multiple-instances already-processed: js 1`] = `
"'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/* 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 = \\".less {\\\\n color: magenta;\\\\n}\\\\n\\";
const stylesheet = css_2f84417a;
injector_14187a73(css_2f84417a);
exports.default = css_2f84417a;
exports.stylesheet = stylesheet;
"
`;

exports[`multiple-instances default: js 1`] = `
"'use strict';
/* eslint-env browser */
/** @type {HTMLElement[]} */
const containers = [];
Expand Down Expand Up @@ -1527,8 +1533,8 @@ injector_14187a73(css_2f84417a);
var css_2f84417a$1 = \\".less {\\\\n color: magenta;\\\\n}\\\\n\\";
injector_14187a73(css_2f84417a$1);
var css_2f84417a$2 = \\".mod_modular {\\\\n color: royalblue;\\\\n}\\\\n\\";
var mod = {\\"modular\\":\\"mod_modular\\",\\"modularalt\\":\\"mod_modular\\"};
var css_2f84417a$2 = \\".mod_mod {\\\\n color: royalblue;\\\\n}\\\\n\\";
var mod = {\\"mod\\":\\"mod_mod\\"};
injector_14187a73(css_2f84417a$2);
console.log(mod);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/multiple-instances/mod.mcss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.modular {
.mod {
color: royalblue;
}
105 changes: 48 additions & 57 deletions __tests__/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable jest/no-export */
import path from "path";
import fs from "fs-extra";
import { rollup } from "rollup";
import commonjs from "@rollup/plugin-commonjs";
import { rollup, Plugin } from "rollup";

import styles from "../../src";
import { Options } from "../../src/types";
Expand All @@ -11,13 +10,10 @@ export interface WriteData {
input: string;
outDir?: string;
options?: Options;
plugins?: Plugin[];
}

export interface Test extends WriteData {
title: string;
}

export interface TestData {
export interface WriteResult {
js: () => Promise<string>;
css: () => Promise<string>;
map: () => Promise<string>;
Expand All @@ -29,77 +25,72 @@ export interface TestData {
export const fixture = (...args: string[]): string =>
path.normalize(path.join(__dirname, "..", "fixtures", ...args));

export async function write(data: WriteData): Promise<TestData> {
export async function write(data: WriteData): Promise<WriteResult> {
const outDir = fixture("dist", data.outDir || "");

const bundle = await rollup({
input: fixture(data.input),
plugins: [commonjs(), styles(data.options)],
});

await bundle.write({
format: "cjs",
file: path.join(outDir, "bundle.js"),
});

const js = path.join(outDir, "bundle.js");
const css =
data.options && typeof data.options.extract === "string"
? data.options.extract
: path.join(outDir, "bundle.css");
const map = `${css}.map`;

const resData: TestData = {
const bundle = await rollup({
input: fixture(data.input),
plugins: data.plugins || [styles(data.options)],
});

await bundle.write({ format: "cjs", file: js });

const res: WriteResult = {
js: () => fs.readFile(js, "utf8"),
css: () => fs.readFile(css, "utf8"),
map: () => fs.readFile(map, "utf8"),
isCss: () => fs.pathExists(css),
isMap: () => fs.pathExists(map),
isFile: (file: string) => fs.pathExists(path.join(outDir, file)),
isFile: file => fs.pathExists(path.join(outDir, file)),
};

return resData;
return res;
}

export interface TestData extends WriteData {
title: string;
}

export function validate({ title, input, outDir, options = {} }: Test): void {
test(
title,
async () => {
let res;
try {
res = await write({ input, outDir, options });
} catch (error) {
const frame = error.codeFrame || error.snippet;
if (frame) throw new Error(`${frame} ${error.message}`);
throw error;
}

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

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

const sourceMap = options && options.sourceMap;
if (sourceMap === "inline") {
await expect(res.isMap()).resolves.toBeFalsy();
} else if (sourceMap === true) {
await expect(res.isMap()).resolves.toBe(Boolean(options.extract));
if (options.extract) await expect(res.map()).resolves.toMatchSnapshot("map");
}
},
30000,
);
export function validate(data: TestData): void {
const options = data.options || {};
test(data.title, async () => {
let res;
try {
res = await write(data);
} catch (error) {
const frame = error.codeFrame || error.snippet;
if (frame) throw new Error(`${frame} ${error.message}`);
throw error;
}

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

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

const sourceMap = options && options.sourceMap;
if (sourceMap === "inline") {
await expect(res.isMap()).resolves.toBeFalsy();
} else if (sourceMap === true) {
await expect(res.isMap()).resolves.toBe(Boolean(options.extract));
if (options.extract) await expect(res.map()).resolves.toMatchSnapshot("map");
}
});
}

export function validateMany(groupName: string, tests: Test[]): void {
export function validateMany(groupName: string, testDatas: TestData[]): void {
describe(groupName, () => {
for (const test of tests) {
validate({
...test,
outDir: path.join(groupName, test.title),
});
for (const testData of testDatas) {
validate({ ...testData, outDir: path.join(groupName, testData.title) });
}
});
}
Loading

0 comments on commit 663c184

Please sign in to comment.