@normed/bundle 4.5.7 → 4.6.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.
@@ -23,6 +23,19 @@ export declare const discoveredPugReferences: Map<string, DiscoveredPugReference
23
23
  * This is populated during build and used by the builder to trigger sub-builds.
24
24
  */
25
25
  export declare const discoveredLessReferences: Map<string, DiscoveredLessReference[]>;
26
+ /**
27
+ * Discovered package CSS reference from HTML.
28
+ * Package CSS files need to go through esbuild's CSS pipeline to handle url() references.
29
+ */
30
+ export interface DiscoveredPackageCssReference {
31
+ originalHref: string;
32
+ absolutePath: string;
33
+ }
34
+ /**
35
+ * Map of source pug file path to discovered package CSS references.
36
+ * This is populated during build and used by the builder to trigger CSS processing.
37
+ */
38
+ export declare const discoveredPackageCssReferences: Map<string, DiscoveredPackageCssReference[]>;
26
39
  /**
27
40
  * Clear discovered pug references. Should be called before a new build.
28
41
  */
@@ -31,5 +44,9 @@ export declare function clearDiscoveredPugReferences(): void;
31
44
  * Clear discovered less references. Should be called before a new build.
32
45
  */
33
46
  export declare function clearDiscoveredLessReferences(): void;
47
+ /**
48
+ * Clear discovered package CSS references. Should be called before a new build.
49
+ */
50
+ export declare function clearDiscoveredPackageCssReferences(): void;
34
51
  declare const plugin: esbuild.Plugin;
35
52
  export default plugin;
package/bundles/index.js CHANGED
@@ -69150,6 +69150,7 @@ var import_pug = __toESM(require_lib14());
69150
69150
  var import_fs4 = __toESM(require("fs"));
69151
69151
  var import_path5 = __toESM(require("path"));
69152
69152
  var import_crypto = __toESM(require("crypto"));
69153
+ var import_module = require("module");
69153
69154
  function computeContentHash(content) {
69154
69155
  const hash = import_crypto.default.createHash("sha256").update(content).digest();
69155
69156
  const num = hash.readUInt32BE(0);
@@ -69185,14 +69186,36 @@ function isPugReference(assetPath) {
69185
69186
  function isLessReference(assetPath) {
69186
69187
  return assetPath.endsWith(".less") && isRelativeAssetPath(assetPath);
69187
69188
  }
69189
+ var PKG_PREFIX = "pkg:";
69190
+ function isPackageReference(assetPath) {
69191
+ return assetPath.startsWith(PKG_PREFIX);
69192
+ }
69193
+ function getPackageSpecifier(assetPath) {
69194
+ return assetPath.slice(PKG_PREFIX.length);
69195
+ }
69196
+ function isPackageCssReference(assetPath) {
69197
+ return isPackageReference(assetPath) && assetPath.endsWith(".css");
69198
+ }
69199
+ function resolvePackagePath(packageSpecifier, fromFile) {
69200
+ try {
69201
+ const require2 = (0, import_module.createRequire)(fromFile);
69202
+ return require2.resolve(packageSpecifier);
69203
+ } catch {
69204
+ return null;
69205
+ }
69206
+ }
69188
69207
  var discoveredPugReferences = /* @__PURE__ */ new Map();
69189
69208
  var discoveredLessReferences = /* @__PURE__ */ new Map();
69209
+ var discoveredPackageCssReferences = /* @__PURE__ */ new Map();
69190
69210
  function clearDiscoveredPugReferences() {
69191
69211
  discoveredPugReferences.clear();
69192
69212
  }
69193
69213
  function clearDiscoveredLessReferences() {
69194
69214
  discoveredLessReferences.clear();
69195
69215
  }
69216
+ function clearDiscoveredPackageCssReferences() {
69217
+ discoveredPackageCssReferences.clear();
69218
+ }
69196
69219
  function applyAssetNamesTemplate(template, originalPath, hash, baseDir) {
69197
69220
  const ext = import_path5.default.extname(originalPath).slice(1);
69198
69221
  const name3 = import_path5.default.basename(originalPath, import_path5.default.extname(originalPath));
@@ -69204,6 +69227,7 @@ async function processHtmlAssets(html, pugFilePath, options) {
69204
69227
  const assets = [];
69205
69228
  const pugReferences = [];
69206
69229
  const lessReferences = [];
69230
+ const packageCssReferences = [];
69207
69231
  const assetNames = options.assetNames || "[name]-[hash]";
69208
69232
  const outdir = options.outdir || ".";
69209
69233
  const outbase = options.outbase || import_path5.default.dirname(pugFilePath);
@@ -69230,6 +69254,7 @@ async function processHtmlAssets(html, pugFilePath, options) {
69230
69254
  }
69231
69255
  const discoveredPugPaths = /* @__PURE__ */ new Set();
69232
69256
  const discoveredLessPaths = /* @__PURE__ */ new Set();
69257
+ const discoveredPackageCssPaths = /* @__PURE__ */ new Set();
69233
69258
  for (const { fullMatch, attr, value } of matches) {
69234
69259
  let newValue = value;
69235
69260
  if (attr === "srcset") {
@@ -69281,6 +69306,32 @@ async function processHtmlAssets(html, pugFilePath, options) {
69281
69306
  discoveredLessPaths.add(absolutePath);
69282
69307
  lessReferences.push({ originalHref: value, absolutePath });
69283
69308
  }
69309
+ } else if (isPackageCssReference(value)) {
69310
+ const packageSpecifier = getPackageSpecifier(value);
69311
+ const absolutePath = resolvePackagePath(packageSpecifier, pugFilePath);
69312
+ if (absolutePath && !discoveredPackageCssPaths.has(absolutePath)) {
69313
+ discoveredPackageCssPaths.add(absolutePath);
69314
+ packageCssReferences.push({ originalHref: value, absolutePath });
69315
+ }
69316
+ } else if (isPackageReference(value)) {
69317
+ const packageSpecifier = getPackageSpecifier(value);
69318
+ const absolutePath = resolvePackagePath(packageSpecifier, pugFilePath);
69319
+ if (absolutePath) {
69320
+ const hashedPath = await processPackageAsset(
69321
+ absolutePath,
69322
+ packageSpecifier,
69323
+ pugFilePath,
69324
+ outdir,
69325
+ outbase,
69326
+ assetNames,
69327
+ publicPath,
69328
+ assets,
69329
+ processedAssets
69330
+ );
69331
+ if (hashedPath) {
69332
+ newValue = hashedPath;
69333
+ }
69334
+ }
69284
69335
  } else if (isRelativeAssetPath(value)) {
69285
69336
  const hashedPath = await processAsset(
69286
69337
  value,
@@ -69302,7 +69353,13 @@ async function processHtmlAssets(html, pugFilePath, options) {
69302
69353
  modifiedHtml = modifiedHtml.replace(fullMatch, newFullMatch);
69303
69354
  }
69304
69355
  }
69305
- return { html: modifiedHtml, assets, pugReferences, lessReferences };
69356
+ return {
69357
+ html: modifiedHtml,
69358
+ assets,
69359
+ pugReferences,
69360
+ lessReferences,
69361
+ packageCssReferences
69362
+ };
69306
69363
  }
69307
69364
  async function processAsset(assetPath, pugDir, pugFilePath, outdir, outbase, assetNames, publicPath, assets, processedAssets) {
69308
69365
  const absoluteSource = import_path5.default.resolve(pugDir, assetPath);
@@ -69343,6 +69400,47 @@ async function processAsset(assetPath, pugDir, pugFilePath, outdir, outbase, ass
69343
69400
  return null;
69344
69401
  }
69345
69402
  }
69403
+ async function processPackageAsset(absoluteSource, packageSpecifier, pugFilePath, outdir, outbase, assetNames, publicPath, assets, processedAssets) {
69404
+ if (processedAssets.has(absoluteSource)) {
69405
+ return processedAssets.get(absoluteSource);
69406
+ }
69407
+ if (!import_fs4.default.existsSync(absoluteSource)) {
69408
+ return null;
69409
+ }
69410
+ try {
69411
+ const content = await import_fs4.default.promises.readFile(absoluteSource);
69412
+ const hash = computeContentHash(content);
69413
+ const filename = import_path5.default.basename(packageSpecifier);
69414
+ const packageAssetNames = assetNames.replace(/\[dir\]\/?/g, "");
69415
+ const hashedFilename = applyAssetNamesTemplate(
69416
+ packageAssetNames,
69417
+ filename,
69418
+ hash,
69419
+ ""
69420
+ // No base dir for package assets
69421
+ );
69422
+ const absoluteOutput = import_path5.default.join(outdir, hashedFilename);
69423
+ const pugRelativeToOutbase = import_path5.default.relative(outbase, pugFilePath);
69424
+ const htmlOutputPath = import_path5.default.join(outdir, pugRelativeToOutbase);
69425
+ const htmlOutputDir = import_path5.default.dirname(htmlOutputPath);
69426
+ let htmlPath;
69427
+ if (publicPath) {
69428
+ htmlPath = publicPath.replace(/\/$/, "") + "/" + hashedFilename;
69429
+ } else {
69430
+ htmlPath = import_path5.default.relative(htmlOutputDir, absoluteOutput);
69431
+ }
69432
+ assets.push({
69433
+ originalPath: packageSpecifier,
69434
+ hashedPath: htmlPath,
69435
+ absoluteSource,
69436
+ absoluteOutput
69437
+ });
69438
+ processedAssets.set(absoluteSource, htmlPath);
69439
+ return htmlPath;
69440
+ } catch {
69441
+ return null;
69442
+ }
69443
+ }
69346
69444
  async function copyAssets(assets) {
69347
69445
  const written = /* @__PURE__ */ new Set();
69348
69446
  for (const asset of assets) {
@@ -69437,7 +69535,8 @@ async function loadAsEntrypoint(filepath, options) {
69437
69535
  html: processedHtml,
69438
69536
  assets,
69439
69537
  pugReferences,
69440
- lessReferences
69538
+ lessReferences,
69539
+ packageCssReferences
69441
69540
  } = await processHtmlAssets(contents, filepath, options);
69442
69541
  contents = processedHtml;
69443
69542
  if (pugReferences.length > 0) {
@@ -69446,6 +69545,9 @@ async function loadAsEntrypoint(filepath, options) {
69446
69545
  if (lessReferences.length > 0) {
69447
69546
  discoveredLessReferences.set(filepath, lessReferences);
69448
69547
  }
69548
+ if (packageCssReferences.length > 0) {
69549
+ discoveredPackageCssReferences.set(filepath, packageCssReferences);
69550
+ }
69449
69551
  if (assets.length > 0) {
69450
69552
  await copyAssets(assets);
69451
69553
  }
@@ -69707,6 +69809,18 @@ function rewriteLessReferencesInHtml(html, lessReferences, lessToOutputPath, htm
69707
69809
  }
69708
69810
  return result;
69709
69811
  }
69812
+ function rewritePackageCssReferencesInHtml(html, packageCssReferences, packageCssToOutputPath, htmlOutputDir, outdir) {
69813
+ let result = html;
69814
+ for (const ref of packageCssReferences) {
69815
+ const outputPath = packageCssToOutputPath.get(ref.absolutePath);
69816
+ if (outputPath) {
69817
+ const absoluteOutputPath = import_path8.default.join(outdir, outputPath);
69818
+ const relativePath = import_path8.default.relative(htmlOutputDir, absoluteOutputPath);
69819
+ result = result.split(ref.originalHref).join(relativePath);
69820
+ }
69821
+ }
69822
+ return result;
69823
+ }
69710
69824
  function outExt(inExt) {
69711
69825
  if (inExt.match(/^((c|m)?sx?|tsx?)$/)) {
69712
69826
  return "js";
@@ -69813,13 +69927,17 @@ var esbuilder = {
69813
69927
  );
69814
69928
  clearDiscoveredPugReferences();
69815
69929
  clearDiscoveredLessReferences();
69930
+ clearDiscoveredPackageCssReferences();
69816
69931
  const pugHtmlOutputs = /* @__PURE__ */ new Map();
69817
69932
  const pugToOutputPath = /* @__PURE__ */ new Map();
69818
69933
  const lessToOutputPath = /* @__PURE__ */ new Map();
69934
+ const packageCssToOutputPath = /* @__PURE__ */ new Map();
69819
69935
  const processedPugFiles = /* @__PURE__ */ new Set();
69820
69936
  const processedLessFiles = /* @__PURE__ */ new Set();
69937
+ const processedPackageCssFiles = /* @__PURE__ */ new Set();
69821
69938
  const pendingPugFiles = [];
69822
69939
  const pendingLessFiles = [];
69940
+ const pendingPackageCssFiles = [];
69823
69941
  const processOutputFiles = async (result, currentOutputFilesMap, isDiscoveredBuild) => {
69824
69942
  if (result.errors.length || result.warnings.length) {
69825
69943
  log_default.info(`Build completed with errors or warnings:`, {
@@ -69880,6 +69998,7 @@ var esbuilder = {
69880
69998
  if (isHtmlOutput && sourcePath) {
69881
69999
  const pugRefs = discoveredPugReferences.get(sourcePath) || [];
69882
70000
  const lessRefs = discoveredLessReferences.get(sourcePath) || [];
70001
+ const packageCssRefs = discoveredPackageCssReferences.get(sourcePath) || [];
69883
70002
  pugToOutputPath.set(sourcePath, relativeTarget);
69884
70003
  for (const ref of pugRefs) {
69885
70004
  if (!processedPugFiles.has(ref.absolutePath) && !pendingPugFiles.includes(ref.absolutePath)) {
@@ -69909,12 +70028,27 @@ var esbuilder = {
69909
70028
  }
69910
70029
  }
69911
70030
  }
70031
+ for (const ref of packageCssRefs) {
70032
+ if (!processedPackageCssFiles.has(ref.absolutePath) && !pendingPackageCssFiles.includes(ref.absolutePath)) {
70033
+ if (import_fs7.default.existsSync(ref.absolutePath)) {
70034
+ pendingPackageCssFiles.push(ref.absolutePath);
70035
+ log_default.debug(
70036
+ `Discovered package CSS reference: ${ref.originalHref} -> ${ref.absolutePath}`
70037
+ );
70038
+ } else {
70039
+ log_default.warn(
70040
+ `Referenced package CSS file not found: ${ref.originalHref} (resolved to ${ref.absolutePath})`
70041
+ );
70042
+ }
70043
+ }
70044
+ }
69912
70045
  pugHtmlOutputs.set(sourcePath, {
69913
70046
  content: file.text,
69914
70047
  sourcePath,
69915
70048
  outputPath: import_path8.default.join(outdir, relativeTarget),
69916
70049
  pugReferences: pugRefs,
69917
- lessReferences: lessRefs
70050
+ lessReferences: lessRefs,
70051
+ packageCssReferences: packageCssRefs
69918
70052
  });
69919
70053
  } else {
69920
70054
  if (relativeTarget.endsWith(".css") && oFM_result?.entrypoint?.infile.extension === "less") {
@@ -70037,6 +70171,39 @@ var esbuilder = {
70037
70171
  }
70038
70172
  }
70039
70173
  }
70174
+ while (pendingPackageCssFiles.length > 0) {
70175
+ const batch = pendingPackageCssFiles.splice(0);
70176
+ const newEntryPoints = batch.filter(
70177
+ (f) => !processedPackageCssFiles.has(f)
70178
+ );
70179
+ if (newEntryPoints.length === 0) break;
70180
+ log_default.debug(
70181
+ `Building ${newEntryPoints.length} discovered package CSS file(s):`,
70182
+ newEntryPoints
70183
+ );
70184
+ for (const cssSourcePath of newEntryPoints) {
70185
+ processedPackageCssFiles.add(cssSourcePath);
70186
+ const entryNames = (finalConfig.assetNames?.replace("[ext]", "") || "[name]-[hash]").replace(/\[dir\]\/?/g, "");
70187
+ const cssConfig = {
70188
+ ...finalConfig,
70189
+ entryPoints: [cssSourcePath],
70190
+ entryNames
70191
+ };
70192
+ const cssResult = await esbuild.build(cssConfig);
70193
+ for (const file of cssResult.outputFiles) {
70194
+ const relativeFilePath = import_path8.default.relative(outdir, file.path);
70195
+ if (relativeFilePath.endsWith(".css")) {
70196
+ packageCssToOutputPath.set(cssSourcePath, relativeFilePath);
70197
+ log_default.debug(
70198
+ `Built package CSS file: ${cssSourcePath} -> ${relativeFilePath}`
70199
+ );
70200
+ }
70201
+ await fileWriter.writeFile(file.path, file.contents, {
70202
+ encoding: "utf-8"
70203
+ });
70204
+ }
70205
+ }
70206
+ }
70040
70207
  log_default.debug(
70041
70208
  `Rewriting pug/less references in ${pugHtmlOutputs.size} HTML file(s)`
70042
70209
  );
@@ -70062,6 +70229,15 @@ var esbuilder = {
70062
70229
  outdir
70063
70230
  );
70064
70231
  }
70232
+ if (output.packageCssReferences.length > 0) {
70233
+ html = rewritePackageCssReferencesInHtml(
70234
+ html,
70235
+ output.packageCssReferences,
70236
+ packageCssToOutputPath,
70237
+ htmlOutputDir,
70238
+ outdir
70239
+ );
70240
+ }
70065
70241
  const promise = fileWriter.writeFile(output.outputPath, html, {
70066
70242
  encoding: "utf-8"
70067
70243
  });