GithubHelp home page GithubHelp logo

isabella232 / generate-page-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from storybook-eol/generate-page-webpack-plugin

0.0 0.0 0.0 10 KB

generate a html-page for every webpack entrypoint

License: MIT License

JavaScript 100.00%

generate-page-webpack-plugin's Introduction

Generate Page Webpack Plugin

Generate HTML-pages for every webpack entrypoint

Install

npm install generate-page-webpack-plugin --dev --save
yarn add generate-page-webpack-plugin --dev

Add to your webpack config

export const config = {
  mode: 'development',
  devtool: 'cheap-module-source-map',
  entry: {
    page1: ['pages/page1.js'],
    page2: ['pages/page2.js'],
  },
  plugins: [
    new GeneratePagePlugin(
      {
        template: require.resolve('../templates/index.html.ejs'),
        parser: require('ejs'),
      },
      {
        data: {
          awesome: true,
        },
      }
    ),
  ],
};

Api

GeneratePagePlugin takes 2 parameters:

Config

Config is data the plugin needs to operate, it will not be passed to the template

  • parser - required - should be a ejs like module that can compile and invoke templates
  • template - required - the template to use to generate pages
  • filename - optional - should be a function that takes the entrypoint name and returns a name for the html-file.

Properties have no default, so if they are required you must supply them!

Options

Options are datapoints that are used when generating the template, they are passed to the template.

If the value of a option is a function, it will be invoked with the name of the entry, so you can change the options per entry!

You can set whatever options you want here, you're totally free to make up your own data (different per entry is possible) and inject it into the template.

Here's a few suggestions:

  • headHtmlSnippet - inject some html into the head
  • footHtmlSnippet - inject some html into the foot
  • script - inject scripts
  • window - inject global scope variables
  • title - meta title for the page

Templates

Templates can be in whatever syntax you like, if you supply a parser for them. We do not ship with a parser or template included.

Rendering chunks and options

The template will be provided with the data which is a combination of:

  • the webpack entrypoint reference values are accessible as global variables The most useful is likely chunks.

  • the webpack compilation values are available under a variable called compilation.

  • the dlls needed for the entrypoint values are available under a variable called dlls. Dlls are an advanced use-case but with this plugin there should be no special setup required for them to be injected into the template. But you do need to render them in the template (see example).

  • the options values are available under a variable called options. If the option was a function in the webpack config, by the time the data is injected into the template it will have been called and whatever the function returned for that property, that's what's injected into the template.

Template example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta content="IE=edge" http-equiv="X-UA-Compatible" />
    <title><%= options.title %></title>
    <link rel="shortcut icon" href="favicon.ico" />
  
    <% if (options.headHtmlSnippet) { %>
      <%- options.headHtmlSnippet %>
    <% } %>
  
</head>
<body>
    
    <% if (options.window) { %>
      <script>
        <% for (key in options.window) { %>
          window['<%= key %>'] = <%= JSON.stringify(options.window[key]) %>;
        <% } %>
      </script>
    <% } %>

    <% if (options.bodyHtmlSnippet) { %>
      <%- options.bodyHtmlSnippet %>
    <% } %>
  
    <div id="root"></div>

    <% for (key in dlls) { %>
      <script src="<%= compilation.outputOptions.publicPath %><%= dlls[key] %>" defer></script>
    <% } %>
  
    <% for (index in chunks) { %>
      <% for (key in chunks[index].files) { %>
        <script src="<%= compilation.outputOptions.publicPath %><%= chunks[index].files[key] %>" defer></script>
      <% } %>
    <% } %>
  
</body>
</html>

Contributing

All contributions are welcome!

generate-page-webpack-plugin's People

Contributors

greenkeeper[bot] avatar jackub avatar joostvanwollingen avatar ndelangen avatar pksunkara avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.