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
73 changes: 38 additions & 35 deletions bin/convert-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const interpret = require("interpret");
const prepareOptions = require("./prepareOptions");
const webpackConfigurationSchema = require("../schemas/webpackConfigurationSchema.json");
const validateSchema = require("webpack").validateSchema;
const WebpackOptionsValidationError = require("webpack").WebpackOptionsValidationError;
const WebpackOptionsValidationError = require("webpack")
.WebpackOptionsValidationError;

module.exports = function(...args) {
const argv = args[1] || args[0];
Expand Down Expand Up @@ -40,12 +41,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 +99,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 +118,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 +153,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 +180,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 +258,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 +286,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 +299,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 +368,7 @@ module.exports = function(...args) {
name = binding;
binding += "-loader";
}
var rule = {
const rule = {
test: new RegExp(
"\\." +
name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") +
Expand All @@ -392,7 +393,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 +407,7 @@ module.exports = function(...args) {
defineObject = {};
},
function() {
var DefinePlugin = require("webpack/lib/DefinePlugin");
const DefinePlugin = require("webpack").DefinePlugin;
addPlugin(options, new DefinePlugin(defineObject));
}
);
Expand Down Expand Up @@ -476,12 +477,13 @@ module.exports = function(...args) {
mapArgToBoolean("cache");

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

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

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

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

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

ifArg("prefetch", function(request) {
var PrefetchPlugin = require("webpack/lib/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/lib/ProvidePlugin");
const ProvidePlugin = require("webpack").ProvidePlugin;
addPlugin(options, new ProvidePlugin(name, value));
});

Expand All @@ -580,7 +583,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 @@ -591,10 +594,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
31 changes: 6 additions & 25 deletions bin/errorHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@
*/
"use strict";

const loaderFlag = "LOADER_EXECUTION";

const webpackOptionsFlag = "WEBPACK_OPTIONS";

exports.cutOffByFlag = (stack, flag) => {
stack = stack.split("\n");
for (let i = 0; i < stack.length; i++)
if (stack[i].includes(flag)) stack.length = i;
if (stack[i].indexOf(flag) >= 0) stack.length = i;
return stack.join("\n");
};

exports.cutOffLoaderExecution = stack =>
exports.cutOffByFlag(stack, loaderFlag);

exports.cutOffWebpackOptinos = stack =>
exports.cutOffWebpackOptions = stack =>
exports.cutOffByFlag(stack, webpackOptionsFlag);

exports.cutOffMultilineMessage = (stack, message) => {
Expand All @@ -28,30 +23,16 @@ exports.cutOffMultilineMessage = (stack, message) => {
return stack
.reduce(
(acc, line, idx) =>
line.includes(message[idx]) ? acc : acc.concat(line),
line === message[idx] || line === `Error: ${message[idx]}`
? acc
: acc.concat(line),
[]
)
.join("\n");
};

exports.cutOffMessage = (stack, message) => {
const nextLine = stack.indexOf("\n");
if (nextLine === -1) {
return stack === message ? "" : stack;
} else {
const firstLine = stack.substr(0, nextLine);
return firstLine === message ? stack.substr(nextLine + 1) : stack;
}
};

exports.cleanUp = (stack, message) => {
stack = exports.cutOffLoaderExecution(stack);
stack = exports.cutOffMessage(stack, message);
return stack;
};

exports.cleanUpWebpackOptions = (stack, message) => {
stack = exports.cutOffWebpackOptinos(stack);
stack = exports.cutOffWebpackOptions(stack);
stack = exports.cutOffMultilineMessage(stack, message);
return stack;
};
14 changes: 9 additions & 5 deletions bin/process-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ 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;
const statsPresetToOptions = require("webpack").Stats.presetToOptions;
options.stats = statsPresetToOptions(options.stats);
}

const outputOptions = Object.create(options.stats || firstOptions.stats || {});
const outputOptions = Object.create(
options.stats || firstOptions.stats || {}
);
if (typeof outputOptions.context === "undefined")
outputOptions.context = firstOptions.context;

Expand Down Expand Up @@ -127,15 +129,17 @@ module.exports = function processOptions(yargs, argv) {
}
});

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

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

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

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

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

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

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

let lastHash = null;
let compiler;
Expand All @@ -427,8 +426,7 @@
console.error(
`\u001b[1m\u001b[31m${err.message}\u001b[39m\u001b[22m`
);
else
console.error(err.message);
else console.error(err.message);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
Expand All @@ -437,7 +435,7 @@
}

if (argv.progress) {
const ProgressPlugin = require("webpack/lib/ProgressPlugin");
const ProgressPlugin = require("webpack").ProcessPlugin;
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand Down
Loading