commenthol / astronomia

An astronomical library
MIT License
121 stars 23 forks source link

Bundle library and VSOP87 data as UMD module #6

Closed raphaelsaunier closed 6 years ago

raphaelsaunier commented 6 years ago

First off, hats off for putting in the time and effort to create this clean port of meeus! :clap:

Lately I've been playing a bit with Observable but I've had some trouble importing Astronomia and the VSOP87 dataset.

While there is a guide on how to require “stubborn modules”, none of the techniques described quite work. I was able to load the main library through wzrd.in, but I've had to bundle and host the VSOP87 files separately on a private server to be able to import them in an Observable notebook.

Given that you’re already shipping the transpiled code in lib/, would you consider adding an extra build step with either Rollup or Webpack to bundle those files (as well as the files in data/) as UMD modules? I'm happy to look into this and submit a PR if you'd like.

commenthol commented 6 years ago

Hi @raphaelsaunier, thanks that you like the lib. To be honest I don't like the idea of providing a big umd data bundle inside the package as IMHO umd is not the way to go. I have created a small sample using browserify which should give you an idea of how you are able to create your own umd bundle with only the things that you require for your code. Pls. check out https://github.com/commenthol/astronomia-bundle-example especially look at the lines

var planetposition = require('astronomia/lib/planetposition')
var venusData = require('astronomia/data/vsop87Bvenus')

Happy to help...

commenthol commented 6 years ago

In fact you are right, it would be good to offer the VSOP87 data sets as exports. I will update the lib such that

const {data} = require('astronomia')

should offer the missing exports.

raphaelsaunier commented 6 years ago

Sure, it's your call! I merely suggested UMD because it would allow users to conveniently load astronomia through unpkg on Observable, just like other libs such as D3, Three.js, or React. But wzrd.in should work too once the dataset is exported in the CommonJS module as well.

Thanks for taking the time to prepare a bundle example. In fact, if you take a look at this notebook I was already able to get it working by using a combination of wzrd.in and a custom UMD bundle of the complete data set created with Webpack.

commenthol commented 6 years ago

I have looked into the UMD bundling using rollup. My final minified file still exceed 1.5 MByte which I consider as not acceptable for bundling with npm. I suppose most people will never use the full lib but only parts of it. So I hope you are not too upset that I won't bundle this into an npm package nor provide in github. If really required, everyone can easily create such with:

npm i -g browserify
browserify -e lib/index.js -s astronomia -o bundle.js
raphaelsaunier commented 6 years ago

No worries! In fact, your changes introduced in 6148dee1e351a2d034e6f53f55f5cdfc8ec3b9c6 do solve the problem; wzrd.in now bundles the data as well. :) https://wzrd.in/standalone/astronomia@latest

sebagr commented 3 years ago

Hi @raphaelsaunier, thanks that you like the lib. To be honest I don't like the idea of providing a big umd data bundle inside the package as IMHO umd is not the way to go. I have created a small sample using browserify which should give you an idea of how you are able to create your own umd bundle with only the things that you require for your code. Pls. check out https://github.com/commenthol/astronomia-bundle-example especially look at the lines

var planetposition = require('astronomia/lib/planetposition')
var venusData = require('astronomia/data/vsop87Bvenus')

Happy to help...

Hi! I came here searching how to browserify the latest astronomia version. I thought this would work but unfortunately with astronomia 4.0 and above I'm getting this when trying to browserify it:

ParseError: 'import' and 'export' may appear only with 'sourceType: module'

Any idea how to fix it? I tried using the --global flag for browserify (to convert all node_modules) but I end up with a bundle of several MB, which is quite a bit for browsers. Any help would be appreciated :)

commenthol commented 3 years ago

Hi,

browserify -e lib/index.cjs -s astronomia -o bundle.js

still works and gives me a ~430kB bundle.

I am unaware of your usecase but if you try to run the lib in the browser and support of ie11 is not a requirement it would be more convenient to use the esm modules directly.

Assuming that astronomia/src is available in your public path with <script type="module" src="index.js"></script> you could do

import { K, AU } from '/astronomia/src/base.js'
console.log(K, AU)

With this you would not need any bundler at all.