Skip to content

test: contenthash #1031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { getOptions, isUrlRequest } from 'loader-utils';
import schema from './options.json';
import { importParser, icssParser, urlParser } from './plugins';
import {
normalizeSourceMap,
getModulesPlugins,
getFilter,
getImportCode,
Expand Down Expand Up @@ -75,8 +74,7 @@ export default function loader(content, map, meta) {
to: this.currentRequest.split('!').pop(),
map: options.sourceMap
? {
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
prev: sourceMap && map ? normalizeSourceMap(map) : null,
prev: sourceMap && map ? map : null,
inline: false,
annotation: false,
}
Expand Down
28 changes: 14 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import localByDefault from 'postcss-modules-local-by-default';
import extractImports from 'postcss-modules-extract-imports';
import modulesScope from 'postcss-modules-scope';
import camelCase from 'camelcase';
import RequestShortener from 'webpack/lib/RequestShortener';

const whitespace = '[\\x20\\t\\r\\n\\f]';
const unescapeRegExp = new RegExp(
Expand Down Expand Up @@ -156,31 +157,28 @@ function getModulesPlugins(options, loaderContext) {
];
}

function normalizeSourceMap(map) {
let newMap = map;

// Some loader emit source map as string
// Strip any JSON XSSI avoidance prefix from the string (as documented in the source maps specification), and then parse the string as JSON.
if (typeof newMap === 'string') {
newMap = JSON.parse(newMap);
}
function normalizeSourceMap(map, rootContext) {
const newMap = map.toJSON();
const requestShortener = new RequestShortener(rootContext);

// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map

if (newMap.file) {
newMap.file = normalizePath(newMap.file);
newMap.file = requestShortener.shorten(newMap.file);
}

if (newMap.sourceRoot) {
newMap.sourceRoot = normalizePath(newMap.sourceRoot);
newMap.sourceRoot = requestShortener.shorten(newMap.sourceRoot);
}

if (newMap.sources) {
newMap.sources = newMap.sources.map((source) => normalizePath(source));
newMap.sources = newMap.sources.map((source) =>
requestShortener.shorten(source)
);
}

return newMap;
return JSON.stringify(newMap);
}

function getImportPrefix(loaderContext, importLoaders) {
Expand Down Expand Up @@ -372,7 +370,10 @@ function getModuleCode(
}

const { css, map } = result;
const sourceMapValue = sourceMap && map ? `,${map}` : '';
const sourceMapValue =
sourceMap && map
? `,${normalizeSourceMap(map, loaderContext.rootContext)}`
: '';

let cssCode = JSON.stringify(css);

Expand Down Expand Up @@ -501,7 +502,6 @@ export {
normalizeUrl,
getFilter,
getModulesPlugins,
normalizeSourceMap,
getImportCode,
getModuleCode,
getExportCode,
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader should have same "contenthash" with "css-loader" and with source maps: errors 1`] = `Array []`;

exports[`loader should have same "contenthash" with "css-loader" and with source maps: warnings 1`] = `Array []`;

exports[`loader should have same "contenthash" with "css-loader" and without source maps: errors 1`] = `Array []`;

exports[`loader should have same "contenthash" with "css-loader" and without source maps: warnings 1`] = `Array []`;

exports[`loader should have same "contenthash" with "postcss-loader" and with source maps: errors 1`] = `Array []`;

exports[`loader should have same "contenthash" with "postcss-loader" and with source maps: warnings 1`] = `Array []`;

exports[`loader should have same "contenthash" with "postcss-loader" and without source maps: errors 1`] = `Array []`;

exports[`loader should have same "contenthash" with "postcss-loader" and without source maps: warnings 1`] = `Array []`;

exports[`loader should have same "contenthash" with "sass-loader" and with source maps: errors 1`] = `Array []`;

exports[`loader should have same "contenthash" with "sass-loader" and with source maps: warnings 1`] = `Array []`;

exports[`loader should have same "contenthash" with "sass-loader" and without source maps: errors 1`] = `Array []`;

exports[`loader should have same "contenthash" with "sass-loader" and without source maps: warnings 1`] = `Array []`;

exports[`loader should reuse \`ast\` from "postcss-loader": errors 1`] = `Array []`;

exports[`loader should reuse \`ast\` from "postcss-loader": module 1`] = `
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/contenthash/basic-css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from './basic.css';

__export__ = css;

export default css;
5 changes: 5 additions & 0 deletions test/fixtures/contenthash/basic-postcss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from './basic.postcss.css';

__export__ = css;

export default css;
5 changes: 5 additions & 0 deletions test/fixtures/contenthash/basic-sass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from './basic.scss';

__export__ = css;

export default css;
3 changes: 3 additions & 0 deletions test/fixtures/contenthash/basic.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a {
color: red;
}
7 changes: 7 additions & 0 deletions test/fixtures/contenthash/basic.postcss.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a {
color: rgb(0 0 100% / 90%);

&:hover {
color: rebeccapurple;
}
}
9 changes: 9 additions & 0 deletions test/fixtures/contenthash/basic.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$var: red;

a {
color: $var;

&:hover {
color: $var;
}
}
Loading