-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Can not import widgets.css file not exported from package.json #9212
Comments
Hey @3DGISKing I think it would make sense to file an issue to the cesium-webpack-example repository instead 😉 |
this is actually related with this repo. |
@3DGISKing I am not sure if it is indeed related to this repo, (it may have to do with the webpack configuration) but I think that reporting it there would be a good start for filing the issue 😃 . However, either way, looks valid to me 👍 |
please have a look at this. |
@3DGISKing i do not know if it is ok to export CSS files through |
Closing this issue as we have not received a response from the author in a while. Please re-open if there is additional discussion to be made. |
Please re-open this issue. I've never had to set up the Once you create an
This includes if your consumer's build tools need to grab stylesheets (as in the OP), images, JSON data, web workers, anything like that. I haven't spent a lot of time with it but I think at a minimum it should have entries for |
Is it possible to add some sort of wildcard export that allows any file in CesiumJS to be imported if desired? |
I've been playing with this for the last half-hour or so. I think this might cover everybody: "exports": {
"./package.json": "./package.json",
"./Assets/": "./Build/Cesium/Assets/",
"./ThirdParty/": "./Build/Cesium/ThirdParty/",
"./Widgets/": "./Build/Cesium/Widgets/",
"./Workers/": "./Build/Cesium/Workers/",
".": {
"require": "./index.cjs",
"import": "./Source/Cesium.js"
}
}, but you could also use Also note, though -- Node has deprecated "subpath folder mappings" (like |
Hmm interesting. Some apps import Cesium source files directly, too. i.e. right from the Source directory, not from Build. Maybe we just need to add a |
This is a tricky question. The package file format has evolved over a relatively long time, which means now you're supporting CJS imports via the If I'm right, it would mean that consumers using While we're thinking about this: have you considered distributing a processed/minified version of ES modules instead of "exports": {
"./package.json": "./package.json",
"./Assets/": "./Build/Cesium/Assets/",
"./Workers/": "./Build/Cesium/Workers/",
"./": "./Build/esm/",
".": {
"require": "./index.cjs",
"import": "./Build/esm/Cesium.js"
}
}, This would let consumers write |
For what it is worth I agree with @thw0rted above, that config works, and the proposal of distributing a minified version of each ES module is exactly what is needed. Just for kicks I upgraded my example with c137.js here to Webpack 5 and it still works without a hitch. |
Another interesting finding came up just now. Your ".": {
"production": {
"require": "./Build/Cesium/Cesium.js",
"import": "./Source/Cesium.js"
},
"development": {
"require": "./Build/CesiumUnminified/Cesium.js",
"import": "./Source/Cesium.js"
},
"node": {
"require": "./index.cjs",
"import": "./Source/Cesium.js"
}
}
}, This is good because it cuts out a runtime decision (i.e. the return value of |
I think this is already possible, at least in Webpack 4. For example: (the Cesium module is called terriajs-cesium here for... reasons, but it's a fork of Cesium without any major changes). But there are other environments to consider, like Node.js. The number of different environments where folks might want to use CesiumJS, some of which seem to be mutually incompatible, has really exploded recently, and I feel I'd need to spend quite a lot of time understanding them all in order to make good decisions here. I don't think I can take that time at the moment, but I'm happy to take pull requests that improve things and demonstrate that the important environments still work. Off the top of my head, some of the axes are:
|
It's definitely already possible to import a tree-shakable module directly -- the key word in my post was "minified". As far as I can tell, Cesium does not distribute processed ESM yet. To be honest, though, I don't when that's desirable, because as you say there are dozens of permutations of use cases. If I'm importing ESM but it's going to go through a minification process as part of a (browser-targeted?) build toolchain, I might not care if the package ships minified code, or I might prefer to minify for myself from the original sources. However, if it's available pre-minified, I might be able to reduce my own build time by excluding Cesium from the minification process, which could be a big win. If I'm importing ESM from a recent version of Node, maybe I don't care about the size of the source because it doesn't have to traverse the network. In that case my only concern about size is total package size (for faster At any rate, I agree that it would take a lot of time to make good decisions, as you say. In the meantime, I think there are non-breaking changes that could be made to better support Webpack 5 specifically, while following the intent of the Bare Module Resolution spec to best convey which version of the code is best for which consumer. |
TJ, I noticed the NPM page for your package does not link to a source repo. Your fork of the cesium repo hasn't been updated in a while. Where is the source for the package hosted? |
Thanks, I might take a look at it at some point, but I'm not planning to tie our build to anything proprietary at this time. For one thing, I'm here on the tracker at least once a week, and I don't want to have to worry about whether the issues I'm having stem from Cesium's original source or from your build process (nothing personal!). |
@thw0rted We are always open to contracts / partnerships so you can see the source. BTW, we do run all the Cesium tests on it and it passes, you can do the same. |
Hi @3DGISKing , was this closed because it's been fixed? I haven't seen this issue mentioned in any recent PRs. |
I also encountered the same problem when using Which the report is:
code in main.js: import { Viewer } from 'cesium'
import './style.css'
import 'cesium/Source/Widgets/widgets.css'
let viewer
const main = () => {
const dom = document.getElementById('app')
viewer = new Viewer(dom)
}
document.addEventListener('DOMContentLoaded', () => {
main()
}) The code above if remove import Btw, using |
I don't think I ever posted this in the rest of the conversation, but the workaround I've been using while I wait for this issue to be resolved is to set Webpack's Speaking of which: it looks like, at the end of last year, @kring wanted to have a bit of a think about exactly what needs to be exported. For the intervening ~8 months, the field has remained -- and I don't mean to be a jerk about it! -- wrong. The Cesium-provided example of use via Webpack will not work in Webpack v5 because of this, and while I can't find documented lifecycle support commitments for v4, I wouldn't expect it to be maintained much longer. (The most recent update I can find is a bugfix that got backported in April.) So: @3DGISKing @lilleyse @kring , would you accept a PR that improves the |
So times, I usually use cesiumjs by cdn but not bundle. |
If the functionality in c137.js works for everyone’s use cases, I will see what I can do about open-sourcing the build process. Please let me know if it does what you need it to do. |
Method 1WEBPACK5 in Webpack4 subModule enhanced-resolve has no ExportsFieldPlugin.js, so guess webpack4 will not fail Method 2If you really want to use exportsFields in cesium/package.json for WEBPACK5 import from cesiumimport Color from "cesium/Source/Core/Color";
import "cesium/Source/Widgets/widgets.css"; change cesium package.json exports# package.json
{
"exports": {
"./package.json": "./package.json",
".": {
"require": "./index.cjs",
"import": "./Source/Cesium.js"
},
# add below
"./Source/": "./Source/", ## expose all below Source folder
"./Source/Core": "./Source/Core/", ## expose all below Source/Core
"./Source/Core/Color": "./Source/Core/Color.js", ## just expose Source/Core/Color.js
"./Source/Widgets/widgets.css": "./Source/Widgets/widgets.css" ## just expose widgets.css
}
} |
For anyone still struggling with this and wanting to keep Cesium installations vanilla, this was my solution. My package.json file: ...
"scripts": {
"postinstall": "node ./scripts/cesium-fix.js", This script can be added to any step as well, such as before running your dev server or prod build for safety. Fix file const fs = require('fs');
const fileName = require.resolve('cesium/package.json');
// try to load the file
try {
const jsonString = fs.readFileSync(fileName);
const file = JSON.parse(jsonString);
// add new field for proper exporting widgets.css
file.exports["./Source/Widgets/widgets.css"] = "./Source/Widgets/widgets.css";
// write the file
fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
if (err) return console.log(err);
console.log('writing to ' + fileName);
});
} catch (err) {
console.log(err);
return;
}; Then in my component file or wherever I'm loading cesium and cesium widgets: Hope this helps! |
C137.js has all this integrated, and is open source. All you have to do is:
|
Agree @prophetw When I remove Or when I add some options to {
"exports": {
"./package.json": "./package.json",
".": {
"require": "./index.cjs",
"import": "./Source/Cesium.js"
},
+ "./index.css": "./Source/Widgets/widgets.css"
}
} I can simply import styles with environment
Using vanillajs. Hope Cesium team can improve this by changing the |
Adding diff --git a/node_modules/cesium/package.json b/node_modules/cesium/package.json
index d0adea6..144b9c6 100644
--- a/node_modules/cesium/package.json
+++ b/node_modules/cesium/package.json
@@ -37,7 +37,8 @@
".": {
"require": "./index.cjs",
"import": "./Source/Cesium.js"
- }
+ },
+ "./Source/Widgets/widgets.css": "./Source/Widgets/widgets.css"
},
"type": "module",
"devDependencies": { I used the The TLDR is that |
Hey y'all, I just spent my whole day on what turned out to be an Basically, I consume Cesium classes from Typescript, using ESM-style If I run my tests with the tsconfig compiler option Fortunately, the Webpack docs included a helpful comment: imports with an absolute path, rather than a Node-style package identifier, always resolve according to the path and ignore So: if you're using CJS in-browser for any reason, the |
Reviewing this in light of recent updates (which actually made the requirements around environments bit stricter), here are some thoughts. For those bundling an app through tools such as Webpack or rollup, "exports": {
"./package.json": "./package.json",
"./Source/*": "./Source/*",
"./Build/*": "./Build/*",
".": {
"require": "./index.cjs",
"import": "./Source/Cesium.js"
}
}, Generally speaking, Given that the minimum NodeJS version officially supported is now 14, and that a bundler is now officially required when using However, loading JS files from the build directory would require a bit of know-how if attempting to use direct paths to certain JS files. For instance, when using CJS, the built If we don't want to allow that flexibility, and we want to narrow this down to just the CSS files for the sake of the original use case in this issue, this could become: "exports": {
"./package.json": "./package.json",
"./Source/*": "./Source/*",
"./Build/*.css": "./Build/*.css",
".": {
"require": "./index.cjs",
"import": "./Source/Cesium.js"
}
}, Am I missing anything? On a slightly tangential note, I think based on the node JS documentation, to avoid the runtime logic in the root ".": {
"production": {
"require": "./Build/Cesium/index.cjs",
"import": "./Source/Cesium.js"
},
"require": "./Build/CesiumUnminified/index.cjs",
"import": "./Source/Cesium.js"
} @thw0rted For your particular use case, it sounds like setting the |
First, I like the idea of user-conditions to get rid of index.cjs, but while we're at it, why have the unminified option in the first place? Does anybody consuming this lack support for sourcemaps? Would it be possible / desirable to just have one Build directory that includes minified JS plus a sourcemap for everything? Probably an issue for another ticket. Also under User Conditions, it says that you can specify {
"/Source/*": {
"require": null,
"import": "./Source/*"
}
} which would result in Second, Typescript recently made a big, somewhat controversial change to how they handle module resolution. They now always look for type definitions according to module-resolution logic. So if This means your proposal would work fine for a top level Re: ".": {
"browser": {
"require": "./Source/Cesium.js"
},
"require": "./index.cjs",
"import": "./Source/Cesium.js"
} As you say, this may have ramifications for other build tools that support CJS but don't support ESM interop the way Webpack does. I just tried it with |
Thanks for pointing that out @thw0rted. To side step the |
I guess as a first pass that makes sense, but I thought part of the point of repackaging all the source in ESM was that people can import directly from This goes back to some old posts upthread -- what kind of public API does Cesium really want to provide? Are you going to have docs with examples that |
Since all it does is export other modules, you can get the benefit of tree shaking by using import { Viewer } from "cesium"; Webpack specifically looks for the "sideEffects": ["./Source/Assets/*", "./Source/ThirdParty/*", "./Source/Widgets/*", "./Source/Workers/*"] but I think that is out of scope for this particular issue.
At the moment all of our documentation uses IIFE-style global variables ( |
I am working on https://github.com/CesiumGS/cesium-webpack-example.
This is very simple web pack configured web app.
It works well with webpack 4(original version).
But after I update web pack version, it shows following error.
Module not found: Error: Package path ./Build/Cesium/Widgets/widgets.css is not exported from package E:\MyResearch\CesiumJsGIS\node_modules\cesium (see exports fiel
d in E:\MyResearch\CesiumJsGIS\node_modules\cesium\package.json)
In a short I could not import css file from node_module.
hope you export widgets.css in package.json.
The text was updated successfully, but these errors were encountered: