Skip to content

Commit

Permalink
chore: fixed mergin conflict webpack#2
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Oct 2, 2019
2 parents b961855 + 5fd64c4 commit 7638b21
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 31 deletions.
75 changes: 57 additions & 18 deletions lib/groups/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AdvancedGroup extends GroupHelper {
}
} catch (e) {
process.cliLogger.error('Invalid plugin arguments ' + name + ' (' + e + ').');
process.exit(-1); // eslint-disable-line
process.exit(-1); // eslint-disable-line
}

let path;
Expand All @@ -24,7 +24,7 @@ class AdvancedGroup extends GroupHelper {
path = resolve.sync(process.cwd(), name);
} catch (e) {
process.cliLogger.error('Cannot resolve plugin ' + name + '.');
process.exit(-1); // eslint-disable-line
process.exit(-1); // eslint-disable-line
}
let Plugin;
try {
Expand All @@ -43,8 +43,13 @@ class AdvancedGroup extends GroupHelper {
resolveOptions() {
const { args } = this;
if (args.hot) {
const HotModuleReplacementPlugin = require('webpack').HotModuleReplacementPlugin;
this.opts.options.plugins = [new HotModuleReplacementPlugin()];
const { HotModuleReplacementPlugin } = require('webpack');
const hotModuleVal = new HotModuleReplacementPlugin();
if (this.opts.options && this.opts.options.plugins) {
this.opts.options.plugins.unshift(hotModuleVal);
} else {
this.opts.options.plugins = [hotModuleVal];
}
}
if (args.prefetch) {
const { PrefetchPlugin } = require('webpack');
Expand All @@ -62,26 +67,60 @@ class AdvancedGroup extends GroupHelper {
this.opts.options.plugins = [this.loadPlugin(args.plugin)];
}
}
if (args.target) {
this.opts.options.target = args.target;
}

if (args.global) {
let value = args.global;
const idx = value.indexOf('=');
let name;
if (idx >= 0) {
name = value.substr(0, idx);
value = value.substr(idx + 1);
} else {
name = value;
const globalArrLen = args.global.length;
if (!globalArrLen) {
process.cliLogger.warn("Argument to global flag is none");
return;
}
const ProvidePlugin = require('webpack').ProvidePlugin;
if (globalArrLen === 1) {
process.cliLogger.warn("Argument to global flag expected a key/value pair");
return;
}

let providePluginObject = {};
args.global.forEach( (arg, idx) => {
const isKey = (idx % 2 === 0) ? true : false;
const isConcatArg = arg.includes('=');
if(isKey && isConcatArg) {
const splitIdx = arg.indexOf('=');
const argVal = arg.substr(splitIdx + 1);
const argKey = arg.substr(0, splitIdx);
if(!argVal.length) {
process.cliLogger.warn(`Found unmatching value for global flag key '${argKey}'`);
return;
}
if (providePluginObject.hasOwnProperty(argKey)) {
process.cliLogger.warn(`Overriding key '${argKey}' for global flag`);
}
providePluginObject[argKey] = argVal;
return;
}
if(isKey) {
const nextArg = args.global[idx+1];
if(providePluginObject.hasOwnProperty(arg)) {
process.cliLogger.warn(`Overriding key '${arg}' for global flag`);
}
if(!nextArg) {
process.cliLogger.warn(`Found unmatching value for global flag key '${arg}'`);
return;
}
providePluginObject[arg] = nextArg;
}
});

const { ProvidePlugin } = require('webpack');
const globalVal = new ProvidePlugin(providePluginObject);
if (this.opts.options && this.opts.options.plugins) {
this.opts.options.plugins.unshift(new ProvidePlugin(name, value));
this.opts.options.plugins.unshift(globalVal);
} else {
this.opts.options.plugins = [this.loadPlugin(args.plugin)];
this.opts.options.plugins = [globalVal];
}
}
if (args.target) {
this.opts.options.target = args.target;
}
}
run() {
this.resolveOptions();
Expand Down
11 changes: 7 additions & 4 deletions lib/groups/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ class ConfigGroup extends GroupHelper {
}

resolveConfigMerging() {
const { merge } = this.args;
if (merge) {
const newConfigPath = this.resolveFilePath(merge, 'webpack.base');
if (this.args.hasOwnProperty('merge')) {
const {merge} = this.args;

const newConfigPath = this.resolveFilePath(merge, 'webpack.base.js');
const newConfig = newConfigPath ? this.require(newConfigPath) : null;
this.opts['options'] = require('webpack-merge')(this.opts['options'], newConfig);

const webpackMerge = require('webpack-merge');
this.opts['options'] = webpackMerge(this.opts['options'], newConfig);
}
}

Expand Down
17 changes: 11 additions & 6 deletions lib/utils/group-helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const { resolve, join } = require('path');
const { existsSync } = require('fs');
const {
resolve,
join
} = require('path');
const {
existsSync
} = require('fs');

const parseAndValidatedFilename = pathToResolve => {
const pathToArray = pathToResolve.split('/');
Expand Down Expand Up @@ -31,7 +36,7 @@ class GroupHelper {
if (!name) {
return name;
}
return name.replace(/-([a-z])/g, function(g) {
return name.replace(/-([a-z])/g, function (g) {
return g[1].toUpperCase();
});
}
Expand Down Expand Up @@ -72,7 +77,7 @@ class GroupHelper {
} */
}
mapArgToBoolean(name, optionName) {
ifArg(name, function(bool) {
ifArg(name, function (bool) {
if ([true, false].includes(bool)) {
options[optionName || name] = bool;
}
Expand Down Expand Up @@ -127,7 +132,7 @@ class GroupHelper {
options.plugins.unshift(plugin);
}

resolveFilePath(filename='', defaultValue) {
resolveFilePath(filename = '', defaultValue) {
if (filename && Array.isArray(filename)) {
return filename.map(fp => this.resolveFilePath(fp, defaultValue)).filter(e => e);
}
Expand All @@ -145,7 +150,7 @@ class GroupHelper {
if (filename && filename.includes('.js')) {
filename = filename + '.js';
}
if (defaultValue && defaultValue.includes('.js')) {
if (defaultValue && !defaultValue.includes('.js')) {
defaultValue = defaultValue + '.js';
}
let configPath;
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function isType(path: Node, type: string): boolean {
function findObjWithOneOfKeys(p: Node, keyNames: string[]): boolean {
return (p.value as Node).properties.reduce((predicate: boolean, prop: Node): boolean => {
const name: string = prop.key.name;
return keyNames.indexOf(name) > -1 || predicate;
return keyNames.includes(name) || predicate;
}, false);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/utils/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export default function runTransform(transformConfig: TransformConfig, action: s

const transformations = mapOptionsToTransform(config);

if (config.topScope && transformations.indexOf("topScope") === -1) {
if (config.topScope && !transformations.includes("topScope")) {
transformations.push("topScope");
}

if (config.merge && transformations.indexOf("merge") === -1) {
if (config.merge && !transformations.includes("merge")) {
transformations.push("merge");
}

Expand Down
53 changes: 53 additions & 0 deletions test/global/global.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

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

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

describe('global flag', () => {
it('warns if there are no arguments to flag', () => {
const { stderr } = run(__dirname, ['--global']);
expect(stderr).toContain('Argument to global flag is none');
});

it('warns if there are no value for key', () => {
const { stderr } = run(__dirname, ['--global', 'myVar']);
expect(stderr).toContain('Argument to global flag expected a key/value pair');
});

it('is able inject one variable to global scope', () => {
const { stderr } = run(__dirname, ['--global', 'myVar', './global1.js']);
expect(stderr).toBe('');
const executable = path.join(__dirname, './bin/bundle.js');
const bundledScript = spawnSync('node', [executable]);
expect(bundledScript.stdout).toEqual('myVar ./global1.js');
});

it('is able inject multiple variables to global scope', () => {
const { stderr } = run(__dirname, ['--global', 'myVar', './global1.js', '--global', 'myVar2', './global2.js']);
expect(stderr).toBe('');
const executable = path.join(__dirname, './bin/bundle.js');
const bundledScript = spawnSync('node', [executable]);
expect(bundledScript.stdout).toEqual('myVar ./global1.js\nmyVar ./global2.js');
});

it('understands = syntax', () => {
const { stderr } = run(__dirname, ['--global', 'myVar', './global1.js', '--global', 'myVar2=./global2.js']);
expect(stderr).toBe('');
const executable = path.join(__dirname, './bin/bundle.js');
const bundledScript = spawnSync('node', [executable]);
expect(bundledScript.stdout).toEqual('myVar ./global1.js\nmyVar ./global2.js');
});

it('warns on multiple flags that are inconsistent', () => {
const result = run(__dirname, ['--global', 'myVar', './global1.js', '--global', 'myVar2']);
// eslint-disable-next-line
expect(result.stderr).toContain("Found unmatching value for global flag key 'myVar2'");

const result2 = run(__dirname, ['--global', 'myVar', './global1.js', '--global', 'myVar2=']);
// eslint-disable-next-line
expect(result2.stderr).toContain("Found unmatching value for global flag key 'myVar2'");
});
});
1 change: 1 addition & 0 deletions test/global/global1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'myVar ./global1.js';
1 change: 1 addition & 0 deletions test/global/global2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'myVar ./global2.js';
5 changes: 5 additions & 0 deletions test/global/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.log(myVar);

try {
console.log(myVar2);
} catch(e) {}
3 changes: 3 additions & 0 deletions test/merge/config/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: './some_entry.js',
};
5 changes: 5 additions & 0 deletions test/merge/config/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
output: {
filename: 'merged.js',
},
};
18 changes: 18 additions & 0 deletions test/merge/config/merge-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const { stat } = require('fs');
const { resolve } = require('path');

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

describe('merge flag configuration', () => {
it('merges two configurations together', done => {
const { stderr } = run(__dirname, ['--config', './1.js', '--merge', './2.js'], false);
expect(stderr).toBe('');
stat(resolve(__dirname, './dist/merged.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
1 change: 1 addition & 0 deletions test/merge/config/some_entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('yass');
3 changes: 3 additions & 0 deletions test/merge/defaults/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: './some_other_entry.js',
};
27 changes: 27 additions & 0 deletions test/merge/defaults/merge-defaults.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const { stat } = require('fs');
const { resolve } = require('path');

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

describe('merge flag defaults', () => {
it('merges a default webpack.base.config with default config lookup', done => {
const { stderr } = run(__dirname, ['-m'], false);
expect(stderr).toBe('');
stat(resolve(__dirname, './dist/default.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
it('merges a configuraiton file with default base config', done => {
const { stderr } = run(__dirname, ['-c', './1.js'], false);
expect(stderr).toBe('');
stat(resolve(__dirname, './dist/bundle.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
1 change: 1 addition & 0 deletions test/merge/defaults/some_entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('<3');
1 change: 1 addition & 0 deletions test/merge/defaults/some_other_entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('moo');
5 changes: 5 additions & 0 deletions test/merge/defaults/webpack.base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
output: {
filename: 'default.js',
},
};
3 changes: 3 additions & 0 deletions test/merge/defaults/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: './some_entry.js',
};

0 comments on commit 7638b21

Please sign in to comment.