Skip to content

Commit

Permalink
Use compiler.outputFileSystem for output
Browse files Browse the repository at this point in the history
  • Loading branch information
ztoben committed Jun 21, 2018
1 parent 4ef08e0 commit 869563b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var fs = require('fs')
var path = require('path')
var merge = require('lodash.merge')

var getAssetKind = require('./lib/getAssetKind')
Expand Down Expand Up @@ -111,7 +113,12 @@ AssetsWebpackPlugin.prototype = {
output.metadata = self.options.metadata
}

self.writer(output, function (err) {
if (!compiler.outputFileSystem.readFile) {
compiler.outputFileSystem.readFile = fs.readFile.bind(fs)
compiler.outputFileSystem.join = path.join.bind(path)
}

self.writer(compiler.outputFileSystem, output, function (err) {
if (err) {
compilation.errors.push(err)
}
Expand Down
15 changes: 8 additions & 7 deletions lib/output/createOutputWriter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var mkdirp = require('mkdirp')
var path = require('path')
var fs = require('fs')
var merge = require('lodash.merge')
var path = require('path')

var error = require('../utils/error')

Expand All @@ -20,22 +18,25 @@ function orderAssets (assets, options) {
}

module.exports = function (options) {
var outputPath = path.join(options.path, options.filename)
var update = options.update
var firstRun = true

options.processOutput = options.processOutput || function (assets) {
return JSON.stringify(assets, null, options.prettyPrint ? 2 : null)
}

return function writeOutput (newAssets, next) {
// if potions.update is false and we're on the first pass of a (possibly) multicompiler
return function writeOutput (fs, newAssets, next) {
// if options.update is false and we're on the first pass of a (possibly) multicompiler
var overwrite = !update && firstRun
var absolutePath = path.resolve(options.path)

mkdirp(options.path, function (err) {
fs.mkdirp(absolutePath, function (err) {
if (err) {
return next(error('Could not create output folder ' + options.path, err))
}

var outputPath = fs.join(options.path, options.filename)

fs.readFile(outputPath, 'utf8', function (err, data) {
// if file does not exist, just write data to it
if (err && err.code !== 'ENOENT') {
Expand Down
8 changes: 4 additions & 4 deletions lib/output/createQueuedWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ module.exports = function createQueuedWriter (processor) {

var next = queue[0]
if (next) {
processor(next.data, iterator(next.callback))
processor(next.fs, next.data, iterator(next.callback))
}
}
}

return function queuedWriter (data, callback) {
return function queuedWriter (fs, data, callback) {
var empty = !queue.length
queue.push({data: data, callback: callback})
queue.push({fs: fs, data: data, callback: callback})

if (empty) {
// start processing
processor(data, iterator(callback))
processor(fs, data, iterator(callback))
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "assets-webpack-plugin",
"version": "3.8.4",
"version": "3.8.4-alpha.0",
"description": "Emits a json file with assets paths",
"main": "dist/index.js",
"scripts": {
Expand Down

0 comments on commit 869563b

Please sign in to comment.