-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzotero-plugin.config.ts
More file actions
134 lines (124 loc) · 4.55 KB
/
Copy pathzotero-plugin.config.ts
File metadata and controls
134 lines (124 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { readFile } from "node:fs/promises";
import { defineConfig } from "zotero-plugin-scaffold";
import type { Plugin } from "esbuild";
import pkg from "./package.json";
// ---------------------------------------------------------------------------
// zotero-plugin-toolkit Zotero 8 compatibility patch
//
// The toolkit (through at least 5.1.2) feature-detects the legacy JSM loader
// with `typeof ChromeUtils.import === "undefined"`. On Zotero 8+ (Firefox
// 140) ChromeUtils.import still *exists* but only throws "ChromeUtils.import()
// has been removed. Use importESModule()", so the toolkit rewrites every
// *.sys.mjs path back to *.jsm and the import fails — one console error per
// toolkit helper constructed at startup (issue #40).
//
// Patch the bundled copy to try importESModule first (available since
// Firefox 106, so on every supported Zotero) and keep the legacy loader as
// the fallback. The build fails loudly if the toolkit source stops matching,
// so a toolkit upgrade can't silently un-apply or double-apply this.
// ---------------------------------------------------------------------------
const TOOLKIT_LEGACY_IMPORT = [
"function _importESModule(path) {",
'\tif (typeof ChromeUtils.import === "undefined") return ChromeUtils.importESModule(path, { global: "contextual" });',
'\tif (path.endsWith(".sys.mjs")) path = path.replace(/\\.sys\\.mjs$/, ".jsm");',
"\treturn ChromeUtils.import(path);",
"}",
].join("\n");
const TOOLKIT_PATCHED_IMPORT = [
"function _importESModule(path) {",
'\tif (typeof ChromeUtils.importESModule !== "undefined") {',
"\t\ttry {",
'\t\t\treturn ChromeUtils.importESModule(path, { global: "contextual" });',
"\t\t} catch {",
"\t\t\t/* fall through to the legacy loader below */",
"\t\t}",
"\t}",
'\tif (path.endsWith(".sys.mjs")) path = path.replace(/\\.sys\\.mjs$/, ".jsm");',
"\treturn ChromeUtils.import(path);",
"}",
].join("\n");
const patchToolkitEsmImport: Plugin = {
name: "patch-toolkit-esm-import",
setup(build) {
let patched = false;
build.onStart(() => {
patched = false;
});
build.onLoad(
{ filter: /[\\/]zotero-plugin-toolkit[\\/]dist[\\/]index\.js$/ },
async (args) => {
const source = await readFile(args.path, "utf8");
if (!source.includes(TOOLKIT_LEGACY_IMPORT)) {
throw new Error(
"zotero-plugin-toolkit's _importESModule no longer matches the " +
"Zotero 8 compatibility patch in zotero-plugin.config.ts. If " +
"the toolkit fixed its ChromeUtils.import feature detection " +
"upstream, delete the patch; otherwise update " +
`TOOLKIT_LEGACY_IMPORT to the new source. (${args.path})`,
);
}
patched = true;
return {
contents: source.replace(
TOOLKIT_LEGACY_IMPORT,
TOOLKIT_PATCHED_IMPORT,
),
loader: "js",
};
},
);
build.onEnd(() => {
if (!patched) {
throw new Error(
"patch-toolkit-esm-import did not run — zotero-plugin-toolkit's " +
"dist layout changed. Update the onLoad filter in " +
"zotero-plugin.config.ts or delete the patch if the toolkit " +
"fixed its ChromeUtils.import feature detection upstream.",
);
}
});
},
};
export default defineConfig({
source: ["src", "addon"],
dist: ".scaffold/build",
name: pkg.config.addonName,
id: pkg.config.addonID,
namespace: pkg.config.addonRef,
updateURL: `https://github.com/{{owner}}/{{repo}}/releases/download/release/${
pkg.version.includes("-") ? "update-beta.json" : "update.json"
}`,
xpiDownloadLink:
"https://github.com/{{owner}}/{{repo}}/releases/download/v{{version}}/{{xpiName}}.xpi",
build: {
assets: ["addon/**/*.*"],
define: {
...pkg.config,
author: pkg.author,
description: pkg.description,
homepage: pkg.homepage,
buildVersion: pkg.version,
buildTime: "{{buildTime}}",
},
prefs: {
prefix: pkg.config.prefsPrefix,
},
esbuildOptions: [
{
entryPoints: ["src/index.ts"],
define: {
__env__: `"${process.env.NODE_ENV}"`,
},
bundle: true,
target: "firefox115",
outfile: `.scaffold/build/addon/content/scripts/${pkg.config.addonRef}.js`,
plugins: [patchToolkitEsmImport],
},
],
},
test: {
waitForPlugin: `() => Zotero.${pkg.config.addonInstance}.data.initialized`,
},
// If you need to see a more detailed log, uncomment the following line:
// logLevel: "trace",
});