freddy38510 / quasar-app-extension-ssg

Static Site Generator App Extension for Quasar.
MIT License
153 stars 16 forks source link

Automatic patch of ES Module import #309

Closed nicolashefti closed 1 year ago

nicolashefti commented 1 year ago

Hi,

I'm using the extension with a Quasar 2 project. I get the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /**/ not supported.
Instead change the require of swiper-vue.js in /**/ server-entry.js to a dynamic import() which is available in all CommonJS modules
  /**/
  code: 'ERR_REQUIRE_ESM'
}

The error message very clear and I am able to patch manually the server-entry.js file, using import instead of require.

Manual patch is ok but it has two limitations:

Is there a way to fix this import once for all?

freddy38510 commented 1 year ago

Related to #85.

Swiper is an ES module only, you need to transpile it to work at server-side.

If using Webpack:

// quasar.config.js
module.exports = configure(function (ctx) {
  return {
    build: {
      transpileDependencies: [/swiper/, 'ssr-window', 'dom7'],
    }
  }
});

If using Vite:

// quasar.config.js
module.exports = configure(function (/* ctx */) {
  return {
    build: {
      extendViteConf(viteConf, { isServer }) {
        if (isServer) {
          viteConf.ssr = viteConf.ssr || {};
          viteConf.ssr.noExternal = viteConf.ssr.noExternal || [];
          viteConf.ssr.noExternal.push("swiper");
        }
      },
    },
  }
});
nicolashefti commented 1 year ago

Works like a charm. Thanks !