Skip to content

Commit 4cb95ce

Browse files
davidmpazmastilver
authored andcommitted
feat: add assets to manifest
BREAKING CHANGE: add extra keys to manifest when using copy-webpack-plugin closes #75
1 parent de15a87 commit 4cb95ce

File tree

3 files changed

+115
-10
lines changed

3 files changed

+115
-10
lines changed

lib/plugin.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ ManifestPlugin.prototype.apply = function(compiler) {
4747

4848
var files = compilation.chunks.reduce(function(files, chunk) {
4949
return chunk.files.reduce(function (files, path) {
50-
// Don't add hot updates to manifest
51-
if (path.indexOf('hot-update') >= 0) {
52-
return files;
53-
}
54-
5550
var name = chunk.name ? chunk.name.replace(this.opts.stripSrc, '') : null;
5651

5752
if (name) {
@@ -77,20 +72,37 @@ ManifestPlugin.prototype.apply = function(compiler) {
7772
// we're getting them this way;
7873
files = stats.assets.reduce(function (files, asset) {
7974
var name = moduleAssets[asset.name];
80-
if (!name) {
75+
if (name) {
76+
return files.concat({
77+
path: asset.name,
78+
name: name,
79+
isInitial: false,
80+
isChunk: false,
81+
isAsset: true,
82+
isModuleAsset: true
83+
});
84+
}
85+
86+
const isEntryAsset = asset.chunks.length > 0;
87+
if (isEntryAsset) {
8188
return files;
8289
}
8390

8491
return files.concat({
8592
path: asset.name,
86-
name: name,
93+
name: asset.name,
8794
isInitial: false,
8895
isChunk: false,
8996
isAsset: true,
90-
isModuleAsset: true
97+
isModuleAsset: false
9198
});
9299
}, files);
93100

101+
files = files.filter(function (file) {
102+
// Don't add hot updates to manifest
103+
return file.path.indexOf('hot-update') === -1;
104+
});
105+
94106
// Append optional basepath onto all references.
95107
// This allows output path to be reflected in the manifest.
96108
if (this.opts.basePath) {

spec/helpers/copy-plugin-mock.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function FakeCopyWebpackPlugin() {
2+
};
3+
4+
FakeCopyWebpackPlugin.prototype.apply = function (compiler) {
5+
compiler.plugin('emit', function (compilation, callback) {
6+
7+
var compiledMock = '// some compilation result\n';
8+
compilation.assets['third.party.js'] = {
9+
size: function () {
10+
return compiledMock.length;
11+
},
12+
source: function () {
13+
return compiledMock;
14+
}
15+
};
16+
17+
callback();
18+
});
19+
};
20+
21+
module.exports = FakeCopyWebpackPlugin;

spec/plugin.spec.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var MemoryFileSystem = require('memory-fs');
44
var webpack = require('webpack');
55
var _ = require('lodash');
66
var ExtractTextPlugin = require('extract-text-webpack-plugin');
7+
var FakeCopyWebpackPlugin = require('./helpers/copy-plugin-mock');
78
var plugin = require('../index.js');
89

910
var OUTPUT_DIR = path.join(__dirname, './webpack-out');
@@ -476,7 +477,7 @@ describe('ManifestPlugin', function() {
476477
var manifest = JSON.parse(fs.readFileSync(manifestPath).toString());
477478

478479
expect(manifest).toEqual({
479-
'main.js': 'main.js'
480+
'main.js': 'main.js'
480481
});
481482

482483
done();
@@ -499,7 +500,7 @@ describe('ManifestPlugin', function() {
499500
var manifest = JSON.parse(fs.readFileSync(manifestPath).toString());
500501

501502
expect(manifest).toEqual({
502-
'main.js': 'main.js'
503+
'main.js': 'main.js'
503504
});
504505

505506
done();
@@ -668,4 +669,75 @@ describe('ManifestPlugin', function() {
668669
});
669670
});
670671
});
672+
673+
describe('with CopyWebpackPlugin', function () {
674+
it('works when including copied assets', function (done) {
675+
webpackCompile({
676+
context: __dirname,
677+
entry: {
678+
one: './fixtures/file.js'
679+
},
680+
plugins: [
681+
new FakeCopyWebpackPlugin(),
682+
new plugin()
683+
]
684+
}, {}, function (manifest, stats) {
685+
expect(manifest).toEqual({
686+
'one.js': 'one.js',
687+
'third.party.js': 'third.party.js'
688+
});
689+
690+
done();
691+
});
692+
});
693+
694+
it('doesn\'t add duplicates when prefixes definitions with a base path', function (done) {
695+
webpackCompile({
696+
context: __dirname,
697+
entry: {
698+
one: './fixtures/file.js',
699+
},
700+
output: {
701+
filename: '[name].[hash].js'
702+
},
703+
plugins: [
704+
new FakeCopyWebpackPlugin(),
705+
new plugin({
706+
basePath: '/app/',
707+
publicPath: '/app/'
708+
})
709+
]
710+
}, {}, function (manifest, stats) {
711+
expect(manifest).toEqual({
712+
'/app/one.js': '/app/one.' + stats.hash + '.js',
713+
'/app/third.party.js': '/app/third.party.js'
714+
});
715+
716+
done();
717+
});
718+
});
719+
720+
it('doesn\'t add duplicates when used with hashes in the filename', function (done) {
721+
webpackCompile({
722+
context: __dirname,
723+
entry: {
724+
one: './fixtures/file.js',
725+
},
726+
output: {
727+
filename: '[name].[hash].js'
728+
},
729+
plugins: [
730+
new FakeCopyWebpackPlugin(),
731+
new plugin()
732+
]
733+
}, {}, function(manifest, stats) {
734+
expect(manifest).toEqual({
735+
'one.js': 'one.' + stats.hash + '.js',
736+
'third.party.js': 'third.party.js'
737+
});
738+
739+
done();
740+
});
741+
});
742+
});
671743
});

0 commit comments

Comments
 (0)