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

feat: add support for none config in dotfolder #1637

Merged
merged 7 commits into from
Jun 30, 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: 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',
};
anshumanv marked this conversation as resolved.
Show resolved Hide resolved

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();
});
});
});
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',
},
};