Skip to content

Commit

Permalink
feat(webpack-cli): add no-mode flag
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Mar 1, 2020
1 parent c0f6f01 commit 629b16f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 27 deletions.
3 changes: 2 additions & 1 deletion packages/webpack-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Available Commands
Options
--entry string The entry point of your application.
-c, --config string Provide path to a webpack configuration file
-c, --config string Provide path to a webpack configuration file
-m, --merge string Merge a configuration file using webpack-merge
--progress Print compilation progress during build
--silent Disable any output that webpack makes
Expand All @@ -55,6 +55,7 @@ Options
-d, --dev Run development build
-p, --prod Run production build
--mode string Defines the mode to pass to webpack
--no-mode Sets mode="none" which disables any default behavior
--version Get current version
--node-args string[] NodeJS flags
```
Expand Down
20 changes: 16 additions & 4 deletions packages/webpack-cli/lib/groups/ZeroConfigGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { logger } = require('@webpack-cli/logger');

const PRODUCTION = 'production';
const DEVELOPMENT = 'development';
const NONE = 'none';
/**
* ZeroConfigGroup creates a zero configuration based on the environment
*/
Expand All @@ -19,13 +20,22 @@ class ZeroConfigGroup extends GroupHelper {
if (process.env.NODE_ENV && (process.env.NODE_ENV === PRODUCTION || process.env.NODE_ENV === DEVELOPMENT)) {
return process.env.NODE_ENV;
} else {
if (this.args.mode && (this.args.dev || this.args.prod)) {
if ((this.args.mode || this.args.noMode) && (this.args.dev || this.args.prod)) {
logger.warn(
`You provided both mode and ${
`You provided both ${this.args.mode ? 'mode' : 'no-mode'} and ${
this.args.prod ? '--prod' : '--dev'
} arguments. You should provide just one. "mode" will be used`,
} arguments. You should provide just one. "${this.args.mode ? 'mode' : 'no-mode'}" will be used`,
);
return this.args.mode;
if (this.args.mode){
return this.args.mode ;
} else {
return NONE ;
}
}
if (this.args.noMode && this.args.mode) {
logger.warn(
'You Provided both mode and no-mode arguments. You Should Provide just one. "mode" will be used.'
)
}
if (this.args.mode) {
return this.args.mode;
Expand All @@ -34,6 +44,8 @@ class ZeroConfigGroup extends GroupHelper {
return PRODUCTION;
} else if (this.args.dev) {
return DEVELOPMENT;
} else if (this.args.noMode) {
return NONE;
}
return PRODUCTION;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ module.exports = {
link: 'https://webpack.js.org/concepts/#mode',
acceptedValues: ["development", "production"]
},
{
name: 'no-mode',
usage: '--no-mode',
type: Boolean,
group: ZERO_CONFIG_GROUP,
description: 'Sets mode="none" which disables any default behavior',
link: 'https://webpack.js.org/concepts/#mode',
},
{
name: 'version',
usage: '--version',
Expand Down
45 changes: 23 additions & 22 deletions test/help/__snapshots__/help-single-arg.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,29 @@ Available Commands
Options
--entry string The entry point of your application.
-c, --config string Provide path to a webpack configuration file
-m, --merge string Merge a configuration file using webpack-merge
--progress Print compilation progress during build
--silent Disable any output that webpack makes
--help Outputs list of supported flags
--defaults Allow webpack to set defaults aggresively
-o, --output string Output location of the file generated by webpack
--plugin string Load a given plugin
-g, --global string[] Declares and exposes a global variable
-t, --target string Sets the build target
-w, --watch Watch for files changes
-h, --hot Enables Hot Module Replacement
-s, --sourcemap string Determine source maps to use
--prefetch string Prefetch this request
-j, --json Prints result as JSON
--standard Prints standard output
-d, --dev Run development build
-p, --prod Run production build
--mode string Defines the mode to pass to webpack
--version Get current version
--node-args string[] NodeJS flags
--entry string The entry point of your application.
-c, --config string Provide path to a webpack configuration file
-m, --merge string Merge a configuration file using webpack-merge
--progress Print compilation progress during build
--silent Disable any output that webpack makes
--help Outputs list of supported flags
--defaults Allow webpack to set defaults aggresively
-o, --output string Output location of the file generated by webpack
--plugin string Load a given plugin
-g, --global string[] Declares and exposes a global variable
-t, --target string Sets the build target
-w, --watch Watch for files changes
-h, --hot Enables Hot Module Replacement
-s, --sourcemap string Determine source maps to use
--prefetch string Prefetch this request
-j, --json Prints result as JSON
--standard Prints standard output
-d, --dev Run development build
-p, --prod Run production build
--mode string Defines the mode to pass to webpack
--no-mode Sets mode="none" which disables any default behavior
--version Get current version
--node-args string[] NodeJS flags
Made with ♥️ by the webpack team "
`;
64 changes: 64 additions & 0 deletions test/no-mode/no-mode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';
const { run } = require('../utils/test-utils');
const { stat } = require('fs');
const { resolve } = require('path');
describe('no-mode flag', () => {
it('should load a development config when --no-mode is passed', done => {
const { stderr, stdout } = run(__dirname, ['--no-mode']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should load a development config when --no-mode and --dev are passed', done => {
const { stderr, stdout } = run(__dirname, ['--no-mode', '--dev']);
expect(stderr).toContain('"no-mode" will be used');
expect(stdout).toBeTruthy();

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should load a development config when --no-mode and --prod are passed', done => {
const { stderr, stdout } = run(__dirname, ['--no-mode', '--prod']);
expect(stderr).toContain('"no-mode" will be used');
expect(stdout).toBeTruthy();

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should load a production config when --mode=production & --no-mode are passed', done => {
const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--no-mode']);
expect(stderr).toContain('"mode" will be used');
expect(stdout).toBeTruthy();

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should load a development config when --mode=development and --no-mode are passed', done => {
const { stderr, stdout } = run(__dirname, ['--mode', 'development', '--no-mode']);
expect(stderr).toContain('"mode" will be used');
expect(stdout).toBeTruthy();

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
1 change: 1 addition & 0 deletions test/no-mode/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Test');

0 comments on commit 629b16f

Please sign in to comment.