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(cli): fix file resolution inside group helper #1221

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/groups/ZeroConfigGroup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const GroupHelper = require('../utils/GroupHelper');

const PRODUCTION = 'production';
const DEVELOPMENT = 'development';
/**
* ZeroConfigGroup creates a zero configuration based on the environment
*/
Expand All @@ -13,25 +15,30 @@ class ZeroConfigGroup extends GroupHelper {
* @returns {string} The mode
*/
getEnvFromOptionsAndMode() {
if (process.env.NODE_ENV && (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'development')) {
if (process.env.NODE_ENV && (process.env.NODE_ENV === PRODUCTION || process.env.NODE_ENV === DEVELOPMENT)) {
return process.env.NODE_ENV;
} else if (this.args.prod) {
return 'production';
return PRODUCTION;
} else if (this.args.dev) {
return 'development';
return DEVELOPMENT;
}
return 'production';
return PRODUCTION;
}

resolveZeroConfig() {
const defaultConfigType = this.getEnvFromOptionsAndMode();
const defaultConfig = require(`./${defaultConfigType}-config`)();
let defaultConfig;
if (defaultConfigType === PRODUCTION) {
defaultConfig = require('../utils/production-config')();
} else {
defaultConfig = require('../utils/development-config')();
}

const isEntryObject = defaultConfig.entry && defaultConfig.entry instanceof Object;
const isOutputDefined = defaultConfig.output && defaultConfig.output.filename;
const isConflictingOutput = isEntryObject && isOutputDefined && defaultConfig.output.filename === 'bundle.js';
const isConflictingOutput = isEntryObject && isOutputDefined && defaultConfig.output.filename === 'main.js';
if (isConflictingOutput) {
defaultConfig.output.filename = '[name].bundle.js';
defaultConfig.output.filename = '[name].main.js';
}
this.opts.options = defaultConfig;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/development-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ module.exports = () => ({
devtool: 'eval',
output: {
path: defaultOutputPath,
filename: 'bundle.js',
filename: 'main.js',
},
});
2 changes: 1 addition & 1 deletion lib/utils/production-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = () => ({
devtool: 'source-map',
output: {
path: defaultOutputPath,
filename: 'bundle.js',
filename: 'main.js',
},
optimization: {
minimizer: [
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/zero-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function getConfigurations(options, outputOptions) {

const isEntryObject = newConfig.entry && newConfig.entry instanceof Object;
const isOutputDefined = newConfig.output && newConfig.output.filename;
const isConflictingOutput = isEntryObject && isOutputDefined && newConfig.output.filename === 'bundle.js';
const isConflictingOutput = isEntryObject && isOutputDefined && newConfig.output.filename === 'main.js';
if (isConflictingOutput) {
newConfig.output.filename = '[name].bundle.js';
newConfig.output.filename = '[name].main.js';
}
return newConfig;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class WebpackCLI extends GroupHelper {

/**
* Responsible for applying defaults, if necessary
* @private
* @private\
* @returns {void}
*/
_handForcedDefaults() {
Expand Down
14 changes: 14 additions & 0 deletions packages/generators/__tests__/add-generator.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions test/ZeroConfigGroup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const ZeroConfigGroup = require('../lib/groups/ZeroConfigGroup');

describe('GroupHelper', function() {
it('should load the dev zero config', () => {
const group = new ZeroConfigGroup([
{
dev: true,
},
]);

const result = group.run();
expect(result.options.mode).toEqual('development');
});
it('should load the prod zero config', () => {
const group = new ZeroConfigGroup([
[
{
prod: true,
},
],
]);

const result = group.run();
expect(result.options.mode).toEqual('production');
});
});
1 change: 1 addition & 0 deletions test/env/prod/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('environment is '.concat(PRODUCTION ? 'production' : 'development'));
27 changes: 27 additions & 0 deletions test/env/prod/prod.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const path = require('path');
const execa = require('execa');
const { sync: spawnSync } = execa;

const { run } = require('../../utils/test-utils');

describe('env object', () => {
it('is able to compile successfully with --prod flag', () => {
const { stderr, stdout } = run(__dirname, ['--prod']);
const executable = path.join(__dirname, './bin/main.js');
const bundledScript = spawnSync('node', [executable]);
expect(bundledScript.stdout).toBe('environment is production');
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});

it('is able to compile successfully with -p flag', () => {
const { stderr, stdout } = run(__dirname, ['-p']);
const executable = path.join(__dirname, './bin/main.js');
const bundledScript = spawnSync('node', [executable]);
expect(bundledScript.stdout).toBe('environment is production');
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});
});
11 changes: 11 additions & 0 deletions test/env/prod/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const webpack = require('webpack');

module.exports = {
mode: 'development',
devtool: 'eval-cheap-module-source-map',
plugins: [
new webpack.DefinePlugin({
PRODUCTION: JSON.stringify(true),
}),
],
};
4 changes: 2 additions & 2 deletions test/output/named-bundles/output-named-bundles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ describe('output flag named bundles', () => {

expect(summary['Output Directory']).toContain(outputDir);

stat(resolve(__dirname, './dist/b.bundle.js'), (err, stats) => {
stat(resolve(__dirname, './dist/b.main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
stat(resolve(__dirname, './dist/c.bundle.js'), (err, stats) => {
stat(resolve(__dirname, './dist/c.main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
Expand Down