Skip to content

Commit

Permalink
fix: write file directly from the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver committed Jul 12, 2017
1 parent a3d3a8e commit 81bdf52
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ spec/webpack-out
yarn.lock
.nyc_output
coverage
spec/output/
20 changes: 9 additions & 11 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,17 @@ ManifestPlugin.prototype.apply = function(compiler) {

var json = JSON.stringify(cache, null, 2);

compilation.assets[outputName] = {
source: function() {
return json;
},
size: function() {
return json.length;
}
};
var outputFolder = compilation.options.output.path;
var outputFile = path.join(outputFolder, this.opts.fileName);

var isMemoryFs = compiler.outputFileSystem.constructor.name === 'MemoryFileSystem';

if (this.opts.writeToFileEmit) {
var outputFolder = compilation.options.output.path;
var outputFile = path.join(outputFolder, this.opts.fileName);
if (isMemoryFs) {
compiler.outputFileSystem.mkdirpSync(path.dirname(outputFile));
compiler.outputFileSystem.writeFileSync(outputFile, json);
}

if (!isMemoryFs || this.opts.writeToFileEmit) {
fse.outputFileSync(outputFile, json);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"jasmine": "^2.2.1",
"memory-fs": "^0.2.0",
"nyc": "^10.3.2",
"rimraf": "^2.6.1",
"style-loader": "^0.8.3",
"webpack": "^1.7.3"
},
Expand Down
96 changes: 96 additions & 0 deletions spec/plugin.integration.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var fs = require('fs');
var path = require('path');

var _ = require('lodash');
var webpack = require('webpack');
var MemoryFileSystem = require('memory-fs');
var rimraf = require('rimraf');

var ManifestPlugin = require('../index.js');

function webpackConfig(webpackOpts, opts) {
return _.merge({
plugins: [
new ManifestPlugin(opts.manifestOptions)
]
}, webpackOpts);
}

function webpackCompile(config, compilerOps, cb) {
var compiler = webpack(config);

_.assign(compiler, compilerOps);

compiler.run(function(err, stats){
expect(err).toBeFalsy();
expect(stats.hasErrors()).toBe(false);

cb(stats);
});
};

describe('ManifestPlugin using real fs', function() {
beforeEach(function() {
rimraf.sync(path.join(__dirname, 'output/single-file'));
});

describe('basic behavior', function() {
it('outputs a manifest of one file', function (done) {
webpackCompile({
context: __dirname,
output: {
filename: '[name].js',
path: path.join(__dirname, 'output/single-file')
},
entry: './fixtures/file.js',
plugins: [
new ManifestPlugin()
]
}, {}, function() {
var manifest = JSON.parse(fs.readFileSync(path.join(__dirname, 'output/single-file/manifest.json')))

expect(manifest).toBeDefined();
expect(manifest).toEqual({
'main.js': 'main.js'
});

done();
});
});
});
});

describe('ManifestPlugin with memory-fs', function() {
describe('writeToFileEmit', function() {
beforeEach(function() {
rimraf.sync(path.join(__dirname, 'output/emit'));
});

it('outputs a manifest of one file', function (done) {
webpackCompile({
context: __dirname,
output: {
filename: '[name].js',
path: path.join(__dirname, 'output/emit')
},
entry: './fixtures/file.js',
plugins: [
new ManifestPlugin({
writeToFileEmit: true
})
]
}, {
outputFileSystem: new MemoryFileSystem()
}, function() {
var manifest = JSON.parse(fs.readFileSync(path.join(__dirname, 'output/emit/manifest.json')))

expect(manifest).toBeDefined();
expect(manifest).toEqual({
'main.js': 'main.js'
});

done();
});
});
});
});

0 comments on commit 81bdf52

Please sign in to comment.