A guide on incorporating and utilizing PhotoSwipe in Aurelia / Typescript applications

I've been attempting to integrate PhotoSwipe into my Aurelia project, but I'm struggling to get it working.

Within my aurelio.json file under bundles, I've included:

{
    "name": "photoswipe",
    "path": "../node_modules/photoswipe/dist/",
    "main": "photoswipe.min",
    "resources": [
        "photoswipe-ui-default.min.js",
        "photoswipe.css",
        "default-skin/default-skin.css"
    ]
}

In my package.json, I have:

"@types/photoswipe": "^4.0.27",
"photoswipe": "^4.1.1"

In my .ts module, I've added:

import PhotoSwipe from 'photoswipe';

The code snippet I'm using to open the gallery is directly from the documentation:

imageClicked() { 

    var pswpElement = document.querySelectorAll('.pswp')[0];
    
    // building items array
    var items = [
        {
            src: 'https://placekitten.com/600/400',
            w: 600,
            h: 400
        },
        {
            src: 'https://placekitten.com/1200/900',
            w: 1200,
            h: 900
        }
    ];
    
    // defining options (if required)
    var options = {
        index: 0 // starting at first slide
    };

    // Initializing and opening PhotoSwipe
    var gallery = new PhotoSwipe<PhotoSwipeUI_Default.Options>(pswpElement as HTMLElement, PhotoSwipeUI_Default, items, options);
    gallery.init();
}

The aurelia project compiles without errors, however, during runtime, I encounter this error:

Uncaught ReferenceError: PhotoSwipeUI_Default is not defined

I attempted adding an export statement in aurelia.json bundle:

"exports": [ "PhotoSwipe", "PhotoSwipeUI_Default" ]

But this did not resolve the issue.

I also experimented with various import statements:

import PhotoSwipeUI_Default from 'photoswipe'; // Now PhotoSwipeUI_Default is of type PhotoSwipe
import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default' // Module not found compile error
import PhotoSwipeUI_Default from 'npm:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7000181f041f030719001530445e415e41">[email protected]</a>/dist/photoswipe-ui-default.min.js'; // Cannot find module

I feel stuck and my attempts to solve this problem are fruitless. What am I overlooking?

Answer №1

Your current configuration needs to be adjusted. It seems like pointing Aurelia to the minified file is causing some issues. Let the CLI take care of minifying the JS files for you.

{
  "name": "photoswipe",
  "path": "../node_modules/photoswipe/dist/",
  "main": "photoswipe",
  "resources": [
    "photoswipe-ui-default.js"
  ]
}

To import, use the following:

import PhotoSwipe from 'photoswipe';
import PhotoSwipeUI_Default from 'photoswipe/photoswipe-ui-default';

Additionally, keep in mind that the photoswipe css files load image files for certain aspects. The Aurelia CLI currently has a limitation in handling this issue. This should be resolved before the final release. In the meantime, you'll have to load the CSS using link elements.

  <!-- Core CSS file -->
  <link rel="stylesheet" href="node_modules/photoswipe/dist/photoswipe.css">

  <!-- Skin CSS file (styling of UI - buttons, caption, etc.)
     In the folder of skin CSS file there are also:
     - .png and .svg icons sprite, 
     - preloader.gif (for browsers that do not support CSS animations) -->
  <link rel="stylesheet" href="node_modules/photoswipe/dist/default-skin/default-skin.css">

If you encounter any difficulties, please let me know so we can find a solution together.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The object labeled as "EventEmitter" does not support the method "storeBuffer"

Sorry for the inconvenience and formatting issues. You can find the package on GitHub here: https://github.com/CollectionFS I am currently working on implementing the "Store a File From the Server" example. In my file lib/collections.js, I have the follo ...

Error encountered: ⨯ Compilation of TypeScript failed

My current project is utilizing Node.js in conjunction with TypeScript An issue has arisen during compilation within my Node.js application: E:\NodeProjects\esshop-mongodb-nodejs\node_modules\ts-node\src\index.ts:859 ret ...

The frontend-maven-plugin encounters issues during the npm install process

I encountered an issue while attempting to use the frontend-maven-plugin in my project. Although I can successfully run npm install and npm run build through the command line, running it with the plugin resulted in the following error: [INFO] ------------ ...

Angular and Visual Studio require the use of "ng" and "npm" commands

Currently using: Windows 10, Visual Studio 2022 17.1.0 (scheduled for an update). In the past, I would simply copy JavaScript library files into a subfolder of my website or application, add some <script...> tags to the appropriate pages, and consid ...

Error occurred while creating a new instance of a generic object

I´m encountering a TypeError: type is not a constructor when attempting to convert API data into Frontend DTOs. The transformation method is as follows: transform<T>(entities: any[]) { const tableDtos = new Array<T>(); for (const ent ...

Accessing and sending only the body part of an HTTP response in Angular 7 test cases: A comprehensive guide

Currently, I am working on creating unit test cases in Angular 7 for a Component that utilizes an asynchronous service. This is the content of my component file: submitLoginForm() { if (this.loginForm.valid) { // send a http request to save t ...

Designing a Tombstone with TypeScript

In TypeScript, I have certain values that are only necessary within the type system and not at runtime. I am seeking a way to void these values without losing their type information. The function bury illustrated below is intended to replace the actual v ...

When attempting to run a npm script, an error occurs with an existing gulp task stating "Task 'dev' is not in your gulpfile"

Summed up: I've got a task set up in my gulpfile that works fine when running gulp mytask. However, when trying to use it as npm's start script, an error occurs. Here's the setup: //gulpfile.js (gulp 4) var dev = gulp.series(a,series,of,t ...

Is there a way to integrate TypeScript into the CDN version of Vue?

For specific areas of my project, I am utilizing the Vue CDN. I would like to incorporate Typescript support for these sections as well. However, our technical stack limitations prevent us from using Vue CLI. Is there a method to import Vue.js into a bas ...

Steps for incorporating a new element in Reactjs

While attempting to insert a new element into a React object, I encountered the following issue: const isAdmin = true let schema = { fname: Yup.string().required('Required'), lname: Yup.string().required('Required&apo ...

Differences between installing and updating a npm package from a private GitHub repository specified in the package.json file

I came across a similar question regarding the differences between npm install and update on Stack Overflow at npm-install-vs-update-whats-the-difference My query pertains to the usage of install versus update for private GitHub repositories using git+ UR ...

Error: React - Unable to access the 'lazy' property because it is undefined

I used a helpful guide on the webpack website to incorporate TypeScript into my existing React App. However, upon launching the app, an error message pops up saying: TypeError: Cannot read property 'lazy' of undefined The version of React being ...

Oh no! A critical mistake has occurred: Mark-compact operations are not working efficiently near the heap limit, leading to a failed allocation due to the

My project is not particularly complex, I only just started it. However, when I run the command npm run dev, node js consumes more than 4GB of memory and eventually crashes with a fatal error: --- Last few GCs --- [16804:0000018EB02350F0] 128835 ms: Mar ...

Trouble in Dockerland: Struggling to Connect to the Database

Today, I encountered an error on my Mac 14.2.1 with Docker 4.27.2. When running npm run wp-env start, the container is created and running, but a database connection issue is displayed. ➜ TEST npm run wp-env start > @ wp-env /Users/ciaran/Projects/T ...

Nodejs is downloaded from NPM on every occasion

Using Fedora, I initially had Node.js and npm installed from the repositories. However, when I needed to install SailsJS, it required a newer version of npm. To accomplish this, I ran sudo npm install npm -g. Subsequently, each package installation began ...

Hey there! I recently developed a Quasar App Website, but I'm having trouble pushing it to Heroku. When attempting to do so, I encountered a "Push

While attempting to push my git to Heroku, I encountered an error at the final stage. Upon inspecting the console during project deployment, I realized that warnings are not necessarily catastrophic for app crashing. However, a critical error message caugh ...

Deduce the Prop Component Type by Examining the Attribute Type

I am facing an issue with a component that requires a `labels` attribute. <Component defaultValue={FURNITURE.BED} labels={[ { value: FURNITURE.BED, text: 'Bed', }, { value: FURNITURE.COUCH, text: 'C ...

How can a particular route parameter in Vue3 with Typescript be used to retrieve an array of strings?

Encountered a build error: src/views/IndividualProgramView.vue:18:63 - error TS2345: Argument of type 'string | string[]' is not assignable to parameter of type 'string'. Type 'string[]' is not assignable to type 'strin ...

Having trouble uploading my React components as a private npm package

After creating a new organization on npm, I attempted to publish my react component as a scoped package so that my team could utilize it across all in-house applications. However, when I ran the npm publish command, I encountered the following error: npm ...

What techniques does Angular2 use to handle dependency injection?

When working with Angular2 components, I know that to inject a dependency, you simply annotate an argument in the constructor, like how ThingService is injected here. However, what I am wondering is how Angular actually knows what to inject at runtime. A ...