Skip to content

kimjoar/generate-asset-webpack-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

generate-asset-webpack-plugin

Sometimes you dynamically want to create files while having access to all the Webpack compilation info.

Install

npm install --save-dev generate-asset-webpack-plugin

Usage

var GenerateAssetPlugin = require('generate-asset-webpack-plugin');

var webpackConfig = {
    plugins: [
        new GenerateAssetPlugin({
            filename: 'index.html',
            fn: (compilation, cb) => {
                cb(null, createHtml(compilation));
            },
            extraFiles: ['favicon.ico']
        })
    ]
    // other webpack config ...
}

Example function using the compilation:

function createHtml(compilation) {
    var chunk = compilation.chunks[0];
    var jsFile = chunk.files[0];
    var cssFile = chunk.files[1];
    return `<!DOCTYPE html>
        <html>
            <head>
                <meta charset="utf-8">
                <title>Awesome!</title>
                <link rel="stylesheet" href="${cssFile}">
            </head>
            <body>
                <div id="app"></div>
                <script src="${jsFile}"></script>
            </body>
        </html>
    `;
};

About

A Webpack plugin for generating assets, e.g. an index.html

Resources

Stars

Watchers

Forks

Packages

No packages published