konnectors/libs

cozy-konnector-libs is too heavy

Closed this issue · 6 comments

Ex: we should move pdfjs-dist as a peerDependency

With Some webpack configuration it is possible to remove some unused dependencies in the connector.

I chose to deactivate it in the template to avoid weird error messages for new connector developers.

@ptbrowne Do you find it satisfying?

I would prefer to have an opt-in strategy instead of opt-out.

Otherwise the majority of the connectors will not do the webpack config and our connectors will be fat. As a connector developer, I would not mind having an error message :

"Cannot use PDF related functionality, please add pdfjs via npm/yarn". as soon as I try to use a function from cozy-konnector-libs related to PDFs.

Do you have a way to intercept failing require in nodejs? I did see https://github.com/cozy/cozy-client/pull/383/files but some test show that it is not intercepted

    try {
      require('pdfjs-dist')
    } catch (err) {
      console.log('could not find pdf-dist, please install it')
      console.log(err)
      process.exit()
    }
yarn standalone
yarn run v1.13.0
$ cozy-konnector-standalone
module.js:550
    throw err;
    ^

Error: Cannot find module 'pdfjs-dist'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load
    ...

I ran your example in a brand new file and it works with the try/catch.

$ node index.js 
could not find pdf-dist, please install it
{ Error: Cannot find module 'pdfjs-dist'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/private/tmp/tutu/index.js:2:4)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3) code: 'MODULE_NOT_FOUND' }
$ node --version
v8.15.1

Ok, but since the connectors are built in one js file, dependency inclusion or not is decided at build time. And when we try to build a connector which does not use pdf features, we get :

Module not found: Error: Can't resolve 'pdfjs-dist'

And when we try to build a connector which does not use pdf features, we get :

When the require is in a try/catch webpack does not fail at build time for unfound modules (the try/catch is detected statically).

Another strategy to reduce the size of connectors based on cozy-konnector-libs would be to ship an es6 version (using import) that would be used at build time. With import/export and the sideEffects: false setting in the package.json, webpack can optimize away modules that are not used.