Skip to content

Commit c6f95d9

Browse files
felixmosheungyeole
authored andcommitted
improve dev
1 parent 514391a commit c6f95d9

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

packages/rsbuild-plugin-web-extension/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
},
3737
"license": "MIT",
3838
"type": "module",
39+
"dependencies": {
40+
"jiti": "^2.0.0"
41+
},
3942
"devDependencies": {
4043
"@biomejs/biome": "1.6.1",
4144
"@rsbuild/core": "1.4.9",

packages/rsbuild-plugin-web-extension/src/index.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import type { RsbuildPlugin } from "@rsbuild/core";
2-
import { makeManifest } from "./manifest/make-manifest.js";
1+
import * as fs from "node:fs";
2+
import path from "node:path";
3+
import { type RsbuildPlugin, rspack } from "@rsbuild/core";
4+
import { createJiti } from "jiti";
5+
import ManifestParser from "./manifest/parser.js";
36

47
interface Options {
5-
manifest: chrome.runtime.ManifestV3;
8+
manifestPath: string;
69
}
710

8-
export const pluginWebExtension = ({ manifest }: Options): RsbuildPlugin => ({
9-
name: "rsbuild:plugin-web-extension",
11+
const pluginName = "rsbuild:plugin-web-extension";
12+
13+
export const pluginWebExtension = ({
14+
manifestPath,
15+
}: Options): RsbuildPlugin => ({
16+
name: pluginName,
1017
setup: async (api) => {
18+
const jiti = createJiti(api.context.rootPath, { moduleCache: false });
19+
20+
const manifestSourcePath = path.resolve(api.context.rootPath, manifestPath);
21+
if (!fs.existsSync(manifestSourcePath)) {
22+
throw new Error(`${pluginName}: Failed to read ${manifestSourcePath}`);
23+
}
24+
25+
const manifestModule = jiti(manifestSourcePath);
26+
const manifest = manifestModule.default || manifestModule.manifest;
27+
1128
const htmlEntryPoints = Object.entries({
1229
popup: manifest.action?.default_popup,
1330
devtools: manifest.devtools_page,
@@ -60,13 +77,39 @@ export const pluginWebExtension = ({ manifest }: Options): RsbuildPlugin => ({
6077
},
6178
},
6279
dev: {
63-
writeToDisk: true
64-
}
80+
writeToDisk: true,
81+
},
6582
});
6683
});
6784

68-
api.onAfterBuild(() => {
69-
makeManifest(manifest, api.context.distPath);
85+
api.onAfterCreateCompiler(({ compiler }) => {
86+
if (!(compiler instanceof rspack.Compiler)) {
87+
return;
88+
}
89+
90+
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
91+
compilation.hooks.processAssets.tap(
92+
{
93+
name: pluginName,
94+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
95+
},
96+
() => {
97+
compilation.fileDependencies.add(manifestSourcePath);
98+
99+
const updatedManifestModule = jiti(manifestPath);
100+
const manifest =
101+
updatedManifestModule.default || updatedManifestModule.manifest;
102+
const content = ManifestParser.convertManifestToString(manifest);
103+
104+
const { RawSource } = compiler.webpack.sources;
105+
106+
const source = new RawSource(content);
107+
const outputFilename = "manifest.json";
108+
109+
compilation.emitAsset(outputFilename, source);
110+
}
111+
);
112+
});
70113
});
71114
},
72115
});

packages/rsbuild-plugin-web-extension/src/manifest/make-manifest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as fs from "node:fs";
22
import { resolve } from "node:path";
3-
import ManifestParser from "./parser.js";
43
import { logger } from "@rsbuild/core";
4+
import ManifestParser from "./parser.js";
55

6-
export const makeManifest = async (
6+
export const makeManifest = (
77
manifest: chrome.runtime.ManifestV3,
88
to: string
99
) => {
@@ -15,7 +15,7 @@ export const makeManifest = async (
1515

1616
fs.writeFileSync(
1717
toManifest,
18-
ManifestParser.convertManifestToString(manifest)
18+
ManifestParser.convertManifestToString(manifest.default || manifest)
1919
);
2020

2121
logger.log("Manifest file created");

packages/rsbuild-plugin-web-extension/src/manifest/parser.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
type Manifest = chrome.runtime.ManifestV3;
22

3-
const getOutputFile = (file: string): string => {
4-
return file
5-
.replace(/\.(scss|stylus|sass)/g, ".css")
6-
.replace(/\.(jsx|ts|tsx)/g, ".js");
7-
};
3+
const fixSuffixes = (file: string): string =>
4+
file.replace(/\.(s[ca]ss|stylus)/g, ".css").replace(/\.(jsx|tsx?)/g, ".js");
85

96
class ManifestParser {
107
private constructor() {}
118

129
static convertManifestToString(_manifest: Manifest): string {
13-
let manifest = { ..._manifest };
10+
let manifest = structuredClone(_manifest);
1411
if (process.env.__FIREFOX__) {
1512
manifest = ManifestParser.convertToFirefoxCompatibleManifest(manifest);
1613
}
1714

18-
return getOutputFile(JSON.stringify(manifest, null, 2));
15+
return fixSuffixes(JSON.stringify(manifest, null, 2));
1916
}
2017

2118
static convertToFirefoxCompatibleManifest(manifest: Manifest) {

0 commit comments

Comments
 (0)