Webpack脚手架webpackOptions合并不起作用,为什么?

时间:2019-01-24 15:37:54

标签: webpack merge configuration options scaffolding

因此,在完成教程webpack-scaffold-demo之后,应将我的 webpack.config.js webpack.dev.js webpack.pro合并.js ,但此处未添加合并。

在示例中,我尝试将其直接作为对象传递,然后尝试将其作为函数传递并尝试使用类,但是不幸的是,他没有将我的common合并添加到我的其他配置中。

myObj.webpackOptions

依赖关系(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 );
    }
};

创建所有文件,并添加topScopes,仅不添加合并。

我的 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'

我想要的是什么

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'

如果有人可以帮助,将不胜感激。

0 个答案:

没有答案