Skip to content

Commit fc1077c

Browse files
committed
add SourceMap support
1 parent b60c16b commit fc1077c

File tree

19 files changed

+64
-5
lines changed

19 files changed

+64
-5
lines changed

src/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import webpack from 'webpack';
44
import sources from 'webpack-sources';
55

6-
const { ConcatSource } = sources;
6+
const { ConcatSource, SourceMapSource, OriginalSource } = sources;
77
const { Template } = webpack;
88

99
const NS = path.dirname(fs.realpathSync(__filename));
@@ -99,7 +99,7 @@ class MiniCssExtractPlugin {
9999
const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === NS);
100100
if (renderedModules.length > 0) {
101101
result.push({
102-
render: () => this.renderContentAsset(renderedModules),
102+
render: () => this.renderContentAsset(renderedModules, compilation.runtimeTemplate.requestShortener),
103103
filenameTemplate: this.options.filename,
104104
pathOptions: {
105105
chunk,
@@ -112,7 +112,7 @@ class MiniCssExtractPlugin {
112112
const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === NS);
113113
if (renderedModules.length > 0) {
114114
result.push({
115-
render: () => this.renderContentAsset(renderedModules),
115+
render: () => this.renderContentAsset(renderedModules, compilation.runtimeTemplate.requestShortener),
116116
filenameTemplate: this.options.chunkFilename,
117117
pathOptions: {
118118
chunk,
@@ -190,7 +190,7 @@ class MiniCssExtractPlugin {
190190
return obj;
191191
}
192192

193-
renderContentAsset(modules) {
193+
renderContentAsset(modules, requestShortener) {
194194
modules.sort((a, b) => a.index2 - b.index2);
195195
const source = new ConcatSource();
196196
const externalsSource = new ConcatSource();
@@ -211,7 +211,11 @@ class MiniCssExtractPlugin {
211211
if (m.media) {
212212
source.add(`@media ${m.media} {\n`);
213213
}
214-
source.add(m.content);
214+
if (m.sourceMap) {
215+
source.add(new SourceMapSource(m.content, m.readableIdentifier(requestShortener), m.sourceMap));
216+
} else {
217+
source.add(new OriginalSource(m.content, m.readableIdentifier(requestShortener)));
218+
}
215219
source.add('\n');
216220
if (m.media) {
217221
source.add('}\n');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.async { background: blue; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './in-async.css';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.async { background: blue; }
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(window.webpackJsonp=window.webpackJsonp||[]).push([[0],{4:function(n,w,o){}}]);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.in-async { background: green; }
2+

test/cases/simple-async-source-map/dist/1.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
body { background: red; }
2+

test/cases/simple-async-source-map/dist/main.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/simple-async-source-map/expected/1.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/simple-async-source-map/expected/1.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/simple-async-source-map/expected/2.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/simple-async-source-map/expected/2.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/simple-async-source-map/expected/main.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/simple-async-source-map/expected/main.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.in-async { background: green; }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import './main.css';
2+
3+
import('./async');
4+
5+
import('./async.css');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: red; }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Self = require('../../../');
2+
3+
module.exports = {
4+
entry: './index.js',
5+
devtool: 'source-map',
6+
module: {
7+
rules: [
8+
{
9+
test: /\.css$/,
10+
use: [
11+
Self.loader,
12+
'css-loader',
13+
],
14+
},
15+
],
16+
},
17+
plugins: [
18+
new Self({
19+
filename: '[name].css',
20+
}),
21+
],
22+
};

0 commit comments

Comments
 (0)