histoire-dev / histoire

⚡ Fast and beautiful interactive component playgrounds, powered by Vite
https://histoire.dev
MIT License
3.05k stars 178 forks source link

Importing UI component libraries built by Webpack will cause errors #480

Open SihenZhang opened 1 year ago

SihenZhang commented 1 year ago

Describe the bug

Actually, I'm not sure whether it's a bug of Histoire or not. But I really have no idea and have to assume it is for now.

I'm currently making a UI component library which is a secondary packaging for other component libraries. I'd like to use Histoire to help me testing. However, when I imported a component library built by Webpack into Histoire, an error occured. The following is the report:

Error while collecting story C:/Users/H-VISIONS/Desktop/histoire-vue3/src/PdfPreviewButton.story.vue:
Error: Automatic publicPath is not supported in this browser
    at C:\Users\H-VISIONS\Desktop\histoire-vue3\node_modules\pdf-viewer-vue\dist\vue3-pdf-viewer.js:1:387870
    at C:\Users\H-VISIONS\Desktop\histoire-vue3\node_modules\pdf-viewer-vue\dist\vue3-pdf-viewer.js:1:388030
    at C:\Users\H-VISIONS\Desktop\histoire-vue3\node_modules\pdf-viewer-vue\dist\vue3-pdf-viewer.js:1:1216179
    at C:\Users\H-VISIONS\Desktop\histoire-vue3\node_modules\pdf-viewer-vue\dist\vue3-pdf-viewer.js:1:81
    at Object.<anonymous> (C:\Users\H-VISIONS\Desktop\histoire-vue3\node_modules\pdf-viewer-vue\dist\vue3-pdf-viewer.js:1:253)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)

This component is working fine in a standard Vite + Vue enviroment. I also tested it in Storybook and it has no issues. Therefore, I can only guess that this is a bug caused by Histoire when collecting stories. I cannot confirm that all component libraries built by Webpack will have this problem. It seems that Webpack added some codes during building to detect publicPath support, which caused this error, based on the uglified code for this component I'm currently using.

var e;__webpack_require__.g.importScripts&&(e=__webpack_require__.g.location+"");
var t=__webpack_require__.g.document;
if(!e&&t&&((e=t.currentScript?t.currentScript.src:e)||(t=t.getElementsByTagName("script")).length&&(e=t[t.length-1].src)),!e)
  throw new Error("Automatic publicPath is not supported in this browser");

Reproduction

Here is a Stackblitz containing a minimal reproduction. https://stackblitz.com/edit/histoire-vue3-starter-89rkjd?file=src/PdfPreviewButton.story.vue

Here is a Stackblitz of a Vite + Vue project that includes this component. https://stackblitz.com/edit/vitejs-vite-r6hmfc?file=src/App.vue

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 2.26 GB / 15.69 GB
  Binaries:
    Node: 18.14.0 - D:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - D:\Program Files\nodejs\yarn.CMD
    npm: 9.3.1 - D:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1265.0), Chromium (110.0.1587.63)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @histoire/plugin-vue: ^0.13.0 => 0.13.2 
    @vitejs/plugin-vue: ^4.0.0 => 4.0.0 
    histoire: ^0.13.0 => 0.13.2 
    vite: ^4.1.0 => 4.1.4

Used Package Manager

npm

Validations

stackblitz[bot] commented 1 year ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.