Skip to content

Commit

Permalink
Merge pull request #85 from homer0/homer0_paramsEvent
Browse files Browse the repository at this point in the history
v9.2.0: feat(services/building/configuration): reduce the configuration params using an event
  • Loading branch information
homer0 committed Sep 2, 2019
2 parents 9e29f51 + 858466f commit b7c9326
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 113 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -120,8 +120,11 @@ All the configurations receive a single object parameter with the following prop
- `targetRules`: The rules to find the target files on the file system.
- `entry`: A dictionary with the `entry` setting for a webpack configuration, generated with the target information.
- `output`: A dictionary with the filenames formats and paths of the different files the bundle can generate (`js`, `css`, `images` and `fonts`).
- `definitions`: A dictionary of defined variables that will be replaced on the bundled code.
- `definitions`: A function that generates a dictionary of variables that will be replaced on the bundled code.
- `buildType`: The indented build type (`development` or `production`).
- `copy`: A list of information for files that need to be copied during the bundling process.
- `additionalWatch`: A list of additional paths webpack should watch for in order to restart the bundle.
- `analyze`: A flag to detect if the bundled should be analyzed or not.

#### Base configuration

Expand Down Expand Up @@ -233,6 +236,13 @@ Now, this plugin only uses the loader if the implementation has `image-webpack-l

If you want to write a plugin that works with this one (like a framework plugin), there are a lot of reducer events you can listen for and use to modify the webpack configuration:

### Configuration parameters

- Name: `webpack-configuration-parameters`
- Reduces: The parameters used by the plugin services to build a target configuration.

This is called before generating any configuration.

### Node target configuration

- Name: `webpack-base-configuration-for-node`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "projext-plugin-webpack",
"description": "Allows projext to use webpack as a build engine.",
"homepage": "https://homer0.github.io/projext-plugin-webpack/",
"version": "9.1.0",
"version": "9.2.0",
"repository": "homer0/projext-plugin-webpack",
"author": "Leonardo Apiwan (@homer0) <me@homer0.com>",
"license": "MIT",
Expand Down
21 changes: 18 additions & 3 deletions src/services/building/configuration.js
Expand Up @@ -66,11 +66,17 @@ class WebpackConfiguration {
}
/**
* This method generates a complete webpack configuration for a target.
* @param {Target} target The target information.
* @param {string} buildType The intended build type: `production` or `development`.
* Before creating the configuration, it uses the reducer event
* `webpack-configuration-parameters-for-browser` or `webpack-configuration-parameters-for-node`,
* depending on the target type, and then `webpack-configuration-parameters` to reduce
* the parameters ({@link WebpackConfigurationParams}) the services will use to generate the
* configuration. The event recevies the parameters and expects updated parameters in return.
* @param {Target} target The target information.
* @param {string} buildType The intended build type: `production` or `development`.
* @return {Object}
* @throws {Error} If there's no base configuration for the target type.
* @throws {Error} If there's no base configuration for the target type and build type.
* @todo Stop using `events` from `targets` and inject it directly on the class.
*/
getConfig(target, buildType) {
const targetType = target.type;
Expand Down Expand Up @@ -99,7 +105,7 @@ class WebpackConfiguration {
const definitions = this._getDefinitionsGenerator(target, buildType);
const additionalWatch = this._getBrowserTargetConfigurationDefinitions(target).files;

const params = {
let params = {
target,
targetRules: this.targetsFileRules.getRulesForTarget(target),
entry: {
Expand All @@ -117,6 +123,15 @@ class WebpackConfiguration {
analyze: !!target.analyze,
};

const eventName = params.target.is.node ?
'webpack-configuration-parameters-for-node' :
'webpack-configuration-parameters-for-browser';

params = this.targets.events.reduce(
[eventName, 'webpack-configuration-parameters'],
params
);

let config = this.targetConfiguration(
`webpack/${target.name}.config.js`,
this.webpackConfigurations[targetType][buildType]
Expand Down

0 comments on commit b7c9326

Please sign in to comment.