Skip to content

Commit

Permalink
feat: option to disable sourcesContent in sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed May 31, 2020
1 parent 45ed964 commit afc440b
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 67 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,14 @@ With that said, here is the basic list of things which differentiate this plugin
- Up-to-date [CSS Modules](https://github.com/css-modules/css-modules) implementation
- Built-in `@import` handler
- Built-in assets handler
- Respects `output.assetFileNames`
- Code splitting support
- Multi entry support
- Ability to emit pure CSS for other plugins
- Correct multiple instance support with check for already processed files
- Complete code splitting support, with respect for multiple entries, `preserveModules` and `manualChunks`
- Multiple instances support, with check for already processed files
- Proper sourcemaps, with included sources content by default
- Respects `assetFileNames` for CSS file names
- Respects sourcemaps from loaded files
- Support for implementation and `fibers` forcing for Sass
- Support for partials and `~` in Less import statements
- Sourcemaps include source content
- Proper sourcemap generation for all loaders
- Correct inline sourcemaps
- Correct relative source paths in extracted sourcemaps
- Extracts sourcemaps from loaded files
- More smaller things that I forgot

## Contributing
Expand Down
6 changes: 0 additions & 6 deletions __tests__/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ module.exports = {
"plugin:jest/recommended",
"plugin:jest/style",
],
globals: {
page: true,
browser: true,
context: true,
jestPuppeteer: true,
},
};
276 changes: 276 additions & 0 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,96 @@ console.log(css_bea182d1, css_ab26163b, modules_b45a4370, css_4e1fe5d3);
"
`;

exports[`modules auto-modules-off: js 1`] = `
"/** @type {HTMLElement[]} */
var containers = [];
/** @type {{prepend:HTMLStyleElement,append:HTMLStyleElement}[]} */
var styleTags = [];
/**
* @param {string} css
* @param {object} [options={}]
* @param {boolean} [options.prepend]
* @param {boolean} [options.singleTag]
* @param {string} [options.container]
* @param {Record<string,string>} [options.attributes]
* @returns {void}
*/
function injector_717f9233 (css, options) {
if (!css || typeof document === \\"undefined\\") return;
if (typeof options === \\"undefined\\") options = {};
var position = options.prepend === true ? \\"prepend\\" : \\"append\\";
var singleTag = typeof options.singleTag !== \\"undefined\\" ? options.singleTag : false;
var container =
typeof options.container !== \\"undefined\\"
? document.querySelector(options.container)
: document.getElementsByTagName(\\"head\\")[0];
function createStyleTag() {
var styleTag = document.createElement(\\"style\\");
styleTag.setAttribute(\\"type\\", \\"text/css\\");
if (options.attributes) {
var k = Object.keys(options.attributes);
for (var i = 0; i < k.length; i++) {
styleTag.setAttribute(k[i], options.attributes[k[i]]);
}
}
var pos = position === \\"prepend\\" ? \\"afterbegin\\" : \\"beforeend\\";
container.insertAdjacentElement(pos, styleTag);
return styleTag;
}
/** @type {HTMLStyleElement} */
var styleTag;
if (singleTag) {
var 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.substring(1);
if (styleTag.styleSheet) {
styleTag.styleSheet.cssText += css;
} else {
styleTag.appendChild(document.createTextNode(css));
}
}
const css_7ffcb4b5 = \\".global {\\\\n color: red;\\\\n}\\\\n\\";
injector_717f9233(css_7ffcb4b5);
const css_bea182d1 = \\".a {\\\\n color: red;\\\\n}\\\\n\\";
injector_717f9233(css_bea182d1);
const css_ab26163b = \\".b {\\\\n color: red; }\\\\n\\";
injector_717f9233(css_ab26163b);
const css_a4ea8038 = \\".c {\\\\n color: red;\\\\n}\\\\n\\";
injector_717f9233(css_a4ea8038);
const css_4e1fe5d3 = \\".d {\\\\n color: #f00;\\\\n}\\\\n\\";
injector_717f9233(css_4e1fe5d3);
console.log(css_bea182d1, css_ab26163b, css_a4ea8038, css_4e1fe5d3);
"
`;

exports[`modules auto-modules-regexp: js 1`] = `
"/** @type {HTMLElement[]} */
var containers = [];
Expand Down Expand Up @@ -3898,6 +3988,192 @@ console.log(css_a87c82eb, css_6e569e18);
"
`;

exports[`sourcemap inline-no-content: js 1`] = `
"/** @type {HTMLElement[]} */
var containers = [];
/** @type {{prepend:HTMLStyleElement,append:HTMLStyleElement}[]} */
var styleTags = [];
/**
* @param {string} css
* @param {object} [options={}]
* @param {boolean} [options.prepend]
* @param {boolean} [options.singleTag]
* @param {string} [options.container]
* @param {Record<string,string>} [options.attributes]
* @returns {void}
*/
function injector_8aeabfb4 (css, options) {
if (!css || typeof document === \\"undefined\\") return;
if (typeof options === \\"undefined\\") options = {};
var position = options.prepend === true ? \\"prepend\\" : \\"append\\";
var singleTag = typeof options.singleTag !== \\"undefined\\" ? options.singleTag : false;
var container =
typeof options.container !== \\"undefined\\"
? document.querySelector(options.container)
: document.getElementsByTagName(\\"head\\")[0];
function createStyleTag() {
var styleTag = document.createElement(\\"style\\");
styleTag.setAttribute(\\"type\\", \\"text/css\\");
if (options.attributes) {
var k = Object.keys(options.attributes);
for (var i = 0; i < k.length; i++) {
styleTag.setAttribute(k[i], options.attributes[k[i]]);
}
}
var pos = position === \\"prepend\\" ? \\"afterbegin\\" : \\"beforeend\\";
container.insertAdjacentElement(pos, styleTag);
return styleTag;
}
/** @type {HTMLStyleElement} */
var styleTag;
if (singleTag) {
var 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.substring(1);
if (styleTag.styleSheet) {
styleTag.styleSheet.cssText += css;
} else {
styleTag.appendChild(document.createTextNode(css));
}
}
const css_a87c82eb = \\".foo {\\\\n color: red;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvZm9vLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWiJ9 */\\";
injector_8aeabfb4(css_a87c82eb);
const css_6e569e18 = \\".bar {\\\\n color: red;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvYmFyLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWiJ9 */\\";
injector_8aeabfb4(css_6e569e18);
const css_a00496d3 = \\".stylus {\\\\n color: #f00;\\\\n background: #f00;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUuc3R5bCJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFdBQU87RUFDUCxnQkFBWTtBQUNkIn0= */\\";
injector_8aeabfb4(css_a00496d3);
const css_855b3ad0 = \\".pcss {\\\\n color: red;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUucGNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWiJ9 */\\";
injector_8aeabfb4(css_855b3ad0);
const css_31899cde = \\".sass {\\\\n width: 30%;\\\\n color: red; }\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUuc2FzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7RUFDVixVQUFVLEVBQUEifQ== */\\";
injector_8aeabfb4(css_31899cde);
const css_aee437ad = \\".less {\\\\n color: #6c94be;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUubGVzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFHQTtFQUNFLGNBQUE7QUFGRiJ9 */\\";
injector_8aeabfb4(css_aee437ad);
console.log(css_a87c82eb, css_6e569e18);
"
`;

exports[`sourcemap no-content: js 1`] = `
"/** @type {HTMLElement[]} */
var containers = [];
/** @type {{prepend:HTMLStyleElement,append:HTMLStyleElement}[]} */
var styleTags = [];
/**
* @param {string} css
* @param {object} [options={}]
* @param {boolean} [options.prepend]
* @param {boolean} [options.singleTag]
* @param {string} [options.container]
* @param {Record<string,string>} [options.attributes]
* @returns {void}
*/
function injector_8aeabfb4 (css, options) {
if (!css || typeof document === \\"undefined\\") return;
if (typeof options === \\"undefined\\") options = {};
var position = options.prepend === true ? \\"prepend\\" : \\"append\\";
var singleTag = typeof options.singleTag !== \\"undefined\\" ? options.singleTag : false;
var container =
typeof options.container !== \\"undefined\\"
? document.querySelector(options.container)
: document.getElementsByTagName(\\"head\\")[0];
function createStyleTag() {
var styleTag = document.createElement(\\"style\\");
styleTag.setAttribute(\\"type\\", \\"text/css\\");
if (options.attributes) {
var k = Object.keys(options.attributes);
for (var i = 0; i < k.length; i++) {
styleTag.setAttribute(k[i], options.attributes[k[i]]);
}
}
var pos = position === \\"prepend\\" ? \\"afterbegin\\" : \\"beforeend\\";
container.insertAdjacentElement(pos, styleTag);
return styleTag;
}
/** @type {HTMLStyleElement} */
var styleTag;
if (singleTag) {
var 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.substring(1);
if (styleTag.styleSheet) {
styleTag.styleSheet.cssText += css;
} else {
styleTag.appendChild(document.createTextNode(css));
}
}
const css_a87c82eb = \\".foo {\\\\n color: red;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvZm9vLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWiJ9 */\\";
injector_8aeabfb4(css_a87c82eb);
const css_6e569e18 = \\".bar {\\\\n color: red;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvYmFyLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWiJ9 */\\";
injector_8aeabfb4(css_6e569e18);
const css_a00496d3 = \\".stylus {\\\\n color: #f00;\\\\n background: #f00;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUuc3R5bCJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFdBQU87RUFDUCxnQkFBWTtBQUNkIn0= */\\";
injector_8aeabfb4(css_a00496d3);
const css_855b3ad0 = \\".pcss {\\\\n color: red;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUucGNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWiJ9 */\\";
injector_8aeabfb4(css_855b3ad0);
const css_31899cde = \\".sass {\\\\n width: 30%;\\\\n color: red; }\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUuc2FzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7RUFDVixVQUFVLEVBQUEifQ== */\\";
injector_8aeabfb4(css_31899cde);
const css_aee437ad = \\".less {\\\\n color: #6c94be;\\\\n}\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9fdGVzdHNfXy9maXh0dXJlcy9zaW1wbGUvc3R5bGUubGVzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFHQTtFQUNFLGNBQUE7QUFGRiJ9 */\\";
injector_8aeabfb4(css_aee437ad);
console.log(css_a87c82eb, css_6e569e18);
"
`;

exports[`sourcemap true: js 1`] = `
"/** @type {HTMLElement[]} */
var containers = [];
Expand Down
15 changes: 7 additions & 8 deletions __tests__/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Plugin, rollup, InputOptions, OutputOptions } from "rollup";

import styles from "../../src";
import { Options } from "../../src/types";
import { inferModeOption } from "../../src/utils/options";
import { inferModeOption, inferSourceMapOption } from "../../src/utils/options";

export interface WriteData {
input: string | string[];
Expand Down Expand Up @@ -96,6 +96,7 @@ export interface TestData extends WriteData {
export function validate(data: TestData): void {
const options = data.options ?? {};
const mode = inferModeOption(options.mode);
const sourceMap = inferSourceMapOption(options.sourceMap);
test(data.title, async () => {
if (data.shouldFail) {
await expect(write(data)).rejects.toThrowErrorMatchingSnapshot();
Expand All @@ -111,16 +112,14 @@ export function validate(data: TestData): void {
for (const f of await res.css()) expect(f).toMatchSnapshot("css");
}

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

if (data.files) {
for await (const f of data.files) await expect(res.isFile(f)).resolves.toBeTruthy();
}
for await (const f of data.files ?? []) await expect(res.isFile(f)).resolves.toBeTruthy();
});
}

Expand Down
15 changes: 15 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ validateMany("modules", [
input: "auto-modules/index.js",
options: { autoModules: true },
},
{
title: "auto-modules-off",
input: "auto-modules/index.js",
options: { autoModules: false },
},
{
title: "auto-modules-regexp",
input: "auto-modules/index.js",
Expand All @@ -189,11 +194,21 @@ validateMany("sourcemap", [
input: "simple/index.js",
options: { sourceMap: true },
},
{
title: "no-content",
input: "simple/index.js",
options: { sourceMap: [true, { content: false }] },
},
{
title: "inline",
input: "simple/index.js",
options: { sourceMap: "inline" },
},
{
title: "inline-no-content",
input: "simple/index.js",
options: { sourceMap: ["inline", { content: false }] },
},
]);

validateMany("extract", [
Expand Down

0 comments on commit afc440b

Please sign in to comment.