Skip to content
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
33 changes: 30 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import webpack from 'webpack';
import sources from 'webpack-sources';
import { contextify } from 'webpack/lib/util/identifier';

import validateOptions from 'schema-utils';

Expand All @@ -24,6 +25,29 @@ const REGEXP_NAME = /\[name\]/i;
const REGEXP_PLACEHOLDERS = /\[(name|id|chunkhash)\]/g;
const DEFAULT_FILENAME = '[name].css';

const contextifySourceMap = (context, sourceMap) => {
if (!sourceMap || !Array.isArray(sourceMap.sources)) return sourceMap;
const { sourceRoot, ...rest } = sourceMap;
const mapper = (source) => {
if (!sourceRoot) return source;
if (sourceRoot.endsWith('/')) {
return source.startsWith('/')
? `${sourceRoot.slice(0, -1)}${source}`
: `${sourceRoot}${source}`;
}
return source.startsWith('/')
? `${sourceRoot}${source}`
: `${sourceRoot}/${source}`;
};
const newSources = sourceMap.sources.map((source) =>
contextify(context, mapper(source))
);
return {
...rest,
sources: newSources,
};
};

class CssDependencyTemplate {
apply() {}
}
Expand All @@ -37,7 +61,7 @@ class CssModule extends webpack.Module {
this._identifierIndex = dependency.identifierIndex;
this.content = dependency.content;
this.media = dependency.media;
this.sourceMap = dependency.sourceMap;
this.sourceMap = contextifySourceMap(this.context, dependency.sourceMap);
}

// no source() so webpack doesn't do add stuff to the bundle
Expand Down Expand Up @@ -70,7 +94,7 @@ class CssModule extends webpack.Module {
updateCacheModule(module) {
this.content = module.content;
this.media = module.media;
this.sourceMap = module.sourceMap;
this.sourceMap = contextifySourceMap(this.context, module.sourceMap);
}

needRebuild() {
Expand All @@ -88,7 +112,10 @@ class CssModule extends webpack.Module {

hash.update(this.content);
hash.update(this.media || '');
hash.update(this.sourceMap ? JSON.stringify(this.sourceMap) : '');

if (this.useSourceMap && this.sourceMap) {
hash.update(JSON.stringify(this.sourceMap));
}
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/cases/contenthash-source-map/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-unresolved
import './style.css';
3 changes: 3 additions & 0 deletions test/cases/contenthash-source-map/style1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
3 changes: 3 additions & 0 deletions test/cases/contenthash-source-map/style2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: green;
}
39 changes: 39 additions & 0 deletions test/cases/contenthash-source-map/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Self from '../../../src';

module.exports = [1, 2].map((n) => {
return {
entry: './index.js',
devtool: 'source-map',
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
],
},
],
},
output: {
filename: `${n}.[name].js`,
},
resolve: {
alias: {
'./style.css': `./style${n}.css`,
},
},
plugins: [
new Self({
filename: `${n}.[name].[contenthash].css`,
}),
],
};
});