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

make Electrode internal webpack config a little easier to override #1536

Merged
merged 1 commit into from
Feb 17, 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
6 changes: 6 additions & 0 deletions packages/webpack-config-composer/lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

module.exports = {
PARTIALS: Symbol("partials"),
PROFILES: Symbol("profiles")
};
27 changes: 18 additions & 9 deletions packages/webpack-config-composer/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const Partial = require("./partial");
const Profile = require("./profile");
const { getConcatMethod } = require("./concat-method");

const { PARTIALS, PROFILES } = require("./constants");

class WebpackConfigComposer {
constructor(options) {
options = options || {};
this._profiles = {};
this._partials = {};
this[PROFILES] = {};
this[PARTIALS] = {};
if (options.profiles) {
this.addProfiles(options.profiles);
}
Expand All @@ -22,11 +24,11 @@ class WebpackConfigComposer {
}

get profiles() {
return this._profiles;
return this[PROFILES];
}

get partials() {
return this._partials;
return this[PARTIALS];
}

addProfiles(profiles) {
Expand All @@ -53,7 +55,7 @@ class WebpackConfigComposer {
profile = new Profile(name, partials);
}

this._profiles[name] = profile;
this[PROFILES][name] = profile;

return profile;
}
Expand Down Expand Up @@ -82,10 +84,10 @@ class WebpackConfigComposer {
}

_addPartial(name, data, addOpt) {
const exist = this._partials[name];
const exist = this[PARTIALS][name];

if (!exist || _.get(addOpt, "method") === "replace") {
this._partials[name] = new Partial(name, data);
this[PARTIALS][name] = data instanceof Partial ? data : new Partial(name, data);
} else {
exist.merge(data, _.get(addOpt, "concatArray"));
}
Expand All @@ -102,11 +104,18 @@ class WebpackConfigComposer {
}

getPartial(name) {
return this._partials[name];
return this[PARTIALS][name];
}

enablePartial(name, flag) {
const partial = this.getPartial(name);
if (partial) {
partial.enable = flag;
}
}

getProfile(name) {
return this._profiles[name];
return this[PROFILES][name];
}

compose(options, ...profiles) {
Expand Down
32 changes: 24 additions & 8 deletions packages/webpack-config-composer/lib/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,49 @@

const _ = require("lodash");
const { getConcatMethod } = require("./concat-method");
const assert = require("assert");

const OVERRIDE = Symbol("override webpack config partial");
const DATA = Symbol("webpack partial data");

class Partial {
constructor(name, data) {
this._name = name;
this._data = Object.assign({ config: {}, options: {} }, data);
if (typeof data === "function") {
this[DATA] = { config: data };
} else {
this[DATA] = Object.assign({ config: {}, options: {} }, data);
}
this.setOverride();
}

set config(config) {
this._data.config = config;
this[DATA].config = config;
}

get config() {
return this._data.config;
return this[DATA].config;
}

set options(options) {
this._data.options = Object.assign({}, options);
this[DATA].options = Object.assign({}, options);
}

get options() {
return this._data.options;
return this[DATA].options;
}

merge(data, concatArray) {
_.mergeWith(this._data, data, getConcatMethod(concatArray));
_.mergeWith(this[DATA], data, getConcatMethod(concatArray));
}

setOverride(fn) {
this[OVERRIDE] = fn || _.identity;
}

compose(options) {
options = Object.assign({}, this.options, options);

const config = this.config;
const configType = typeof config;

Expand All @@ -38,7 +53,6 @@ class Partial {
if (configType === "object") {
ret = config;
} else if (configType === "function") {
options = Object.assign({}, this.options, options);
ret = config(options);
if (typeof ret === "function") {
ret = ret(options);
Expand All @@ -47,7 +61,9 @@ class Partial {
throw new Error(`can't process config from Partial ${this._name}`);
}

return ret;
const override = this[OVERRIDE](ret, options);

return override || ret;
}
}

Expand Down
12 changes: 7 additions & 5 deletions packages/webpack-config-composer/lib/profile.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
"use strict";

const { PARTIALS } = require("./constants");

class Profile {
constructor(name, partials) {
this._name = name;
this._partials = partials || {};
this[PARTIALS] = partials || {};
}

get name() {
return this._name;
}

get partials() {
return this._partials;
return this[PARTIALS];
}

setPartial(name, options) {
this._partials[name] = options || {};
this[PARTIALS][name] = options || {};
}

getPartial(name) {
return this._partials[name];
return this[PARTIALS][name];
}

delPartial(name) {
delete this._partials[name];
delete this[PARTIALS][name];
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/xarc-app-dev/config/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = {
initWebpackConfigComposer,
compose: generateConfig,
env: profile,
options
options,
partials: require("./partials")
};
4 changes: 2 additions & 2 deletions packages/xarc-app-dev/config/webpack/partials/dll-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const AppMode = archetype.AppMode;
const Path = require("path");
const clientDllConfig = require(Path.resolve(AppMode.src.client, "dll.config.js"));

module.exports = {
module.exports = () => ({
entry: clientDllConfig
};
});
4 changes: 2 additions & 2 deletions packages/xarc-app-dev/config/webpack/partials/dll-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const Path = require("path");

module.exports = {
module.exports = () => ({
output: {
path: Path.resolve("dll/js"),
filename: "[name].bundle.[hash].js",
library: "[name]_[hash]"
}
};
});
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/config/webpack/partials/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,4 @@ if (module.hot) {
return partial;
}

module.exports = makeEntryPartial();
module.exports = makeEntryPartial;
18 changes: 9 additions & 9 deletions packages/xarc-app-dev/config/webpack/partials/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Fs = require("fs");
const assert = require("assert");
const Partial = require("webpack-config-composer/lib/partial");

//
// This specifies a general order of partials to be applied.
Expand Down Expand Up @@ -50,12 +51,11 @@ const files = Fs.readdirSync(__dirname)
.filter(x => x !== "index.js")
.map(x => x.substr(0, x.length - 3));

module.exports = {
orders,
partials: files.reduce((a, p) => {
const k = `_${p}`;
assert(orders.indexOf(k) >= 0, `No default order specified for partial ${p}`);
a[k] = { config: () => require(`./${p}`) };
return a;
}, {})
};
const partials = files.reduce((a, p) => {
const k = `_${p}`;
assert(orders.indexOf(k) >= 0, `No default order specified for partial ${p}`);
a[k] = new Partial(k, { config: () => require(`./${p}`) });
return a;
}, {});

module.exports = partials;
4 changes: 2 additions & 2 deletions packages/xarc-app-dev/config/webpack/partials/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getOutputPath = () => {
}
};

module.exports = {
module.exports = () => ({
output: {
path: getOutputPath(),
pathinfo: inspectpack, // Enable path information for inspectpack
Expand All @@ -36,4 +36,4 @@ module.exports = {
: "[contenthash].[name].js",
filename: getOutputFilename()
}
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const Path = require("path");
const ModuleResolver = require("electrode-node-resolver/lib/webpack-plugin");
const archetype = require("@xarc/app/config/archetype");

module.exports = {
module.exports = () => ({
resolveLoader: {
symlinks: !archetype.webpack.preserveSymlinks,
modules: [Path.resolve("lib"), process.cwd()]
.concat(archetype.webpack.loaderDirectories)
.filter(identity),
plugins: [new ModuleResolver("module", "resolve", archetype.devDir, undefined)]
}
};
});
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/config/webpack/partials/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ if (AppMode.hasSubApps) {
resolve.mainFields = ["module", "browser", "main"];
}

module.exports = { resolve };
module.exports = () => ({ resolve });
Loading