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 9, 2017
1 parent fc57ae0 commit ce697a8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 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: 8 additions & 12 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,16 @@ ManifestPlugin.prototype.apply = function(compiler) {

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

compilation.assets[outputName] = {
source: function() {
return json;
},
size: function() {
return json.length;
}
};

if (this.opts.writeToFileEmit) {
var outputFolder = compilation.options.output.path;
var outputFile = path.join(outputFolder, this.opts.fileName);
var outputFolder = compilation.options.output.path;
var outputFile = path.join(outputFolder, this.opts.fileName);

const shouldUseNodeFs = compiler.outputFileSystem.constructor.name !== 'MemoryFileSystem' ||
this.opts.writeToFileEmit;
if (shouldUseNodeFs) {
fse.outputFileSync(outputFile, json);
} else {
compiler.outputFileSystem.mkdirpSync(path.dirname(outputFile));
compiler.outputFileSystem.writeFileSync(outputFile, json);
}

compileCallback();
Expand Down
53 changes: 53 additions & 0 deletions spec/plugin.integration.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var fs = require('fs');
var path = require('path');

var _ = require('lodash');
var webpack = require('webpack');

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

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

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

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

cb(stats);
});
};

describe('ManifestPlugin using real fs', function () {
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();
});
});
});
});
2 changes: 1 addition & 1 deletion spec/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function webpackCompile(webpackOpts, opts, cb) {
var fs = compiler.outputFileSystem = new MemoryFileSystem();

compiler.run(function(err, stats){
var manifestFile = JSON.parse( fs.readFileSync(manifestPath).toString() );
var manifestFile = JSON.parse(fs.readFileSync(manifestPath).toString());

expect(err).toBeFalsy();
expect(stats.hasErrors()).toBe(false);
Expand Down

0 comments on commit ce697a8

Please sign in to comment.