Skip to content

Commit

Permalink
fix: Support webpack 5.x by reading source from file-system
Browse files Browse the repository at this point in the history
BREAKING CHANGE: HtmlRenderPlugin exported as named and default variable. commonjs require statements will need to be changed as follows:

```js
// old
const HtmlRenderPlugin = require('html-render-webpack-plugin');
// new
const { HtmlRenderPlugin } = require('html-render-webpack-plugin');
```
  • Loading branch information
jahredhope authored Feb 10, 2021
1 parent edd758b commit b83be46
Show file tree
Hide file tree
Showing 45 changed files with 2,772 additions and 3,349 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ module.exports = {
node: true,
},
rules: {
"@typescript-eslint/ban-ts-ignore": "warn",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"no-console": "off",
"no-inner-declarations": "off",
},
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Release
on:
push:
branches:
- master
- alpha
jobs:
release:
name: Release
runs-on: ubuntu-18.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: yarn release
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on: [push]

jobs:
test:
name: Test
runs-on: ubuntu-18.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Lint
run: yarn lint
- name: Test
run: yarn test
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ HtmlRenderPlugin will then pass the [Webpack Stats](https://webpack.js.org/api/s
**webpack.config.js**

```js
const HtmlRenderPlugin = require("html-render-webpack-plugin");
const { HtmlRenderPlugin } = require("html-render-webpack-plugin");

const htmlRenderPlugin = new HtmlRenderPlugin();
module.exports = [
Expand Down Expand Up @@ -111,7 +111,7 @@ module.exports = {

The location to create rendered files. Defaults to the rendered assets output.

Useful when deploying HTML files seperate to other build assets.
Useful when deploying HTML files separate to other build assets.

## Option: routes _Array<object|string>_

Expand Down
53 changes: 27 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"lint.eslint": "eslint src/**/*.ts",
"lint.prettier": "prettier --check .",
"lint": "npm run lint.eslint && npm run lint.prettier",
"jest": "jest",
"prepublishOnly": "npm run build",
"test": "npm run lint && npm run jest",
"test": "jest",
"posttest": "npm run lint",
"format": "prettier --write .",
"release": "semantic-release"
},
Expand All @@ -32,34 +32,35 @@
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/express": "^4.17.6",
"@types/jest": "^25.2.1",
"@types/memory-fs": "^0.3.2",
"@types/schema-utils": "^2.4.0",
"@types/webpack": "^4.41.12",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"commitizen": "4.0.5",
"cz-conventional-changelog": "3.1.0",
"eslint": "6.8.0",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.20",
"@types/webpack": "^4.41.26",
"@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
"commitizen": "4.2.3",
"cz-conventional-changelog": "3.3.0",
"eslint": "7.19.0",
"express": "^4.17.1",
"jest": "25.5.4",
"memoizee": "^0.4.14",
"memory-fs": "^0.5.0",
"prettier": "2.0.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"semantic-release": "17.0.7",
"ts-jest": "^25.4.0",
"ts-node": "^8.10.1",
"typescript": "^3.8.3",
"webpack": "4.43.0",
"webpack-merge": "4.2.2"
"jest": "26.6.3",
"memfs": "^3.2.0",
"memoizee": "^0.4.15",
"prettier": "2.2.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"semantic-release": "17.3.8",
"ts-jest": "^26.5.1",
"ts-node": "^9.1.1",
"typescript": "^4.1.4",
"webpack": "^5.0.0",
"webpack-merge": "^5.0.0"
},
"dependencies": {
"chalk": "^4.0.0",
"chalk": "^4.1.0",
"eval": "^0.1.4",
"exception-formatter": "^2.1.2"
"exception-formatter": "^2.1.2",
"fs-monkey": "^1.0.1",
"schema-utils": "^3.0.0",
"unionfs": "^4.4.0"
},
"config": {
"commitizen": {
Expand Down
2 changes: 1 addition & 1 deletion src/RenderError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default class RenderError extends Error {
constructor(error: Error) {
super(
`html-render-webpack-plugin: An error occured during render: \n${error}`
`html-render-webpack-plugin: An error occurred during render: \n${error}`
);

this.name = "RenderError";
Expand Down
11 changes: 7 additions & 4 deletions src/common-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Stats } from "webpack";
import MultiStats from "webpack/lib/MultiStats";
import { Request } from "express";

export type WebpackStats = Stats | MultiStats;
export type WebpackStats = Stats;
export type BaseRoute = { route: string };
export type Render<Route = BaseRoute> = (route: Route) => Promise<string>;
export type OnRendererReady<Route = BaseRoute> = (
Expand All @@ -14,16 +13,20 @@ export type SourceModules = Record<string, string>;
export type RouteInput<Route = BaseRoute> = Route | string;
export type Trace = (...values: any[]) => void;
export type Specifier = string;
export type ExtraGlobals = object;
export type ExtraGlobals = Record<string, unknown>;
export type RenderConcurrency = "parallel" | "serial";
export type MapStatsToParams = ({
webpackStats,
}: {
webpackStats: WebpackStats;
}) => object;
}) => Record<string, unknown>;
export type TransformPath<Route = BaseRoute> = (route: Route) => string;
export type TransformExpressPath<Route = BaseRoute> = (route: Route) => string;
export type GetRouteFromRequest<Route> = (
req: Request,
routes: Route[]
) => Route;

import originalFs from "fs";

export type FileSystem = typeof originalFs;
5 changes: 2 additions & 3 deletions src/createDevRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
TransformExpressPath,
GetRouteFromRequest,
} from "./common-types";
import { Stats } from "webpack";

interface Params<Route> {
routes: Route[];
Expand All @@ -28,15 +27,15 @@ export = <Route extends BaseRoute>({
log("Create dev server");
const formatErrorResponse = (
error: string,
webpackStats: Stats.ToJsonOutput
webpackStats: Record<string, any>
) => {
let devServerScripts: string[] = [];
if ("entrypoints" in webpackStats && webpackStats.entrypoints) {
try {
const devServerAssets = webpackStats.entrypoints.main.assets;

devServerScripts = devServerAssets.map(
(asset) =>
(asset: string) =>
`<script src="${webpackStats.publicPath}${asset}"></script>`
);
} catch (err) {
Expand Down
17 changes: 11 additions & 6 deletions src/createRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import evalutateFromSource from "./evalutateFromSource";
import { SourceModules, ExtraGlobals } from "./common-types";
import evaluateFromFileSystem from "./evaluateFromFileSystem";
import { FileSystem, ExtraGlobals } from "./common-types";

export = function createRenderer({
fileName,
source,
fileSystem,
rootDir,
extraGlobals,
}: {
fileName: string;
source: SourceModules;
fileSystem: FileSystem;
extraGlobals: ExtraGlobals;
rootDir: string;
}) {
if (!fileName) {
throw new Error("Missing filename");
}
if (!source) {
if (!fileSystem) {
throw new Error("Missing source");
}
return evalutateFromSource(fileName, source, extraGlobals);
if (!rootDir || typeof rootDir !== "string") {
throw new Error(`Recieved rootDir as ${typeof rootDir}`);
}
return evaluateFromFileSystem(fileName, fileSystem, rootDir, extraGlobals);
};
68 changes: 68 additions & 0 deletions src/evaluateFromFileSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import path from "path";
import evaluate from "eval";
import { ExtraGlobals, FileSystem } from "./common-types";

import { log } from "./logging";

function getFromSourceModules(
specifier: string,
fsModule: FileSystem,
rootDir: string
) {
const sourceModuleSpecifier = specifier.replace(/^\.\//, "");
if (!fsModule.existsSync(path.resolve(rootDir, sourceModuleSpecifier))) {
log(
`Unable to find file specifier ${sourceModuleSpecifier}. Root: ${rootDir}.`
);
return undefined;
}
return fsModule.readFileSync(path.resolve(rootDir, sourceModuleSpecifier));
}

function evaluateFromFileSystem(
specifier: string,
fsModule: FileSystem,
rootDir: string,
extraGlobals: ExtraGlobals
) {
log(
`Evaluating source for ${specifier}". From root directory: "${rootDir}".`
);
let source;
try {
source = getFromSourceModules(specifier, fsModule, rootDir);
} catch (error) {
throw new Error(`An error reading "${specifier}". Error: ${error}`);
}
return evaluate(
source,
/* filename: */ specifier,
/* scope: */ {
console,
process,
...(extraGlobals || {}),
require: createLinker(specifier, fsModule, rootDir, extraGlobals),
},
/* includeGlobals: */ true
);
}

function createLinker(
parentModulePath: string,
fsModule: FileSystem,
rootDir: string,
extraGlobals: ExtraGlobals
) {
log("Creating linker for", parentModulePath);
return function linker(specifier: string) {
const absPath = path.join(path.dirname(parentModulePath), specifier);
if (!getFromSourceModules(specifier, fsModule, rootDir)) {
log(`Using external require for ${specifier} from ${parentModulePath}`);
return require(specifier);
}
log(`Linking ${parentModulePath} to asset ${specifier}`);
return evaluateFromFileSystem(absPath, fsModule, rootDir, extraGlobals);
};
}

export = evaluateFromFileSystem;
54 changes: 0 additions & 54 deletions src/evalutateFromSource.ts

This file was deleted.

Loading

0 comments on commit b83be46

Please sign in to comment.