Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed Aug 31, 2021
2 parents bf89ac4 + 3287835 commit 3fc84f0
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 176 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '6'
- '8'
- 10
- 12
- 14
- 'node'

2 changes: 1 addition & 1 deletion LICENSE-MIT
@@ -1,4 +1,4 @@
Copyright (c) 2018 Kyle Robinson Young
Copyright (c) 2021 Kyle Robinson Young

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
9 changes: 5 additions & 4 deletions gulpfile.js
@@ -1,13 +1,14 @@
var gulp = require('gulp');
var webpack = require('./');
var rimraf = require('rimraf');
var named = require('vinyl-named');
const gulp = require('gulp');
const webpack = require('./');
const rimraf = require('rimraf');
const named = require('vinyl-named');

gulp.task('default', function () {
rimraf.sync('tmp');
return gulp.src(['test/fixtures/entry.js', 'test/fixtures/anotherentrypoint.js'])
.pipe(named())
.pipe(webpack({
mode: 'production',
devtool: 'source-map'
}))
.pipe(gulp.dest('tmp/'));
Expand Down
157 changes: 98 additions & 59 deletions index.js
@@ -1,16 +1,16 @@
'use strict';

var fancyLog = require('fancy-log');
var PluginError = require('plugin-error');
var supportsColor = require('supports-color');
var File = require('vinyl');
var MemoryFileSystem = require('memory-fs');
var nodePath = require('path');
var through = require('through');
var ProgressPlugin = require('webpack/lib/ProgressPlugin');
var clone = require('lodash.clone');

var defaultStatsOptions = {
const fancyLog = require('fancy-log');
const PluginError = require('plugin-error');
const supportsColor = require('supports-color');
const File = require('vinyl');
const MemoryFileSystem = require('memory-fs');
const nodePath = require('path');
const through = require('through');
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
const clone = require('lodash.clone');

const defaultStatsOptions = {
colors: supportsColor.stdout.hasBasic,
hash: false,
timings: false,
Expand All @@ -26,32 +26,42 @@ var defaultStatsOptions = {
errorDetails: false
};

var cache = {};

module.exports = function (options, wp, done) {
if (cache.wp !== wp || cache.options !== options) {
cache = {};
const cache = {
options: options,
wp: wp
};

options = clone(options) || {};
const config = options.config || options;

const isInWatchMode = !!options.watch;
delete options.watch;

if (typeof config === 'string') {
config = require(config);
}

cache.options = options;
cache.wp = wp;
// Webpack 4 doesn't support the `quiet` attribute, however supports
// setting `stats` to a string within an array of configurations
// (errors-only|minimal|none|normal|verbose) or an object with an absurd
// amount of config
const isSilent = options.quiet || (typeof options.stats === 'string' && (options.stats.match(/^(errors-only|minimal|none)$/)));

options = clone(options) || {};
var config = options.config || options;
if (typeof done !== 'function') {
var callingDone = false;
let callingDone = false;
done = function (err, stats) {
if (err) {
// The err is here just to match the API but isnt used
return;
}
stats = stats || {};
if (options.quiet || callingDone) {
if (isSilent || callingDone) {
return;
}

// Debounce output a little for when in watch mode
if (options.watch) {
if (isInWatchMode) {
callingDone = true;
setTimeout(function () {
callingDone = false;
Expand All @@ -63,7 +73,7 @@ module.exports = function (options, wp, done) {
colors: supportsColor.stdout.hasBasic
}));
} else {
var statsOptions = (options && options.stats) || {};
const statsOptions = (options && options.stats) || {};

if (typeof statsOptions === 'object') {
Object.keys(defaultStatsOptions).forEach(function (key) {
Expand All @@ -72,19 +82,19 @@ module.exports = function (options, wp, done) {
}
});
}
var statusLog = stats.toString(statsOptions);
const statusLog = stats.toString(statsOptions);
if (statusLog) {
fancyLog(statusLog);
}
}
};
}

var webpack = wp || require('webpack');
var entry = [];
var entries = Object.create(null);
const webpack = wp || require('webpack');
let entry = [];
const entries = Object.create(null);

var stream = through(function (file) {
const stream = through(function (file) {
if (file.isNull()) {
return;
}
Expand All @@ -98,10 +108,9 @@ module.exports = function (options, wp, done) {
entry.push(file.path);
}
}, function () {
var self = this;
var handleConfig = function (config) {
const self = this;
const handleConfig = function (config) {
config.output = config.output || {};
config.watch = !!options.watch;

// Determine pipe'd in entry
if (Object.keys(entries).length > 0) {
Expand All @@ -127,9 +136,9 @@ module.exports = function (options, wp, done) {
return true;
};

var succeeded;
let succeeded;
if (Array.isArray(config)) {
for (var i = 0; i < config.length; i++) {
for (let i = 0; i < config.length; i++) {
succeeded = handleConfig(config[i]);
if (!succeeded) {
return false;
Expand All @@ -143,41 +152,62 @@ module.exports = function (options, wp, done) {
}

// Cache compiler for future use
var compiler = cache.compiler || webpack(config);
const compiler = cache.compiler || webpack(config);
cache.compiler = compiler;

const callback = function (err, stats) {
if (err) {
self.emit('error', new PluginError('webpack-stream', err));
return;
}
var jsonStats = stats ? stats.toJson() || {} : {};
var errors = jsonStats.errors || [];
const jsonStats = stats ? stats.toJson() || {} : {};
const errors = jsonStats.errors || [];
if (errors.length) {
var errorMessage = errors.join('\n');
var compilationError = new PluginError('webpack-stream', errorMessage);
if (!options.watch) {
const resolveErrorMessage = (err) => {
if (
typeof err === 'object' &&
err !== null &&
Object.prototype.hasOwnProperty.call(err, 'message')
) {
return err.message;
} else if (
typeof err === 'object' &&
err !== null &&
'toString' in err &&
err.toString() !== '[object Object]'
) {
return err.toString();
} else if (Array.isArray(err)) {
return err.map(resolveErrorMessage).join('\n');
} else {
return err;
}
};

const errorMessage = errors.map(resolveErrorMessage).join('\n');
const compilationError = new PluginError('webpack-stream', errorMessage);
if (!isInWatchMode) {
self.emit('error', compilationError);
}
self.emit('compilation-error', compilationError);
}
if (!options.watch) {
if (!isInWatchMode) {
self.queue(null);
}
done(err, stats);
if (options.watch && !options.quiet) {
if (isInWatchMode && !isSilent) {
fancyLog('webpack is watching for changes');
}
};

if (options.watch) {
const watchOptions = {};
if (isInWatchMode) {
const watchOptions = options.watchOptions || {};
compiler.watch(watchOptions, callback);
} else {
compiler.run(callback);
}

var handleCompiler = function (compiler) {
const handleCompiler = function (compiler) {
if (options.progress) {
(new ProgressPlugin(function (percentage, msg) {
percentage = Math.floor(percentage * 100);
Expand All @@ -189,21 +219,17 @@ module.exports = function (options, wp, done) {

cache.mfs = cache.mfs || new MemoryFileSystem();

var fs = compiler.outputFileSystem = cache.mfs;
const fs = compiler.outputFileSystem = cache.mfs;

var afterEmitPlugin = compiler.hooks
const assetEmittedPlugin = compiler.hooks
// Webpack 4
? function (callback) { compiler.hooks.afterEmit.tapAsync('WebpackStream', callback); }
? function (callback) { compiler.hooks.assetEmitted.tapAsync('WebpackStream', callback); }
// Webpack 2/3
: function (callback) { compiler.plugin('after-emit', callback); };
: function (callback) { compiler.plugin('asset-emitted', callback); };

afterEmitPlugin(function (compilation, callback) {
Object.keys(compilation.assets).forEach(function (outname) {
if (compilation.assets[outname].emitted) {
var file = prepareFile(fs, compiler, outname);
self.queue(file);
}
});
assetEmittedPlugin(function (outname, _, callback) {
const file = prepareFile(fs, compiler, outname);
self.queue(file);
callback();
});
};
Expand All @@ -215,10 +241,23 @@ module.exports = function (options, wp, done) {
} else {
handleCompiler(compiler);
}

if (options.watch && !isSilent) {
const watchRunPlugin = compiler.hooks
// Webpack 4
? callback => compiler.hooks.watchRun.tapAsync('WebpackInfo', callback)
// Webpack 2/3
: callback => compiler.plugin('watch-run', callback);

watchRunPlugin((compilation, callback) => {
fancyLog('webpack compilation starting...');
callback();
});
}
});

// If entry point manually specified, trigger that
var hasEntry = Array.isArray(config)
const hasEntry = Array.isArray(config)
? config.some(function (c) { return c.entry; })
: config.entry;
if (hasEntry) {
Expand All @@ -229,14 +268,14 @@ module.exports = function (options, wp, done) {
};

function prepareFile (fs, compiler, outname) {
var path = fs.join(compiler.outputPath, outname);
let path = fs.join(compiler.outputPath, outname);
if (path.indexOf('?') !== -1) {
path = path.split('?')[0];
}

var contents = fs.readFileSync(path);
const contents = fs.readFileSync(path);

var file = new File({
const file = new File({
base: compiler.outputPath,
path: nodePath.join(compiler.outputPath, outname),
contents: contents
Expand Down
44 changes: 29 additions & 15 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "webpack-stream",
"version": "5.1.1",
"version": "6.1.2",
"description": "Run webpack as a stream",
"license": "MIT",
"homepage": "https://github.com/shama/webpack-stream",
Expand All @@ -11,33 +11,47 @@
"url": "http://dontkry.com"
},
"engines": {
"node": ">= 6.11.5"
"node": ">= 10.0.0"
},
"scripts": {
"start": "gulp",
"debug": "node --inspect-brk ./node_modules/.bin/gulp",
"test": "semistandard && node test/test.js"
},
"files": ["index.js"],
"files": [
"index.js"
],
"semistandard": {
"ignore": ["test/fixtures", "examples"]
"ignore": [
"test/fixtures",
"examples"
]
},
"dependencies": {
"fancy-log": "^1.3.2",
"fancy-log": "^1.3.3",
"lodash.clone": "^4.3.2",
"lodash.some": "^4.2.2",
"memory-fs": "^0.4.1",
"memory-fs": "^0.5.0",
"plugin-error": "^1.0.1",
"supports-color": "^5.3.0",
"supports-color": "^8.1.1",
"through": "^2.3.8",
"vinyl": "^2.1.0",
"webpack": "^4.7.0"
"vinyl": "^2.2.1"
},
"devDependencies": {
"gulp": "^3.9.0",
"rimraf": "^2.4.4",
"semistandard": "^12.0.1",
"tape": "^4.9.0",
"gulp": "^4.0.2",
"rimraf": "^3.0.2",
"semistandard": "^16.0.1",
"tape": "^5.3.1",
"vinyl-fs": "^3.0.2",
"vinyl-named": "^1.1.0"
"vinyl-named": "^1.1.0",
"webpack": "^5.21.2"
},
"keywords": ["gulpplugin", "webpack", "stream"]
"peerDependencies": {
"webpack": "^5.21.2"
},
"keywords": [
"gulpplugin",
"webpack",
"stream"
]
}

0 comments on commit 3fc84f0

Please sign in to comment.