Skip to content

Commit a9e05b9

Browse files
Handle case where there are no source maps.
This turns out to be really hard to do in practice. Generally speaking any time you use `ExtractTextPlugin` you get a `ConcatSource` which always has `sourceAndMap` available. The only case you don't get a source map is when another plugin generates a CSS file that uses `RawSource` or something similar. One such plugin is `optimize-css-assets-webpack-plugin` when you don't tell the CSS processor to generate a source map (e.g. `cssProcessorOptions` has no `map` entry).
1 parent 38e3286 commit a9e05b9

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"less-loader": "^2.2.3",
3636
"memory-fs": "^0.3.0",
3737
"mocha": "^2.4.5",
38+
"optimize-css-assets-webpack-plugin": "^3.2.0",
3839
"style-loader": "^0.13.1",
3940
"webpack": "^1.13.0"
4041
},

src/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export default class CSSSplitWebpackPlugin {
102102
const input = asset.sourceAndMap ? asset.sourceAndMap() : {
103103
source: asset.source(),
104104
};
105-
const name = (i) => this.options.filename({
105+
const getName = (i) => this.options.filename({
106106
...asset,
107107
content: input.source,
108108
file: key,
@@ -116,11 +116,14 @@ export default class CSSSplitWebpackPlugin {
116116
return Promise.resolve({
117117
file: key,
118118
chunks: result.chunks.map(({css, map}, i) => {
119-
return new SourceMapSource(
119+
const name = getName(i);
120+
const result = map ? new SourceMapSource(
120121
css,
121-
name(i),
122+
name,
122123
map.toString()
123-
);
124+
) : new RawSource(css);
125+
result.name = name;
126+
return result;
124127
}),
125128
});
126129
});
@@ -141,8 +144,8 @@ export default class CSSSplitWebpackPlugin {
141144
}
142145
// Inject the new files into the chunk.
143146
entry.chunks.forEach((file) => {
144-
assets[file._name] = file;
145-
chunk.files.push(file._name);
147+
assets[file.name] = file;
148+
chunk.files.push(file.name);
146149
});
147150
const content = entry.chunks.map((file) => {
148151
return `@import "${publicPath}/${file._name}";`;

test/spec/index.spec.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _webpack from 'webpack';
22
import ExtractTextPlugin from 'extract-text-webpack-plugin';
3+
import OptimizeCssPlugin from 'optimize-css-assets-webpack-plugin';
34
import CSSSplitWebpackPlugin from '../../src';
45
import path from 'path';
56
import MemoryFileSystem from 'memory-fs';
@@ -17,7 +18,10 @@ const extract = ExtractTextPlugin.extract.length !== 1 ?
1718
loader: fallbackLoader,
1819
});
1920

20-
const config = (options, entry = basic, extra) => {
21+
const config = (options, entry = basic, {
22+
plugins,
23+
...extra
24+
} = {devtool: 'source-map'}) => {
2125
return {
2226
entry: path.join(__dirname, '..', '..', 'example', entry),
2327
context: path.join(__dirname, '..', '..', 'example'),
@@ -40,10 +44,10 @@ const config = (options, entry = basic, extra) => {
4044
),
4145
}],
4246
},
43-
devtool: 'source-map',
4447
plugins: [
4548
new ExtractTextPlugin('styles.css'),
4649
new CSSSplitWebpackPlugin(options),
50+
...(plugins || []),
4751
],
4852
...extra,
4953
};
@@ -139,7 +143,7 @@ describe('CSSSplitWebpackPlugin', () => {
139143
).to.throw(TypeError);
140144
});
141145
describe('deferred emit', () => {
142-
it('should split css files when necessary', (done) => {
146+
it('should split css files when necessary', () =>
143147
webpack({size: 3, defer: true}).then(({stats, files}) => {
144148
expect(stats.assetsByChunkName)
145149
.to.have.property('main')
@@ -148,10 +152,9 @@ describe('CSSSplitWebpackPlugin', () => {
148152
expect(files).to.have.property('styles-1.css');
149153
expect(files).to.have.property('styles-2.css');
150154
expect(files).to.have.property('styles.css.map');
151-
done();
152-
});
153-
});
154-
it('should ignore files that do not need splitting', (done) => {
155+
})
156+
);
157+
it('should ignore files that do not need splitting', () =>
155158
webpack({size: 10, defer: true}).then(({stats, files}) => {
156159
expect(stats.assetsByChunkName)
157160
.to.have.property('main')
@@ -161,8 +164,23 @@ describe('CSSSplitWebpackPlugin', () => {
161164
expect(files).to.have.property('styles.css');
162165
expect(files).to.not.have.property('styles-1.css');
163166
expect(files).to.not.have.property('styles-2.css');
164-
done();
165-
});
166-
});
167+
})
168+
);
169+
it('should handle cases when there are no source maps', () =>
170+
webpack({
171+
size: 3,
172+
defer: true,
173+
}, basic, {
174+
devtool: null,
175+
plugins: [
176+
new OptimizeCssPlugin(),
177+
],
178+
}).then(({stats, files}) => {
179+
expect(files).to.not.have.property('styles-1.css.map');
180+
expect(stats.assetsByChunkName)
181+
.to.have.property('main')
182+
.to.contain('styles-1.css');
183+
})
184+
);
167185
});
168186
});

0 commit comments

Comments
 (0)