A
A
Azat Yumagulov2017-01-15 14:06:12
JavaScript
Azat Yumagulov, 2017-01-15 14:06:12

How to merge configs that have Promise with webpack-merge?

Hello!
There are two webpack configs: dev and prod. They are merged with common config using webpack-merge
Webpack 2
Abbreviated:
webpack.dev.js

const webpackMerge = require('webpack-merge'); 
const commonConfig = require('./webpack.common.js');

const config = {
    ...
};

module.exports = function (options) {
    return webpackMerge(commonConfig({env: ENV}), config);
}

Shorthand:
webpack.common.js
const config = {
    ...
};
let pages = new Array();
let promise = new Promise((resolve, reject) => {
    fs.readdir('./src/templates', function (err, files) {
        files
            .filter(function(file) { return file.substr(-4) === '.ejs'; })
            .forEach(function (file) {
                pages.push({
                    name: file.replace('.ejs', '.html'),
                    template: './src/templates/' + file
                });
            });
        resolve(pages);
    });
});

promise.then(result => {

    result.forEach(function (page) {
        config.plugins.push(
            new HtmlWebpackPlugin({
                template: page.template,
                filename: page.name,
                inject: 'body',
                chunksSortMode: 'dependency',
                minify: false
            })
        );
    });

    module.exports = function (options) {
        return config;
    };
});

Actually, it is clear that we get the error TypeError: commonConfig is not a function .
The trick is that even if you don’t merge, but simply give the config in webpack.dev.js after the resolve promise, then the webpack does not perceive this, it says no output.
Maybe you can somehow make webpack or webpack-merge wait?
I would be very grateful for advice.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Q
qtuz, 2017-02-10
@qtuz

Only if you write a nodejs script that launches the webpack compiler when the promise resolves. But the easiest way is to scan the directory synchronously, for example via glob.sync . I did this on more than one project and there are no problems.

A
Anton, 2017-07-12
@lemonlimelike

$array = $_GET;
if ($array == $bar && isset($bar['id'])) {
    unset($bar['id']);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question