From 81bdf52ec20314a18e9b88d87193afe56a046133 Mon Sep 17 00:00:00 2001 From: Thomas Sileghem Date: Sun, 9 Jul 2017 15:36:43 +0100 Subject: [PATCH] fix: write file directly from the plugin --- .gitignore | 1 + lib/plugin.js | 20 ++++--- package.json | 1 + spec/plugin.integration.spec.js | 96 +++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 spec/plugin.integration.spec.js diff --git a/.gitignore b/.gitignore index ddc9b08..a76582a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ spec/webpack-out yarn.lock .nyc_output coverage +spec/output/ diff --git a/lib/plugin.js b/lib/plugin.js index 85175cd..f1c3a4c 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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); } diff --git a/package.json b/package.json index 6dc2956..6660442 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/spec/plugin.integration.spec.js b/spec/plugin.integration.spec.js new file mode 100644 index 0000000..0c80edd --- /dev/null +++ b/spec/plugin.integration.spec.js @@ -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(); + }); + }); + }); +});