pages-webpack-plugin

0.3.0 • Public • Published

pages-webpack-plugin

Generate static pages with webpack.

build status coverage license version downloads

Usage:

const PagesPlugin = require('pages-webpack-plugin');

const renderBody = (path) => {
  switch (path) {
  case '/':
    return 'See all our <a href="/products">Products</a>.';
  case '/products':
    return 'We have the hugest products.';
  default:
    return 'Page not found.';
  }
}

module.exports = {
  // ...
  plugins: [
    new PagesPlugin({
      // Required Config
      mapStatsToProps: (stats) => {
        // Map webpack stats object to render function props
        return {stats: stats.toJson(/* webpack toJson options*/)};
      },
      render: (props) => {
        const {stats, path} = props;
        const scriptSrc = stats.publicPath + stats.assetsByChunkName.main;
        const markup = `
          <!DOCTYPE html>
          <html>
            <body>
              ${renderBody(path)}
              <script src="${scriptSrc}"></script>
            </body>
          </html>
        `

        const result = {markup, /* status, redirect */}
        return result;
      },
      // Optional Config
      paths: ['/'], // Define initial seed routes
      mapResultToFilename(result) {
        // Map rendered path to output file name
        return result.path;
      },
      mapResults(results, compilation) {
        // Intercept generated files before emit
        return results.map((result) => {
          return result;
        });
      },
      // Only explore the paths provided in the `paths` option
      // parsePathsFromMarkup: () => []
    }),
  ],
};

Readme

Keywords

none

Package Sidebar

Install

npm i pages-webpack-plugin

Weekly Downloads

0

Version

0.3.0

License

CC0-1.0

Unpacked Size

32.5 kB

Total Files

6

Last publish

Collaborators

  • izaakschroeder