0.1.5 • Published 1 year ago

webpack-critical-pages v0.1.5

Weekly downloads
63
License
MIT
Repository
github
Last release
1 year ago

webpack-critical-pages

npm version

Automatically generate critical css for a well-defined paging structure on multiple entrypoints.
Initially designed for project structures using Python's Django Web Framework.

Blends in well with webpack5-bundle-tracker.

For each app, define an entrypoints.json file with the following structure:

{
  "vocdashboard": {
    "file": "./pages/vocdashboard/dashboard.js",
    "exampleUrl": "/vocabulary/"
  },
  "search": {
    "file": "./pages/search/search.js",
    "exampleUrl": "/vocabulary/search/?q=friends",
    "totalEntrypoints": ["main", "vocabulary/search"]
  }
}

You may then use this node script for generating the critical css directly in the desired output folder:

const criticalPages = require("webpack-critical-pages");

const BASE = path.dirname(__dirname);
const APPS = ["app1", "app2", "feedback"];
const pages = criticalPages.getCriticalPages(BASE, APPS);
criticalPages.extractCriticalCSS(pages).then(() => {
  console.log("Finished.");
});

Retrieve webpack entrypoints from the defined json files like this:

const criticalPages = require("webpack-critical-pages");
const entrypoints = criticalPages.loadEntrypoints(BASE, ROOT_ENTRYPOINTS, APPS);

If you want to use a service worker, this package also provides automatic generation of hashes from fetched page content to use in the fantastic workbox plugin for webpack. It will treat the dynamically generated pages as static content.

const path = require("path");
const merge = require("webpack-merge");
const basePromise = require("./base.config.js");
const WorkboxPlugin = require("workbox-webpack-plugin");
const criticalPages = require("webpack-critical-pages");

const PAGES_TO_PRECACHE = [
  "/en/",
  "/de/",
  "/en/my-page/",
];

module.exports = async () => {
  let base = await basePromise();
  return merge.merge(base.config, {
    mode: "development",
    output: {
      path: path.resolve("./.dev/bundles"),
      filename: "[name].js"
    },
    module: {
      rules: [...]
    },
    plugins: [
      new WorkboxPlugin.InjectManifest({
        swSrc: "./frontend/service-worker.js",
        swDest: "sw.js",
        chunks: ["main", "landingpage"],
        additionalManifestEntries: await criticalPages.fetchPageEntries(PAGES_TO_PRECACHE)
      })
    ]
  });
}

Development

To publish, run

yarn build
yarn publish
0.1.5

1 year ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.0.9

2 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.5

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago