0.3.0 • Published 5 years ago

pages-webpack-plugin v0.3.0

Weekly downloads
1
License
CC0-1.0
Repository
-
Last release
5 years ago

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: () => []
    }),
  ],
};