Skip to content

Commit

Permalink
fix(hmr): readFile, writeFile not exported by fs-extra (#106)
Browse files Browse the repository at this point in the history
also update node imports to use node: prefix
  • Loading branch information
samrum authored May 1, 2023
1 parent 8c096d6 commit c9c2ebf
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typescript from "@rollup/plugin-typescript";
import pkg from "./package.json" assert { type: "json" };

const external = ["path", "fs", "crypto"].concat(
const external = ["node:crypto", "node:fs/promises", "node:path"].concat(
Object.keys(pkg.dependencies ?? {})
);

Expand Down
13 changes: 7 additions & 6 deletions src/devBuilder/devBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { copy, emptyDir, ensureDir, readFile, writeFile } from "fs-extra";
import path from "path";
import { copy, emptyDir, ensureDir } from "fs-extra";
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { ResolvedConfig, ViteDevServer, normalizePath } from "vite";
import { getScriptLoaderFile } from "../utils/loader";
import { getInputFileName, getOutputFileName } from "../utils/file";
import { getVirtualModule } from "../utils/virtualModule";
import { addHmrSupportToCsp } from "../utils/addHmrSupportToCsp";
import { AdditionalInput, ViteWebExtensionOptions } from "../../types";
import { addHmrSupportToCsp } from "../utils/addHmrSupportToCsp";
import { getInputFileName, getOutputFileName } from "../utils/file";
import getAdditionalInputAsWebAccessibleResource from "../utils/getAdditionalInputAsWebAccessibleResource";
import getNormalizedAdditionalInput from "../utils/getNormalizedAdditionalInput";
import { getScriptLoaderFile } from "../utils/loader";
import { getVirtualModule } from "../utils/virtualModule";

export default abstract class DevBuilder<
Manifest extends chrome.runtime.Manifest
Expand Down
4 changes: 2 additions & 2 deletions src/devBuilder/devBuilderManifestV2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import crypto from "crypto";
import { createHash } from "node:crypto";
import { ViteWebExtensionOptions } from "../../types";
import DevBuilder from "./devBuilder";

Expand All @@ -13,7 +13,7 @@ export default class DevBuilderManifestV2 extends DevBuilder<chrome.runtime.Mani
protected parseInlineScriptHashes(content: string) {
const matches = content.matchAll(/<script.*?>([^<]+)<\/script>/gs);
for (const match of matches) {
const shasum = crypto.createHash("sha256");
const shasum = createHash("sha256");
shasum.update(match[1]);

this.inlineScriptHashes.add(`'sha256-${shasum.digest("base64")}'`);
Expand Down
5 changes: 3 additions & 2 deletions src/devBuilder/devBuilderManifestV3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ensureDir, writeFile } from "fs-extra";
import path from "path";
import { ensureDir } from "fs-extra";
import { writeFile } from "node:fs/promises";
import path from "node:path";
import { ViteWebExtensionOptions } from "../../types";
import { getServiceWorkerLoaderFile } from "../utils/loader";
import DevBuilder from "./devBuilder";
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { EmittedFile, OutputBundle } from "rollup";
import type { Plugin, ResolvedConfig } from "vite";
import type { ViteWebExtensionOptions } from "../types";
import { addInputScriptsToOptionsInput } from "./utils/rollup";
import ManifestParser from "./manifestParser/manifestParser";
import ManifestParserFactory from "./manifestParser/manifestParserFactory";
import { getVirtualModule } from "./utils/virtualModule";
import viteClientModifier from "./middleware/viteClientModifier";
import { addInputScriptsToOptionsInput } from "./utils/rollup";
import { getVirtualModule } from "./utils/virtualModule";
import {
transformSelfLocationAssets,
updateConfigForExtensionSupport,
Expand Down
22 changes: 12 additions & 10 deletions src/manifestParser/manifestParser.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { ResolvedConfig, ViteDevServer } from "vite";
import DevBuilder from "../devBuilder/devBuilder";
import { getInputFileName, getOutputFileName } from "../utils/file";
import type {
EmittedFile,
OutputAsset,
OutputBundle,
OutputChunk,
} from "rollup";
import { getScriptLoaderForOutputChunk } from "../utils/loader";
import {
getCssAssetInfoFromBundle,
getChunkInfoFromBundle,
getOutputInfoFromBundle,
} from "../utils/rollup";
import { ResolvedConfig, ViteDevServer } from "vite";
import type {
NormalizedAdditionalInput,
ViteWebExtensionOptions,
} from "../../types";
import { getScriptHtmlLoaderFile } from "../utils/loader";
import DevBuilder from "../devBuilder/devBuilder";
import { getInputFileName, getOutputFileName } from "../utils/file";
import {
getScriptHtmlLoaderFile,
getScriptLoaderForOutputChunk,
} from "../utils/loader";
import {
getChunkInfoFromBundle,
getCssAssetInfoFromBundle,
getOutputInfoFromBundle,
} from "../utils/rollup";
import { setVirtualModule } from "../utils/virtualModule";

export interface ParseResult<Manifest extends chrome.runtime.Manifest> {
Expand Down
7 changes: 3 additions & 4 deletions src/manifestParser/manifestV2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ParseResult } from "./manifestParser";
import DevBuilderManifestV2 from "../devBuilder/devBuilderManifestV2";
import ManifestParser from "./manifestParser";
import DevBuilder from "./../devBuilder/devBuilder";
import { OutputBundle } from "rollup";
import { ViteWebExtensionOptions } from "../../types";
import DevBuilderManifestV2 from "../devBuilder/devBuilderManifestV2";
import getNormalizedAdditionalInput from "../utils/getNormalizedAdditionalInput";
import DevBuilder from "./../devBuilder/devBuilder";
import ManifestParser, { ParseResult } from "./manifestParser";

type Manifest = chrome.runtime.ManifestV2;
type ManifestParseResult = ParseResult<Manifest>;
Expand Down
9 changes: 4 additions & 5 deletions src/manifestParser/manifestV3.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { OutputBundle } from "rollup";
import { ParseResult } from "./manifestParser";
import ManifestParser from "./manifestParser";
import { ViteWebExtensionOptions } from "../../types";
import DevBuilder from "../devBuilder/devBuilder";
import { getServiceWorkerLoaderFile } from "../utils/loader";
import DevBuilderManifestV3 from "../devBuilder/devBuilderManifestV3";
import { getChunkInfoFromBundle } from "../utils/rollup";
import { ViteWebExtensionOptions } from "../../types";
import getAdditionalInputAsWebAccessibleResource from "../utils/getAdditionalInputAsWebAccessibleResource";
import getNormalizedAdditionalInput from "../utils/getNormalizedAdditionalInput";
import { getServiceWorkerLoaderFile } from "../utils/loader";
import { getChunkInfoFromBundle } from "../utils/rollup";
import ManifestParser, { ParseResult } from "./manifestParser";

type Manifest = chrome.runtime.ManifestV3;
type ManifestParseResult = ParseResult<Manifest>;
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/viteClientModifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Connect } from "vite";
import getEtag from "etag";
import { Connect } from "vite";

// Modifies the vite HMR client to support various web extension features including:
// Exporting a function to add HMR style injection targets
Expand Down
2 changes: 1 addition & 1 deletion src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from "path";
import path from "node:path";
import { normalizePath } from "vite";

export function getNormalizedFileName(
Expand Down
6 changes: 3 additions & 3 deletions test/manifest/manifestTestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import { expect, test } from "vitest";
import { build, InlineConfig, normalizePath } from "vite";
import path from "node:path";
import type { RollupOutput } from "rollup";
import { build, normalizePath } from "vite";
import { expect, test } from "vitest";
import webExtension from "../../src/index";
import { ViteWebExtensionOptions } from "../../types";

Expand Down

0 comments on commit c9c2ebf

Please sign in to comment.