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
70 changes: 36 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/lib/DefinePlugin");
const DefinePlugin = require("webpack").DefinePlugin;
addPlugin(options, new DefinePlugin(defineObject));
}
);
Expand Down Expand Up @@ -476,12 +476,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 +518,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 +529,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 +539,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 +549,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 +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 @@ -591,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
37 changes: 5 additions & 32 deletions bin/errorHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,27 @@
*/
"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.cutOffByFlag(stack, webpackOptionsFlag);
exports.cutOffWebpackOptions = (stack) => exports.cutOffByFlag(stack, webpackOptionsFlag);
Copy link
Member

Choose a reason for hiding this comment

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

Could you tell me what these things are doing?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ev1stensberg the file name change is slightly confused things here. I guess, this was the old errorhelpers content, I think I have to take the latest from the bin/errorhelpers.js right ?

correct me if I am wrong

Copy link
Member

Choose a reason for hiding this comment

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

yep, that would be prefered, thought you did that, so that's why I'm asking

Copy link
Member

Choose a reason for hiding this comment

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

Where did the old errorHelper come from? Tobias? Maybe he improved/fixed some things there

Copy link
Member Author

Choose a reason for hiding this comment

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

yup, the old errorhelper was from Tobias, but the current master errorHelper was having all the methods what was there and new methods were added on top of it. So I decided to have this version.

Copy link
Member

Choose a reason for hiding this comment

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

So if I understand: You've looked at Tobias's PR, that is where this implementation of ErrorHelper is from?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah... But when I merged with the master I saw yours (current master) which has more methods.

Tobias PR :
cf82edb

Copy link
Member

Choose a reason for hiding this comment

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

Keep what Tobias refactored, see if there's any old calls using the old code in the CLI, I'll re-review after


exports.cutOffMultilineMessage = (stack, message) => {
stack = stack.split("\n");
message = message.split("\n");

return stack
.reduce(
(acc, line, idx) =>
line.includes(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;
return stack.reduce((acc, line, idx) => line === message[idx] || line === `Error: ${message[idx]}` ? acc : acc.concat(line), []).join("\n");
};

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

Expand Down Expand Up @@ -127,15 +127,16 @@ 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 +149,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
7 changes: 3 additions & 4 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 @@ -437,7 +436,7 @@
}

if (argv.progress) {
const ProgressPlugin = require("webpack/lib/ProgressPlugin");
const ProgressPlugin = require("webpack").ProcessPlugin;
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand Down
9 changes: 4 additions & 5 deletions lib/commands/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const inquirer = require("inquirer");
const PLazy = require("p-lazy");
const Listr = require("listr");

const validateSchema = require("webpack/lib/validateSchema");
const WebpackOptionsValidationError = require("webpack/lib/WebpackOptionsValidationError");
const webpackOptionsSchema = require("webpack/schemas/WebpackOptions.json");
const validate = require("webpack").validate;
const WebpackOptionsValidationError = require("webpack")
.WebpackOptionsValidationError;

const runPrettier = require("../utils/run-prettier");

Expand Down Expand Up @@ -121,8 +121,7 @@ module.exports = function migrate(
});

if (answer["confirmValidation"]) {
const webpackOptionsValidationErrors = validateSchema(
webpackOptionsSchema,
const webpackOptionsValidationErrors = validate(
require(outputConfigPath)
);
if (webpackOptionsValidationErrors.length) {
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -100,7 +100,7 @@
"nyc": "^11.4.1",
"prettier-eslint-cli": "^4.6.1",
"schema-utils": "^0.4.2",
"webpack": "^4.0.0-alpha.3",
"webpack": "^4.0.0-beta.0",
"webpack-dev-server": "^2.9.7"
}
}
Loading