ol-ext icon indicating copy to clipboard operation
ol-ext copied to clipboard

Svelte and CJS vs ES module issue

Open SimonSchneider opened this issue 2 years ago • 14 comments

Trying to import ol-ext in a svelteKit project gives the following error:

ol-ext doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.

Given that OpenLayers defines "type": "module" (link) maybe it'd be reasonable to ol-ext to do the same?

SimonSchneider avatar Sep 26 '22 08:09 SimonSchneider

Added to the package.json. Look at https://github.com/Siedlerchr/types-ol-ext to have typescript definition.

Viglino avatar Sep 26 '22 09:09 Viglino

That was fast, thank you very much!

Will check if the typescript definitions require "type": "module", thanks!

Any idea on when a new release will be available?

SimonSchneider avatar Sep 26 '22 10:09 SimonSchneider

I've published a 4.0.1. Tell me if it runs.

Viglino avatar Sep 26 '22 12:09 Viglino

This breaks webpack for me with the following error:

ERROR in ./node_modules/ol-ext/layer/GeoImage.js 5:0-43 Module not found: Error: Can't resolve 'ol/layer/Image' in '/home/isti/prog/collmot/skybrush-io/live/node_modules/ol-ext/layer' Did you mean 'Image.js'? BREAKING CHANGE: The request 'ol/layer/Image' failed to resolve only because it was resolved as fully specified (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '.mjs' file, or a '.js' file where the package.json contains '"type": "module"'). The extension in the request is mandatory for it to be fully specified. Add the extension to the request.

I guess that if you want to keep the "type": "module" option moving forward, then file extensions will have to be added to all imports.

isti115 avatar Oct 01 '22 23:10 isti115

See #868

Viglino avatar Oct 03 '22 07:10 Viglino

Sorry for the delay, I've tested it and 4.0.1 has been working great

SimonSchneider avatar Oct 03 '22 09:10 SimonSchneider

@SimonSchneider

Sorry for the delay, I've tested it and 4.0.1 has been working great

It seems that extension is required when using "type": "module"

Viglino avatar Oct 03 '22 10:10 Viglino

I'm unsure, when I tested a local branch I only had issues with the gulpfile needing to be changed to be .mjs the rest seemed to work. For my project the addition of "type": "module" which you released resolved the issue.

SimonSchneider avatar Oct 03 '22 11:10 SimonSchneider

I have quickly grepped through the source and didn't see any reason not to simply just add the .js extension to all the import declarations, so I executed s/(^import .*["'])(.*)(["'])/$1$2.js$3/g in the repo and created a pull request from it (#869), so in case you agree with this approach, @Viglino, you can just merge it right away.

  • The CSS files were left untouched, as those import lines begin with @ symbols.
  • I couldn't find any imports spanning over multiple lines. (Using import .*[^';]\n.*from.)

ps.: It might be worth getting rid of the double quoted imports and turning them into single quotes for consistency, but I didn't want to make unnecessary side effects. Also, some imports had semicolons at the end and some didn't.

isti115 avatar Oct 03 '22 22:10 isti115

@Isti115 That's what I planned to do! you beat me 🚀

Viglino avatar Oct 04 '22 07:10 Viglino

[email protected] is out

Viglino avatar Oct 04 '22 07:10 Viglino

For what it worth, in the types-ol-ext repo I used the following addtion to the webpack to compile the examples:


  {
        test: /\.m?js/,
        type: "javascript/auto",
      },
      {
        test: /\.m?js/,
        resolve: {
          fullySpecified: false,
        },
      },
    ],

Siedlerchr avatar Oct 04 '22 07:10 Siedlerchr

@Viglino I ust noticed that there is a duplicate .js extension: in Overlay/Tooltip

import {getArea as ol_sphere_getArea} from 'ol/sphere.js.js';
import {getLength as ol_sphere_getLength} from 'ol/sphere.js.js';

Siedlerchr avatar Oct 11 '22 16:10 Siedlerchr

@Siedlerchr fixed duplicated extension (there was some more)

Viglino avatar Oct 13 '22 06:10 Viglino

Ouch, sorry, that was my bad, I should've checked for already present extensions before running the RegEx replacement, but for some reason I assumed that all imports were extensionless so far.

isti115 avatar Oct 18 '22 20:10 isti115