Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importing in typescript project does not work #72

Closed
CramericaIndustries opened this issue Sep 20, 2023 · 9 comments · Fixed by #146
Closed

importing in typescript project does not work #72

CramericaIndustries opened this issue Sep 20, 2023 · 9 comments · Fixed by #146

Comments

@CramericaIndustries
Copy link

I'm trying to use the maplibre-contour library in my typescript file, but I'm getting a compiler error:

Error: export 'default' (imported as 'mlcontour') was not found in 'maplibre-contour' (module has no exports)

installed it with yarn
yarn add maplibre-contour --save

Here is the code snippet:

import mlcontour from "maplibre-contour";
import type {
    Map as MapLibreMap
} from 'maplibre-gl';


export abstract class MlContourLines {
    public static addContourLines(map: MapLibreMap) {
        ...
        const contourSource = new mlcontour.DemSource({...});          // this line triggers the compiler error
        ...
    }
}

Could it be that the "maplibre-contour" es6 module only exports types and not the actual code?

@CramericaIndustries
Copy link
Author

using import mlcontour from "maplibre-contour/dist/index"; seems to work.

@msbarry
Copy link
Contributor

msbarry commented Sep 22, 2023

Does this work: import * as mlcontour from "maplibre-contour" ?

I include these exports in the package.json:

"browser": "dist/index.min.js",
"unpkg": "dist/index.min.js",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",

that I build using rollup like this:

create("dist/index.cjs", "cjs"),
create("dist/index.mjs", "esm"),
create("dist/index.js", "umd"),
create("dist/index.min.js", "umd", [terser()]),

I'm not quite sure which it's picking up when you do import "maplibre-contour/dist/index", maybe I have the mapping wrong in package.json?

@CramericaIndustries
Copy link
Author

using import mlcontour from "maplibre-contour/dist/index"; seems to work.

Quick update... using that line of code, everything compiles without errors, but then in the browser the following error is thrown...

dea5dd08-9496-45e2-97b2-8ef1170e1732:1770 Uncaught ReferenceError: C_tmp_main_nx_monorepo_node_modules_angular_devkit_build_angular_node_modules_babel_runtime_helpers_esm_asyncToGenerator_js__WEBPACK_IMPORTED_MODULE_0__ is not defined
    at dea5dd08-9496-45e2-97b2-8ef1170e1732:1770:176
    at new Actor (dea5dd08-9496-45e2-97b2-8ef1170e1732:1817:8)
    at dea5dd08-9496-45e2-97b2-8ef1170e1732:1897:13
    at dea5dd08-9496-45e2-97b2-8ef1170e1732:1898:3

@CramericaIndustries
Copy link
Author

import * as mlcontour from "maplibre-contour"

Nope sorry.

import * as mlcontour from "maplibre-contour"
...
new mlcontour.default.DemSource({...});

still throws...
Error: export 'default' (imported as 'mlcontour') was not found in 'maplibre-contour' (module has no exports)

I ended up loading the "maplibre-contour.min.js" file on demand via vanilla javascript and am not using TS typings for the mlcontour lib.

@msbarry
Copy link
Contributor

msbarry commented Oct 1, 2023

I'm trying to reproduce this issue in a minimal test project to make sure it works in CI - can you see what's different from this test project to yours so I can reproduce the issue? https://github.com/onthegomap/maplibre-contour/tree/test-build/test-project

@caspg
Copy link

caspg commented Dec 27, 2023

I have a similar issue and found out that the issue is related to esbuild. But only when run with --bundle option https://esbuild.github.io/api/#bundle

You can run the below command inside the test-project and it should fail.

npx esbuild index.ts --bundle
npx esbuild --version
0.19.10

node -v
v20.10.0

@msbarry
Copy link
Contributor

msbarry commented Jan 1, 2024

OK I think I fixed this in #146 - try using version 0.0.6.

@CramericaIndustries
Copy link
Author

I can confirm that with v0.0.6 the "maplibre-contour" import is now working as expected in my project. Thank you!

@bdon
Copy link
Contributor

bdon commented Aug 4, 2024

using publint reports missing types:

https://publint.dev/[email protected]

I ended up having to switch to tsup or another project to generate .cjs, .cts etc: protomaps/protomaps-leaflet#163

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants