@fz-firefly/unplugin 0.0.0-stage → 0.2.1

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.
package/dist/vite.js ADDED
@@ -0,0 +1,68 @@
1
+ import { n as buildInlineScript, t as runSourcemapUpload } from "./run-C7OeN-YM.js";
2
+ import { resolve } from "node:path";
3
+ // src/vite.ts
4
+ /**
5
+ * Firefly Vite 插件。
6
+ *
7
+ * - `sourcemap.generate: true` 时自动设置 `build.sourcemap = true`
8
+ * - `closeBundle` 后扫描并上传 source map
9
+ * - 只处理 build,不影响 dev server
10
+ */
11
+ function fireflyVite(options = {}) {
12
+ var _options$sourcemap;
13
+ const sourcemap = options.sourcemap === false ? null : (_options$sourcemap = options.sourcemap) !== null && _options$sourcemap !== void 0 ? _options$sourcemap : null;
14
+ let resolvedConfig = null;
15
+ return {
16
+ name: "firefly",
17
+ apply: "build",
18
+ config() {
19
+ if (!(sourcemap === null || sourcemap === void 0 ? void 0 : sourcemap.generate)) return void 0;
20
+ return { build: { sourcemap: true } };
21
+ },
22
+ configResolved(config) {
23
+ resolvedConfig = config;
24
+ },
25
+ transformIndexHtml: {
26
+ order: "pre",
27
+ handler() {
28
+ const code = buildInlineScript(options);
29
+ if (!code) return void 0;
30
+ return [{
31
+ tag: "script",
32
+ attrs: {
33
+ "data-firefly-inline": "",
34
+ ...options.script && options.script.nonce ? { nonce: options.script.nonce } : {}
35
+ },
36
+ children: code,
37
+ injectTo: "head-prepend"
38
+ }];
39
+ }
40
+ },
41
+ async closeBundle() {
42
+ var _sourcemap$platform, _sourcemap$base, _sourcemap$sourceRoot;
43
+ if (!sourcemap || !resolvedConfig) return;
44
+ const root = resolvedConfig.root;
45
+ const outDir = resolve(root, resolvedConfig.build.outDir);
46
+ const platform = (_sourcemap$platform = sourcemap.platform) !== null && _sourcemap$platform !== void 0 ? _sourcemap$platform : process.env.UNI_PLATFORM;
47
+ const mapDirs = [
48
+ sourcemap.mapDir,
49
+ platform ? resolve(outDir, "..", ".sourcemap", platform) : "",
50
+ outDir
51
+ ].filter((value) => Boolean(value));
52
+ await runSourcemapUpload({
53
+ options,
54
+ sourcemap,
55
+ outDir,
56
+ mapDirs,
57
+ base: (_sourcemap$base = sourcemap.base) !== null && _sourcemap$base !== void 0 ? _sourcemap$base : resolvedConfig.base,
58
+ mode: resolvedConfig.mode,
59
+ isWatch: resolvedConfig.build.watch != null,
60
+ context: root,
61
+ sourceRoot: (_sourcemap$sourceRoot = sourcemap.sourceRoot) !== null && _sourcemap$sourceRoot !== void 0 ? _sourcemap$sourceRoot : root
62
+ });
63
+ }
64
+ };
65
+ }
66
+ export { fireflyVite as default, fireflyVite };
67
+
68
+ //# sourceMappingURL=vite.js.map
@@ -0,0 +1,170 @@
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ const require_run = require("./run-Bp9vcqXk.cjs");
6
+ let node_path = require("node:path");
7
+ // src/webpack.ts
8
+ const PLUGIN_NAME = "FireflyWebpackPlugin";
9
+ /** 同一个 build_id 只真正上传一次,避免 Taro 多 compiler 重复上传。 */
10
+ const uploadedBuildIds = /* @__PURE__ */ new Set();
11
+ const uploadingBuildIds = /* @__PURE__ */ new Map();
12
+ /**
13
+ * Firefly webpack 插件。
14
+ *
15
+ * 兼容 webpack 4 / 5:只依赖 compiler.options 和 afterEmit hook,
16
+ * 不直接 import webpack,避免不同版本的类型冲突。
17
+ */
18
+ var FireflyWebpackPlugin = class {
19
+ constructor(options = {}) {
20
+ this.options = options;
21
+ }
22
+ apply(compiler) {
23
+ var _this$options$sourcem;
24
+ if (this.options.script) this.installInlineScript(compiler, this.options.script);
25
+ const sourcemap = this.options.sourcemap === false ? null : (_this$options$sourcem = this.options.sourcemap) !== null && _this$options$sourcem !== void 0 ? _this$options$sourcem : null;
26
+ if (sourcemap === null || sourcemap === void 0 ? void 0 : sourcemap.generate) {
27
+ process.env.SOURCEMAP = "true";
28
+ if (!compiler.options.devtool) compiler.options.devtool = "source-map";
29
+ }
30
+ if (!sourcemap) return;
31
+ if (Boolean(process.env.TARO_ENV) && compiler.hooks.shutdown) {
32
+ compiler.hooks.shutdown.tapPromise(PLUGIN_NAME, async () => {
33
+ await this.uploadOnce(compiler, {}, sourcemap);
34
+ });
35
+ return;
36
+ }
37
+ compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {
38
+ if (compilation.errors && compilation.errors.length > 0) return;
39
+ await this.uploadOnce(compiler, compilation, sourcemap);
40
+ });
41
+ }
42
+ /** 把浏览器 SDK 以 inline script 注入 HTML 产物。 */
43
+ installInlineScript(compiler, script) {
44
+ var _compiler$webpack;
45
+ const code = require_run.buildInlineScript(this.options);
46
+ if (!code) return;
47
+ const inject = (compilation) => {
48
+ try {
49
+ this.injectInlineAssets(compilation, code, script.nonce, compiler);
50
+ } catch (error) {
51
+ console.warn(`[firefly] 内联 SDK 注入失败:${error instanceof Error ? error.message : String(error)}`);
52
+ }
53
+ };
54
+ if (compiler.hooks.compilation && ((_compiler$webpack = compiler.webpack) === null || _compiler$webpack === void 0 || (_compiler$webpack = _compiler$webpack.Compilation) === null || _compiler$webpack === void 0 ? void 0 : _compiler$webpack.PROCESS_ASSETS_STAGE_REPORT) !== void 0) {
55
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
56
+ var _compilation$hooks, _compiler$webpack2;
57
+ (_compilation$hooks = compilation.hooks) === null || _compilation$hooks === void 0 || (_compilation$hooks = _compilation$hooks.processAssets) === null || _compilation$hooks === void 0 || _compilation$hooks.tap({
58
+ name: `${PLUGIN_NAME}:inline-script`,
59
+ stage: (_compiler$webpack2 = compiler.webpack) === null || _compiler$webpack2 === void 0 || (_compiler$webpack2 = _compiler$webpack2.Compilation) === null || _compiler$webpack2 === void 0 ? void 0 : _compiler$webpack2.PROCESS_ASSETS_STAGE_REPORT
60
+ }, () => inject(compilation));
61
+ });
62
+ return;
63
+ }
64
+ if (!compiler.hooks.emit) {
65
+ console.warn("[firefly] 跳过内联 SDK:当前 webpack 没有 emit hook");
66
+ return;
67
+ }
68
+ compiler.hooks.emit.tapAsync({
69
+ name: `${PLUGIN_NAME}:inline-script`,
70
+ stage: 1e4
71
+ }, (compilation, callback) => {
72
+ inject(compilation);
73
+ callback();
74
+ });
75
+ }
76
+ /** 只修改 HTML asset;没有 HTML 时静默跳过(多 compiler 构建很常见)。 */
77
+ injectInlineAssets(compilation, code, nonce, compiler) {
78
+ const assets = compilation.assets;
79
+ if (!assets) return;
80
+ const htmlNames = Object.keys(assets).filter((name) => /\.html?$/i.test(name));
81
+ for (const name of htmlNames) {
82
+ const asset = assets[name];
83
+ if (!asset || typeof asset.source !== "function") continue;
84
+ const html = asset.source().toString();
85
+ const next = require_run.injectInlineScriptHtml(html, code, nonce);
86
+ if (next === html) continue;
87
+ const source = this.createAsset(next, compiler);
88
+ if (compilation.updateAsset) compilation.updateAsset(name, source);
89
+ else assets[name] = source;
90
+ }
91
+ }
92
+ /** 兼容 webpack 4 / 5 的 RawSource。 */
93
+ createAsset(content, compiler) {
94
+ var _compiler$webpack3;
95
+ const RawSource = (_compiler$webpack3 = compiler.webpack) === null || _compiler$webpack3 === void 0 || (_compiler$webpack3 = _compiler$webpack3.sources) === null || _compiler$webpack3 === void 0 ? void 0 : _compiler$webpack3.RawSource;
96
+ if (RawSource) return new RawSource(content);
97
+ return {
98
+ source: () => content,
99
+ size: () => Buffer.byteLength(content)
100
+ };
101
+ }
102
+ /** 同一个 build_id 在多个 compiler 之间只允许一个上传任务。 */
103
+ async uploadOnce(compiler, compilation, sourcemap) {
104
+ var _this$options$buildId, _this$options$release, _this$options$dist;
105
+ const buildId = (_this$options$buildId = this.options.buildId) === null || _this$options$buildId === void 0 ? void 0 : _this$options$buildId.trim();
106
+ const key = buildId ? `${(_this$options$release = this.options.release) !== null && _this$options$release !== void 0 ? _this$options$release : ""}\u0000${(_this$options$dist = this.options.dist) !== null && _this$options$dist !== void 0 ? _this$options$dist : ""}\u0000${buildId}` : "";
107
+ if (key) {
108
+ if (uploadedBuildIds.has(key)) return;
109
+ const running = uploadingBuildIds.get(key);
110
+ if (running) return running;
111
+ }
112
+ const task = (async () => {
113
+ await this.upload(compiler, compilation, sourcemap);
114
+ if (key) uploadedBuildIds.add(key);
115
+ })();
116
+ if (key) uploadingBuildIds.set(key, task);
117
+ try {
118
+ await task;
119
+ } finally {
120
+ if (key) uploadingBuildIds.delete(key);
121
+ }
122
+ }
123
+ async upload(compiler, compilation, sourcemap) {
124
+ var _compiler$options$con, _ref, _sourcemap$outDir, _compilation$outputOp, _compiler$options$out, _sourcemap$platform, _compiler$options$out2, _sourcemap$base, _sourcemap$sourceRoot;
125
+ const context = (_compiler$options$con = compiler.options.context) !== null && _compiler$options$con !== void 0 ? _compiler$options$con : process.cwd();
126
+ const rawOutDir = (_ref = (_sourcemap$outDir = sourcemap.outDir) !== null && _sourcemap$outDir !== void 0 ? _sourcemap$outDir : (_compilation$outputOp = compilation.outputOptions) === null || _compilation$outputOp === void 0 ? void 0 : _compilation$outputOp.path) !== null && _ref !== void 0 ? _ref : (_compiler$options$out = compiler.options.output) === null || _compiler$options$out === void 0 ? void 0 : _compiler$options$out.path;
127
+ if (!rawOutDir) {
128
+ if (sourcemap.log !== false) console.warn("[firefly] 跳过 sourcemap 上传:找不到 webpack output.path");
129
+ return;
130
+ }
131
+ const outDir = (0, node_path.resolve)(context, rawOutDir);
132
+ const platform = (_sourcemap$platform = sourcemap.platform) !== null && _sourcemap$platform !== void 0 ? _sourcemap$platform : process.env.UNI_PLATFORM;
133
+ const mapDirs = [
134
+ sourcemap.mapDir,
135
+ platform ? (0, node_path.resolve)(outDir, "..", ".sourcemap", platform) : "",
136
+ outDir
137
+ ].filter((value) => Boolean(value));
138
+ const publicPath = (_compiler$options$out2 = compiler.options.output) === null || _compiler$options$out2 === void 0 ? void 0 : _compiler$options$out2.publicPath;
139
+ const base = (_sourcemap$base = sourcemap.base) !== null && _sourcemap$base !== void 0 ? _sourcemap$base : typeof publicPath === "string" ? publicPath : void 0;
140
+ await require_run.runSourcemapUpload({
141
+ options: this.options,
142
+ sourcemap,
143
+ outDir,
144
+ mapDirs,
145
+ base,
146
+ mode: compiler.options.mode,
147
+ isWatch: compiler.watchMode === true,
148
+ context,
149
+ sourceRoot: (_sourcemap$sourceRoot = sourcemap.sourceRoot) !== null && _sourcemap$sourceRoot !== void 0 ? _sourcemap$sourceRoot : context
150
+ });
151
+ }
152
+ };
153
+ /**
154
+ * 创建 webpack 插件。
155
+ *
156
+ * 用法:
157
+ * ```js
158
+ * const { fireflyWebpack } = require('@fz-firefly/unplugin/webpack')
159
+ * plugins: [fireflyWebpack({ endpoint, release, dist, sourcemap: { token } })]
160
+ * ```
161
+ */
162
+ function fireflyWebpack(options = {}) {
163
+ if (options.sourcemap && options.sourcemap.generate) process.env.SOURCEMAP = "true";
164
+ return new FireflyWebpackPlugin(options);
165
+ }
166
+ exports.FireflyWebpackPlugin = FireflyWebpackPlugin;
167
+ exports.default = fireflyWebpack;
168
+ exports.fireflyWebpack = fireflyWebpack;
169
+
170
+ //# sourceMappingURL=webpack.cjs.map
@@ -0,0 +1,91 @@
1
+ import { a as FireflyWebpackOptions } from "./options-Dn99v-iv.cjs";
2
+ interface WebpackOutputLike {
3
+ path?: string;
4
+ publicPath?: string | ((...args: unknown[]) => string);
5
+ }
6
+ interface WebpackCompilerLike {
7
+ options: {
8
+ output?: WebpackOutputLike;
9
+ mode?: string;
10
+ devtool?: string | false;
11
+ context?: string;
12
+ };
13
+ webpack?: {
14
+ sources?: {
15
+ RawSource?: new (value: string) => unknown;
16
+ };
17
+ Compilation?: {
18
+ PROCESS_ASSETS_STAGE_REPORT?: number;
19
+ };
20
+ };
21
+ hooks: {
22
+ afterEmit: {
23
+ tapPromise(name: string, handler: (compilation: WebpackCompilationLike) => Promise<void>): void;
24
+ };
25
+ compilation?: {
26
+ tap(name: string, handler: (compilation: WebpackCompilationLike) => void): void;
27
+ };
28
+ emit?: {
29
+ tapAsync(options: {
30
+ name: string;
31
+ stage?: number;
32
+ }, handler: (compilation: WebpackCompilationLike, callback: (error?: Error | null) => void) => void): void;
33
+ };
34
+ shutdown?: {
35
+ tapPromise(name: string, handler: () => Promise<void>): void;
36
+ };
37
+ };
38
+ watchMode?: boolean;
39
+ }
40
+ interface WebpackAssetLike {
41
+ source(): string | Buffer;
42
+ size?(): number;
43
+ }
44
+ interface WebpackCompilationLike {
45
+ errors?: unknown[];
46
+ assets?: Record<string, WebpackAssetLike>;
47
+ outputOptions?: {
48
+ path?: string;
49
+ };
50
+ hooks?: {
51
+ processAssets?: {
52
+ tap(options: {
53
+ name: string;
54
+ stage?: number;
55
+ }, handler: (assets: Record<string, WebpackAssetLike>) => void): void;
56
+ };
57
+ };
58
+ updateAsset?: (name: string, source: unknown) => void;
59
+ }
60
+ /**
61
+ * Firefly webpack 插件。
62
+ *
63
+ * 兼容 webpack 4 / 5:只依赖 compiler.options 和 afterEmit hook,
64
+ * 不直接 import webpack,避免不同版本的类型冲突。
65
+ */
66
+ export declare class FireflyWebpackPlugin {
67
+ private readonly options;
68
+ constructor(options?: FireflyWebpackOptions);
69
+ apply(compiler: WebpackCompilerLike): void;
70
+ /** 把浏览器 SDK 以 inline script 注入 HTML 产物。 */
71
+ private installInlineScript;
72
+ /** 只修改 HTML asset;没有 HTML 时静默跳过(多 compiler 构建很常见)。 */
73
+ private injectInlineAssets;
74
+ /** 兼容 webpack 4 / 5 的 RawSource。 */
75
+ private createAsset;
76
+ /** 同一个 build_id 在多个 compiler 之间只允许一个上传任务。 */
77
+ private uploadOnce;
78
+ private upload;
79
+ }
80
+ /**
81
+ * 创建 webpack 插件。
82
+ *
83
+ * 用法:
84
+ * ```js
85
+ * const { fireflyWebpack } = require('@fz-firefly/unplugin/webpack')
86
+ * plugins: [fireflyWebpack({ endpoint, release, dist, sourcemap: { token } })]
87
+ * ```
88
+ */
89
+ export declare function fireflyWebpack(options?: FireflyWebpackOptions): FireflyWebpackPlugin;
90
+ export { fireflyWebpack as default };
91
+ //# sourceMappingURL=webpack.d.cts.map
@@ -0,0 +1,91 @@
1
+ import { a as FireflyWebpackOptions } from "./options-Dn99v-iv.js";
2
+ interface WebpackOutputLike {
3
+ path?: string;
4
+ publicPath?: string | ((...args: unknown[]) => string);
5
+ }
6
+ interface WebpackCompilerLike {
7
+ options: {
8
+ output?: WebpackOutputLike;
9
+ mode?: string;
10
+ devtool?: string | false;
11
+ context?: string;
12
+ };
13
+ webpack?: {
14
+ sources?: {
15
+ RawSource?: new (value: string) => unknown;
16
+ };
17
+ Compilation?: {
18
+ PROCESS_ASSETS_STAGE_REPORT?: number;
19
+ };
20
+ };
21
+ hooks: {
22
+ afterEmit: {
23
+ tapPromise(name: string, handler: (compilation: WebpackCompilationLike) => Promise<void>): void;
24
+ };
25
+ compilation?: {
26
+ tap(name: string, handler: (compilation: WebpackCompilationLike) => void): void;
27
+ };
28
+ emit?: {
29
+ tapAsync(options: {
30
+ name: string;
31
+ stage?: number;
32
+ }, handler: (compilation: WebpackCompilationLike, callback: (error?: Error | null) => void) => void): void;
33
+ };
34
+ shutdown?: {
35
+ tapPromise(name: string, handler: () => Promise<void>): void;
36
+ };
37
+ };
38
+ watchMode?: boolean;
39
+ }
40
+ interface WebpackAssetLike {
41
+ source(): string | Buffer;
42
+ size?(): number;
43
+ }
44
+ interface WebpackCompilationLike {
45
+ errors?: unknown[];
46
+ assets?: Record<string, WebpackAssetLike>;
47
+ outputOptions?: {
48
+ path?: string;
49
+ };
50
+ hooks?: {
51
+ processAssets?: {
52
+ tap(options: {
53
+ name: string;
54
+ stage?: number;
55
+ }, handler: (assets: Record<string, WebpackAssetLike>) => void): void;
56
+ };
57
+ };
58
+ updateAsset?: (name: string, source: unknown) => void;
59
+ }
60
+ /**
61
+ * Firefly webpack 插件。
62
+ *
63
+ * 兼容 webpack 4 / 5:只依赖 compiler.options 和 afterEmit hook,
64
+ * 不直接 import webpack,避免不同版本的类型冲突。
65
+ */
66
+ export declare class FireflyWebpackPlugin {
67
+ private readonly options;
68
+ constructor(options?: FireflyWebpackOptions);
69
+ apply(compiler: WebpackCompilerLike): void;
70
+ /** 把浏览器 SDK 以 inline script 注入 HTML 产物。 */
71
+ private installInlineScript;
72
+ /** 只修改 HTML asset;没有 HTML 时静默跳过(多 compiler 构建很常见)。 */
73
+ private injectInlineAssets;
74
+ /** 兼容 webpack 4 / 5 的 RawSource。 */
75
+ private createAsset;
76
+ /** 同一个 build_id 在多个 compiler 之间只允许一个上传任务。 */
77
+ private uploadOnce;
78
+ private upload;
79
+ }
80
+ /**
81
+ * 创建 webpack 插件。
82
+ *
83
+ * 用法:
84
+ * ```js
85
+ * const { fireflyWebpack } = require('@fz-firefly/unplugin/webpack')
86
+ * plugins: [fireflyWebpack({ endpoint, release, dist, sourcemap: { token } })]
87
+ * ```
88
+ */
89
+ export declare function fireflyWebpack(options?: FireflyWebpackOptions): FireflyWebpackPlugin;
90
+ export { fireflyWebpack as default };
91
+ //# sourceMappingURL=webpack.d.ts.map
@@ -0,0 +1,164 @@
1
+ import { n as buildInlineScript, r as injectInlineScriptHtml, t as runSourcemapUpload } from "./run-C7OeN-YM.js";
2
+ import { resolve } from "node:path";
3
+ // src/webpack.ts
4
+ const PLUGIN_NAME = "FireflyWebpackPlugin";
5
+ /** 同一个 build_id 只真正上传一次,避免 Taro 多 compiler 重复上传。 */
6
+ const uploadedBuildIds = /* @__PURE__ */ new Set();
7
+ const uploadingBuildIds = /* @__PURE__ */ new Map();
8
+ /**
9
+ * Firefly webpack 插件。
10
+ *
11
+ * 兼容 webpack 4 / 5:只依赖 compiler.options 和 afterEmit hook,
12
+ * 不直接 import webpack,避免不同版本的类型冲突。
13
+ */
14
+ var FireflyWebpackPlugin = class {
15
+ constructor(options = {}) {
16
+ this.options = options;
17
+ }
18
+ apply(compiler) {
19
+ var _this$options$sourcem;
20
+ if (this.options.script) this.installInlineScript(compiler, this.options.script);
21
+ const sourcemap = this.options.sourcemap === false ? null : (_this$options$sourcem = this.options.sourcemap) !== null && _this$options$sourcem !== void 0 ? _this$options$sourcem : null;
22
+ if (sourcemap === null || sourcemap === void 0 ? void 0 : sourcemap.generate) {
23
+ process.env.SOURCEMAP = "true";
24
+ if (!compiler.options.devtool) compiler.options.devtool = "source-map";
25
+ }
26
+ if (!sourcemap) return;
27
+ if (Boolean(process.env.TARO_ENV) && compiler.hooks.shutdown) {
28
+ compiler.hooks.shutdown.tapPromise(PLUGIN_NAME, async () => {
29
+ await this.uploadOnce(compiler, {}, sourcemap);
30
+ });
31
+ return;
32
+ }
33
+ compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {
34
+ if (compilation.errors && compilation.errors.length > 0) return;
35
+ await this.uploadOnce(compiler, compilation, sourcemap);
36
+ });
37
+ }
38
+ /** 把浏览器 SDK 以 inline script 注入 HTML 产物。 */
39
+ installInlineScript(compiler, script) {
40
+ var _compiler$webpack;
41
+ const code = buildInlineScript(this.options);
42
+ if (!code) return;
43
+ const inject = (compilation) => {
44
+ try {
45
+ this.injectInlineAssets(compilation, code, script.nonce, compiler);
46
+ } catch (error) {
47
+ console.warn(`[firefly] 内联 SDK 注入失败:${error instanceof Error ? error.message : String(error)}`);
48
+ }
49
+ };
50
+ if (compiler.hooks.compilation && ((_compiler$webpack = compiler.webpack) === null || _compiler$webpack === void 0 || (_compiler$webpack = _compiler$webpack.Compilation) === null || _compiler$webpack === void 0 ? void 0 : _compiler$webpack.PROCESS_ASSETS_STAGE_REPORT) !== void 0) {
51
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
52
+ var _compilation$hooks, _compiler$webpack2;
53
+ (_compilation$hooks = compilation.hooks) === null || _compilation$hooks === void 0 || (_compilation$hooks = _compilation$hooks.processAssets) === null || _compilation$hooks === void 0 || _compilation$hooks.tap({
54
+ name: `${PLUGIN_NAME}:inline-script`,
55
+ stage: (_compiler$webpack2 = compiler.webpack) === null || _compiler$webpack2 === void 0 || (_compiler$webpack2 = _compiler$webpack2.Compilation) === null || _compiler$webpack2 === void 0 ? void 0 : _compiler$webpack2.PROCESS_ASSETS_STAGE_REPORT
56
+ }, () => inject(compilation));
57
+ });
58
+ return;
59
+ }
60
+ if (!compiler.hooks.emit) {
61
+ console.warn("[firefly] 跳过内联 SDK:当前 webpack 没有 emit hook");
62
+ return;
63
+ }
64
+ compiler.hooks.emit.tapAsync({
65
+ name: `${PLUGIN_NAME}:inline-script`,
66
+ stage: 1e4
67
+ }, (compilation, callback) => {
68
+ inject(compilation);
69
+ callback();
70
+ });
71
+ }
72
+ /** 只修改 HTML asset;没有 HTML 时静默跳过(多 compiler 构建很常见)。 */
73
+ injectInlineAssets(compilation, code, nonce, compiler) {
74
+ const assets = compilation.assets;
75
+ if (!assets) return;
76
+ const htmlNames = Object.keys(assets).filter((name) => /\.html?$/i.test(name));
77
+ for (const name of htmlNames) {
78
+ const asset = assets[name];
79
+ if (!asset || typeof asset.source !== "function") continue;
80
+ const html = asset.source().toString();
81
+ const next = injectInlineScriptHtml(html, code, nonce);
82
+ if (next === html) continue;
83
+ const source = this.createAsset(next, compiler);
84
+ if (compilation.updateAsset) compilation.updateAsset(name, source);
85
+ else assets[name] = source;
86
+ }
87
+ }
88
+ /** 兼容 webpack 4 / 5 的 RawSource。 */
89
+ createAsset(content, compiler) {
90
+ var _compiler$webpack3;
91
+ const RawSource = (_compiler$webpack3 = compiler.webpack) === null || _compiler$webpack3 === void 0 || (_compiler$webpack3 = _compiler$webpack3.sources) === null || _compiler$webpack3 === void 0 ? void 0 : _compiler$webpack3.RawSource;
92
+ if (RawSource) return new RawSource(content);
93
+ return {
94
+ source: () => content,
95
+ size: () => Buffer.byteLength(content)
96
+ };
97
+ }
98
+ /** 同一个 build_id 在多个 compiler 之间只允许一个上传任务。 */
99
+ async uploadOnce(compiler, compilation, sourcemap) {
100
+ var _this$options$buildId, _this$options$release, _this$options$dist;
101
+ const buildId = (_this$options$buildId = this.options.buildId) === null || _this$options$buildId === void 0 ? void 0 : _this$options$buildId.trim();
102
+ const key = buildId ? `${(_this$options$release = this.options.release) !== null && _this$options$release !== void 0 ? _this$options$release : ""}\u0000${(_this$options$dist = this.options.dist) !== null && _this$options$dist !== void 0 ? _this$options$dist : ""}\u0000${buildId}` : "";
103
+ if (key) {
104
+ if (uploadedBuildIds.has(key)) return;
105
+ const running = uploadingBuildIds.get(key);
106
+ if (running) return running;
107
+ }
108
+ const task = (async () => {
109
+ await this.upload(compiler, compilation, sourcemap);
110
+ if (key) uploadedBuildIds.add(key);
111
+ })();
112
+ if (key) uploadingBuildIds.set(key, task);
113
+ try {
114
+ await task;
115
+ } finally {
116
+ if (key) uploadingBuildIds.delete(key);
117
+ }
118
+ }
119
+ async upload(compiler, compilation, sourcemap) {
120
+ var _compiler$options$con, _ref, _sourcemap$outDir, _compilation$outputOp, _compiler$options$out, _sourcemap$platform, _compiler$options$out2, _sourcemap$base, _sourcemap$sourceRoot;
121
+ const context = (_compiler$options$con = compiler.options.context) !== null && _compiler$options$con !== void 0 ? _compiler$options$con : process.cwd();
122
+ const rawOutDir = (_ref = (_sourcemap$outDir = sourcemap.outDir) !== null && _sourcemap$outDir !== void 0 ? _sourcemap$outDir : (_compilation$outputOp = compilation.outputOptions) === null || _compilation$outputOp === void 0 ? void 0 : _compilation$outputOp.path) !== null && _ref !== void 0 ? _ref : (_compiler$options$out = compiler.options.output) === null || _compiler$options$out === void 0 ? void 0 : _compiler$options$out.path;
123
+ if (!rawOutDir) {
124
+ if (sourcemap.log !== false) console.warn("[firefly] 跳过 sourcemap 上传:找不到 webpack output.path");
125
+ return;
126
+ }
127
+ const outDir = resolve(context, rawOutDir);
128
+ const platform = (_sourcemap$platform = sourcemap.platform) !== null && _sourcemap$platform !== void 0 ? _sourcemap$platform : process.env.UNI_PLATFORM;
129
+ const mapDirs = [
130
+ sourcemap.mapDir,
131
+ platform ? resolve(outDir, "..", ".sourcemap", platform) : "",
132
+ outDir
133
+ ].filter((value) => Boolean(value));
134
+ const publicPath = (_compiler$options$out2 = compiler.options.output) === null || _compiler$options$out2 === void 0 ? void 0 : _compiler$options$out2.publicPath;
135
+ const base = (_sourcemap$base = sourcemap.base) !== null && _sourcemap$base !== void 0 ? _sourcemap$base : typeof publicPath === "string" ? publicPath : void 0;
136
+ await runSourcemapUpload({
137
+ options: this.options,
138
+ sourcemap,
139
+ outDir,
140
+ mapDirs,
141
+ base,
142
+ mode: compiler.options.mode,
143
+ isWatch: compiler.watchMode === true,
144
+ context,
145
+ sourceRoot: (_sourcemap$sourceRoot = sourcemap.sourceRoot) !== null && _sourcemap$sourceRoot !== void 0 ? _sourcemap$sourceRoot : context
146
+ });
147
+ }
148
+ };
149
+ /**
150
+ * 创建 webpack 插件。
151
+ *
152
+ * 用法:
153
+ * ```js
154
+ * const { fireflyWebpack } = require('@fz-firefly/unplugin/webpack')
155
+ * plugins: [fireflyWebpack({ endpoint, release, dist, sourcemap: { token } })]
156
+ * ```
157
+ */
158
+ function fireflyWebpack(options = {}) {
159
+ if (options.sourcemap && options.sourcemap.generate) process.env.SOURCEMAP = "true";
160
+ return new FireflyWebpackPlugin(options);
161
+ }
162
+ export { FireflyWebpackPlugin, fireflyWebpack as default, fireflyWebpack };
163
+
164
+ //# sourceMappingURL=webpack.js.map
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@fz-firefly/unplugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.1",
4
+ "description": "萤火虫 SDK 的构建插件:webpack 4/5 source map 生成开关、扫描与自动上传。",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ },
15
+ "./webpack": {
16
+ "types": "./dist/webpack.d.ts",
17
+ "import": "./dist/webpack.js",
18
+ "require": "./dist/webpack.cjs"
19
+ },
20
+ "./vite": {
21
+ "types": "./dist/vite.d.ts",
22
+ "import": "./dist/vite.js",
23
+ "require": "./dist/vite.cjs"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "!dist/**/*.map"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "peerDependencies": {
35
+ "vite": ">=4",
36
+ "webpack": ">=4"
37
+ },
38
+ "peerDependenciesMeta": {
39
+ "vite": {
40
+ "optional": true
41
+ },
42
+ "webpack": {
43
+ "optional": true
44
+ }
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^22.0.0",
48
+ "tsdown": "0.23.2",
49
+ "typescript": "^5.6.0",
50
+ "vite": "^8.0.0",
51
+ "vitest": "^5.0.5",
52
+ "webpack": "^5.0.0"
53
+ },
54
+ "scripts": {
55
+ "build": "tsdown",
56
+ "test": "vitest run",
57
+ "typecheck": "tsc --noEmit"
58
+ }
6
59
  }