diff --git a/packages/core/package.json b/packages/core/package.json index fbabc4c..26648ae 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -77,7 +77,8 @@ "homepage": "https://bundlejs.com", "dependencies": { "esbuild": "^0.17.11", - "esbuild-wasm": "^0.17.11" + "esbuild-wasm": "^0.17.11", + "resolve.exports": "^2.0.0" }, "devDependencies": { "@types/node": "^18.14.6", diff --git a/packages/core/src/build.ts b/packages/core/src/build.ts index da59c92..9967c0b 100644 --- a/packages/core/src/build.ts +++ b/packages/core/src/build.ts @@ -15,7 +15,7 @@ import { createNotice } from "./utils/create-notice.ts"; import { DEFAULT_CDN_HOST } from "./utils/util-cdn.ts"; import { init } from "./init.ts"; -import { BUILD_ERROR, INIT_LOADING, LOGGER_ERROR, LOGGER_LOG, dispatchEvent } from "./configs/events.ts"; +import { BUILD_ERROR, INIT_LOADING, LOGGER_ERROR, LOGGER_LOG, LOGGER_WARN, dispatchEvent } from "./configs/events.ts"; /** * Local state available to all plugins @@ -147,9 +147,8 @@ export async function build(opts: BuildConfig = {}, filesystem = TheFileSystem): } catch (e) { if (e.errors) { // Log errors with added color info. to the virtual console - const asciMsgs = [...await createNotice(e.errors, "error", false)]; const htmlMsgs = [...await createNotice(e.errors, "error")]; - dispatchEvent(LOGGER_ERROR, new Error(JSON.stringify({ asciMsgs, htmlMsgs }))); + dispatchEvent(LOGGER_ERROR, new Error(JSON.stringify({ htmlMsgs }))); const message = (htmlMsgs.length > 1 ? `${htmlMsgs.length} error(s) ` : "") + "(if you are having trouble solving this issue, please create a new issue in the repo, https://github.com/okikio/bundlejs)"; dispatchEvent(LOGGER_ERROR, new Error(message)); @@ -157,6 +156,15 @@ export async function build(opts: BuildConfig = {}, filesystem = TheFileSystem): } else throw e; } + if (result.warnings) { + // Log errors with added color info. to the virtual console + const ansiMsgs = [...await createNotice(result.warnings, "warning", false)]; + dispatchEvent(LOGGER_WARN, ansiMsgs.join("\n")); + + const message = `${ansiMsgs.length} warning(s) `; + dispatchEvent(LOGGER_WARN, message); + } + // Create an array of assets and actual output files, this will later be used to calculate total file size outputs = await Promise.all( [...get()["assets"]] diff --git a/packages/core/src/plugins/cdn.ts b/packages/core/src/plugins/cdn.ts index 585bdce..f7cbe7a 100644 --- a/packages/core/src/plugins/cdn.ts +++ b/packages/core/src/plugins/cdn.ts @@ -5,7 +5,7 @@ import type { ESBUILD } from "../types.ts"; import { dispatchEvent, LOGGER_WARN } from "../configs/events.ts"; import { HTTP_NAMESPACE } from "./http.ts"; -import { resolveExports, legacy } from "../utils/resolve-exports.ts"; +import { resolve as resolveExports, legacy } from "resolve.exports"; import { parsePackageName as parsePackageName } from "../utils/parse-package-name.ts"; import { isBareImport } from "../utils/path.ts"; @@ -43,7 +43,8 @@ export const CDN_RESOLVE = (cdn = DEFAULT_CDN_HOST) => { // https://nodejs.org/api/packages.html#subpath-imports if (argPath[0] == "#") { const path = resolveImports({ ...pkg, exports: pkg.imports }, argPath, { - require: args.kind === "require-call" || args.kind === "require-resolve" + require: args.kind === "require-call" || args.kind === "require-resolve", + browser: true }); if (typeof path === "string") { @@ -53,10 +54,11 @@ export const CDN_RESOLVE = (cdn = DEFAULT_CDN_HOST) => { subpath = `/${subpath}`; const version = NPM_CDN ? "@" + pkg.version : ""; - const { url: { href } } = getCDNUrl(`${pkg.name}${version}${subpath}`); + const { url } = getCDNUrl(`${pkg.name}${version}`); + if (subpath) url.pathname = subpath; return { namespace: HTTP_NAMESPACE, - path: href, + path: url.toString(), pluginData: { pkg } }; } @@ -85,11 +87,13 @@ export const CDN_RESOLVE = (cdn = DEFAULT_CDN_HOST) => { // Strongly cache package.json files pkg = await getRequest(PACKAGE_JSON_URL, true).then((res) => res.json()); - const path = resolveExports(pkg, subpath ? "." + subpath.replace(/^\.?\/?/, "/") : ".", { + let path = resolveExports(pkg, subpath ? "." + subpath.replace(/^\.?\/?/, "/") : ".", { require: args.kind === "require-call" || args.kind === "require-resolve", + browser: true }) || legacy(pkg); - if (typeof path === "string") + if (Array.isArray(path)) path = path[0]; + if (typeof path === "string") subpath = path.replace(/^\.?\/?/, "/").replace(/\.js\.js$/, ".js"); if (subpath && subpath[0] !== "/") diff --git a/packages/core/src/plugins/external.ts b/packages/core/src/plugins/external.ts index b805759..ed62f46 100644 --- a/packages/core/src/plugins/external.ts +++ b/packages/core/src/plugins/external.ts @@ -118,7 +118,7 @@ export function EXTERNAL (state: StateArray, config: BuildConfig): E contents: EMPTY_EXPORT, warnings: [{ text: `${args.path} is marked as an external module and will be ignored.`, - details: `"${args.path}" is a built-in node module thus can't be bundled by https://bundlejs.com, sorry about that.` + details: `"${args.path}" is a built-in node module thus can't be bundled by https://bundlejs.com (technically it can be supported but...it's currently disabled. If you'd like this functionallity, please reach out at https://github.com/okikio/bundlejs), sorry about that.` }] }; }); diff --git a/packages/core/src/plugins/http.ts b/packages/core/src/plugins/http.ts index ad550ee..a3a8a1a 100644 --- a/packages/core/src/plugins/http.ts +++ b/packages/core/src/plugins/http.ts @@ -165,6 +165,18 @@ export function HTTP (state: StateArray, config: BuildConfig): ESBUI const FileSystem = get().filesystem; // const FileSystem = config.filesystem; + // Imports have various extentions, fetch each extention to confirm what the user meant + const fileEndings = ["", "/index"]; + const exts = ["", ".js", ".mjs", ".ts", ".tsx", ".cjs", ".d.ts"]; + + // It's possible to have `./lib/index.d.ts` or `./lib/index.mjs`, and have a user enter use `./lib` as the import + // It's very annoying but you have to check all variants + const allEndingVariants = Array.from(new Set(fileEndings.map(ending => { + return exts.map(extension => ending + extension) + }).flat())); + + const endingVariantsLength = allEndingVariants.length; + return { name: HTTP_NAMESPACE, setup(build) { @@ -197,23 +209,19 @@ export function HTTP (state: StateArray, config: BuildConfig): ESBUI const argPath = (suffix = "") => ext.length > 0 ? args.path : args.path + suffix; let content: Uint8Array, url: string; - // Imports have various extentions, fetch each extention to confirm what the user meant - const exts = ext.length > 0 ? [""] : ["", ".ts", ".tsx", ".js", ".mjs", ".cjs"]; - const extLength = exts.length; let err: Error; - - for (let i = 0; i < extLength; i++) { - const extPath = exts[i]; + for (let i = 0; i < endingVariantsLength; i++) { + const endings = allEndingVariants[i]; try { - ({ content, url } = await fetchPkg(argPath(extPath))); + ({ content, url } = await fetchPkg(argPath(endings))); break; } catch (e) { - if (i == 0) + if (i === 0) err = e as Error; // If after checking all the different file extensions none of them are valid // Throw the first fetch error encountered, as that is generally the most accurate error - if (i >= extLength - 1) { + if (i >= endingVariantsLength - 1) { dispatchEvent(LOGGER_ERROR, e); throw err; } diff --git a/packages/edge/deno.lock b/packages/edge/deno.lock index 261e4c7..ae413c3 100644 --- a/packages/edge/deno.lock +++ b/packages/edge/deno.lock @@ -35,6 +35,7 @@ "esbuild-wasm": "esbuild-wasm@0.17.11", "esbuild-wasm@^0.17.11": "esbuild-wasm@0.17.11", "pnpm@^7.28.0": "pnpm@7.28.0", + "resolve.exports": "resolve.exports@2.0.0", "text-encoding-shim@^1.0.5": "text-encoding-shim@1.0.5", "typescript@5.0.1-rc": "typescript@5.0.1-rc", "vite-plugin-cp@^1.0.0": "vite-plugin-cp@1.0.0", @@ -1280,6 +1281,10 @@ "strip-indent": "strip-indent@4.0.0" } }, + "resolve.exports@2.0.0": { + "integrity": "sha512-6K/gDlqgQscOlg9fSRpWstA8sYe8rbELsSTNpx+3kTrsVCzvSl0zIvRErM7fdl9ERWDsKnrLnwB+Ne89918XOg==", + "dependencies": {} + }, "resolve@1.22.1": { "integrity": "sha512-nBpuuYuY5jFsli/JIs1oldw6fOQCBioohqWZg/2hiaOybXOft4lonv85uDOKXdf8rhyK159cxU5cDcK/NKk8zw==", "dependencies": { diff --git a/packages/edge/deno.ts b/packages/edge/deno.ts index 73e97d1..7b7f347 100644 --- a/packages/edge/deno.ts +++ b/packages/edge/deno.ts @@ -31,6 +31,11 @@ serve(async (req: Request) => { const start = performance.now(); const url = new URL(req.url); + console.log(url.href) + + if (url.pathname === "/favicon.ico") + return Response.redirect("https://bundlejs.com/favicon/favicon.ico"); + const initialValue = parseShareURLQuery(url) || inputModelResetValue; const { init: _, entryPoints: _2, ascii: _3, ...initialConfig } = parseConfig(url) || {}; @@ -41,14 +46,15 @@ serve(async (req: Request) => { const fileQuery = url.searchParams.has("file"); const badgeQuery = url.searchParams.has("badge"); + + const enableMetafile = analysisQuery || + metafileQuery || + Boolean(initialConfig?.analysis); const configObj: BuildConfig & CompressConfig = deepAssign({}, initialConfig, { entryPoints: ["/index.tsx"], - esbuild: { - treeShaking: true, - metafile: analysisQuery || - metafileQuery || - Boolean(initialConfig?.analysis) - }, + esbuild: enableMetafile ? { + metafile: enableMetafile + } : {}, init: { platform: "deno-wasm", worker: false, diff --git a/packages/edge/import_map.json b/packages/edge/import_map.json index 439d772..0f065e1 100644 --- a/packages/edge/import_map.json +++ b/packages/edge/import_map.json @@ -2,9 +2,8 @@ "imports": { "@bundlejs/core/": "../core/", "@bundlejs/core": "../core/src/index.ts", - "esbuild-wasm/": "npm:esbuild-wasm/", - "esbuild/": "npm:esbuild/", "esbuild-wasm": "npm:esbuild-wasm", - "esbuild": "npm:esbuild" + "esbuild": "npm:esbuild", + "resolve.exports": "npm:resolve.exports" } } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 188d6c0..c7a4311 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -111,6 +111,7 @@ importers: esbuild-wasm: ^0.17.11 esno: ^0.16.3 pnpm: ^7.28.0 + resolve.exports: ^2.0.0 typescript: 5.0.1-rc vite: ^4.1.4 vite-plugin-cp: ^1.0.0 @@ -118,6 +119,7 @@ importers: dependencies: esbuild: 0.17.11 esbuild-wasm: 0.17.11 + resolve.exports: 2.0.0 devDependencies: '@types/node': 18.14.6 del-cli: 5.0.0 @@ -9028,6 +9030,11 @@ packages: global-dirs: 0.1.1 dev: true + /resolve.exports/2.0.0: + resolution: {integrity: sha512-6K/gDlqgQscOlg9fSRpWstA8sYe8rbELsSTNpx+3kTrsVCzvSl0zIvRErM7fdl9ERWDsKnrLnwB+Ne89918XOg==} + engines: {node: '>=10'} + dev: false + /resolve/1.22.1: resolution: {integrity: sha512-nBpuuYuY5jFsli/JIs1oldw6fOQCBioohqWZg/2hiaOybXOft4lonv85uDOKXdf8rhyK159cxU5cDcK/NKk8zw==} hasBin: true