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

feat: convert source to pure ESM; remove dependency on parcel v1; fix broken Pool test #242

Closed
wants to merge 11 commits into from

Conversation

manzt
Copy link
Contributor

@manzt manzt commented Sep 24, 2021

The geotiff.js source is written nearly as a pure Node ESM. This means that without any build step, it can run natively in a nodejs.


Use full paths for imports

The primary change is to use full file paths for imports import x from '.'; → import x from './index.js', which in turn means that the module can be used via node >12:

import { fromUrl } from 'geotiff';

Additionally an implication of this change means that mocha can run without @babel/register for transpiling the source to commonjs. This means that the test for Pool has been fixed.

Replace parcel v1 with Vite

Parcel v1 has been deprecated and Vite is a good modern fit for the development of geotiff.js. Starting up the development environment happens instantly with:

vite # open localhost:3000

In addition, I have configured vite build to generate the commonjs build target for geotiff (dist-node/). However, in contrast to parcel, I have chosen to avoid bundling the external dependencies since the CJS output is intended to be run in a node environment using require.

Bundle the UMD output with rollup

Vite had some issues bundling the UMD export (can't deal with dynamic imports), so a very minimal rollup config is used to preserve the dist-browser target. The Pool still does not work for this export.

Add conditional exports to package.json

  "exports": {
    ".": {
      "import": "./src/geotiff.mjs",
      "require": "./dist-node/geotiff.js"
    }
  },

These exports are conditionally understood in nodejs and will allow goetiff to be imported or required depending on the runtime environment. So both of these will work natively in Nodejs.

import * as GeoTIFF from 'geotiff'; // imports from './node_modules/geotiff/src/geotiff.mjs
const GeoTIFF = require('geotiff'); // ./node_modules/geotiff/dist-node/geotiff.js

Note that the txml/txml import causing issues in #241 runs natively in ESM.

@manzt
Copy link
Contributor Author

manzt commented Sep 24, 2021

I know the PR looks massive, but most of this is due to changing dev dependencies in the package-lock.json. Here is a nicer summary of the diff:

geotiff.js on  manzt/vite [$!] is 📦 v1.0.6 via  v16.9.1
❯ git diff --stat master . ':(exclude)package-lock.json'


 test/index.html => index.html                       |  4 ++--
 package.json                                        | 41 ++++++++++++++++++++---------------------
 rollup.config.js                                    | 19 +++++++++++++++++++
 src/compression/{basedecoder.js => basedecoder.mjs} |  2 +-
 src/compression/{deflate.js => deflate.mjs}         |  2 +-
 src/compression/{index.js => index.mjs}             | 12 ++++++------
 src/compression/{jpeg.js => jpeg.mjs}               |  2 +-
 src/compression/{lerc.js => lerc.mjs}               |  4 ++--
 src/compression/{lzw.js => lzw.mjs}                 |  2 +-
 src/compression/{packbits.js => packbits.mjs}       |  2 +-
 src/compression/{raw.js => raw.mjs}                 |  2 +-
 src/{dataslice.js => dataslice.mjs}                 |  0
 src/{dataview64.js => dataview64.mjs}               |  0
 src/{decoder.worker.js => decoder.worker.mjs}       |  2 +-
 src/{geotiff.js => geotiff.mjs}                     | 32 ++++++++++++++++----------------
 src/{geotiffimage.js => geotiffimage.mjs}           |  8 ++++----
 src/{geotiffwriter.js => geotiffwriter.mjs}         |  4 ++--
 src/{globals.js => globals.mjs}                     |  0
 src/{logging.js => logging.mjs}                     |  0
 src/{pool.js => pool.mjs}                           |  2 +-
 src/{predictor.js => predictor.mjs}                 |  0
 src/{resample.js => resample.mjs}                   |  0
 src/{rgb.js => rgb.mjs}                             |  0
 src/source/{arraybuffer.js => arraybuffer.mjs}      |  4 ++--
 src/source/{basesource.js => basesource.mjs}        |  0
 src/source/{blockedsource.js => blockedsource.mjs}  |  4 ++--
 src/source/client/{base.js => base.mjs}             |  0
 src/source/client/{fetch.js => fetch.mjs}           |  2 +-
 src/source/client/{http.js => http.mjs}             |  4 ++--
 src/source/client/{xhr.js => xhr.mjs}               |  4 ++--
 src/source/{file.js => file.mjs}                    |  2 +-
 src/source/{filereader.js => filereader.mjs}        |  2 +-
 src/source/{httputils.js => httputils.mjs}          |  0
 src/source/{index.js => index.mjs}                  |  0
 src/source/{remote.js => remote.mjs}                | 12 ++++++------
 src/{utils.js => utils.mjs}                         |  0
 test/.dockerignore                                  |  1 -
 test/Dockerfile                                     |  3 ---
 test/dev.js                                         | 20 +++++++++++---------
 test/{geotiff.spec.js => geotiff.spec.mjs}          | 31 +++++++++++++++++--------------
 test/nginx.conf                                     | 87 ---------------------------------------------------------------------------------------
 vite.config.js                                      | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 42 files changed, 176 insertions(+), 192 deletions(-)

@tschaub
Copy link
Contributor

tschaub commented Oct 6, 2021

Since I created most of the conflicts in #250, I thought I'd take a shot at resolving them. Here is a rebased branch with conflicts fixed: https://github.com/tschaub/geotiff.js/tree/vite

With the head of that branch, the tests pass, but it looks like the test runner never exits. I haven't tried debugging this yet.

I also found that npm run build fails with this:

> vite build --mode umd

vite v2.6.3 building for umd...
✓ 38 modules transformed.
✓ 82 modules transformed.
[vite:worker] Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
file: /Users/tschaub/projects/geotiff.js/src/decoder.worker.js?worker
error during build:
Error: Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
    at error (/Users/tschaub/projects/geotiff.js/node_modules/rollup/dist/shared/rollup.js:158:30)
    at validateOptionsForMultiChunkOutput (/Users/tschaub/projects/geotiff.js/node_modules/rollup/dist/shared/rollup.js:16066:16)
    at Bundle.generate (/Users/tschaub/projects/geotiff.js/node_modules/rollup/dist/shared/rollup.js:15905:17)
    at async handleGenerateWrite (/Users/tschaub/projects/geotiff.js/node_modules/rollup/dist/shared/rollup.js:23400:23)
    at async Object.transform (/Users/tschaub/projects/geotiff.js/node_modules/vite/dist/node/chunks/dep-f5e099f1.js:67696:40)
    at async ModuleLoader.addModuleSource (/Users/tschaub/projects/geotiff.js/node_modules/rollup/dist/shared/rollup.js:22105:30)
    at async ModuleLoader.fetchModule (/Users/tschaub/projects/geotiff.js/node_modules/rollup/dist/shared/rollup.js:22158:9)
    at async Promise.all (index 1)
    at async ModuleLoader.fetchStaticDependencies (/Users/tschaub/projects/geotiff.js/node_modules/rollup/dist/shared/rollup.js:22193:34)
    at async Promise.all (index 0)

Perhaps related. I also haven't dug into this yet.

Apologies for the conflicts. Feel free to cherry pick or otherwise make use of any of those commits if that simplifies things.

@tschaub
Copy link
Contributor

tschaub commented Oct 6, 2021

It is likely that after #226, the rollup output config will need inlineDynamicImports for any iife bundles.

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 this pull request may close these issues.

2 participants