Skip to content

Commit

Permalink
Merge branch 'next' into ref/basic-group
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv authored Sep 30, 2020
2 parents 64ba031 + f7ec953 commit ac1ab7f
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 110 deletions.
3 changes: 1 addition & 2 deletions packages/generators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
"@webpack-cli/package-utils": "^1.0.1-rc.0",
"@webpack-cli/utils": "^1.0.1-rc.0",
"@webpack-cli/webpack-scaffold": "^1.0.1-rc.0",
"lodash": "4.17.19",
"colorette": "^1.2.1",
"log-symbols": "3.0.0",
"mkdirp": "1.0.3",
"yeoman-generator": "4.7.2"
},
"peerDependencies": {
"webpack": "4.x.x || 5.x.x",
"webpack-cli": "3.x.x || 4.x.x"
},
"devDependencies": {
"@types/lodash": "4.14.149",
"@types/mkdirp": "1.0.0",
"@types/yeoman-assert": "^3.1.1",
"@types/yeoman-generator": "3.1.4",
Expand Down
5 changes: 2 additions & 3 deletions packages/generators/src/loader-generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ from 'lodash';
import path from 'path';
import addonGenerator from './addon-generator';

import { toKebabCase } from './utils/helpers';
/**
* Formats a string into webpack loader format
* (eg: 'style-loader', 'raw-loader')
Expand All @@ -10,7 +9,7 @@ import addonGenerator from './addon-generator';
* @returns {string} The formatted string
*/
export function makeLoaderName(name: string): string {
name = _.kebabCase(name);
name = toKebabCase(name);
if (!/loader$/.test(name)) {
name += '-loader';
}
Expand Down
7 changes: 3 additions & 4 deletions packages/generators/src/plugin-generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ from 'lodash';
import path from 'path';
import addonGenerator from './addon-generator';

import { toKebabCase, toUpperCamelCase } from './utils/helpers';
/**
* A yeoman generator class for creating a webpack
* plugin project. It adds some starter plugin code
Expand All @@ -14,7 +13,7 @@ const PluginGenerator = addonGenerator(
[
{
default: 'my-webpack-plugin',
filter: _.kebabCase,
filter: toKebabCase,
message: 'Plugin name',
name: 'name',
type: 'input',
Expand All @@ -31,7 +30,7 @@ const PluginGenerator = addonGenerator(
'examples/simple/src/static-esm-module.js.tpl',
],
['src/_index.js.tpl', 'examples/simple/_webpack.config.js.tpl'],
(gen): object => ({ name: _.upperFirst(_.camelCase(gen.props.name)) }),
(gen): object => ({ name: toUpperCamelCase(gen.props.name) }),
);

export default PluginGenerator;
22 changes: 22 additions & 0 deletions packages/generators/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const regex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;

/**
* Convert str to kebab-case
* @param str input string
* @returns output string
*/
export function toKebabCase(str: string): string {
return str.match(regex).join('-').toLowerCase();
}

/**
* Convert str to UpperCamelCase
* @param str import string
* @returns {string} output string
*/
export function toUpperCamelCase(str: string): string {
return str
.match(regex)
.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
.join('');
}
4 changes: 4 additions & 0 deletions packages/init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
"jscodeshift": "0.7.0",
"p-each-series": "2.1.0"
},
"peerDependencies": {
"webpack": "4.x.x || 5.x.x",
"webpack-cli": "4.x.x"
},
"gitHead": "fb50f766851f500ca12867a2aa9de81fa6e368f9"
}
1 change: 0 additions & 1 deletion packages/migrate/__testfixtures__/failing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const webpack = require('webpack');
const nodeEnvironment = process.env.NODE_ENV;
const _ = require("lodash");

const config = {
entry: {
Expand Down
2 changes: 0 additions & 2 deletions packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"inquirer": "7.1.0",
"jscodeshift": "0.7.0",
"listr": "0.14.3",
"lodash": "4.17.19",
"p-each-series": "2.1.0",
"p-lazy": "3.0.0"
},
Expand All @@ -27,7 +26,6 @@
"@types/diff": "4.0.2",
"@types/inquirer": "6.5.0",
"@types/listr": "0.14.2",
"@types/lodash": "^4.14.149",
"webpack": "4.x.x"
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import isEmpty = require('lodash/isEmpty');

import { createOrUpdatePluginByName, findPluginsByName, safeTraverse } from '@webpack-cli/utils';

import { JSCodeshift, Node } from '../types/NodePath';
Expand All @@ -9,6 +7,8 @@ interface LoaderOptions {
minimize?: boolean;
}

const isEmpty = (obj: LoaderOptions): boolean => Object.keys(obj).length === 0;

/**
*
* Transform which adds context information for old loaders by injecting a `LoaderOptionsPlugin`
Expand Down
3 changes: 3 additions & 0 deletions packages/package-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"@types/cross-spawn": "6.0.1"
},
"peerDependenciesMeta": {
"webpack": {
"optional": true
},
"@webpack-cli/info": {
"optional": true
},
Expand Down
8 changes: 6 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
"got": "10.7.0",
"jscodeshift": "0.7.0",
"p-each-series": "2.1.0",
"prettier": "1.19.1",
"yeoman-environment": "2.8.1",
"yeoman-generator": "4.7.2"
},
"peerDependenciesMeta": {
"prettier": {
"optional": true
}
},
"peerDependencies": {
"webpack": "4.x.x || 5.x.x",
"webpack-cli": "4.x.x || 5.x.x"
"webpack-cli": "4.x.x"
},
"devDependencies": {
"@types/got": "9.6.9",
Expand Down
14 changes: 12 additions & 2 deletions packages/utils/src/run-prettier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'fs';
import prettier from 'prettier';
import logger from 'webpack-cli/lib/utils/logger';

/**
Expand All @@ -13,6 +12,18 @@ import logger from 'webpack-cli/lib/utils/logger';

export function runPrettier(outputPath: string, source: string): void {
let prettySource: string = source;

let prettier;
try {
// eslint-disable-next-line node/no-extraneous-require
prettier = require('prettier');
} catch (err) {
logger.warn(
"File is not properly formatted because you don't have prettier installed, you can either install it or format it manually",
);
return fs.writeFileSync(outputPath, source, 'utf8');
}

try {
prettySource = prettier.format(source, {
filepath: outputPath,
Expand All @@ -25,6 +36,5 @@ export function runPrettier(outputPath: string, source: string): void {
logger.warn(`\nWARNING: Could not apply prettier to ${outputPath} due to validation error, but the file has been created`);
prettySource = source;
}

return fs.writeFileSync(outputPath, prettySource, 'utf8');
}
15 changes: 0 additions & 15 deletions packages/webpack-cli/__tests__/StatsGroup.test.js

This file was deleted.

19 changes: 19 additions & 0 deletions packages/webpack-cli/__tests__/resolveStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const resolveStats = require('../lib/groups/resolveStats');

describe('StatsGroup', function () {
it('should assign json correctly', () => {
const result = resolveStats({
json: true,
});
expect(result.options.stats).toBeFalsy();
expect(result.outputOptions.json).toBeTruthy();
});

it('should assign stats correctly', () => {
const result = resolveStats({
stats: 'warning',
});
expect(result.options.stats).toEqual('warning');
expect(result.outputOptions.json).toBeFalsy();
});
});
15 changes: 12 additions & 3 deletions packages/webpack-cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
require('v8-compile-cache');
const importLocal = require('import-local');
const runCLI = require('../lib/bootstrap');
const { yellow } = require('colorette');
const { error } = require('../lib/utils/logger');
const { packageExists, promptInstallation } = require('@webpack-cli/package-utils');

// Prefer the local installation of webpack-cli
if (importLocal(__filename)) {
return;
}
process.title = 'webpack';

const [, , ...rawArgs] = process.argv;

runCLI(rawArgs);
if (packageExists('webpack')) {
const [, , ...rawArgs] = process.argv;
runCLI(rawArgs);
} else {
promptInstallation('webpack', () => {
error(`It looks like ${yellow('webpack')} is not installed.`);
});
return;
}
26 changes: 0 additions & 26 deletions packages/webpack-cli/lib/groups/StatsGroup.js

This file was deleted.

22 changes: 22 additions & 0 deletions packages/webpack-cli/lib/groups/resolveStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Resolve flags which deal with compilation stats
* @param {args} args - Parsed args passed to CLI
*/
const resolveStats = (args) => {
const { stats, json } = args;

const finalOptions = {
options: {},
outputOptions: {},
};

if (stats !== undefined) {
finalOptions.options.stats = stats;
}
if (json) {
finalOptions.outputOptions.json = true;
}
return finalOptions;
};

module.exports = resolveStats;
3 changes: 2 additions & 1 deletion packages/webpack-cli/lib/utils/Compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const webpack = require('webpack');
const { packageExists } = require('@webpack-cli/package-utils');
const webpack = packageExists('webpack') ? require('webpack') : undefined;
const logger = require('./logger');
const bailAndWatchWarning = require('./warnings/bailAndWatchWarning');
const { CompilerOutput } = require('./CompilerOutput');
Expand Down
16 changes: 2 additions & 14 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
const { cli } = require('webpack');
const { packageExists } = require('@webpack-cli/package-utils');
const cli = packageExists('webpack') ? require('webpack').cli : undefined;

const HELP_GROUP = 'help';
const CONFIG_GROUP = 'config';
const BASIC_GROUP = 'basic';
const OUTPUT_GROUP = 'output';
const ADVANCED_GROUP = 'advanced';
const DISPLAY_GROUP = 'stats';
const ZERO_CONFIG_GROUP = 'zero-config';

const groups = {
HELP_GROUP,
CONFIG_GROUP,
BASIC_GROUP,
OUTPUT_GROUP,
ADVANCED_GROUP,
DISPLAY_GROUP,
ZERO_CONFIG_GROUP,
};

const commands = [
Expand Down Expand Up @@ -90,7 +85,6 @@ const core = [
usage: '--config <path to webpack configuration file>',
alias: 'c',
type: String,
group: CONFIG_GROUP,
multiple: true,
description: 'Provide path to a webpack configuration file e.g. ./webpack.config.js',
link: 'https://webpack.js.org/configuration/',
Expand All @@ -99,7 +93,6 @@ const core = [
name: 'color',
usage: '--color',
type: Boolean,
group: DISPLAY_GROUP,
negative: true,
defaultValue: true,
description: 'Enable/Disable colors on console',
Expand All @@ -109,7 +102,6 @@ const core = [
usage: '--merge',
alias: 'm',
type: Boolean,
group: CONFIG_GROUP,
description: 'Merge two or more configurations using webpack-merge e.g. -c ./webpack.config.js -c ./webpack.test.config.js --merge',
link: 'https://github.com/survivejs/webpack-merge',
},
Expand Down Expand Up @@ -189,7 +181,6 @@ const core = [
type: Boolean,
alias: 'j',
description: 'Prints result as JSON',
group: DISPLAY_GROUP,
},
{
name: 'mode',
Expand All @@ -210,7 +201,6 @@ const core = [
name: 'stats',
usage: '--stats <value>',
type: [String, Boolean],
group: DISPLAY_GROUP,
negative: true,
description: 'It instructs webpack on how to treat the stats e.g. verbose',
link: 'https://webpack.js.org/configuration/stats/#stats',
Expand All @@ -220,7 +210,6 @@ const core = [
usage: '--env',
type: String,
multiple: true,
group: CONFIG_GROUP,
description: 'Environment passed to the configuration when it is a function',
},
{
Expand All @@ -235,7 +224,6 @@ const core = [
usage: '--config-name <name of config>',
type: String,
multiple: true,
group: CONFIG_GROUP,
description: 'Name of the configuration to use',
},
/* {
Expand Down
Loading

0 comments on commit ac1ab7f

Please sign in to comment.