Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve webpack dependencies #251

Merged
68 changes: 34 additions & 34 deletions bin/convert-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ module.exports = function(...args) {
argv["output-path"] = path.dirname(output);
}

var configFileLoaded = false;
var configFiles = [];
var extensions = Object.keys(interpret.extensions).sort(function(a, b) {
let configFileLoaded = false;
let configFiles = [];
const extensions = Object.keys(interpret.extensions).sort(function(a, b) {
return a === ".js" ? -1 : b === ".js" ? 1 : a.length - b.length;
});
var defaultConfigFiles = ["webpack.config", "webpackfile"]
const defaultConfigFiles = ["webpack.config", "webpackfile"]
.map(function(filename) {
return extensions.map(function(ext) {
return {
Expand Down Expand Up @@ -98,14 +98,14 @@ module.exports = function(...args) {
}
}
if (configFiles.length > 0) {
var registerCompiler = function registerCompiler(moduleDescriptor) {
const registerCompiler = function registerCompiler(moduleDescriptor) {
if (moduleDescriptor) {
if (typeof moduleDescriptor === "string") {
require(moduleDescriptor);
} else if (!Array.isArray(moduleDescriptor)) {
moduleDescriptor.register(require(moduleDescriptor.module));
} else {
for (var i = 0; i < moduleDescriptor.length; i++) {
for (let i = 0; i < moduleDescriptor.length; i++) {
try {
registerCompiler(moduleDescriptor[i]);
break;
Expand All @@ -117,8 +117,8 @@ module.exports = function(...args) {
}
};

var requireConfig = function requireConfig(configPath) {
var options = (function WEBPACK_OPTIONS() {
const requireConfig = function requireConfig(configPath) {
let options = (function WEBPACK_OPTIONS() {
if (argv.configRegister && argv.configRegister.length) {
module.paths.unshift(
path.resolve(process.cwd(), "node_modules"),
Expand Down Expand Up @@ -152,12 +152,12 @@ module.exports = function(...args) {
}

function processConfiguredOptions(options) {
var webpackConfigurationValidationErrors = validateSchema(
const webpackConfigurationValidationErrors = validateSchema(
webpackConfigurationSchema,
options
);
if (webpackConfigurationValidationErrors.length) {
var error = new WebpackOptionsValidationError(
const error = new WebpackOptionsValidationError(
webpackConfigurationValidationErrors
);
console.error(
Expand All @@ -179,7 +179,7 @@ module.exports = function(...args) {

// filter multi-config by name
if (Array.isArray(options) && argv["config-name"]) {
var namedOptions = options.filter(function(opt) {
const namedOptions = options.filter(function(opt) {
return opt.name === argv["config-name"];
});
if (namedOptions.length === 0) {
Expand Down Expand Up @@ -257,7 +257,7 @@ module.exports = function(...args) {
ifArg(
name,
function(content, idx) {
var i = content.indexOf("=");
const i = content.indexOf("=");
if (i < 0) {
return fn(null, content, idx);
} else {
Expand Down Expand Up @@ -285,10 +285,10 @@ module.exports = function(...args) {
}

function loadPlugin(name) {
var loadUtils = require("loader-utils");
var args;
const loadUtils = require("loader-utils");
let args;
try {
var p = name && name.indexOf("?");
const p = name && name.indexOf("?");
if (p > -1) {
args = loadUtils.parseQuery(name.substring(p));
name = name.substring(0, p);
Expand All @@ -298,15 +298,15 @@ module.exports = function(...args) {
process.exit(-1); // eslint-disable-line
}

var path;
let path;
try {
var resolve = require("enhanced-resolve");
const resolve = require("enhanced-resolve");
path = resolve.sync(process.cwd(), name);
} catch (e) {
console.log("Cannot resolve plugin " + name + ".");
process.exit(-1); // eslint-disable-line
}
var Plugin;
let Plugin;
try {
Plugin = require(path);
} catch (e) {
Expand Down Expand Up @@ -367,7 +367,7 @@ module.exports = function(...args) {
name = binding;
binding += "-loader";
}
var rule = {
const rule = {
test: new RegExp(
"\\." +
name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") +
Expand All @@ -392,7 +392,7 @@ module.exports = function(...args) {
bindRules("module-bind-pre");
bindRules("module-bind-post");

var defineObject;
let defineObject;
ifArgPair(
"define",
function(name, value) {
Expand All @@ -406,7 +406,7 @@ module.exports = function(...args) {
defineObject = {};
},
function() {
var DefinePlugin = require("webpack").DefinePlugin;
const DefinePlugin = require("webpack").DefinePlugin;
addPlugin(options, new DefinePlugin(defineObject));
}
);
Expand Down Expand Up @@ -476,13 +476,13 @@ module.exports = function(...args) {
mapArgToBoolean("cache");

ifBooleanArg("hot", function() {
var HotModuleReplacementPlugin = require("webpack")
const HotModuleReplacementPlugin = require("webpack")
.HotModuleReplacementPlugin;
addPlugin(options, new HotModuleReplacementPlugin());
});

ifBooleanArg("debug", function() {
var LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
const LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
addPlugin(
options,
new LoaderOptionsPlugin({
Expand Down Expand Up @@ -518,7 +518,7 @@ module.exports = function(...args) {
});

ifArg("optimize-max-chunks", function(value) {
var LimitChunkCountPlugin = require("webpack").optimize
const LimitChunkCountPlugin = require("webpack").optimize
.LimitChunkCountPlugin;
addPlugin(
options,
Expand All @@ -529,7 +529,7 @@ module.exports = function(...args) {
});

ifArg("optimize-min-chunk-size", function(value) {
var MinChunkSizePlugin = require("webpack").optimize.MinChunkSizePlugin;
const MinChunkSizePlugin = require("webpack").optimize.MinChunkSizePlugin;
addPlugin(
options,
new MinChunkSizePlugin({
Expand All @@ -539,7 +539,7 @@ module.exports = function(...args) {
});

ifBooleanArg("optimize-minimize", function() {
var LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
const LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
addPlugin(
options,
new LoaderOptionsPlugin({
Expand All @@ -549,20 +549,20 @@ module.exports = function(...args) {
});

ifArg("prefetch", function(request) {
var PrefetchPlugin = require("webpack").PrefetchPlugin;
const PrefetchPlugin = require("webpack").PrefetchPlugin;
addPlugin(options, new PrefetchPlugin(request));
});

ifArg("provide", function(value) {
var idx = value.indexOf("=");
var name;
const idx = value.indexOf("=");
let name;
if (idx >= 0) {
name = value.substr(0, idx);
value = value.substr(idx + 1);
} else {
name = value;
}
var ProvidePlugin = require("webpack").ProvidePlugin;
const ProvidePlugin = require("webpack").ProvidePlugin;
addPlugin(options, new ProvidePlugin(name, value));
});

Expand All @@ -582,7 +582,7 @@ module.exports = function(...args) {
}
ensureObject(options, "entry");

var addTo = function addTo(name, entry) {
const addTo = function addTo(name, entry) {
if (options.entry[name]) {
if (!Array.isArray(options.entry[name])) {
options.entry[name] = [options.entry[name]];
Expand All @@ -593,10 +593,10 @@ module.exports = function(...args) {
}
};
argv._.forEach(function(content) {
var i = content.indexOf("=");
var j = content.indexOf("?");
const i = content.indexOf("=");
const j = content.indexOf("?");
if (i < 0 || (j >= 0 && j < i)) {
var resolved = path.resolve(content);
const resolved = path.resolve(content);
if (fs.existsSync(resolved)) {
addTo(
"main",
Expand Down
14 changes: 5 additions & 9 deletions bin/process-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ module.exports = function processOptions(yargs, argv) {
const firstOptions = Array.isArray(options) ? options[0] || {} : options;

if (typeof options.stats === "boolean" || typeof options.stats === "string") {
const statsPresetToOptions = require("webpack/lib/Stats.js").presetToOptions;
var statsPresetToOptions = require("webpack").Stats.presetToOptions;
const statsPresetToOptions = require("webpack").Stats.presetToOptions;
options.stats = statsPresetToOptions(options.stats);
}

Expand Down Expand Up @@ -128,18 +127,16 @@ module.exports = function processOptions(yargs, argv) {
}
});

const webpack = require("webpack/lib/webpack.js");
var webpack = require("webpack");
const webpack = require("webpack");

Error.stackTraceLimit = 30;
let lastHash = null;
let compiler;
try {
compiler = webpack(options);
} catch (e) {
const WebpackOptionsValidationError = require("webpack/lib/WebpackOptionsValidationError");
var WebpackOptionsValidationError = require("webpack")
.WebpackOptionsValidationError;
const WebpackOptionsValidationError = require("webpack").WebpackOptionsValidationError;

if (e instanceof WebpackOptionsValidationError) {
if (argv.color)
console.error(
Expand All @@ -152,8 +149,7 @@ module.exports = function processOptions(yargs, argv) {
}

if (argv.progress) {
const ProgressPlugin = require("webpack/lib/ProgressPlugin");
var ProgressPlugin = require("webpack").ProgressPlugin;
const ProgressPlugin = require("webpack").ProgressPlugin;
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand Down
11 changes: 3 additions & 8 deletions bin/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@
}

const firstOptions = [].concat(options)[0];
const statsPresetToOptions = require("webpack/lib/Stats.js")
.presetToOptions;
var firstOptions = [].concat(options)[0];
var statsPresetToOptions = require("webpack").Stats.presetToOptions;
const statsPresetToOptions = require("webpack").Stats.presetToOptions;

let outputOptions = options.stats;
if (
Expand Down Expand Up @@ -417,8 +414,7 @@
outputOptions.infoVerbosity = value;
});

const webpack = require("webpack/lib/webpack.js");
var webpack = require("webpack");
const webpack = require("webpack");

let lastHash = null;
let compiler;
Expand All @@ -440,8 +436,7 @@
}

if (argv.progress) {
const ProgressPlugin = require("webpack/lib/ProgressPlugin");
var ProgressPlugin = require("webpack").ProcessPlugin;
const ProgressPlugin = require("webpack").ProcessPlugin;
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"precommit": "lint-staged",
"prepare": "yarn format",
"pretest": "yarn lint",
"test": "nyc jest",
"test": "nyc jest test/BinTestCases.test.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was there a need for this change?

"reportCoverage": "nyc report --reporter=json && codecov -f coverage/coverage-final.json --disable=gcov",
"jsdoc": "jsdoc -c jsdoc.json -r -d docs",
"appveyor:prepare": "yarn prepare",
Expand Down
4 changes: 2 additions & 2 deletions test/binCases/config-type/array/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Child");
expect(stdout[6]).toContain("entry-a.bundle.js");
expect(stdout[8]).toContain("Child");
expect(stdout[12]).toContain("entry-b.bundle.js");
expect(stdout[9]).toContain("Child");
expect(stdout[13]).toContain("entry-b.bundle.js");
expect(stderr).toHaveLength(0);
};
Empty file.
13 changes: 13 additions & 0 deletions test/binCases/errors/clean-webpack-options/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I understand that testing it isolated doesn't make much sense, but having some test checking for correct error output is already better then nothing :)


module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(1);

expect(stdout).toHaveLength(0);

expect(stderr[0]).toContain("Invalid configuration object.");
expect(stderr[1]).toContain("configuration.context should be a string");
expect(stderr[2]).toContain("The base directory ");

expect(stderr).toHaveLength(4);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
context: [__dirname],
entry: "./index"
};