Convert to esm module syntax#606
Conversation
without the package updates this still works
|
It seems like given static es6 imports, maybe the 'right' way to do the light version would be to have a separate entrypoint into the app. It'd probably be a lot to add now, but maybe in the future with some refactoring. |
|
I'm definitely up for suggestions on the light version. I had tried a few ideas I had found online, but this is the one I ended up getting to work. I was just happy to not have to get rid of that feature at the time. The light version basically takes a copy of the whole directory, and then switches some things off based on the package name. To be honest I'm more a general scripting programmer than a javascript programmer, so I am always open to tips of how i can do things better. |
|
I'd be fine with waiting on this until after the 4.0.0 release. If we can get the package updates and readme update in, we can do this one later. the main feature is maplibre and updates to all the packages. |
| const styleFileRel = styleName + '/style.json'; | ||
| const styleFile = path.resolve(styleDir, 'styles', styleFileRel); | ||
| if (fs.existsSync(styleFile)) { | ||
| config['styles'][styleName] = { |
There was a problem hiding this comment.
This could probably be JS instead of json.
config.styles[styleName] = {
style: styleFileRel,
tilejson: {
bounds,
},
};| .replace(/:/g, '_') | ||
| .replace(/\?/g, '_')] = { | ||
| "mbtiles": path.basename(mbtilesFile) | ||
| .replace(/\//g, '_') |
There was a problem hiding this comment.
This may be able to be flattened into a single replace; I realized this wasn't a change, just something for future reference..replace(/[\/:\?]+/g, '_')
| import {getTileUrls, fixTileJSONCenter} from './utils.js'; | ||
|
|
||
| module.exports = { | ||
| export const serve_data = { |
There was a problem hiding this comment.
Maybe also not for this PR, but we might consider replacing all of the snake_case exports with camelCase ones.
| // do not require `serve_rendered` in the light package | ||
| serve_rendered = require('./serve_rendered'); | ||
| } | ||
| const serve_rendered = (await import(`${!isLight ? `./serve_rendered.js` : `./serve_light.js`}`)).serve_rendered; |
There was a problem hiding this comment.
I think this is actually the optimal solution for now.
The other idea I floated was to have two entrypoints (src/main.js and src/light.js or something) and main.js would import server_rendered.js while light.js would include the stub, then they each would import src/server.js and pass serve_rendered method to it. But in its current state it'd be a lot of duplication and a fair amount of passing serve_rendered around, or a global variable or something. Neither is better than what we have here, IMO.
|
This is awesome ! Thanks for the updates :) |
Updates package to esm module syntax, updates many dependencies. Replaced requires with imports.
One note relating to 'serve_light.js'. I had a lot of trouble making this so it would switch between full and light version without require. I ended up making this dummy file to load when 'serve_rendered.js' isn't needed.
Minimum node version recommended is v14.20.0 with this. node 18 still doesn't work but that seems to be a canvas issue that I image will get an update eventually.