| import { n as createHooks, t as sharedPluginContext } from "./context-BkEw38sK.mjs"; | ||
| import module from "node:module"; | ||
| import process from "node:process"; | ||
| import { createBirpc } from "birpc"; | ||
| import { createDebug } from "obug"; | ||
| //#region src/utils/debug.ts | ||
| const debug = createDebug("unloader"); | ||
| //#endregion | ||
| //#region src/rpc.ts | ||
| const mainFunctions = { | ||
| log(...messages) { | ||
| console.info(...messages); | ||
| }, | ||
| debug(...args) { | ||
| debug(...args); | ||
| } | ||
| }; | ||
| function createRpc(port) { | ||
| return createBirpc(mainFunctions, { | ||
| post: (data) => port.postMessage(data), | ||
| on: (fn) => port.on("message", fn) | ||
| }); | ||
| } | ||
| //#endregion | ||
| //#region src/api.ts | ||
| async function register(inlineConfig) { | ||
| if (!module.register) throw new Error(`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v20.6 and above.`); | ||
| const { port1, port2 } = new MessageChannel(); | ||
| const rpc = createRpc(port1); | ||
| port1.unref(); | ||
| const data = { | ||
| port: port2, | ||
| inlineConfig | ||
| }; | ||
| const transferList = [port2]; | ||
| module.register("./worker.mjs", { | ||
| parentURL: import.meta.url, | ||
| data, | ||
| transferList | ||
| }); | ||
| if (await rpc.ping()) process.setSourceMapsEnabled(true); | ||
| return () => rpc.deactivate(); | ||
| } | ||
| function registerSync(inlineConfig) { | ||
| const registerHooks = module.registerHooks; | ||
| if (!registerHooks) throw new Error(`This version of Node.js (${process.version}) does not support module.registerHooks(). Please upgrade to Node v22.15 or v23.5 and above.`); | ||
| const { init, resolve, load, deactivate } = createHooks(); | ||
| const context = { | ||
| ...sharedPluginContext, | ||
| log: (message) => console.info(message), | ||
| debug | ||
| }; | ||
| if (init.sync(context, inlineConfig).sourcemap && !process.sourceMapsEnabled) process.setSourceMapsEnabled(true); | ||
| registerHooks({ | ||
| resolve: resolve.sync, | ||
| load: load.sync | ||
| }); | ||
| return deactivate; | ||
| } | ||
| //#endregion | ||
| export { registerSync as n, register as t }; |
| import process from "node:process"; | ||
| import path from "node:path"; | ||
| import remapping from "@jridgewell/remapping"; | ||
| import { getIsAsync } from "quansync"; | ||
| import { quansync } from "quansync/macro"; | ||
| import picomatch from "picomatch"; | ||
| import { loadConfig } from "unconfig"; | ||
| import { Buffer } from "node:buffer"; | ||
| import { fileURLToPath, pathToFileURL } from "node:url"; | ||
| //#region src/plugin.ts | ||
| function normalizePluginHook(plugin, key) { | ||
| const hook = plugin?.[key]; | ||
| if (!hook) return {}; | ||
| if (typeof hook === "function") return { handler: hook }; | ||
| return hook; | ||
| } | ||
| //#endregion | ||
| //#region node_modules/.pnpm/@antfu+utils@9.3.0/node_modules/@antfu/utils/dist/index.mjs | ||
| function toArray(array) { | ||
| array = array ?? []; | ||
| return Array.isArray(array) ? array : [array]; | ||
| } | ||
| //#endregion | ||
| //#region src/plugin-filter.ts | ||
| function getMatcherString(glob, cwd) { | ||
| if (glob.startsWith("**") || path.isAbsolute(glob)) return path.normalize(glob); | ||
| const resolved = path.resolve(cwd, glob); | ||
| return path.normalize(resolved); | ||
| } | ||
| function patternToIdFilter(pattern) { | ||
| if (pattern instanceof RegExp) return (id) => { | ||
| const normalizedId = path.normalize(id); | ||
| const result = pattern.test(normalizedId); | ||
| pattern.lastIndex = 0; | ||
| return result; | ||
| }; | ||
| const matcher = picomatch(getMatcherString(pattern, process.cwd()), { dot: true }); | ||
| return (id) => { | ||
| return matcher(path.normalize(id)); | ||
| }; | ||
| } | ||
| function patternToCodeFilter(pattern) { | ||
| if (pattern instanceof RegExp) return (code) => { | ||
| const result = pattern.test(code); | ||
| pattern.lastIndex = 0; | ||
| return result; | ||
| }; | ||
| return (code) => code.includes(pattern); | ||
| } | ||
| function createFilter(exclude, include) { | ||
| if (!exclude && !include) return; | ||
| return (input) => { | ||
| if (exclude?.some((filter) => filter(input))) return false; | ||
| if (include?.some((filter) => filter(input))) return true; | ||
| return !include || include.length <= 0; | ||
| }; | ||
| } | ||
| function normalizeFilter(filter) { | ||
| if (typeof filter === "string" || filter instanceof RegExp) return { include: [filter] }; | ||
| if (Array.isArray(filter)) return { include: filter }; | ||
| return { | ||
| exclude: filter.exclude ? toArray(filter.exclude) : void 0, | ||
| include: filter.include ? toArray(filter.include) : void 0 | ||
| }; | ||
| } | ||
| function createIdFilter(filter) { | ||
| if (!filter) return; | ||
| const { exclude, include } = normalizeFilter(filter); | ||
| const excludeFilter = exclude?.map(patternToIdFilter); | ||
| const includeFilter = include?.map(patternToIdFilter); | ||
| return createFilter(excludeFilter, includeFilter); | ||
| } | ||
| function createCodeFilter(filter) { | ||
| if (!filter) return; | ||
| const { exclude, include } = normalizeFilter(filter); | ||
| const excludeFilter = exclude?.map(patternToCodeFilter); | ||
| const includeFilter = include?.map(patternToCodeFilter); | ||
| return createFilter(excludeFilter, includeFilter); | ||
| } | ||
| function createFilterForId(filter) { | ||
| const filterFunction = createIdFilter(filter); | ||
| return filterFunction ? (id) => !!filterFunction(id) : void 0; | ||
| } | ||
| function createFilterForTransform(idFilter, codeFilter) { | ||
| if (!idFilter && !codeFilter) return; | ||
| const idFilterFunction = createIdFilter(idFilter); | ||
| const codeFilterFunction = createCodeFilter(codeFilter); | ||
| return (id, code) => { | ||
| let fallback = true; | ||
| if (idFilterFunction) fallback &&= idFilterFunction(id); | ||
| if (!fallback) return false; | ||
| if (codeFilterFunction) fallback &&= codeFilterFunction(code); | ||
| return fallback; | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/config.ts | ||
| const loadConfig$1 = quansync(function* () { | ||
| const { config } = yield loadConfig({ | ||
| sources: [{ | ||
| files: "unloader.config", | ||
| extensions: [ | ||
| "ts", | ||
| "mts", | ||
| "cts", | ||
| "js", | ||
| "mjs", | ||
| "cjs", | ||
| "json", | ||
| "" | ||
| ] | ||
| }], | ||
| cwd: process.cwd(), | ||
| defaults: {} | ||
| }); | ||
| return config; | ||
| }); | ||
| //#endregion | ||
| //#region src/utils/map.ts | ||
| function attachSourceMap(map, code) { | ||
| if (map) { | ||
| if (!map.sourcesContent || map.sourcesContent.length === 0) map.sourcesContent = [code]; | ||
| code += `\n//# sourceMappingURL=${toUrl(map)}`; | ||
| } | ||
| return code; | ||
| } | ||
| function toUrl(map) { | ||
| return `data:application/json;charset=utf-8;base64,${Buffer.from(map.toString()).toString("base64")}`; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/url.ts | ||
| function urlToPath(url) { | ||
| if (!url) return url; | ||
| return url.startsWith("file://") ? fileURLToPath(url) : url; | ||
| } | ||
| function pathToUrl(isRequire, path$1) { | ||
| if (isRequire || path$1.startsWith("file://") || path$1.startsWith("data://") || path$1.startsWith("node:")) return path$1; | ||
| return pathToFileURL(path$1).href; | ||
| } | ||
| //#endregion | ||
| //#region src/hooks.ts | ||
| function createHooks() { | ||
| let config; | ||
| let deactivated = false; | ||
| let pluginContext; | ||
| const init = quansync(function* (context, inlineConfig) { | ||
| pluginContext = context; | ||
| config = inlineConfig || (yield loadConfig$1()); | ||
| for (const plugin of config.plugins || []) { | ||
| const { handler } = normalizePluginHook(plugin, "options"); | ||
| config = handler?.call(context, config) || config; | ||
| } | ||
| for (const plugin of config.plugins || []) { | ||
| const { handler } = normalizePluginHook(plugin, "buildStart"); | ||
| yield handler?.call(context); | ||
| context.debug(`loaded plugin: %s`, plugin.name); | ||
| } | ||
| return config; | ||
| }); | ||
| const resolve = quansync(function* (specifier, context, nextResolve) { | ||
| if (deactivated) return nextResolve(specifier, context); | ||
| const isRequire = !Array.isArray(context.conditions) || context.conditions[0] === "require"; | ||
| pluginContext?.debug(`resolving %s with context %o`, specifier, context); | ||
| if (config?.plugins && pluginContext) for (const plugin of config.plugins) { | ||
| const resolve$1 = createResolve(nextResolve, isRequire, pluginContext.debug); | ||
| const isAsync = yield getIsAsync(); | ||
| const { handler, filter } = normalizePluginHook(plugin, "resolveId"); | ||
| const filterFn = createFilterForId(filter?.id); | ||
| const id = urlToPath(specifier); | ||
| if (filterFn && !filterFn(id)) continue; | ||
| const result = yield handler?.call({ | ||
| resolve: isAsync ? resolve$1 : resolve$1.sync, | ||
| ...pluginContext | ||
| }, id, urlToPath(context.parentURL), { | ||
| conditions: context.conditions, | ||
| attributes: context.importAttributes | ||
| }); | ||
| if (result) { | ||
| let output; | ||
| if (typeof result === "string") output = { | ||
| url: pathToUrl(isRequire, result), | ||
| importAttributes: context.importAttributes, | ||
| shortCircuit: true | ||
| }; | ||
| else output = { | ||
| url: pathToUrl(isRequire, result.id), | ||
| format: result.format, | ||
| importAttributes: result.attributes || context.importAttributes, | ||
| shortCircuit: true | ||
| }; | ||
| pluginContext.debug(`resolved %s to %s with format %s`, specifier, output.url, output.format); | ||
| return output; | ||
| } | ||
| } | ||
| return nextResolve(specifier, context); | ||
| }); | ||
| const load = quansync(function* (url, context, nextLoad) { | ||
| if (deactivated || !config?.plugins) return nextLoad(url, context); | ||
| pluginContext?.debug(`load %s with context %o`, url, context); | ||
| let result; | ||
| const defaultFormat = context.format || "module"; | ||
| const maps = []; | ||
| for (const plugin of config.plugins) { | ||
| const { handler, filter } = normalizePluginHook(plugin, "load"); | ||
| const filterFn = createFilterForId(filter?.id); | ||
| const id = urlToPath(url); | ||
| if (filterFn && !filterFn(id)) continue; | ||
| const loadResult = yield handler?.call(pluginContext, id, { | ||
| format: context.format, | ||
| conditions: context.conditions, | ||
| attributes: context.importAttributes | ||
| }); | ||
| if (loadResult) { | ||
| if (isModuleSource(loadResult)) result = { | ||
| source: loadResult, | ||
| format: defaultFormat, | ||
| shortCircuit: true | ||
| }; | ||
| else { | ||
| if (loadResult.map) maps.unshift(loadResult.map); | ||
| result = { | ||
| source: loadResult.code, | ||
| format: loadResult.format || defaultFormat, | ||
| shortCircuit: true | ||
| }; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| result ||= yield nextLoad(url, context); | ||
| for (const plugin of config.plugins) { | ||
| const { handler, filter } = normalizePluginHook(plugin, "transform"); | ||
| const filterFn = createFilterForTransform(filter?.id, filter?.code); | ||
| const code = result.source || ""; | ||
| const id = urlToPath(url); | ||
| if (filterFn && (typeof code !== "string" || !filterFn(id, code || ""))) continue; | ||
| const transformResult = yield handler?.call(pluginContext, code, id, { | ||
| format: result.format, | ||
| conditions: context.conditions, | ||
| attributes: context.importAttributes | ||
| }); | ||
| if (transformResult) if (isModuleSource(transformResult)) result = { | ||
| ...result, | ||
| source: transformResult | ||
| }; | ||
| else { | ||
| if (transformResult.map) maps.unshift(transformResult.map); | ||
| result = { | ||
| ...result, | ||
| source: transformResult.code, | ||
| format: transformResult.format || result.format | ||
| }; | ||
| } | ||
| } | ||
| if (maps.length && typeof result.source === "string") { | ||
| const code = attachSourceMap(remapping(maps, () => null), result.source); | ||
| result.source = code; | ||
| } | ||
| return result; | ||
| }); | ||
| const deactivate = () => { | ||
| deactivated = true; | ||
| }; | ||
| return { | ||
| init, | ||
| resolve, | ||
| load, | ||
| deactivate | ||
| }; | ||
| } | ||
| function createResolve(nextResolve, isRequire, debug) { | ||
| return quansync(function* (source, importer, options) { | ||
| if (!path.isAbsolute(source) && importer) source = path.resolve(importer, "..", source); | ||
| try { | ||
| const resolved = yield nextResolve(pathToUrl(isRequire, source), { | ||
| parentURL: importer, | ||
| conditions: options?.conditions, | ||
| importAttributes: options?.attributes | ||
| }); | ||
| debug("resolved %s to %s with format %s", source, resolved.url, resolved.format); | ||
| return { | ||
| id: urlToPath(resolved.url), | ||
| attributes: resolved.importAttributes, | ||
| format: resolved.format | ||
| }; | ||
| } catch (error) { | ||
| debug("error resolving %s: %o", source, error); | ||
| return null; | ||
| } | ||
| }); | ||
| } | ||
| function isModuleSource(v) { | ||
| return typeof v === "string" || ArrayBuffer.isView(v) || v instanceof ArrayBuffer; | ||
| } | ||
| //#endregion | ||
| //#region package.json | ||
| var version = "0.8.2"; | ||
| //#endregion | ||
| //#region src/utils/context.ts | ||
| const sharedPluginContext = { | ||
| error: (message) => { | ||
| throw typeof message === "string" ? new Error(message) : message; | ||
| }, | ||
| meta: { unloaderVersion: version } | ||
| }; | ||
| //#endregion | ||
| export { normalizePluginHook as i, createHooks as n, loadConfig$1 as r, sharedPluginContext as t }; |
+2
-2
@@ -1,3 +0,3 @@ | ||
| import { i as normalizePluginHook, r as loadConfig } from "./context-agoAYOnI.mjs"; | ||
| import { n as registerSync, t as register } from "./api-CASf37zs.mjs"; | ||
| import { i as normalizePluginHook, r as loadConfig } from "./context-BkEw38sK.mjs"; | ||
| import { n as registerSync, t as register } from "./api-xUCr2Pgm.mjs"; | ||
@@ -4,0 +4,0 @@ //#region src/index.ts |
@@ -1,3 +0,3 @@ | ||
| import "./context-agoAYOnI.mjs"; | ||
| import { n as registerSync } from "./api-CASf37zs.mjs"; | ||
| import "./context-BkEw38sK.mjs"; | ||
| import { n as registerSync } from "./api-xUCr2Pgm.mjs"; | ||
@@ -4,0 +4,0 @@ //#region src/register-sync.ts |
@@ -1,3 +0,3 @@ | ||
| import "./context-agoAYOnI.mjs"; | ||
| import { t as register } from "./api-CASf37zs.mjs"; | ||
| import "./context-BkEw38sK.mjs"; | ||
| import { t as register } from "./api-xUCr2Pgm.mjs"; | ||
@@ -4,0 +4,0 @@ //#region src/register.ts |
+1
-1
@@ -1,2 +0,2 @@ | ||
| import { n as createHooks, t as sharedPluginContext } from "./context-agoAYOnI.mjs"; | ||
| import { n as createHooks, t as sharedPluginContext } from "./context-BkEw38sK.mjs"; | ||
| import process from "node:process"; | ||
@@ -3,0 +3,0 @@ import { createBirpc } from "birpc"; |
+7
-7
| { | ||
| "name": "unloader", | ||
| "type": "module", | ||
| "version": "0.8.1", | ||
| "version": "0.8.2", | ||
| "description": "Node.js loader with a Rollup-like interface.", | ||
@@ -41,8 +41,8 @@ "author": "Kevin Deng <sxzz@sxzz.moe>", | ||
| "picomatch": "^4.0.3", | ||
| "quansync": "^0.3.0", | ||
| "unconfig": "^7.4.1" | ||
| "quansync": "^1.0.0", | ||
| "unconfig": "^7.4.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@antfu/utils": "^9.3.0", | ||
| "@quansync/fs": "^0.1.6", | ||
| "@quansync/fs": "^1.0.0", | ||
| "@sxzz/eslint-config": "^7.4.1", | ||
@@ -52,3 +52,3 @@ "@sxzz/prettier-config": "^2.2.6", | ||
| "@types/picomatch": "^4.0.2", | ||
| "@typescript/native-preview": "7.0.0-dev.20251203.1", | ||
| "@typescript/native-preview": "7.0.0-dev.20251204.1", | ||
| "bumpp": "^10.3.2", | ||
@@ -58,6 +58,6 @@ "eslint": "^9.39.1", | ||
| "prettier": "^3.7.4", | ||
| "tsdown": "^0.17.0-beta.6", | ||
| "tsdown": "^0.17.0", | ||
| "tsx": "^4.21.0", | ||
| "typescript": "^5.9.3", | ||
| "unplugin-quansync": "^0.4.5" | ||
| "unplugin-quansync": "^0.5.0" | ||
| }, | ||
@@ -64,0 +64,0 @@ "prettier": "@sxzz/prettier-config", |
| import { n as createHooks, t as sharedPluginContext } from "./context-agoAYOnI.mjs"; | ||
| import module from "node:module"; | ||
| import process from "node:process"; | ||
| import { createBirpc } from "birpc"; | ||
| import { createDebug } from "obug"; | ||
| //#region src/utils/debug.ts | ||
| const debug = createDebug("unloader"); | ||
| //#endregion | ||
| //#region src/rpc.ts | ||
| const mainFunctions = { | ||
| log(...messages) { | ||
| console.info(...messages); | ||
| }, | ||
| debug(...args) { | ||
| debug(...args); | ||
| } | ||
| }; | ||
| function createRpc(port) { | ||
| return createBirpc(mainFunctions, { | ||
| post: (data) => port.postMessage(data), | ||
| on: (fn) => port.on("message", fn) | ||
| }); | ||
| } | ||
| //#endregion | ||
| //#region src/api.ts | ||
| async function register(inlineConfig) { | ||
| if (!module.register) throw new Error(`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v20.6 and above.`); | ||
| const { port1, port2 } = new MessageChannel(); | ||
| const rpc = createRpc(port1); | ||
| port1.unref(); | ||
| const data = { | ||
| port: port2, | ||
| inlineConfig | ||
| }; | ||
| const transferList = [port2]; | ||
| module.register("./worker.mjs", { | ||
| parentURL: import.meta.url, | ||
| data, | ||
| transferList | ||
| }); | ||
| if (await rpc.ping()) process.setSourceMapsEnabled(true); | ||
| return () => rpc.deactivate(); | ||
| } | ||
| function registerSync(inlineConfig) { | ||
| const registerHooks = module.registerHooks; | ||
| if (!registerHooks) throw new Error(`This version of Node.js (${process.version}) does not support module.registerHooks(). Please upgrade to Node v22.15 or v23.5 and above.`); | ||
| const { init, resolve, load, deactivate } = createHooks(); | ||
| const context = { | ||
| ...sharedPluginContext, | ||
| log: (message) => console.info(message), | ||
| debug | ||
| }; | ||
| if (init.sync(context, inlineConfig).sourcemap && !process.sourceMapsEnabled) process.setSourceMapsEnabled(true); | ||
| registerHooks({ | ||
| resolve: resolve.sync, | ||
| load: load.sync | ||
| }); | ||
| return deactivate; | ||
| } | ||
| //#endregion | ||
| export { registerSync as n, register as t }; |
| import process from "node:process"; | ||
| import path from "node:path"; | ||
| import remapping from "@jridgewell/remapping"; | ||
| import { getIsAsync } from "quansync"; | ||
| import { quansync } from "quansync/macro"; | ||
| import picomatch from "picomatch"; | ||
| import { loadConfig } from "unconfig"; | ||
| import { Buffer } from "node:buffer"; | ||
| import { fileURLToPath, pathToFileURL } from "node:url"; | ||
| //#region src/plugin.ts | ||
| function normalizePluginHook(plugin, key) { | ||
| const hook = plugin?.[key]; | ||
| if (!hook) return {}; | ||
| if (typeof hook === "function") return { handler: hook }; | ||
| return hook; | ||
| } | ||
| //#endregion | ||
| //#region node_modules/.pnpm/@antfu+utils@9.3.0/node_modules/@antfu/utils/dist/index.mjs | ||
| function toArray(array) { | ||
| array = array ?? []; | ||
| return Array.isArray(array) ? array : [array]; | ||
| } | ||
| //#endregion | ||
| //#region src/plugin-filter.ts | ||
| function getMatcherString(glob, cwd) { | ||
| if (glob.startsWith("**") || path.isAbsolute(glob)) return path.normalize(glob); | ||
| const resolved = path.resolve(cwd, glob); | ||
| return path.normalize(resolved); | ||
| } | ||
| function patternToIdFilter(pattern) { | ||
| if (pattern instanceof RegExp) return (id) => { | ||
| const normalizedId = path.normalize(id); | ||
| const result = pattern.test(normalizedId); | ||
| pattern.lastIndex = 0; | ||
| return result; | ||
| }; | ||
| const matcher = picomatch(getMatcherString(pattern, process.cwd()), { dot: true }); | ||
| return (id) => { | ||
| return matcher(path.normalize(id)); | ||
| }; | ||
| } | ||
| function patternToCodeFilter(pattern) { | ||
| if (pattern instanceof RegExp) return (code) => { | ||
| const result = pattern.test(code); | ||
| pattern.lastIndex = 0; | ||
| return result; | ||
| }; | ||
| return (code) => code.includes(pattern); | ||
| } | ||
| function createFilter(exclude, include) { | ||
| if (!exclude && !include) return; | ||
| return (input) => { | ||
| if (exclude?.some((filter) => filter(input))) return false; | ||
| if (include?.some((filter) => filter(input))) return true; | ||
| return !include || include.length <= 0; | ||
| }; | ||
| } | ||
| function normalizeFilter(filter) { | ||
| if (typeof filter === "string" || filter instanceof RegExp) return { include: [filter] }; | ||
| if (Array.isArray(filter)) return { include: filter }; | ||
| return { | ||
| exclude: filter.exclude ? toArray(filter.exclude) : void 0, | ||
| include: filter.include ? toArray(filter.include) : void 0 | ||
| }; | ||
| } | ||
| function createIdFilter(filter) { | ||
| if (!filter) return; | ||
| const { exclude, include } = normalizeFilter(filter); | ||
| const excludeFilter = exclude?.map(patternToIdFilter); | ||
| const includeFilter = include?.map(patternToIdFilter); | ||
| return createFilter(excludeFilter, includeFilter); | ||
| } | ||
| function createCodeFilter(filter) { | ||
| if (!filter) return; | ||
| const { exclude, include } = normalizeFilter(filter); | ||
| const excludeFilter = exclude?.map(patternToCodeFilter); | ||
| const includeFilter = include?.map(patternToCodeFilter); | ||
| return createFilter(excludeFilter, includeFilter); | ||
| } | ||
| function createFilterForId(filter) { | ||
| const filterFunction = createIdFilter(filter); | ||
| return filterFunction ? (id) => !!filterFunction(id) : void 0; | ||
| } | ||
| function createFilterForTransform(idFilter, codeFilter) { | ||
| if (!idFilter && !codeFilter) return; | ||
| const idFilterFunction = createIdFilter(idFilter); | ||
| const codeFilterFunction = createCodeFilter(codeFilter); | ||
| return (id, code) => { | ||
| let fallback = true; | ||
| if (idFilterFunction) fallback &&= idFilterFunction(id); | ||
| if (!fallback) return false; | ||
| if (codeFilterFunction) fallback &&= codeFilterFunction(code); | ||
| return fallback; | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/config.ts | ||
| const loadConfig$1 = quansync(function* () { | ||
| const { config } = yield loadConfig({ | ||
| sources: [{ | ||
| files: "unloader.config", | ||
| extensions: [ | ||
| "ts", | ||
| "mts", | ||
| "cts", | ||
| "js", | ||
| "mjs", | ||
| "cjs", | ||
| "json", | ||
| "" | ||
| ] | ||
| }], | ||
| cwd: process.cwd(), | ||
| defaults: {} | ||
| }); | ||
| return config; | ||
| }); | ||
| //#endregion | ||
| //#region src/utils/map.ts | ||
| function attachSourceMap(map, code) { | ||
| if (map) { | ||
| if (!map.sourcesContent || map.sourcesContent.length === 0) map.sourcesContent = [code]; | ||
| code += `\n//# sourceMappingURL=${toUrl(map)}`; | ||
| } | ||
| return code; | ||
| } | ||
| function toUrl(map) { | ||
| return `data:application/json;charset=utf-8;base64,${Buffer.from(map.toString()).toString("base64")}`; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/url.ts | ||
| function urlToPath(url) { | ||
| if (!url) return url; | ||
| return url.startsWith("file://") ? fileURLToPath(url) : url; | ||
| } | ||
| function pathToUrl(isRequire, path$1) { | ||
| if (isRequire || path$1.startsWith("file://") || path$1.startsWith("data://") || path$1.startsWith("node:")) return path$1; | ||
| return pathToFileURL(path$1).href; | ||
| } | ||
| //#endregion | ||
| //#region src/hooks.ts | ||
| function createHooks() { | ||
| let config; | ||
| let deactivated = false; | ||
| let pluginContext; | ||
| const init = quansync(function* (context, inlineConfig) { | ||
| pluginContext = context; | ||
| config = inlineConfig || (yield loadConfig$1()); | ||
| for (const plugin of config.plugins || []) { | ||
| const { handler } = normalizePluginHook(plugin, "options"); | ||
| config = handler?.call(context, config) || config; | ||
| } | ||
| for (const plugin of config.plugins || []) { | ||
| const { handler } = normalizePluginHook(plugin, "buildStart"); | ||
| yield handler?.call(context); | ||
| context.debug(`loaded plugin: %s`, plugin.name); | ||
| } | ||
| return config; | ||
| }); | ||
| const resolve = quansync(function* (specifier, context, nextResolve) { | ||
| if (deactivated) return nextResolve(specifier, context); | ||
| const isRequire = !Array.isArray(context.conditions) || context.conditions[0] === "require"; | ||
| pluginContext?.debug(`resolving %s with context %o`, specifier, context); | ||
| if (config?.plugins && pluginContext) for (const plugin of config.plugins) { | ||
| const resolve$1 = createResolve(nextResolve, isRequire, pluginContext.debug); | ||
| const isAsync = yield getIsAsync(); | ||
| const { handler, filter } = normalizePluginHook(plugin, "resolveId"); | ||
| const filterFn = createFilterForId(filter?.id); | ||
| const id = urlToPath(specifier); | ||
| if (filterFn && !filterFn(id)) continue; | ||
| const result = yield handler?.call({ | ||
| resolve: isAsync ? resolve$1 : resolve$1.sync, | ||
| ...pluginContext | ||
| }, id, urlToPath(context.parentURL), { | ||
| conditions: context.conditions, | ||
| attributes: context.importAttributes | ||
| }); | ||
| if (result) { | ||
| let output; | ||
| if (typeof result === "string") output = { | ||
| url: pathToUrl(isRequire, result), | ||
| importAttributes: context.importAttributes, | ||
| shortCircuit: true | ||
| }; | ||
| else output = { | ||
| url: pathToUrl(isRequire, result.id), | ||
| format: result.format, | ||
| importAttributes: result.attributes || context.importAttributes, | ||
| shortCircuit: true | ||
| }; | ||
| pluginContext.debug(`resolved %s to %s with format %s`, specifier, output.url, output.format); | ||
| return output; | ||
| } | ||
| } | ||
| return nextResolve(specifier, context); | ||
| }); | ||
| const load = quansync(function* (url, context, nextLoad) { | ||
| if (deactivated || !config?.plugins) return nextLoad(url, context); | ||
| pluginContext?.debug(`load %s with context %o`, url, context); | ||
| let result; | ||
| const defaultFormat = context.format || "module"; | ||
| const maps = []; | ||
| for (const plugin of config.plugins) { | ||
| const { handler, filter } = normalizePluginHook(plugin, "load"); | ||
| const filterFn = createFilterForId(filter?.id); | ||
| const id = urlToPath(url); | ||
| if (filterFn && !filterFn(id)) continue; | ||
| const loadResult = yield handler?.call(pluginContext, id, { | ||
| format: context.format, | ||
| conditions: context.conditions, | ||
| attributes: context.importAttributes | ||
| }); | ||
| if (loadResult) { | ||
| if (isModuleSource(loadResult)) result = { | ||
| source: loadResult, | ||
| format: defaultFormat, | ||
| shortCircuit: true | ||
| }; | ||
| else { | ||
| if (loadResult.map) maps.unshift(loadResult.map); | ||
| result = { | ||
| source: loadResult.code, | ||
| format: loadResult.format || defaultFormat, | ||
| shortCircuit: true | ||
| }; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| result ||= yield nextLoad(url, context); | ||
| for (const plugin of config.plugins) { | ||
| const { handler, filter } = normalizePluginHook(plugin, "transform"); | ||
| const filterFn = createFilterForTransform(filter?.id, filter?.code); | ||
| const code = result.source || ""; | ||
| const id = urlToPath(url); | ||
| if (filterFn && (typeof code !== "string" || !filterFn(id, code || ""))) continue; | ||
| const transformResult = yield handler?.call(pluginContext, code, id, { | ||
| format: result.format, | ||
| conditions: context.conditions, | ||
| attributes: context.importAttributes | ||
| }); | ||
| if (transformResult) if (isModuleSource(transformResult)) result = { | ||
| ...result, | ||
| source: transformResult | ||
| }; | ||
| else { | ||
| if (transformResult.map) maps.unshift(transformResult.map); | ||
| result = { | ||
| ...result, | ||
| source: transformResult.code, | ||
| format: transformResult.format || result.format | ||
| }; | ||
| } | ||
| } | ||
| if (maps.length && typeof result.source === "string") { | ||
| const code = attachSourceMap(remapping(maps, () => null), result.source); | ||
| result.source = code; | ||
| } | ||
| return result; | ||
| }); | ||
| const deactivate = () => { | ||
| deactivated = true; | ||
| }; | ||
| return { | ||
| init, | ||
| resolve, | ||
| load, | ||
| deactivate | ||
| }; | ||
| } | ||
| function createResolve(nextResolve, isRequire, debug) { | ||
| return quansync(function* (source, importer, options) { | ||
| if (!path.isAbsolute(source) && importer) source = path.resolve(importer, "..", source); | ||
| try { | ||
| const resolved = yield nextResolve(pathToUrl(isRequire, source), { | ||
| parentURL: importer, | ||
| conditions: options?.conditions, | ||
| importAttributes: options?.attributes | ||
| }); | ||
| debug("resolved %s to %s with format %s", source, resolved.url, resolved.format); | ||
| return { | ||
| id: urlToPath(resolved.url), | ||
| attributes: resolved.importAttributes, | ||
| format: resolved.format | ||
| }; | ||
| } catch (error) { | ||
| debug("error resolving %s: %o", source, error); | ||
| return null; | ||
| } | ||
| }); | ||
| } | ||
| function isModuleSource(v) { | ||
| return typeof v === "string" || ArrayBuffer.isView(v) || v instanceof ArrayBuffer; | ||
| } | ||
| //#endregion | ||
| //#region package.json | ||
| var version = "0.8.1"; | ||
| //#endregion | ||
| //#region src/utils/context.ts | ||
| const sharedPluginContext = { | ||
| error: (message) => { | ||
| throw typeof message === "string" ? new Error(message) : message; | ||
| }, | ||
| meta: { unloaderVersion: version } | ||
| }; | ||
| //#endregion | ||
| export { normalizePluginHook as i, createHooks as n, loadConfig$1 as r, sharedPluginContext as t }; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
26539
-0.03%- Removed
Updated
Updated