Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit a63fee2

Browse files
lili21michael-ciniawsky
authored andcommitted
fix(index): remove empty assets (#746)
1 parent 5dbbcd9 commit a63fee2

File tree

13 files changed

+479
-189
lines changed

13 files changed

+479
-189
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"dependencies": {
3535
"async": "^2.4.1",
36+
"glob": "^7.1.2",
3637
"loader-utils": "^1.1.0",
3738
"schema-utils": "^0.4.5",
3839
"webpack-sources": "^1.1.0"
@@ -65,7 +66,7 @@
6566
"raw-loader": "^0.5.1",
6667
"standard-version": "^4.3.0",
6768
"style-loader": "^0.19.1",
68-
"webpack": "^4.0.0",
69+
"webpack": "^4.1.1",
6970
"webpack-defaults": "^2.0.0-rc.4"
7071
},
7172
"engines": {

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ class ExtractTextPlugin {
351351
extractedChunk
352352
);
353353

354+
if (!source.size()) {
355+
return;
356+
}
357+
354358
const getPath = (format) =>
355359
compilation
356360
.getPath(format, {

test/__snapshots__/webpack-integration.test.js.snap

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ c
4242
"
4343
`;
4444

45+
exports[`Webpack Integration Tests empty-chunk 1`] = `
46+
"a
47+
"
48+
`;
49+
4550
exports[`Webpack Integration Tests merging-chunk 1`] = `
4651
"a
4752
b
@@ -95,8 +100,6 @@ c
95100
"
96101
`;
97102

98-
exports[`Webpack Integration Tests multiple-entries-filename 3`] = `""`;
99-
100103
exports[`Webpack Integration Tests nested 1`] = `
101104
"a
102105
b

test/cases/empty-chunk/a.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('./a.txt');
2+
require('./b.txt');

test/cases/empty-chunk/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

test/cases/empty-chunk/b.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('./b.txt');
2+
require('./c.txt');

test/cases/empty-chunk/b.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b

test/cases/empty-chunk/c.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

test/cases/empty-chunk/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require('./a.txt');
2+
3+
require.ensure(
4+
[],
5+
() => {
6+
require('./a.js');
7+
},
8+
'async-chunk-a',
9+
);
10+
require.ensure(
11+
[],
12+
() => {
13+
require('./b.js');
14+
},
15+
'async-chunk-b',
16+
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ExtractTextPlugin from '../../../src/index';
2+
3+
module.exports = {
4+
entry: './index',
5+
optimization: {
6+
splitChunks: {
7+
cacheGroups: {
8+
common: {
9+
name: 'common',
10+
chunks: 'async',
11+
test: /async-chunk-(a|b)/,
12+
},
13+
},
14+
},
15+
runtimeChunk: true
16+
},
17+
plugins: [
18+
new ExtractTextPlugin({
19+
filename: '[name].css'
20+
}),
21+
],
22+
};

test/webpack-integration.test.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import fs from 'fs';
77
import path from 'path';
88
import webpack from 'webpack';
9+
import glob from 'glob';
910
import ExtractTextPlugin from '../src';
1011

1112
const cases = process.env.CASES
@@ -47,24 +48,20 @@ describe('Webpack Integration Tests', () => {
4748
require(testFile)(suite);
4849
}
4950
const expectedDirectory = path.join(testDirectory, 'expected');
50-
fs.readdirSync(expectedDirectory).forEach((file) => {
51-
const filePath = path.join(expectedDirectory, file);
52-
const actualPath = path.join(outputDirectory, file);
53-
expect(readFileOrEmpty(actualPath)).toEqual(
54-
readFileOrEmpty(filePath)
55-
);
56-
expect(readFileOrEmpty(actualPath)).toMatchSnapshot();
57-
});
51+
52+
glob
53+
.sync('**/*.@(txt|css)', { cwd: outputDirectory })
54+
.forEach((file) => {
55+
const filePath = path.join(expectedDirectory, file);
56+
const actualPath = path.join(outputDirectory, file);
57+
expect(fs.existsSync(filePath)).toBeTruthy();
58+
expect(fs.readFileSync(actualPath, 'utf-8')).toEqual(
59+
fs.readFileSync(filePath, 'utf-8')
60+
);
61+
expect(fs.readFileSync(actualPath, 'utf-8')).toMatchSnapshot();
62+
});
5863
done();
5964
});
6065
});
6166
});
6267
});
63-
64-
function readFileOrEmpty(filePath) {
65-
try {
66-
return fs.readFileSync(filePath, 'utf-8');
67-
} catch (e) {
68-
return '';
69-
}
70-
}

0 commit comments

Comments
 (0)