Search code examples
webpackmergeconfigurationscaffolding

Webpack Scaffold webpackOptions merge not work, why?


So after the tutorial webpack-scaffold-demo, my webpack.config.js should be merged with the webpack.dev.js and webpack.pro.js, but merge is not added here.

I tried to pass it directly as an object in the example, then I tried to pass it as a function and tried with a class but unfortunately he does not add my merge with common to my other configs.

myObj.webpackOptions

dependencies (package.json)

"@webpack-cli/webpack-scaffold": "^0.1.2",
"yeoman-generator": "^3.1.1"

generator.js

// Yeoman
const Generator = require( 'yeoman-generator' );
// Scaffold
const List = require( '@webpack-cli/webpack-scaffold' ).List;
const Input = require( '@webpack-cli/webpack-scaffold' ).Input;
// Default abstracted configs
const createCommonConfig = require( './common-config' );
const createProConfig = require( './pro-config' );
const createDevConfig = require( './dev-config' );

module.exports = class WebpackGenerator extends Generator {

    constructor( args, opts ) {

        super( args, opts );

        opts.env.configuration = {
            config: {
                webpackOptions: {}
            },
            dev: {
                webpackOptions: {}
            },
            pro: {
                webpackOptions: {}
            }
        }
    }

    prompting() {

        return this.prompt(
            [
                List( 'confirm', 'Welcome to the tnado Scaffold! Are you ready?', ['Yes', 'No', 'tnado'] ),
                Input( 'entry', 'What is the entry point in your app?' )
            ]
        ).then( answer => {

            if ( answer['confirm'] === 'tnado' ) {

                // Common
                this.options.env.configuration.config.webpackOptions = createCommonConfig( answer );
                this.options.env.configuration.config.topScope = [
                    'const path = require("path")',
                    'const webpack = require("webpack")'
                ];
                this.options.env.configuration.config.configName = 'config'; // Manipulate name

                // DEV
                this.options.env.configuration.dev.webpackOptions = {
                    mode: "'development'",
                    merge: 'common'
                };
                this.options.env.configuration.dev.topScope = [
                    'const path = require("path")',
                    'const webpack = require("webpack")',
                    'const merge = require("webpack-merge")',
                    'let common = require("./webpack.config.js")'
                ];
                this.options.env.configuration.dev.configName = 'dev'; // Manipulate name

                // PRO
                this.options.env.configuration.pro.webpackOptions = new createProConfig( answer );
                this.options.env.configuration.pro.topScope = [
                    'const merge = require("webpack-merge")',
                    'let common = require("./webpack.config.js")'
                ];
                this.options.env.configuration.pro.configName = 'pro'; // Manipulate name
            }
        } );
    }

    writing() {
        this.config.set( 'configuration', this.options.env.configuration );
    }
};

All files are created and also the topScopes are added only the merge is not added.

My result for webpack.dev.js:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
let common = require('./webpack.config.js');

module.exports = {
    mode: 'development'

And what I want:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
let common = require('./webpack.config.js');

module.exports = merge( common, {
    mode: 'development'

Would be very grateful if someone could help.


Solution

  • The solution:

    this.options.env.configuration.dev.merge = 'common';
    

    Pull request:

    https://github.com/webpack/webpack-cli/pull/747/files

    Merge of pull request:

    https://github.com/webpack/webpack-cli/blob/master/SCAFFOLDING.md#myobjmerge-optional