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
30 changes: 30 additions & 0 deletions bin/ErrorHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copy link
Member

Choose a reason for hiding this comment

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

Seem to be weird to have errorHelpers and ErrorHelpers.

Copy link
Member Author

Choose a reason for hiding this comment

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

it should be errorHelpers right?

MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";

const webpackOptionsFlag = "WEBPACK_OPTIONS";

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

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

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

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.cutOffWebpackOptions(stack);
stack = exports.cutOffMultilineMessage(stack, message);
return stack;
};
18 changes: 10 additions & 8 deletions bin/convert-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ module.exports = function(yargs, argv, convertOptions) {
defineObject = {};
},
function() {
var DefinePlugin = require("webpack/lib/DefinePlugin");
var DefinePlugin = require("webpack").DefinePlugin;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you mind to change vars into const and let where possible? We already have a PR that aims to this kind of refactor, this change could avoid some conflicts

Copy link
Member

Choose a reason for hiding this comment

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

Rebase from master first

Copy link
Member Author

Choose a reason for hiding this comment

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

good point both of you 👍 rebased and changed

addPlugin(options, new DefinePlugin(defineObject));
}
);
Expand Down Expand Up @@ -463,12 +463,13 @@ module.exports = function(yargs, argv, convertOptions) {
mapArgToBoolean("cache");

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

ifBooleanArg("debug", function() {
var LoaderOptionsPlugin = require("webpack/lib/LoaderOptionsPlugin");
var LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
addPlugin(
options,
new LoaderOptionsPlugin({
Expand Down Expand Up @@ -504,7 +505,8 @@ module.exports = function(yargs, argv, convertOptions) {
});

ifArg("optimize-max-chunks", function(value) {
var LimitChunkCountPlugin = require("webpack/lib/optimize/LimitChunkCountPlugin");
var LimitChunkCountPlugin = require("webpack").optimize
.LimitChunkCountPlugin;
addPlugin(
options,
new LimitChunkCountPlugin({
Expand All @@ -514,7 +516,7 @@ module.exports = function(yargs, argv, convertOptions) {
});

ifArg("optimize-min-chunk-size", function(value) {
var MinChunkSizePlugin = require("webpack/lib/optimize/MinChunkSizePlugin");
var MinChunkSizePlugin = require("webpack").optimize.MinChunkSizePlugin;
addPlugin(
options,
new MinChunkSizePlugin({
Expand All @@ -524,7 +526,7 @@ module.exports = function(yargs, argv, convertOptions) {
});

ifBooleanArg("optimize-minimize", function() {
var LoaderOptionsPlugin = require("webpack/lib/LoaderOptionsPlugin");
var LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
addPlugin(
options,
new LoaderOptionsPlugin({
Expand All @@ -534,7 +536,7 @@ module.exports = function(yargs, argv, convertOptions) {
});

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

Expand All @@ -547,7 +549,7 @@ module.exports = function(yargs, argv, convertOptions) {
} else {
name = value;
}
var ProvidePlugin = require("webpack/lib/ProvidePlugin");
var ProvidePlugin = require("webpack").ProvidePlugin;
addPlugin(options, new ProvidePlugin(name, value));
});

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].indexOf(flag) >= 0) 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.indexOf(message[idx]) < 0 ? acc.concat(line) : acc,
[]
)
.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) {
var firstOptions = Array.isArray(options) ? options[0] || {} : options;

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

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

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

Error.stackTraceLimit = 30;
var lastHash = null;
var compiler;
try {
compiler = webpack(options);
} catch (e) {
var WebpackOptionsValidationError = require("webpack/lib/WebpackOptionsValidationError");
var 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) {
var ProgressPlugin = require("webpack/lib/ProgressPlugin");
var ProgressPlugin = require("webpack").ProgressPlugin;
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand Down
18 changes: 10 additions & 8 deletions bin/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@
type: "string",
default: "info",
group: DISPLAY_GROUP,
describe: "Controls the output of lifecycle messaging e.g. Started watching files... (verbose, info, none)"
describe:
"Controls the output of lifecycle messaging e.g. Started watching files... (verbose, info, none)"
}
});

Expand Down Expand Up @@ -270,8 +271,7 @@
}

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

var outputOptions = options.stats;
if (
Expand Down Expand Up @@ -407,11 +407,13 @@

ifArg("info-verbosity", function(value) {
if (!["none", "info", "verbose"].includes(value))
throw new Error("Invalid configuration object. \n configuration['info-verbosity'] should be one of these:\n \"none\" | \"info\" | \"verbose\"");
throw new Error(
"Invalid configuration object. \n configuration['info-verbosity'] should be one of these:\n \"none\" | \"info\" | \"verbose\""
);
outputOptions.infoVerbosity = value;
});

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

var lastHash = null;
var compiler;
Expand All @@ -432,7 +434,7 @@
}

if (argv.progress) {
var ProgressPlugin = require("webpack/lib/ProgressPlugin");
var ProgressPlugin = require("webpack").ProcessPlugin;
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand All @@ -441,10 +443,10 @@
}

if (outputOptions.infoVerbosity === "verbose") {
compiler.hooks.beforeCompile.tap("WebpackInfo", (compilation) => {
compiler.hooks.beforeCompile.tap("WebpackInfo", compilation => {
Copy link
Member

Choose a reason for hiding this comment

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

I think maybe this is already commited in master

Copy link
Member Author

Choose a reason for hiding this comment

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

addressed.

console.log("\nCompilation starting…\n");
});
compiler.hooks.afterCompile.tap("WebpackInfo", (compilation) => {
compiler.hooks.afterCompile.tap("WebpackInfo", compilation => {
console.log("\nCompilation finished\n");
});
}
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
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"
}
}
4 changes: 2 additions & 2 deletions test/binCases/config-name/found-many/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[7]).toContain("./index2.js");
expect(stdout[13]).toContain("./index3.js");
expect(stdout[8]).toContain("./index2.js");
expect(stdout[15]).toContain("./index3.js");
expect(stderr).toHaveLength(0);
};
2 changes: 1 addition & 1 deletion test/binCases/config-name/found-one/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());

expect(stdout[5]).toContain("./index2.js");
expect(stdout[6]).toContain("./index2.js");
expect(stderr).toHaveLength(0);
};
2 changes: 1 addition & 1 deletion test/binCases/configFile/plugins-presedence/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[5]).toContain("ok.js");
expect(stdout[6]).toContain("ok.js");
expect(stderr).toHaveLength(0);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var DefinePlugin = require("webpack/lib/DefinePlugin");
var DefinePlugin = require("webpack").DefinePlugin;
var path = require("path");

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions test/binCases/configFile/profile/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());

expect(stdout[6]).toContain("factory:");
expect(stdout[8]).toContain("factory:");
expect(stdout[10]).toContain("factory:");
expect(stdout[7]).toContain("factory:");
expect(stdout[9]).toContain("factory:");
expect(stdout[11]).toContain("factory:");
expect(stderr).toHaveLength(0);
};
6 changes: 3 additions & 3 deletions test/binCases/entry/multi-file/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[4]).toContain("null.js");
expect(stdout[5]).toMatch(/a\.js.*\{0\}/);
expect(stdout[6]).toMatch(/index\.js.*\{0\}/);
expect(stdout[7]).toMatch(/multi.*index\.js.*a\.js/); // should have multi-file entry
expect(stdout[6]).toMatch(/a\.js.*\{0\}/);
expect(stdout[7]).toMatch(/index\.js.*\{0\}/);
expect(stdout[8]).toMatch(/multi.*index\.js.*a\.js/); // should have multi-file entry
expect(stderr).toHaveLength(0);
};
6 changes: 3 additions & 3 deletions test/binCases/entry/named-entry/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[4]).toContain("null.js");
expect(stdout[5]).toContain("foo.js"); // named entry from --entry foo=./a.js
expect(stdout[6]).toMatch(/index\.js.*\{0\}/);
expect(stdout[7]).toMatch(/a\.js.*\{1\}/);
expect(stdout[6]).toContain("foo.js"); // named entry from --entry foo=./a.js
expect(stdout[8]).toMatch(/index\.js.*\{0\}/);
expect(stdout[9]).toMatch(/a\.js.*\{1\}/);
expect(stderr).toHaveLength(0);
};
6 changes: 3 additions & 3 deletions test/binCases/entry/non-hyphenated-args/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[4]).toContain("main.js"); // non-hyphenated arg ./a.js should create chunk "main"
expect(stdout[5]).toContain("null.js");
expect(stdout[6]).toMatch(/a\.js.*\{0\}/); // a.js should be in chunk 0
expect(stdout[7]).toMatch(/index\.js.*\{1\}/); // index.js should be in chunk 1
expect(stdout[6]).toContain("null.js");
expect(stdout[8]).toMatch(/a\.js.*\{0\}/); // a.js should be in chunk 0
expect(stdout[9]).toMatch(/index\.js.*\{1\}/); // index.js should be in chunk 1
expect(stderr).toHaveLength(0);
};
4 changes: 2 additions & 2 deletions test/binCases/env/complex/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(stdout[8]).toContain("\"baz\": true");
expect(stdout[9]).toContain("}");
expect(stdout[11]).toContain("null.js");
expect(stdout[12]).toContain("./index.js");
expect(stdout[12]).toContain("[built]");
expect(stdout[13]).toContain("./index.js");
expect(stdout[13]).toContain("[built]");
expect(stderr).toHaveLength(0);
};
2 changes: 1 addition & 1 deletion test/binCases/env/string/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Time: ");
expect(stdout[3]).toContain("Environment (--env): \"foo\"");
expect(stdout[5]).toContain("null.js");
expect(stdout[6]).toContain("null.js");
expect(stderr).toHaveLength(0);
};
2 changes: 1 addition & 1 deletion test/binCases/errors/issue-5576/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function testAssertions(code, stdout, stderr) {
expect(stdout[0]).toContain("Hash: ");
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Time: ");
expect(stdout[4]).toContain("bundle.js");
expect(stdout[5]).toContain("bundle.js");

expect(stderr).toHaveLength(0);
};
Loading