@zk-tech/rrt-plugin-rspack 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/config.ts","../src/utilities/scripts.ts","../src/index.ts"],"sourcesContent":["export const coreSrc = 'perf-monitor-core.bundle.js';\nexport const clientSrc = 'perf-monitor-client.bundle.js';\n","import type { BackendEnv } from '@zk-tech/rrt-shared';\nimport type { PluginOptions } from '..';\n\nexport const getPluginScripts = (options?: PluginOptions) => {\n const { rendererId, diffMode = 'lite', updateTrace = true, commitTrace = true } = options || {};\n\n const env: BackendEnv = {\n clientHost: 'inpage',\n bundler: 'rspack',\n bundlerVersion: __PLUGIN_VERSION__,\n diffMode,\n updateTrace,\n commitTrace,\n };\n\n let settingScript = `window.__PERF_MONITOR_DEVTOOLS_ENV__ = ${JSON.stringify(env)};`;\n\n if (rendererId) {\n settingScript += `window.__PERF_MONITOR_DEVTOOLS_CONTEXT__ = { rendererId: ${rendererId} };`;\n }\n\n const coreScript = __CORE_SCRIPT__;\n const clientScript = __CLIENT_SCRIPT__;\n\n return {\n settingScript,\n coreScript,\n clientScript,\n };\n};\n","import type { BackendEnv } from '@zk-tech/rrt-shared';\n\nimport { clientSrc, coreSrc } from './config';\nimport { getPluginScripts } from './utilities/scripts';\n\ninterface PluginOptions {\n /**\n * Specifies whether the devtools should be disabled.\n */\n disabled?: boolean;\n /**\n * Sets the rendererId to inspect, only if you know what it is used for.\n *\n * It is recommended to use the plugin's rendererId selection feature before manually configuring the rendererId when using the plugin for the first time.\n */\n rendererId?: number;\n /**\n * The diff mode to use. Defaults to lite diff mode. Full diff mode is not supported yet.\n * - \"off\": No diffs (Performance will be slightly improved).\n * - \"lite\": Only simple diffs (Performance will be slightly impacted).\n * - \"full\": All nested diffs (Performance will be significantly impacted).\n * @default \"lite\"\n */\n diffMode?: BackendEnv['diffMode'];\n /**\n * Whether to record update related events.\n * @default true\n */\n updateTrace?: boolean;\n /**\n * Whether to record commit.\n * @default true\n */\n commitTrace?: boolean;\n}\n\ninterface HtmlTag {\n attributes: Record<string, string | boolean>;\n tagName: string;\n voidTag: boolean;\n innerHTML?: string;\n meta: Record<string, unknown>;\n}\n\ninterface HtmlTagGroupsData {\n headTags: HtmlTag[];\n}\n\ninterface HtmlTagGroupsHook {\n tapPromise: (\n name: string,\n fn: (data: HtmlTagGroupsData) => Promise<HtmlTagGroupsData | void> | HtmlTagGroupsData | void,\n ) => void;\n}\n\ninterface HtmlRspackPluginLike {\n getCompilationHooks: (compilation: CompilationLike) => {\n alterAssetTagGroups: HtmlTagGroupsHook;\n };\n}\n\ninterface RawSourceConstructor {\n new (source: string): unknown;\n}\n\ninterface CompilationLike {\n assets?: Record<string, unknown>;\n emitAsset?: (name: string, source: unknown) => void;\n}\n\ninterface CompilerLike {\n hooks: {\n compilation: {\n tap: (name: string, fn: (compilation: CompilationLike) => void) => void;\n };\n };\n webpack?: {\n sources?: {\n RawSource?: RawSourceConstructor;\n };\n };\n rspack?: {\n HtmlRspackPlugin?: HtmlRspackPluginLike;\n };\n}\n\nconst pluginName = 'PerfMonitorRspackPlugin';\n\nfunction emitAsset(compilation: CompilationLike, filename: string, source: unknown) {\n if (typeof compilation.emitAsset === 'function') {\n compilation.emitAsset(filename, source);\n return;\n }\n\n if (!compilation.assets) {\n compilation.assets = {};\n }\n compilation.assets[filename] = source;\n}\n\nclass PerfMonitorRspackPlugin {\n options: PluginOptions;\n\n constructor(pluginOptions?: PluginOptions) {\n this.options = pluginOptions || {\n disabled: false,\n };\n }\n\n apply(compiler: CompilerLike) {\n if (this.options.disabled) {\n return;\n }\n\n const HtmlRspackPlugin = compiler.rspack?.HtmlRspackPlugin;\n if (!HtmlRspackPlugin?.getCompilationHooks) {\n throw new Error(\n `${pluginName} requires rspack.HtmlRspackPlugin. Add \"new rspack.HtmlRspackPlugin()\" before this plugin.`,\n );\n }\n\n compiler.hooks.compilation.tap(pluginName, (compilation) => {\n const RawSource = compiler.webpack?.sources?.RawSource;\n if (!RawSource) {\n throw new Error(`${pluginName} requires compiler.webpack.sources.RawSource.`);\n }\n\n const { coreScript, clientScript, settingScript } = getPluginScripts(this.options);\n emitAsset(compilation, coreSrc, new RawSource(coreScript));\n emitAsset(compilation, clientSrc, new RawSource(clientScript));\n\n HtmlRspackPlugin.getCompilationHooks(compilation).alterAssetTagGroups.tapPromise(\n pluginName,\n async (data) => {\n data.headTags.unshift(\n {\n attributes: { defer: true, src: `/${clientSrc}` },\n tagName: 'script',\n voidTag: false,\n meta: {},\n },\n {\n attributes: { defer: true, src: `/${coreSrc}` },\n tagName: 'script',\n voidTag: false,\n meta: {},\n },\n );\n\n data.headTags.unshift({\n attributes: {},\n tagName: 'script',\n voidTag: false,\n innerHTML: settingScript,\n meta: {},\n });\n\n return data;\n },\n );\n });\n }\n}\n\nexport { PerfMonitorRspackPlugin, type PluginOptions };\n"],"names":["coreSrc","clientSrc","getPluginScripts","options","rendererId","diffMode","updateTrace","commitTrace","settingScript","pluginName","emitAsset","compilation","filename","source","PerfMonitorRspackPlugin","pluginOptions","compiler","HtmlRspackPlugin","_a","RawSource","_b","coreScript","clientScript","data"],"mappings":"gFAAO,MAAMA,EAAU,8BACVC,EAAY,gCCEZC,EAAoBC,GAA4B,CAC3D,KAAM,CAAE,WAAAC,EAAY,SAAAC,EAAW,OAAQ,YAAAC,EAAc,GAAM,YAAAC,EAAc,IAASJ,GAAW,CAAA,EAW7F,IAAIK,EAAgB,0CAA0C,KAAK,UAT3C,CACtB,WAAY,SACZ,QAAS,SACT,eAAgB,QAChB,SAAAH,EACA,YAAAC,EACA,YAAAC,CAAA,CAG8E,CAAC,IAEjF,OAAIH,IACFI,GAAiB,4DAA4DJ,CAAU,OAMlF,CACL,cAAAI,EACA,WALiB,675KAMjB,aALmB,oll6CAKnB,CAEJ,ECyDMC,EAAa,0BAEnB,SAASC,EAAUC,EAA8BC,EAAkBC,EAAiB,CAClF,GA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CACR,EAEMe,EACT,CAEJ,CAAC,CACH,CACF"}
@@ -0,0 +1,77 @@
1
+ import { BackendEnv } from '@zk-tech/rrt-shared';
2
+ interface PluginOptions {
3
+ /**
4
+ * Specifies whether the devtools should be disabled.
5
+ */
6
+ disabled?: boolean;
7
+ /**
8
+ * Sets the rendererId to inspect, only if you know what it is used for.
9
+ *
10
+ * It is recommended to use the plugin's rendererId selection feature before manually configuring the rendererId when using the plugin for the first time.
11
+ */
12
+ rendererId?: number;
13
+ /**
14
+ * The diff mode to use. Defaults to lite diff mode. Full diff mode is not supported yet.
15
+ * - "off": No diffs (Performance will be slightly improved).
16
+ * - "lite": Only simple diffs (Performance will be slightly impacted).
17
+ * - "full": All nested diffs (Performance will be significantly impacted).
18
+ * @default "lite"
19
+ */
20
+ diffMode?: BackendEnv['diffMode'];
21
+ /**
22
+ * Whether to record update related events.
23
+ * @default true
24
+ */
25
+ updateTrace?: boolean;
26
+ /**
27
+ * Whether to record commit.
28
+ * @default true
29
+ */
30
+ commitTrace?: boolean;
31
+ }
32
+ interface HtmlTag {
33
+ attributes: Record<string, string | boolean>;
34
+ tagName: string;
35
+ voidTag: boolean;
36
+ innerHTML?: string;
37
+ meta: Record<string, unknown>;
38
+ }
39
+ interface HtmlTagGroupsData {
40
+ headTags: HtmlTag[];
41
+ }
42
+ interface HtmlTagGroupsHook {
43
+ tapPromise: (name: string, fn: (data: HtmlTagGroupsData) => Promise<HtmlTagGroupsData | void> | HtmlTagGroupsData | void) => void;
44
+ }
45
+ interface HtmlRspackPluginLike {
46
+ getCompilationHooks: (compilation: CompilationLike) => {
47
+ alterAssetTagGroups: HtmlTagGroupsHook;
48
+ };
49
+ }
50
+ interface RawSourceConstructor {
51
+ new (source: string): unknown;
52
+ }
53
+ interface CompilationLike {
54
+ assets?: Record<string, unknown>;
55
+ emitAsset?: (name: string, source: unknown) => void;
56
+ }
57
+ interface CompilerLike {
58
+ hooks: {
59
+ compilation: {
60
+ tap: (name: string, fn: (compilation: CompilationLike) => void) => void;
61
+ };
62
+ };
63
+ webpack?: {
64
+ sources?: {
65
+ RawSource?: RawSourceConstructor;
66
+ };
67
+ };
68
+ rspack?: {
69
+ HtmlRspackPlugin?: HtmlRspackPluginLike;
70
+ };
71
+ }
72
+ declare class PerfMonitorRspackPlugin {
73
+ options: PluginOptions;
74
+ constructor(pluginOptions?: PluginOptions);
75
+ apply(compiler: CompilerLike): void;
76
+ }
77
+ export { PerfMonitorRspackPlugin, type PluginOptions };
@@ -0,0 +1,6 @@
1
+ import { PluginOptions } from '..';
2
+ export declare const getPluginScripts: (options?: PluginOptions) => {
3
+ settingScript: string;
4
+ coreScript: string;
5
+ clientScript: string;
6
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@zk-tech/rrt-plugin-rspack",
3
+ "version": "0.0.2",
4
+ "main": "./dist/index.cjs",
5
+ "module": "./dist/index.cjs",
6
+ "dependencies": {
7
+ "@zk-tech/rrt-client": "0.0.2",
8
+ "@zk-tech/rrt-core": "0.0.2",
9
+ "@zk-tech/rrt-shared": "0.0.2"
10
+ },
11
+ "peerDependencies": {
12
+ "@rspack/core": ">=0.5.0"
13
+ },
14
+ "devDependencies": {
15
+ "@types/node": "^18.15.3",
16
+ "rimraf": "~3.0.2",
17
+ "typescript": "~5.0.4"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "require": "./dist/index.cjs",
27
+ "import": "./dist/index.cjs"
28
+ }
29
+ },
30
+ "private": false,
31
+ "scripts": {
32
+ "build": "vite build",
33
+ "dev": "vite build --watch",
34
+ "lint": "eslint src --ext .ts,.tsx",
35
+ "typecheck": "tsc --noEmit --emitDeclarationOnly false"
36
+ }
37
+ }