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

fix(index): remove empty assets #746

Merged
merged 3 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
579 changes: 409 additions & 170 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"async": "^2.4.1",
"glob": "^7.1.2",
"loader-utils": "^1.1.0",
"schema-utils": "^0.4.5",
"webpack-sources": "^1.1.0"
Expand Down Expand Up @@ -65,7 +66,7 @@
"raw-loader": "^0.5.1",
"standard-version": "^4.3.0",
"style-loader": "^0.19.1",
"webpack": "^4.0.0",
"webpack": "^4.1.1",
"webpack-defaults": "^2.0.0-rc.4"
},
"engines": {
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ class ExtractTextPlugin {
extractedChunk
);

if (!source.size()) {
return;
}

const getPath = (format) =>
compilation
.getPath(format, {
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/webpack-integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ c
"
`;

exports[`Webpack Integration Tests empty-chunk 1`] = `"a"`;

exports[`Webpack Integration Tests merging-chunk 1`] = `
"a
b
Expand Down Expand Up @@ -95,8 +97,6 @@ c
"
`;

exports[`Webpack Integration Tests multiple-entries-filename 3`] = `""`;

exports[`Webpack Integration Tests nested 1`] = `
"a
b
Expand Down
2 changes: 2 additions & 0 deletions test/cases/empty-chunk/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./a.txt');
require('./b.txt');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add \n in end of file 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

1 change: 1 addition & 0 deletions test/cases/empty-chunk/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
2 changes: 2 additions & 0 deletions test/cases/empty-chunk/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./b.txt');
require('./c.txt');
1 change: 1 addition & 0 deletions test/cases/empty-chunk/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions test/cases/empty-chunk/c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
1 change: 1 addition & 0 deletions test/cases/empty-chunk/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
16 changes: 16 additions & 0 deletions test/cases/empty-chunk/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('./a.txt');

require.ensure(
[],
() => {
require('./a.js');
},
'async-chunk-a',
);
require.ensure(
[],
() => {
require('./b.js');
},
'async-chunk-b',
);
22 changes: 22 additions & 0 deletions test/cases/empty-chunk/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ExtractTextPlugin from '../../../src/index';

module.exports = {
entry: './index',
optimization: {
splitChunks: {
cacheGroups: {
common: {
name: 'common',
chunks: 'async',
test: /async-chunk-(a|b)/,
},
},
},
runtimeChunk: true
},
plugins: [
new ExtractTextPlugin({
filename: '[name].css'
}),
],
};
29 changes: 13 additions & 16 deletions test/webpack-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fs from 'fs';
import path from 'path';
import webpack from 'webpack';
import glob from 'glob';
import ExtractTextPlugin from '../src';

const cases = process.env.CASES
Expand Down Expand Up @@ -47,24 +48,20 @@ describe('Webpack Integration Tests', () => {
require(testFile)(suite);
}
const expectedDirectory = path.join(testDirectory, 'expected');
fs.readdirSync(expectedDirectory).forEach((file) => {
const filePath = path.join(expectedDirectory, file);
const actualPath = path.join(outputDirectory, file);
expect(readFileOrEmpty(actualPath)).toEqual(
readFileOrEmpty(filePath)
);
expect(readFileOrEmpty(actualPath)).toMatchSnapshot();
});

glob
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need glob here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to get all files in a directory recursively. I can do it without glob, but why not ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.sync('**/*.@(txt|css)', { cwd: outputDirectory })
.forEach((file) => {
const filePath = path.join(expectedDirectory, file);
const actualPath = path.join(outputDirectory, file);
expect(fs.existsSync(filePath)).toBeTruthy();
expect(fs.readFileSync(actualPath, 'utf-8')).toEqual(
fs.readFileSync(filePath, 'utf-8')
);
expect(fs.readFileSync(actualPath, 'utf-8')).toMatchSnapshot();
});
done();
});
});
});
});

function readFileOrEmpty(filePath) {
try {
return fs.readFileSync(filePath, 'utf-8');
} catch (e) {
return '';
}
}