ThatOpen/web-ifc-viewer

unexpected token 'export' trying to import in a sveltekit typescript app

sanzoghenzo opened this issue · 2 comments

Hi! I'm having some troubles trying to integrate this library into a sveltekit app.

npm create svelte@latest my-app
cd my-app
npm install
npm install web-ifc-viewer

Then I adapted the svelte example to accomodate the sveltekit structure

  • moved the store and the components in src/lib
  • app.svelte becomes routes/+page.svelte and the imports are adjusted accordingly:
<script lang="ts">
	import { onMount } from 'svelte';
	import { viewer, modelID, ifcTree, expID } from '$lib/ifc-store'
	import PropertyView from '$lib/PropertyView.svelte';
	import SpatialTree from '$lib/SpatialTree.svelte';

// ...

ifc-store.ts is the same as the original IfcStore.js

import { writable, derived } from 'svelte/store';
import { IfcViewerAPI } from 'web-ifc-viewer';
//...

when I run npm run dev, the following error occurs:

[vite] Error when evaluating SSR module /src/lib/ifc-store.ts: failed to import "web-ifc-viewer"
|- /home/xxxx/dev/ifc-toolkit-frontend/node_modules/web-ifc-viewer/dist/index.js:1
export * from './ifc-viewer-api';
^^^^^^

Searching for the error online, I only found the suggestion to add "type": "module" in the package.json file, but this needs to be done in the web-ifc-viewer.

I tried to edit it in my node_modules, but then the next error is that it cannot find the ifc-viewer-api module, so I gave up trying to understand what is goin on....

Somebody can please help me?

Update: Svelte's FAQs states You may wish to suggest to library authors that they provide an ESM version.

Running publint displays the following warning: dist/index.js is written in ESM, but is interpreted as CJS. Consider using the .mjs extension, e.g. dist/index.mjs (but this can also resolved using the aforementioned "type": "module").

Hey, FYI it is likely that before the end of the summer this library will be substituted by this better one (as well as a full documentation). I'm sure this problem will be solved there as well.