@file-viewer/vite-plugin 2.1.15 → 2.1.16

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.
Files changed (2) hide show
  1. package/dist/index.js +165 -13
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -384,13 +384,15 @@ const presetModules = {
384
384
  chunkName: 'file-viewer-preset-engineering'
385
385
  }
386
386
  };
387
- // Vite dep optimization rewrites import.meta.url into node_modules/.vite/deps,
388
- // which breaks package-local worker URLs such as @file-viewer/pptx.
389
- const workerUrlSensitivePackages = [
387
+ // Vite dep optimization rewrites import.meta.url into node_modules/.vite/deps
388
+ // and can trigger full-preset dev-server re-optimization reloads. Keep File
389
+ // Viewer packages out of dep optimization while still optimizing their
390
+ // CommonJS/UMD helper dependencies below.
391
+ const fileViewerOptimizationExcludedPackages = [
392
+ '@file-viewer/core',
390
393
  '@file-viewer/pptx',
391
- '@file-viewer/renderer-presentation',
392
- '@file-viewer/preset-all',
393
- '@file-viewer/preset-office',
394
+ ...rendererModules.map((descriptor) => descriptor.packageName),
395
+ ...Object.values(presetModules).map((preset) => preset.packageName),
394
396
  '@file-viewer/react-full',
395
397
  '@file-viewer/react-legacy-full',
396
398
  '@file-viewer/vue3-full',
@@ -400,6 +402,10 @@ const workerUrlSensitivePackages = [
400
402
  '@file-viewer/jquery-full',
401
403
  '@file-viewer/svelte-full'
402
404
  ];
405
+ const cjsInteropPackages = [
406
+ '@file-viewer/docx',
407
+ 'jszip'
408
+ ];
403
409
  const defaultScanRoots = ['src', 'app', 'pages', 'components'];
404
410
  const defaultScanExtensions = [
405
411
  'js',
@@ -688,7 +694,14 @@ function hasManualChunks(config) {
688
694
  function createOptimizeDepsExclude(config) {
689
695
  return unique([
690
696
  ...(config.optimizeDeps?.exclude || []),
691
- ...workerUrlSensitivePackages
697
+ ...fileViewerOptimizationExcludedPackages
698
+ ]);
699
+ }
700
+ function createOptimizeDepsInclude(config, exclude, anchorPackages) {
701
+ const excluded = new Set(exclude);
702
+ return unique([
703
+ ...(config.optimizeDeps?.include || []),
704
+ ...cjsInteropPackages.filter((packageName) => !excluded.has(packageName) && Boolean(resolvePackageJson(packageName, anchorPackages)))
692
705
  ]);
693
706
  }
694
707
  function createManualChunks(selection, autoPresetIds = []) {
@@ -783,10 +796,14 @@ function collectWorkspacePackageJsons(root) {
783
796
  return packageJsons;
784
797
  }
785
798
  function resolveWorkspacePackageJson(packageName) {
786
- if (!workspacePackageJsonCache) {
787
- workspacePackageJsonCache = collectWorkspacePackageJsons(process.cwd());
799
+ const root = process.cwd();
800
+ if (!workspacePackageJsonCache || workspacePackageJsonCache.root !== root) {
801
+ workspacePackageJsonCache = {
802
+ root,
803
+ packages: collectWorkspacePackageJsons(root)
804
+ };
788
805
  }
789
- return workspacePackageJsonCache.get(packageName) || null;
806
+ return workspacePackageJsonCache.packages.get(packageName) || null;
790
807
  }
791
808
  function resolvePackageJson(packageName, anchorPackages = []) {
792
809
  const requireFns = [projectRequire(), pluginRequire];
@@ -800,7 +817,8 @@ function resolvePackageJson(packageName, anchorPackages = []) {
800
817
  discoveredAnchor = false;
801
818
  for (const anchorPackage of effectiveAnchorPackages) {
802
819
  for (const requireFn of [...requireFns]) {
803
- const anchorPackageJson = tryResolvePackageJson(anchorPackage, requireFn);
820
+ const anchorPackageJson = tryResolvePackageJson(anchorPackage, requireFn) ||
821
+ resolveWorkspacePackageJson(anchorPackage);
804
822
  if (anchorPackageJson && !anchorPackageJsons.has(anchorPackageJson)) {
805
823
  anchorPackageJsons.add(anchorPackageJson);
806
824
  requireFns.push(createRequire(anchorPackageJson));
@@ -850,10 +868,122 @@ function collectSelectedPackages(selection, autoPresetIds = []) {
850
868
  ...selection.descriptors.map((descriptor) => descriptor.packageName)
851
869
  ]);
852
870
  }
871
+ function collectDependencyAnchorPackages(selection, autoPresetIds = []) {
872
+ const presetIds = unique([
873
+ ...(selection.preset ? [selection.preset] : []),
874
+ ...autoPresetIds
875
+ ]);
876
+ return unique([
877
+ ...collectSelectedPackages(selection, autoPresetIds),
878
+ ...presetIds.flatMap((presetId) => presetModules[presetId].rendererIds
879
+ .map((rendererId) => descriptorsById.get(rendererId)?.packageName)
880
+ .filter((packageName) => Boolean(packageName))),
881
+ ...selection.descriptors.map((descriptor) => descriptor.packageName)
882
+ ]);
883
+ }
853
884
  function resolvePackageRoot(packageName, anchorPackages = []) {
854
885
  const packageJson = resolvePackageJson(packageName, anchorPackages);
855
886
  return packageJson ? dirname(packageJson) : null;
856
887
  }
888
+ function resolvePackageEntry(packageName, anchorPackages = []) {
889
+ const packageJsonPath = resolvePackageJson(packageName, anchorPackages);
890
+ if (!packageJsonPath) {
891
+ return null;
892
+ }
893
+ const packageRoot = dirname(packageJsonPath);
894
+ try {
895
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
896
+ const candidates = unique([
897
+ packageJson.module,
898
+ packageJson.main,
899
+ 'index.js'
900
+ ].filter((value) => Boolean(value)));
901
+ for (const candidate of candidates) {
902
+ const entry = resolve(packageRoot, candidate);
903
+ if (existsSync(entry) && statSync(entry).isFile()) {
904
+ return entry;
905
+ }
906
+ }
907
+ }
908
+ catch {
909
+ // Fall back to Node resolution below.
910
+ }
911
+ const anchorPackageJson = anchorPackages
912
+ .map((anchorPackage) => resolvePackageJson(anchorPackage))
913
+ .find(Boolean);
914
+ const requireFns = [
915
+ projectRequire(),
916
+ pluginRequire,
917
+ ...(anchorPackageJson ? [createRequire(anchorPackageJson)] : [])
918
+ ];
919
+ for (const requireFn of requireFns) {
920
+ try {
921
+ return requireFn.resolve(packageName);
922
+ }
923
+ catch {
924
+ // Continue probing alternate anchors.
925
+ }
926
+ }
927
+ return null;
928
+ }
929
+ function readPackageExportEntry(value) {
930
+ if (typeof value === 'string') {
931
+ return value;
932
+ }
933
+ if (!value || typeof value !== 'object') {
934
+ return null;
935
+ }
936
+ const record = value;
937
+ return readPackageExportEntry(record.import) ||
938
+ readPackageExportEntry(record.default) ||
939
+ readPackageExportEntry(record.require);
940
+ }
941
+ function resolvePackageExportEntry(packageName, exportPath, anchorPackages = []) {
942
+ const packageJsonPath = resolvePackageJson(packageName, anchorPackages);
943
+ if (!packageJsonPath) {
944
+ return null;
945
+ }
946
+ try {
947
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
948
+ const exportEntry = readPackageExportEntry(packageJson.exports?.[exportPath]);
949
+ if (!exportEntry) {
950
+ return null;
951
+ }
952
+ const entry = resolve(dirname(packageJsonPath), exportEntry);
953
+ return existsSync(entry) && statSync(entry).isFile() ? entry : null;
954
+ }
955
+ catch {
956
+ return null;
957
+ }
958
+ }
959
+ function normalizeViteAlias(alias) {
960
+ if (!alias) {
961
+ return [];
962
+ }
963
+ if (Array.isArray(alias)) {
964
+ return [...alias];
965
+ }
966
+ return Object.entries(alias).map(([find, replacement]) => ({ find, replacement }));
967
+ }
968
+ function createFileViewerResolveAliases(anchorPackages) {
969
+ const packageNames = unique([
970
+ '@file-viewer/core',
971
+ ...anchorPackages,
972
+ ...cjsInteropPackages
973
+ ]);
974
+ const aliases = [];
975
+ const coreAssetsEntry = resolvePackageExportEntry('@file-viewer/core', './assets', anchorPackages);
976
+ if (coreAssetsEntry) {
977
+ aliases.push({ find: '@file-viewer/core/assets', replacement: coreAssetsEntry });
978
+ }
979
+ aliases.push(...packageNames
980
+ .map((packageName) => {
981
+ const entry = resolvePackageEntry(packageName, anchorPackages);
982
+ return entry ? { find: packageName, replacement: entry } : null;
983
+ })
984
+ .filter((alias) => Boolean(alias)));
985
+ return aliases;
986
+ }
857
987
  async function copyFileIfPresent(from, to) {
858
988
  if (!from || !existsSync(from)) {
859
989
  return false;
@@ -900,6 +1030,11 @@ async function copyKnownRendererAssets(targetRoot, rendererIds) {
900
1030
  await push('pdf', 'pdf-cmaps', join(targetRoot, 'vendor/pdf/cmaps'), () => copyDirectoryIfPresent(pdfRoot ? join(pdfRoot, 'cmaps') : null, join(targetRoot, 'vendor/pdf/cmaps')));
901
1031
  await push('pdf', 'pdf-wasm', join(targetRoot, 'vendor/pdf/wasm'), () => copyDirectoryIfPresent(pdfRoot ? join(pdfRoot, 'wasm') : null, join(targetRoot, 'vendor/pdf/wasm')));
902
1032
  await push('pdf', 'pdf-standard-fonts', join(targetRoot, 'vendor/pdf/standard_fonts'), () => copyDirectoryIfPresent(pdfRoot ? join(pdfRoot, 'standard_fonts') : null, join(targetRoot, 'vendor/pdf/standard_fonts')));
1033
+ const pptxRoot = resolvePackageRoot('@file-viewer/pptx', ['@file-viewer/renderer-presentation']);
1034
+ await push('office-presentation', 'pptx-worker', join(targetRoot, 'vendor/pptx/pptx.worker.js'), () => copyFileIfPresent(pptxRoot ? join(pptxRoot, 'dist/worker/pptx.worker.js') : null, join(targetRoot, 'vendor/pptx/pptx.worker.js')));
1035
+ const docxRoot = resolvePackageRoot('@file-viewer/docx', ['@file-viewer/renderer-word']);
1036
+ await push('office-word-openxml', 'docx-worker', join(targetRoot, 'vendor/docx/docx.worker.js'), () => copyFileIfPresent(docxRoot ? join(docxRoot, 'dist/docx-preview.worker.js') : null, join(targetRoot, 'vendor/docx/docx.worker.js')));
1037
+ await push('office-word-openxml', 'docx-worker-jszip', join(targetRoot, 'vendor/docx/jszip.min.js'), () => copyFileIfPresent(docxRoot ? join(docxRoot, 'dist/jszip.min.js') : null, join(targetRoot, 'vendor/docx/jszip.min.js')));
903
1038
  const cadRoot = resolvePackageRoot('@flyfish-dev/cad-viewer', ['@file-viewer/renderer-cad']);
904
1039
  await push('cad', 'cad-wasm-directory', join(targetRoot, 'wasm/cad'), () => copyDirectoryIfPresent(cadRoot ? join(cadRoot, 'dist/wasm') : null, join(targetRoot, 'wasm/cad')));
905
1040
  const typstCompilerRoot = resolvePackageRoot('@myriaddreamin/typst-ts-web-compiler', [
@@ -947,7 +1082,14 @@ function collectAssetRendererIds(selection, autoPresetIds = []) {
947
1082
  ]);
948
1083
  }
949
1084
  function reportAssetCopy(results, targetRoot, mode) {
950
- const failedRequired = results.filter((result) => !result.copied && ['pdf', 'cad', 'typst'].includes(result.rendererId));
1085
+ const failedRequired = results.filter((result) => !result.copied && [
1086
+ 'pdf',
1087
+ 'office-word-openxml',
1088
+ 'office-presentation',
1089
+ 'archive',
1090
+ 'cad',
1091
+ 'typst'
1092
+ ].includes(result.rendererId));
951
1093
  if (!results.length) {
952
1094
  return;
953
1095
  }
@@ -999,9 +1141,19 @@ export function fileViewerRenderers(options = {}) {
999
1141
  config(userConfig) {
1000
1142
  const projectRoot = resolve(process.cwd(), userConfig.root || '.');
1001
1143
  refreshSelection(projectRoot);
1144
+ const selectedPackages = collectSelectedPackages(selection, autoPresetIds);
1145
+ const dependencyAnchorPackages = collectDependencyAnchorPackages(selection, autoPresetIds);
1146
+ const optimizeDepsExclude = createOptimizeDepsExclude(userConfig);
1002
1147
  const nextConfig = {
1003
1148
  optimizeDeps: {
1004
- exclude: createOptimizeDepsExclude(userConfig)
1149
+ exclude: optimizeDepsExclude,
1150
+ include: createOptimizeDepsInclude(userConfig, optimizeDepsExclude, dependencyAnchorPackages)
1151
+ },
1152
+ resolve: {
1153
+ alias: [
1154
+ ...normalizeViteAlias(userConfig.resolve?.alias),
1155
+ ...createFileViewerResolveAliases(dependencyAnchorPackages)
1156
+ ]
1005
1157
  }
1006
1158
  };
1007
1159
  if ((options.chunkStrategy || 'renderer') === 'none' || hasManualChunks(userConfig)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/vite-plugin",
3
- "version": "2.1.15",
3
+ "version": "2.1.16",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Vite plugin for Flyfish File Viewer on-demand renderer assembly, chunk planning, and offline asset deployment.",
@@ -24,12 +24,12 @@
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",
27
- "url": "git+https://github.com/flyfish-dev/file-viewer-vite-plugin.git",
27
+ "url": "git+https://github.com/flyfish-dev/file-viewer.git",
28
28
  "directory": "packages/presets/vite-plugin"
29
29
  },
30
30
  "homepage": "https://doc.file-viewer.app/guide/on-demand-renderers",
31
31
  "bugs": {
32
- "url": "https://github.com/flyfish-dev/file-viewer-vite-plugin/issues"
32
+ "url": "https://github.com/flyfish-dev/file-viewer/issues"
33
33
  },
34
34
  "funding": {
35
35
  "type": "individual",