wxt 0.20.13 → 0.20.14
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/browser.d.mts +20 -0
- package/dist/browser.mjs +18 -3
- package/dist/builtin-modules/index.mjs +7 -2
- package/dist/builtin-modules/unimport.mjs +69 -75
- package/dist/cli/cli-utils.mjs +52 -50
- package/dist/cli/commands.mjs +77 -103
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +9 -4
- package/dist/core/{build.d.ts → build.d.mts} +6 -2
- package/dist/core/build.mjs +23 -4
- package/dist/core/builders/vite/index.mjs +292 -343
- package/dist/core/builders/vite/plugins/bundleAnalysis.mjs +11 -12
- package/dist/core/builders/vite/plugins/cssEntrypoints.mjs +27 -20
- package/dist/core/builders/vite/plugins/defineImportMeta.mjs +17 -12
- package/dist/core/builders/vite/plugins/devHtmlPrerender.mjs +104 -136
- package/dist/core/builders/vite/plugins/devServerGlobals.mjs +15 -14
- package/dist/core/builders/vite/plugins/download.mjs +23 -12
- package/dist/core/builders/vite/plugins/entrypointGroupGlobals.mjs +18 -14
- package/dist/core/builders/vite/plugins/extensionApiMock.mjs +31 -33
- package/dist/core/builders/vite/plugins/globals.mjs +14 -13
- package/dist/core/builders/vite/plugins/iifeFooter.mjs +17 -11
- package/dist/core/builders/vite/plugins/index.mjs +18 -16
- package/dist/core/builders/vite/plugins/noopBackground.mjs +21 -15
- package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.mjs +25 -23
- package/dist/core/builders/vite/plugins/resolveAppConfig.mjs +25 -23
- package/dist/core/builders/vite/plugins/resolveVirtualModules.mjs +27 -26
- package/dist/core/builders/vite/plugins/tsconfigPaths.mjs +11 -11
- package/dist/core/builders/vite/plugins/wxtPluginLoader.mjs +41 -47
- package/dist/core/{clean.d.ts → clean.d.mts} +7 -3
- package/dist/core/clean.mjs +38 -35
- package/dist/core/{create-server.d.ts → create-server.d.mts} +6 -2
- package/dist/core/create-server.mjs +236 -247
- package/dist/core/define-config.d.mts +6 -0
- package/dist/core/define-config.mjs +6 -2
- package/dist/core/define-web-ext-config.d.mts +13 -0
- package/dist/core/define-web-ext-config.mjs +16 -7
- package/dist/core/generate-wxt-dir.mjs +109 -148
- package/dist/core/index.d.mts +8 -0
- package/dist/core/index.mjs +10 -8
- package/dist/core/initialize.d.mts +8 -0
- package/dist/core/initialize.mjs +110 -124
- package/dist/core/keyboard-shortcuts.mjs +30 -28
- package/dist/core/package-managers/bun.mjs +18 -16
- package/dist/core/package-managers/deno.mjs +12 -8
- package/dist/core/package-managers/index.mjs +52 -60
- package/dist/core/package-managers/npm.mjs +51 -52
- package/dist/core/package-managers/pnpm.mjs +21 -18
- package/dist/core/package-managers/yarn.mjs +31 -28
- package/dist/core/prepare.d.mts +6 -0
- package/dist/core/prepare.mjs +12 -7
- package/dist/core/resolve-config.mjs +393 -462
- package/dist/core/runners/index.mjs +11 -6
- package/dist/core/runners/manual.mjs +16 -14
- package/dist/core/runners/safari.mjs +16 -14
- package/dist/core/runners/web-ext.mjs +62 -83
- package/dist/core/runners/wsl.mjs +16 -14
- package/dist/core/utils/arrays.mjs +23 -12
- package/dist/core/utils/building/build-entrypoints.mjs +44 -40
- package/dist/core/utils/building/detect-dev-changes.mjs +84 -105
- package/dist/core/utils/building/find-entrypoints.mjs +275 -359
- package/dist/core/utils/building/group-entrypoints.mjs +42 -35
- package/dist/core/utils/building/index.mjs +8 -6
- package/dist/core/utils/building/internal-build.mjs +87 -102
- package/dist/core/utils/building/rebuild.mjs +50 -30
- package/dist/core/utils/cache.mjs +29 -18
- package/dist/core/utils/constants.mjs +9 -1
- package/dist/core/utils/content-scripts.mjs +56 -54
- package/dist/core/utils/content-security-policy.mjs +36 -39
- package/dist/core/utils/entrypoints.mjs +51 -28
- package/dist/core/utils/env.mjs +22 -17
- package/dist/core/utils/environments/browser-environment.mjs +13 -12
- package/dist/core/utils/environments/environment.mjs +36 -33
- package/dist/core/utils/environments/extension-environment.mjs +15 -10
- package/dist/core/utils/environments/index.mjs +4 -2
- package/dist/core/utils/eslint.mjs +11 -10
- package/dist/core/utils/fs.mjs +23 -11
- package/dist/core/utils/globals.mjs +53 -51
- package/dist/core/utils/i18n.mjs +41 -32
- package/dist/core/utils/log/index.mjs +6 -4
- package/dist/core/utils/log/printBuildSummary.mjs +20 -25
- package/dist/core/utils/log/printFileList.mjs +30 -33
- package/dist/core/utils/log/printHeader.mjs +10 -5
- package/dist/core/utils/log/printTable.mjs +19 -21
- package/dist/core/utils/manifest.mjs +364 -479
- package/dist/core/utils/minimatch-multiple.mjs +26 -15
- package/dist/core/utils/network.mjs +30 -33
- package/dist/core/utils/number.mjs +7 -3
- package/dist/core/utils/package.mjs +20 -13
- package/dist/core/utils/paths.mjs +26 -7
- package/dist/core/utils/strings.mjs +19 -14
- package/dist/core/utils/syntax-errors.mjs +14 -16
- package/dist/core/utils/time.mjs +19 -15
- package/dist/core/utils/transform.mjs +129 -160
- package/dist/core/utils/types.d.mts +6 -0
- package/dist/core/utils/validation.mjs +43 -50
- package/dist/core/utils/virtual-modules.mjs +20 -12
- package/dist/core/utils/wsl.mjs +10 -3
- package/dist/core/wxt.mjs +66 -60
- package/dist/core/{zip.d.ts → zip.d.mts} +6 -2
- package/dist/core/zip.mjs +116 -140
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +12 -3
- package/dist/{modules.d.ts → modules.d.mts} +14 -16
- package/dist/modules.mjs +181 -51
- package/dist/testing/fake-browser.d.mts +2 -0
- package/dist/testing/fake-browser.mjs +3 -1
- package/dist/testing/index.d.mts +3 -0
- package/dist/testing/index.mjs +4 -2
- package/dist/testing/{wxt-vitest-plugin.d.ts → wxt-vitest-plugin.d.mts} +7 -9
- package/dist/testing/wxt-vitest-plugin.mjs +38 -19
- package/dist/types.d.mts +1453 -0
- package/dist/utils/app-config.d.mts +6 -0
- package/dist/utils/app-config.mjs +8 -2
- package/dist/utils/content-script-context.d.mts +137 -0
- package/dist/utils/content-script-context.mjs +192 -175
- package/dist/utils/content-script-ui/iframe.d.mts +42 -0
- package/dist/utils/content-script-ui/iframe.mjs +42 -27
- package/dist/utils/content-script-ui/integrated.d.mts +37 -0
- package/dist/utils/content-script-ui/integrated.mjs +36 -29
- package/dist/utils/content-script-ui/shadow-root.d.mts +80 -0
- package/dist/utils/content-script-ui/shadow-root.mjs +75 -82
- package/dist/utils/content-script-ui/shared.mjs +117 -150
- package/dist/utils/content-script-ui/types.d.mts +107 -0
- package/dist/utils/content-script-ui/types.mjs +1 -0
- package/dist/utils/{define-app-config.d.ts → define-app-config.d.mts} +5 -3
- package/dist/utils/define-app-config.mjs +22 -2
- package/dist/utils/define-background.d.mts +7 -0
- package/dist/utils/define-background.mjs +7 -3
- package/dist/utils/define-content-script.d.mts +6 -0
- package/dist/utils/define-content-script.mjs +6 -2
- package/dist/utils/define-unlisted-script.d.mts +7 -0
- package/dist/utils/define-unlisted-script.mjs +7 -3
- package/dist/utils/define-wxt-plugin.d.mts +6 -0
- package/dist/utils/define-wxt-plugin.mjs +6 -2
- package/dist/utils/inject-script.d.mts +41 -0
- package/dist/utils/inject-script.mjs +44 -34
- package/dist/utils/internal/custom-events.d.mts +9 -0
- package/dist/utils/internal/custom-events.mjs +18 -10
- package/dist/utils/internal/location-watcher.mjs +23 -20
- package/dist/utils/internal/logger.mjs +15 -12
- package/dist/utils/match-patterns.d.mts +1 -0
- package/dist/utils/match-patterns.mjs +3 -1
- package/dist/utils/{split-shadow-root-css.d.ts → split-shadow-root-css.d.mts} +6 -3
- package/dist/utils/split-shadow-root-css.mjs +16 -7
- package/dist/utils/storage.d.mts +1 -0
- package/dist/utils/storage.mjs +3 -1
- package/dist/version.d.mts +4 -0
- package/dist/version.mjs +5 -1
- package/dist/virtual/background-entrypoint.d.mts +4 -0
- package/dist/virtual/background-entrypoint.mjs +146 -161
- package/dist/virtual/content-script-isolated-world-entrypoint.d.mts +4 -0
- package/dist/virtual/content-script-isolated-world-entrypoint.mjs +26 -27
- package/dist/virtual/content-script-main-world-entrypoint.d.mts +4 -0
- package/dist/virtual/content-script-main-world-entrypoint.mjs +24 -24
- package/dist/virtual/mock-browser.d.mts +6 -0
- package/dist/virtual/mock-browser.mjs +5 -3
- package/dist/virtual/reload-html.d.mts +1 -0
- package/dist/virtual/reload-html.mjs +60 -53
- package/dist/virtual/unlisted-script-entrypoint.d.mts +4 -0
- package/dist/virtual/unlisted-script-entrypoint.mjs +35 -43
- package/package.json +47 -49
- package/dist/browser.d.ts +0 -30
- package/dist/builtin-modules/index.d.ts +0 -2
- package/dist/builtin-modules/unimport.d.ts +0 -5
- package/dist/cli/cli-utils.d.ts +0 -25
- package/dist/cli/commands.d.ts +0 -2
- package/dist/cli/index.d.ts +0 -1
- package/dist/core/builders/vite/index.d.ts +0 -7
- package/dist/core/builders/vite/plugins/bundleAnalysis.d.ts +0 -7
- package/dist/core/builders/vite/plugins/cssEntrypoints.d.ts +0 -13
- package/dist/core/builders/vite/plugins/defineImportMeta.d.ts +0 -14
- package/dist/core/builders/vite/plugins/devHtmlPrerender.d.ts +0 -7
- package/dist/core/builders/vite/plugins/devServerGlobals.d.ts +0 -6
- package/dist/core/builders/vite/plugins/download.d.ts +0 -10
- package/dist/core/builders/vite/plugins/entrypointGroupGlobals.d.ts +0 -6
- package/dist/core/builders/vite/plugins/extensionApiMock.d.ts +0 -6
- package/dist/core/builders/vite/plugins/globals.d.ts +0 -3
- package/dist/core/builders/vite/plugins/iifeFooter.d.ts +0 -8
- package/dist/core/builders/vite/plugins/index.d.ts +0 -16
- package/dist/core/builders/vite/plugins/noopBackground.d.ts +0 -6
- package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.d.ts +0 -6
- package/dist/core/builders/vite/plugins/resolveAppConfig.d.ts +0 -6
- package/dist/core/builders/vite/plugins/resolveVirtualModules.d.ts +0 -6
- package/dist/core/builders/vite/plugins/tsconfigPaths.d.ts +0 -3
- package/dist/core/builders/vite/plugins/wxtPluginLoader.d.ts +0 -6
- package/dist/core/define-config.d.ts +0 -2
- package/dist/core/define-web-ext-config.d.ts +0 -9
- package/dist/core/generate-wxt-dir.d.ts +0 -5
- package/dist/core/index.d.ts +0 -8
- package/dist/core/initialize.d.ts +0 -5
- package/dist/core/keyboard-shortcuts.d.ts +0 -12
- package/dist/core/package-managers/bun.d.ts +0 -2
- package/dist/core/package-managers/deno.d.ts +0 -2
- package/dist/core/package-managers/index.d.ts +0 -2
- package/dist/core/package-managers/npm.d.ts +0 -17
- package/dist/core/package-managers/pnpm.d.ts +0 -2
- package/dist/core/package-managers/types.d.ts +0 -2
- package/dist/core/package-managers/types.mjs +0 -0
- package/dist/core/package-managers/yarn.d.ts +0 -2
- package/dist/core/prepare.d.ts +0 -2
- package/dist/core/resolve-config.d.ts +0 -11
- package/dist/core/runners/index.d.ts +0 -2
- package/dist/core/runners/manual.d.ts +0 -5
- package/dist/core/runners/safari.d.ts +0 -5
- package/dist/core/runners/web-ext.d.ts +0 -5
- package/dist/core/runners/wsl.d.ts +0 -5
- package/dist/core/utils/arrays.d.ts +0 -13
- package/dist/core/utils/building/build-entrypoints.d.ts +0 -3
- package/dist/core/utils/building/detect-dev-changes.d.ts +0 -61
- package/dist/core/utils/building/find-entrypoints.d.ts +0 -5
- package/dist/core/utils/building/group-entrypoints.d.ts +0 -8
- package/dist/core/utils/building/index.d.ts +0 -6
- package/dist/core/utils/building/internal-build.d.ts +0 -12
- package/dist/core/utils/building/rebuild.d.ts +0 -23
- package/dist/core/utils/cache.d.ts +0 -8
- package/dist/core/utils/cli.d.ts +0 -3
- package/dist/core/utils/cli.mjs +0 -26
- package/dist/core/utils/constants.d.ts +0 -5
- package/dist/core/utils/content-scripts.d.ts +0 -12
- package/dist/core/utils/content-security-policy.d.ts +0 -14
- package/dist/core/utils/entrypoints.d.ts +0 -31
- package/dist/core/utils/env.d.ts +0 -5
- package/dist/core/utils/environments/browser-environment.d.ts +0 -3
- package/dist/core/utils/environments/environment.d.ts +0 -8
- package/dist/core/utils/environments/extension-environment.d.ts +0 -6
- package/dist/core/utils/environments/index.d.ts +0 -2
- package/dist/core/utils/eslint.d.ts +0 -1
- package/dist/core/utils/fs.d.ts +0 -13
- package/dist/core/utils/globals.d.ts +0 -11
- package/dist/core/utils/i18n.d.ts +0 -11
- package/dist/core/utils/log/index.d.ts +0 -4
- package/dist/core/utils/log/printBuildSummary.d.ts +0 -2
- package/dist/core/utils/log/printFileList.d.ts +0 -1
- package/dist/core/utils/log/printHeader.d.ts +0 -1
- package/dist/core/utils/log/printTable.d.ts +0 -1
- package/dist/core/utils/manifest.d.ts +0 -40
- package/dist/core/utils/minimatch-multiple.d.ts +0 -15
- package/dist/core/utils/network.d.ts +0 -7
- package/dist/core/utils/number.d.ts +0 -1
- package/dist/core/utils/package.d.ts +0 -6
- package/dist/core/utils/paths.d.ts +0 -11
- package/dist/core/utils/strings.d.ts +0 -14
- package/dist/core/utils/syntax-errors.d.ts +0 -11
- package/dist/core/utils/testing/fake-objects.d.ts +0 -11665
- package/dist/core/utils/testing/fake-objects.mjs +0 -326
- package/dist/core/utils/time.d.ts +0 -9
- package/dist/core/utils/transform.d.ts +0 -11
- package/dist/core/utils/types.d.ts +0 -13
- package/dist/core/utils/types.mjs +0 -0
- package/dist/core/utils/validation.d.ts +0 -15
- package/dist/core/utils/virtual-modules.d.ts +0 -22
- package/dist/core/utils/wsl.d.ts +0 -4
- package/dist/core/wxt.d.ts +0 -24
- package/dist/index.d.ts +0 -11
- package/dist/testing/fake-browser.d.ts +0 -10
- package/dist/testing/index.d.ts +0 -10
- package/dist/types.d.ts +0 -1459
- package/dist/types.mjs +0 -0
- package/dist/utils/app-config.d.ts +0 -2
- package/dist/utils/content-script-context.d.ts +0 -134
- package/dist/utils/content-script-ui/iframe.d.ts +0 -32
- package/dist/utils/content-script-ui/integrated.d.ts +0 -34
- package/dist/utils/content-script-ui/shadow-root.d.ts +0 -76
- package/dist/utils/content-script-ui/shared.d.ts +0 -5
- package/dist/utils/content-script-ui/types.d.ts +0 -104
- package/dist/utils/define-background.d.ts +0 -4
- package/dist/utils/define-content-script.d.ts +0 -3
- package/dist/utils/define-unlisted-script.d.ts +0 -4
- package/dist/utils/define-wxt-plugin.d.ts +0 -3
- package/dist/utils/inject-script.d.ts +0 -36
- package/dist/utils/internal/custom-events.d.ts +0 -10
- package/dist/utils/internal/dev-server-websocket.d.ts +0 -21
- package/dist/utils/internal/dev-server-websocket.mjs +0 -37
- package/dist/utils/internal/location-watcher.d.ts +0 -12
- package/dist/utils/internal/logger.d.ts +0 -9
- package/dist/utils/match-patterns.d.ts +0 -5
- package/dist/utils/storage.d.ts +0 -5
- package/dist/version.d.ts +0 -1
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
import { minimatch } from "minimatch";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
2
|
+
|
|
3
|
+
//#region src/core/utils/minimatch-multiple.ts
|
|
4
|
+
/**
|
|
5
|
+
* Run [`minimatch`](https://npmjs.com/package/minimatch) against multiple
|
|
6
|
+
* patterns.
|
|
7
|
+
*
|
|
8
|
+
* Supports negated patterns, the order does not matter. If your `search` string
|
|
9
|
+
* matches any of the negative patterns, it will return `false`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* minimatchMultiple('a.json', ['*.json', '!b.json']); // => true
|
|
14
|
+
* minimatchMultiple('b.json', ['*.json', '!b.json']); // => false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function minimatchMultiple(search, patterns, options) {
|
|
18
|
+
if (patterns == null) return false;
|
|
19
|
+
const negatePatterns = [];
|
|
20
|
+
const positivePatterns = [];
|
|
21
|
+
for (const pattern of patterns) if (pattern[0] === "!") negatePatterns.push(pattern.slice(1));
|
|
22
|
+
else positivePatterns.push(pattern);
|
|
23
|
+
if (negatePatterns.some((negatePattern) => minimatch(search, negatePattern, options))) return false;
|
|
24
|
+
return positivePatterns.some((positivePattern) => minimatch(search, positivePattern, options));
|
|
17
25
|
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { minimatchMultiple };
|
|
@@ -1,38 +1,35 @@
|
|
|
1
|
-
import dns from "node:dns";
|
|
2
1
|
import { withTimeout } from "./time.mjs";
|
|
2
|
+
import dns from "node:dns";
|
|
3
|
+
|
|
4
|
+
//#region src/core/utils/network.ts
|
|
3
5
|
function isOffline() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
return withTimeout(isOffline2, 1e3).catch(() => true);
|
|
6
|
+
return withTimeout(new Promise((res) => {
|
|
7
|
+
dns.resolve("google.com", (err) => {
|
|
8
|
+
if (err == null) res(false);
|
|
9
|
+
else res(true);
|
|
10
|
+
});
|
|
11
|
+
}), 1e3).catch(() => true);
|
|
14
12
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return !offline;
|
|
13
|
+
async function isOnline() {
|
|
14
|
+
return !await isOffline();
|
|
18
15
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
`Offline and "${url}" has not been cached. Try again when online.`
|
|
36
|
-
);
|
|
37
|
-
return content;
|
|
16
|
+
/**
|
|
17
|
+
* Fetches a URL with a simple GET request. Grabs it from cache if it doesn't exist, or throws an
|
|
18
|
+
* error if it can't be resolved via the network or cache.
|
|
19
|
+
*/
|
|
20
|
+
async function fetchCached(url, config) {
|
|
21
|
+
let content = "";
|
|
22
|
+
if (await isOnline()) {
|
|
23
|
+
const res = await fetch(url);
|
|
24
|
+
if (res.status < 300) {
|
|
25
|
+
content = await res.text();
|
|
26
|
+
await config.fsCache.set(url, content);
|
|
27
|
+
} else config.logger.debug(`Failed to download "${url}", falling back to cache...`);
|
|
28
|
+
}
|
|
29
|
+
if (!content) content = await config.fsCache.get(url) ?? "";
|
|
30
|
+
if (!content) throw Error(`Offline and "${url}" has not been cached. Try again when online.`);
|
|
31
|
+
return content;
|
|
38
32
|
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { fetchCached };
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
//#region src/core/utils/number.ts
|
|
2
|
+
function safeStringToNumber(str) {
|
|
3
|
+
const num = Number(str);
|
|
4
|
+
return isNaN(num) ? null : num;
|
|
4
5
|
}
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
export { safeStringToNumber };
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
import fs from "fs-extra";
|
|
3
1
|
import { wxt } from "../wxt.mjs";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
//#region src/core/utils/package.ts
|
|
6
|
+
/**
|
|
7
|
+
* Read the project's package.json.
|
|
8
|
+
*
|
|
9
|
+
* TODO: look in root and up directories until it's found
|
|
10
|
+
*/
|
|
11
|
+
async function getPackageJson() {
|
|
12
|
+
const file = resolve(wxt.config.root, "package.json");
|
|
13
|
+
try {
|
|
14
|
+
return await fs.readJson(file);
|
|
15
|
+
} catch (err) {
|
|
16
|
+
wxt.logger.debug(`Failed to read package.json at: ${file}. Returning undefined.`, err);
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
15
19
|
}
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { getPackageJson };
|
|
@@ -1,10 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import path from "node:path";
|
|
2
2
|
import normalize from "normalize-path";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
//#region src/core/utils/paths.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts system paths to normalized bundler path. On windows and unix, this returns paths with /
|
|
7
|
+
* instead of \.
|
|
8
|
+
*/
|
|
9
|
+
function normalizePath(path) {
|
|
10
|
+
return normalize(path);
|
|
5
11
|
}
|
|
6
|
-
|
|
7
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Given a normalized path, convert it to the system path style. On Windows, switch to \, otherwise use /.
|
|
14
|
+
*/
|
|
15
|
+
function unnormalizePath(path$1) {
|
|
16
|
+
return path.normalize(path$1);
|
|
8
17
|
}
|
|
9
|
-
|
|
10
|
-
|
|
18
|
+
const CSS_EXTENSIONS = [
|
|
19
|
+
"css",
|
|
20
|
+
"scss",
|
|
21
|
+
"sass",
|
|
22
|
+
"less",
|
|
23
|
+
"styl",
|
|
24
|
+
"stylus"
|
|
25
|
+
];
|
|
26
|
+
const CSS_EXTENSIONS_PATTERN = `+(${CSS_EXTENSIONS.join("|")})`;
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { CSS_EXTENSIONS_PATTERN, normalizePath, unnormalizePath };
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import { camelCase } from "scule";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
//#region src/core/utils/strings.ts
|
|
4
|
+
function kebabCaseAlphanumeric(str) {
|
|
5
|
+
return str.toLowerCase().replace(/[^a-z0-9-\s]/g, "").replace(/\s+/g, "-");
|
|
4
6
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Return a safe variable name for a given string.
|
|
9
|
+
*/
|
|
10
|
+
function safeVarName(str) {
|
|
11
|
+
const name = camelCase(kebabCaseAlphanumeric(str));
|
|
12
|
+
if (name.match(/^[a-z]/)) return name;
|
|
13
|
+
return "_" + name;
|
|
9
14
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/(import\s?[\s\S]*?from\s?["'][\s\S]*?["'];?|import\s?["'][\s\S]*?["'];?)/gm,
|
|
16
|
-
""
|
|
17
|
-
);
|
|
15
|
+
/**
|
|
16
|
+
* Converts a string to a valid filename (NOT path), stripping out invalid characters.
|
|
17
|
+
*/
|
|
18
|
+
function safeFilename(str) {
|
|
19
|
+
return kebabCaseAlphanumeric(str);
|
|
18
20
|
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { safeFilename, safeVarName };
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
+
import { wxt } from "../wxt.mjs";
|
|
1
2
|
import { relative } from "node:path";
|
|
2
3
|
import pc from "picocolors";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
//#region src/core/utils/syntax-errors.ts
|
|
6
|
+
function isBabelSyntaxError(error) {
|
|
7
|
+
return error instanceof SyntaxError && error.code === "BABEL_PARSER_SYNTAX_ERROR";
|
|
6
8
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/\(\d+:\d+\)$/,
|
|
14
|
-
`(${filename}:${error.loc.line}:${error.loc.column + 1})`
|
|
15
|
-
);
|
|
16
|
-
if (error.frame) {
|
|
17
|
-
message += "\n\n" + pc.red(error.frame);
|
|
18
|
-
}
|
|
19
|
-
wxt.logger.error(message);
|
|
9
|
+
function logBabelSyntaxError(error) {
|
|
10
|
+
let filename = relative(wxt.config.root, error.id);
|
|
11
|
+
if (filename.startsWith("..")) filename = error.id;
|
|
12
|
+
let message = error.message.replace(/\(\d+:\d+\)$/, `(${filename}:${error.loc.line}:${error.loc.column + 1})`);
|
|
13
|
+
if (error.frame) message += "\n\n" + pc.red(error.frame);
|
|
14
|
+
wxt.logger.error(message);
|
|
20
15
|
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { isBabelSyntaxError, logBabelSyntaxError };
|
package/dist/core/utils/time.mjs
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
//#region src/core/utils/time.ts
|
|
2
|
+
function formatDuration(duration) {
|
|
3
|
+
if (duration < 1e3) return `${duration} ms`;
|
|
4
|
+
if (duration < 1e4) return `${(duration / 1e3).toFixed(3)} s`;
|
|
5
|
+
if (duration < 6e4) return `${(duration / 1e3).toFixed(1)} s`;
|
|
6
|
+
return `${(duration / 1e3).toFixed(0)} s`;
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Add a timeout to a promise.
|
|
10
|
+
*/
|
|
11
|
+
function withTimeout(promise, duration) {
|
|
12
|
+
return new Promise((res, rej) => {
|
|
13
|
+
const timeout = setTimeout(() => {
|
|
14
|
+
rej(`Promise timed out after ${duration}ms`);
|
|
15
|
+
}, duration);
|
|
16
|
+
promise.then(res).catch(rej).finally(() => clearTimeout(timeout));
|
|
17
|
+
});
|
|
17
18
|
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { formatDuration, withTimeout };
|
|
@@ -1,178 +1,147 @@
|
|
|
1
1
|
import { parseModule } from "magicast";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
|
|
3
|
+
//#region src/core/utils/transform.ts
|
|
4
|
+
/**
|
|
5
|
+
* Removes any code used at runtime related to an entrypoint's main function.
|
|
6
|
+
* 1. Removes or clears out `main` function from returned object
|
|
7
|
+
* 2. Removes any unused functions/variables outside the definition that aren't being called/used
|
|
8
|
+
* 3. Removes unused imports
|
|
9
|
+
* 3. Removes value-less, side-effect only imports (like `import "./styles.css"` or `import "polyfill"`)
|
|
10
|
+
*/
|
|
11
|
+
function removeMainFunctionCode(code) {
|
|
12
|
+
const mod = parseModule(code);
|
|
13
|
+
emptyMainFunction(mod);
|
|
14
|
+
let removedCount = 0;
|
|
15
|
+
let depth = 0;
|
|
16
|
+
const maxDepth = 10;
|
|
17
|
+
do {
|
|
18
|
+
removedCount = 0;
|
|
19
|
+
removedCount += removeUnusedTopLevelVariables(mod);
|
|
20
|
+
removedCount += removeUnusedTopLevelFunctions(mod);
|
|
21
|
+
removedCount += removeUnusedImports(mod);
|
|
22
|
+
} while (removedCount > 0 && depth++ <= maxDepth);
|
|
23
|
+
removeSideEffectImports(mod);
|
|
24
|
+
return mod.generate();
|
|
16
25
|
}
|
|
17
26
|
function emptyMainFunction(mod) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
mod.exports.default.$ast.arguments[0].properties = mod.exports.default.$ast.arguments[0].properties.filter(
|
|
23
|
-
(prop) => prop.key.name !== "main"
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
+
if (mod.exports?.default?.$type === "function-call") {
|
|
28
|
+
if (mod.exports.default.$ast?.arguments?.[0]?.body) delete mod.exports.default.$ast.arguments[0];
|
|
29
|
+
else if (mod.exports.default.$ast?.arguments?.[0]?.properties) mod.exports.default.$ast.arguments[0].properties = mod.exports.default.$ast.arguments[0].properties.filter((prop) => prop.key.name !== "main");
|
|
30
|
+
}
|
|
27
31
|
}
|
|
28
32
|
function removeUnusedTopLevelVariables(mod) {
|
|
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
|
-
} else if (id.type === "ArrayPattern") {
|
|
86
|
-
shouldRemove = cleanArrayPattern(id);
|
|
87
|
-
} else if (id.type === "ObjectPattern") {
|
|
88
|
-
shouldRemove = cleanObjectPattern(id);
|
|
89
|
-
}
|
|
90
|
-
if (shouldRemove) {
|
|
91
|
-
ast.body[i].declarations.splice(j, 1);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (ast.body[i].declarations.length === 0) {
|
|
95
|
-
ast.body.splice(i, 1);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return deletedCount;
|
|
33
|
+
const usedMap = findUsedIdentifiers(getSimpleAstJson(mod.$ast));
|
|
34
|
+
let deletedCount = 0;
|
|
35
|
+
const ast = mod.$ast;
|
|
36
|
+
const isUsed = (id) => {
|
|
37
|
+
return id?.type === "Identifier" && usedMap.get(id.name);
|
|
38
|
+
};
|
|
39
|
+
const cleanArrayPattern = (pattern) => {
|
|
40
|
+
const elements = pattern.elements;
|
|
41
|
+
for (let i = elements.length - 1; i >= 0; i--) {
|
|
42
|
+
const el = elements[i];
|
|
43
|
+
if (el?.type === "Identifier" && !isUsed(el)) {
|
|
44
|
+
elements.splice(i, 1);
|
|
45
|
+
deletedCount++;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return elements.length === 0;
|
|
49
|
+
};
|
|
50
|
+
const cleanObjectPattern = (pattern) => {
|
|
51
|
+
const properties = pattern.properties;
|
|
52
|
+
for (let i = properties.length - 1; i >= 0; i--) {
|
|
53
|
+
const prop = properties[i];
|
|
54
|
+
if (prop.type === "Property") {
|
|
55
|
+
const value = prop.value;
|
|
56
|
+
if (value.type === "ObjectPattern") {
|
|
57
|
+
if (cleanObjectPattern(value)) properties.splice(i, 1);
|
|
58
|
+
} else if (value.type === "ArrayPattern") {
|
|
59
|
+
if (cleanArrayPattern(value)) properties.splice(i, 1);
|
|
60
|
+
} else if (value.type === "Identifier" && !isUsed(value)) {
|
|
61
|
+
properties.splice(i, 1);
|
|
62
|
+
deletedCount++;
|
|
63
|
+
}
|
|
64
|
+
} else if (prop.type === "RestElement") {
|
|
65
|
+
const arg = prop.argument;
|
|
66
|
+
if (arg.type === "Identifier" && !isUsed(arg)) {
|
|
67
|
+
properties.splice(i, 1);
|
|
68
|
+
deletedCount++;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return properties.length === 0;
|
|
73
|
+
};
|
|
74
|
+
for (let i = ast.body.length - 1; i >= 0; i--) {
|
|
75
|
+
if (ast.body[i].type !== "VariableDeclaration") continue;
|
|
76
|
+
for (let j = ast.body[i].declarations.length - 1; j >= 0; j--) {
|
|
77
|
+
const id = ast.body[i].declarations[j].id;
|
|
78
|
+
let shouldRemove = false;
|
|
79
|
+
if (id.type === "Identifier") {
|
|
80
|
+
shouldRemove = !isUsed(id);
|
|
81
|
+
if (shouldRemove) deletedCount++;
|
|
82
|
+
} else if (id.type === "ArrayPattern") shouldRemove = cleanArrayPattern(id);
|
|
83
|
+
else if (id.type === "ObjectPattern") shouldRemove = cleanObjectPattern(id);
|
|
84
|
+
if (shouldRemove) ast.body[i].declarations.splice(j, 1);
|
|
85
|
+
}
|
|
86
|
+
if (ast.body[i].declarations.length === 0) ast.body.splice(i, 1);
|
|
87
|
+
}
|
|
88
|
+
return deletedCount;
|
|
99
89
|
}
|
|
100
90
|
function removeUnusedTopLevelFunctions(mod) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return deletedCount;
|
|
91
|
+
const usedMap = findUsedIdentifiers(getSimpleAstJson(mod.$ast));
|
|
92
|
+
let deletedCount = 0;
|
|
93
|
+
const ast = mod.$ast;
|
|
94
|
+
for (let i = ast.body.length - 1; i >= 0; i--) if (ast.body[i].type === "FunctionDeclaration" && !usedMap.get(ast.body[i].id.name)) {
|
|
95
|
+
ast.body.splice(i, 1);
|
|
96
|
+
deletedCount++;
|
|
97
|
+
}
|
|
98
|
+
return deletedCount;
|
|
112
99
|
}
|
|
113
100
|
function removeUnusedImports(mod) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return deletedCount;
|
|
101
|
+
const usedMap = findUsedIdentifiers(getSimpleAstJson(mod.$ast));
|
|
102
|
+
const importSymbols = Object.keys(mod.imports);
|
|
103
|
+
let deletedCount = 0;
|
|
104
|
+
importSymbols.forEach((name) => {
|
|
105
|
+
if (usedMap.get(name)) return;
|
|
106
|
+
delete mod.imports[name];
|
|
107
|
+
deletedCount++;
|
|
108
|
+
});
|
|
109
|
+
return deletedCount;
|
|
124
110
|
}
|
|
125
111
|
function findUsedIdentifiers(simpleAst) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const filterFns = {
|
|
138
|
-
// Ignore the function declaration's name
|
|
139
|
-
FunctionDeclaration: ([key]) => key !== "id",
|
|
140
|
-
// Ignore object property names
|
|
141
|
-
ObjectProperty: ([key]) => key !== "key",
|
|
142
|
-
// Ignore variable declaration's name
|
|
143
|
-
VariableDeclarator: ([key]) => key !== "id"
|
|
144
|
-
};
|
|
145
|
-
queue.push(
|
|
146
|
-
Object.entries(item).filter(filterFns[item.type] ?? (() => true)).map(([_, value]) => value)
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return usedMap;
|
|
112
|
+
const usedMap = /* @__PURE__ */ new Map();
|
|
113
|
+
const queue = [simpleAst];
|
|
114
|
+
for (const item of queue) if (!item) {} else if (Array.isArray(item)) queue.push(...item);
|
|
115
|
+
else if (item.type === "ImportDeclaration") continue;
|
|
116
|
+
else if (item.type === "Identifier") usedMap.set(item.name, true);
|
|
117
|
+
else if (typeof item === "object") queue.push(Object.entries(item).filter({
|
|
118
|
+
FunctionDeclaration: ([key]) => key !== "id",
|
|
119
|
+
ObjectProperty: ([key]) => key !== "key",
|
|
120
|
+
VariableDeclarator: ([key]) => key !== "id"
|
|
121
|
+
}[item.type] ?? (() => true)).map(([_, value]) => value));
|
|
122
|
+
return usedMap;
|
|
151
123
|
}
|
|
152
124
|
function deleteImportAst(mod, shouldDelete) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
delete mod.$ast.body[i];
|
|
161
|
-
});
|
|
125
|
+
const importIndexesToDelete = [];
|
|
126
|
+
mod.$ast.body.forEach((node, index) => {
|
|
127
|
+
if (node.type === "ImportDeclaration" && shouldDelete(node)) importIndexesToDelete.push(index);
|
|
128
|
+
});
|
|
129
|
+
importIndexesToDelete.reverse().forEach((i) => {
|
|
130
|
+
delete mod.$ast.body[i];
|
|
131
|
+
});
|
|
162
132
|
}
|
|
163
133
|
function removeSideEffectImports(mod) {
|
|
164
|
-
|
|
134
|
+
deleteImportAst(mod, (node) => node.specifiers.length === 0);
|
|
165
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Util to get the AST as a simple JSON object, stripping out large objects and file locations to keep it readible
|
|
138
|
+
*/
|
|
166
139
|
function getSimpleAstJson(ast) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
} else if (typeof ast === "object") {
|
|
172
|
-
return Object.fromEntries(
|
|
173
|
-
Object.entries(ast).filter(([key]) => key !== "loc" && key !== "start" && key !== "end").map(([key, value]) => [key, getSimpleAstJson(value)])
|
|
174
|
-
);
|
|
175
|
-
} else {
|
|
176
|
-
return ast;
|
|
177
|
-
}
|
|
140
|
+
if (!ast) return ast;
|
|
141
|
+
else if (Array.isArray(ast)) return ast.map(getSimpleAstJson);
|
|
142
|
+
else if (typeof ast === "object") return Object.fromEntries(Object.entries(ast).filter(([key]) => key !== "loc" && key !== "start" && key !== "end").map(([key, value]) => [key, getSimpleAstJson(value)]));
|
|
143
|
+
else return ast;
|
|
178
144
|
}
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
export { removeMainFunctionCode };
|