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

Add Node guide #6

Open
jaredpalmer opened this issue Jan 26, 2019 · 10 comments
Open

Add Node guide #6

jaredpalmer opened this issue Jan 26, 2019 · 10 comments
Labels
kind: feature New feature or request

Comments

@jaredpalmer
Copy link
Owner

No description provided.

@jaredpalmer jaredpalmer added the kind: feature New feature or request label Jan 31, 2019
@jaredpalmer jaredpalmer changed the title Add node guide Add Node guide Jan 31, 2019
@James-Quigley
Copy link

What kind of things would you want in the Node guide?

@jaredpalmer
Copy link
Owner Author

So this could be used to build a CLI tool (it will preserve shebangs) or even a lambda function. Probably want to just show off adding @types/node maybe?

@aleclarson
Copy link
Contributor

And you'll want "moduleResolution": "node" in your tsconfig.json

@kevinold
Copy link

@jaredpalmer I'd love to use tsdx to compile code for Lambda's, but think I'm missing something in that a yarn build doesn't include dependencies in the dist files.

I have a simple tsdx project here with 2 dependencies, but neither (aws-sdk and lodash/kebabCase) and neither are in the dist from a yarn build.

What am I missing to allow these dependencies to be in the build files so I could use this in a Lambda?

@kevinold
Copy link

Found the answer in that UMD was disabled by default -- e81f5a6

@kevinold
Copy link

Thought I found the answer, but even producing a UMD build does not bundle the dependencies. I took a look at the rollup config and feel like it should work, but not certain what I'm missing.

Any help is appreciated.

@cogell
Copy link

cogell commented Feb 25, 2020

@kevinold did you ever figure this out? just came to tsdx looking for a starter project to use for writing some lambdas

@kevinold
Copy link

@cogell I did not find a solution

@lpolito
Copy link
Contributor

lpolito commented Mar 1, 2020

So I just dug into this because I also wanted to start using this for building lambda functions. I was able to initially make simple code, but as soon as you add dependencies it gets complicated.

There's a few limitations with the current implementation that make this difficult - namely around the rollup configuration.

Currently createRollupConfig.ts does the following:

  1. Marks all imports as external if they are not relative / are absolute (seen here).
    • The intention is so that rollup doesn't bundle external dependencies. It assumes they're available in the environment.
  2. Only uses the commonjs rollup plugin if the umd format is used (seen here).
    • The commonjs plugin is needed because there's a chance your dependencies are not written in es6 and need to be converted so you can bundle them.

Both of these become an issue for this use case. Firstly we need all external dependencies bundled. Secondly a number of our bundles are probably not written in es6 and we'll need this conversion.

My work around is fairly simple, but there's potential implications I don't currently full understand:

Create a tsdx.config.js (in your project root):

const commonjs = require('@rollup/plugin-commonjs');

module.exports = {
  rollup(config) {
    // Delete the external config property.
    // This essentially means we're allowing all packages to be bundled.
    delete config.external;

    // Manually use the commonjs plugin.
    // This is opposed to specifying umd as the format as there's more implications that, again, are unclear.
    config.plugins.push(commonjs());

    return config;
  },
};

You can then call yarn build and use the resulting dist/ as your lambda. When building expect a ton of warnings around dependencies, e.g.

'zlib' is imported by node_modules/node-fetch/lib/index.mjs, but could not be resolved – treating it as an external dependency

I want to either update the docs with this in mind, or completely rethink how this is handled using the guided installation. The latter would be a lot of work, but I think the benefits would be great.

I hope I covered everything I found, as I've been fighting this for a few hours.

@agilgur5
Copy link
Collaborator

agilgur5 commented Mar 28, 2020

@lpolito I looked into the warnings, and I'm pretty sure the ones you're getting are for Node built-ins (zlib is a built-in, and that's the only one you listed). Which means they should be ignorable. If you'd like to silence them, you can configure external to do so. Basically just uses the builtin-modules package as a safelist.

RE: UMD vs. CJS, I think you should be fine using UMD as it works for Node too (it is "universal"). I'm not 100% sure how it imports built-ins, but IIRC UMD will use the require function if it exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: feature New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants