Skip to content

Commit

Permalink
feat: add support for none config in dotfolder (#1637)
Browse files Browse the repository at this point in the history
* feat: add support for none config in dotfolder

* feat: add support for picking config as per mode

* feat: add support for picking config as per mode

* tests: add config test for dev config in diff folders

* tests: prefer .webpack over root config

* tests: prefer .webpack over root config
  • Loading branch information
anshumanv authored Jun 30, 2020
1 parent 29c8b40 commit 28526a6
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 7 deletions.
21 changes: 17 additions & 4 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ const { extensions } = require('interpret');
const GroupHelper = require('../utils/GroupHelper');
const rechoir = require('rechoir');

// Order defines the priority, in increasing order
// example - config file lookup will be in order of .webpack/webpack.config.development.js -> webpack.config.development.js -> webpack.config.js
const DEFAULT_CONFIG_LOC = [
'webpack.config',
'webpack.config.dev',
'webpack.config.development',
'webpack.config.prod',
'webpack.config.production',
'.webpack/webpack.config',
'.webpack/webpack.config.none',
'.webpack/webpack.config.dev',
'.webpack/webpack.config.development',
'.webpack/webpack.config.prod',
'.webpack/webpack.config.production',
'.webpack/webpackfile',
'webpack.config',
];

const modeAlias = {
production: 'prod',
development: 'dev',
};

const fileTypes = {
'.babel.js': ['@babel/register', 'babel-register', 'babel-core/register', 'babel/register'],
'.babel.ts': ['@babel/register'],
Expand Down Expand Up @@ -136,10 +150,9 @@ class ConfigGroup extends GroupHelper {

const configFiles = tmpConfigFiles.map(this.requireConfig.bind(this));
if (configFiles.length) {
const defaultConfig = configFiles.find((p) => p.path.includes(mode));
const defaultConfig = configFiles.find((p) => p.path.includes(mode) || p.path.includes(modeAlias[mode]));
if (defaultConfig) {
const envConfig = defaultConfig.map((c) => c.content);
this.opts = this.finalize(envConfig);
this.opts = this.finalize(defaultConfig);
return;
}
const foundConfig = configFiles.pop();
Expand Down
7 changes: 6 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WebpackCLI extends GroupHelper {
`./src/${this.defaultEntry}.js`,
`src/${this.defaultEntry}.js`,
];
this.compilerConfiguration = undefined;
this.compilerConfiguration = {};
this.outputConfiguration = {};
}
setMappedGroups(args, inlineOptions) {
Expand Down Expand Up @@ -62,6 +62,11 @@ class WebpackCLI extends GroupHelper {
*/
resolveGroups() {
let mode;
// determine the passed mode for ConfigGroup
if (this.groupMap.has(groups.ZERO_CONFIG_GROUP)) {
const modePresent = this.groupMap.get(groups.ZERO_CONFIG_GROUP).find((t) => !!t.mode);
if (modePresent) mode = modePresent.mode;
}
for (const [key, value] of this.groupMap.entries()) {
switch (key) {
case groups.ZERO_CONFIG_GROUP: {
Expand Down
9 changes: 9 additions & 0 deletions test/config/multiple/all/.webpack/webpack.config.none.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, '../binary'),
filename: 'none.bundle.js',
},
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'use strict';
const { stat } = require('fs');
const { resolve } = require('path');
const { run } = require('../../utils/test-utils');
const { resolve, join } = require('path');
const { run } = require('../../../utils/test-utils');
const rimraf = require('rimraf');

const outputPath = join(__dirname, 'binary');

describe('multiple config files', () => {
afterEach(() => rimraf.sync(outputPath));
beforeAll(() => rimraf.sync(outputPath));

it('Uses prod config from dot folder if present', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
expect(stderr).toBeFalsy();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, '../binary'),
filename: 'dev.folder.js',
},
};
1 change: 1 addition & 0 deletions test/config/multiple/multiple-location/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Tanjiro")
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const { stat } = require('fs');
const { resolve, join } = require('path');
const { run } = require('../../../utils/test-utils');
const rimraf = require('rimraf');

const outputPath = join(__dirname, 'binary');

describe('multiple dev config files with webpack.config.js', () => {
afterEach(() => rimraf.sync(outputPath));
beforeAll(() => rimraf.sync(outputPath));

it('Uses webpack.config.development.js', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
expect(stderr).toBeFalsy();
expect(stdout).not.toBe(undefined);
stat(resolve(__dirname, './binary/dev.folder.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, './binary'),
filename: 'development.bundle.js',
},
};
9 changes: 9 additions & 0 deletions test/config/multiple/multiple-location/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, './binary'),
filename: 'root.bundle.js',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, '../binary'),
filename: 'dev.bundle.js',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, '../binary'),
filename: 'none.bundle.js',
},
};
23 changes: 23 additions & 0 deletions test/config/multiple/none and dev/dev-none-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const { stat } = require('fs');
const { resolve, join } = require('path');
const { run } = require('../../../utils/test-utils');
const rimraf = require('rimraf');

const outputPath = join(__dirname, 'binary');

describe('multiple config files', () => {
afterEach(() => rimraf.sync(outputPath));
beforeAll(() => rimraf.sync(outputPath));

it('Uses dev config when both dev and none are present', (done) => {
const { stdout, stderr } = run(__dirname, [], false);
expect(stderr).toBeFalsy();
expect(stdout).not.toBe(undefined);
stat(resolve(__dirname, './binary/dev.bundle.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
1 change: 1 addition & 0 deletions test/config/multiple/none and dev/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Kageyama")
1 change: 1 addition & 0 deletions test/config/multiple/with-mode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Iwaizumi")
23 changes: 23 additions & 0 deletions test/config/multiple/with-mode/multiple-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const { stat } = require('fs');
const { resolve, join } = require('path');
const { run } = require('../../../utils/test-utils');
const rimraf = require('rimraf');

const outputPath = join(__dirname, 'binary');

describe('multiple config files', () => {
afterEach(() => rimraf.sync(outputPath));
beforeAll(() => rimraf.sync(outputPath));

it('Uses dev config when development mode is supplied', (done) => {
const { stdout, stderr } = run(__dirname, ['--mode', 'development'], false);
expect(stderr).toBeFalsy();
expect(stdout).not.toBe(undefined);
stat(resolve(__dirname, './binary/dev.bundle.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
9 changes: 9 additions & 0 deletions test/config/multiple/with-mode/webpack.config.development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, './binary'),
filename: 'dev.bundle.js',
},
};
9 changes: 9 additions & 0 deletions test/config/multiple/with-mode/webpack.config.none.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, './binary'),
filename: 'none.bundle.js',
},
};
9 changes: 9 additions & 0 deletions test/config/multiple/with-mode/webpack.config.production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { resolve } = require('path');

module.exports = {
entry: './index.js',
output: {
path: resolve(__dirname, './binary'),
filename: 'prod.bundle.js',
},
};

0 comments on commit 28526a6

Please sign in to comment.