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

Transform dynamic imports to static imports #59

Open
jankapunkt opened this issue Sep 24, 2019 · 24 comments
Open

Transform dynamic imports to static imports #59

jankapunkt opened this issue Sep 24, 2019 · 24 comments

Comments

@jankapunkt
Copy link
Contributor

The dynamic-import package does not work with a client-only app, so we need to bundle all imports while building statically.

A similar approach is done by bundle-visualizer. I could provide a PR if there is some alive-ping.

@dr-dimitru
Copy link
Collaborator

Hello @jankapunkt ,

Don't think it requires an additional PR to the codebase.
I'd accept PR to the docs with note explaining that if you're building static client bundle, — do not use dynamic imports!. I mean when we build application and we plan to use it as static Client bundled app — we know that we can not use dynamic imports.

And this package pending general update, hope to put my hands on it soon.

Open for further discussion in case if I'm wrong

@jankapunkt
Copy link
Contributor Author

Thank you for the fast response @dr-dimitru

I am using this package to build a demo application for one of my client-side packages that runs on github.io but the problem is, that the package uses dynamic imports to reduce initial bundle size.

I tried to introduce static imports that are only included when I set a flag in Meteor.settings but then the build of the package always seems to use static imports.

I will try to figure out how to code-split a dynamic and a static version of the same package and will update some of the README here when I found (or not found) a solution.

dr-dimitru added a commit that referenced this issue May 28, 2020
- ES6;
- Overall codebase refactoring and linting;
- Remove spinner, `simple-spinner` dependency, and `-hide,
--hideSpinner` command line options;
- Support for modern (ES6) and legacy (ES5) builds (legacy build is
used by default);
- Support for Meteor's `<meteor-bundled-js/>` and
`<meteor-bundled-css/>`;
- Support for `body.html` and `blaze-html-templates` (see updated
readme);
- Support for `{{url-to-meteor-bundled-css}}` placeholder, which can be
used in `<link rel="preload">`;
- `--usebuild` option now accepts `<path>` to the build;
- Update NPM dependencies;
- Closing: #53, #40;
- Solving: #61, #57, #55, #42;
- Taking into account: #59, #35;
@dr-dimitru dr-dimitru mentioned this issue May 28, 2020
Merged
@dr-dimitru
Copy link
Collaborator

@jankapunkt do you think it would possible to pull all dynamic imports as static files, and rewrite import() function to send request to a static file using file map?

Can't find how meteor generates assets for static import 🤔

@jankapunkt
Copy link
Contributor Author

I haven't figured it out, too. However, we may have a Chance to override the dynamic import to use static require under the hood, since it is not the native esmodule implementation.

dr-dimitru added a commit that referenced this issue Aug 11, 2020
## v1.0

- Update codebase to ES6;
- Overall codebase refactoring and linting;
- __Breaking__: Remove spinner, `simple-spinner` dependency, and `-hide, --hideSpinner` command line options;
- Support for modern (ES6) and legacy (ES5) builds (legacy build is used by default);
- Support for Meteor's `<meteor-bundled-js/>` and `<meteor-bundled-css/>`;
- Support for `body.html` and `blaze-html-templates` (see updated readme);
- Support for `{{url-to-meteor-bundled-css}}` placeholder, which can be used in `<link rel="preload">`;
- __Breaking__: `--usebuild` option now accepts `<path>` to the build;
- Update NPM dependencies;
- Use `fs-extra` module to move files around;
- Check that `Meteor` and `Meteor.disconnect` exist in global space;
- If `--url` option is not passed create dummy `sockjs/info` to avoid connect DDP exceptions;
- Closing: #53, #40;
- Solving: #61, #57, #55, #42, #18;
- Taking into account: #59, #35;
@dr-dimitru
Copy link
Collaborator

Have an idea for case when building Client only for modern browsers link script with type="module" attribute, in that case dynamic modules would be pulled from Server.

To this moment all my tests were throwing errors... and yes, it requires Server, while this package focus on Client only solution.

I hiope this comment would inspire someone for a solution or spark discussion.

@technicalbirdVayuz
Copy link

technicalbirdVayuz commented Jun 21, 2022

Hi @dr-dimitru ,

Just wanted to ask, will this package support Dynamic Imports? If yes, then can you share by when we will be able to use it in our projects?

@dr-dimitru
Copy link
Collaborator

Hello @technicalbirdVayuz ,

  1. You can use dynamic imports as long as there a server to serve the code
  2. Implementing this one would require some time/funds investment, unless @jankapunkt got some spare time

@technicalbirdVayuz
Copy link

Can you please share an example of this?

  • You can use dynamic imports as long as there a server to serve the code

@dr-dimitru
Copy link
Collaborator

@technicalbirdVayuz
Copy link

How does it relate to Dynamic imports? Can you please explain

@dr-dimitru
Copy link
Collaborator

@technicalbirdVayuz dynamic import(/*....*/) imports code dynamically on demand from a server via DDP

@technicalbirdVayuz
Copy link

Hi,

@dr-dimitru Can you share a small sample please? Not sure, where to put code for this

@dr-dimitru
Copy link
Collaborator

@technicalbirdVayuz it's just an example. No actual changes in your codebase required.

By default your app will connect to a server address passed into --url CLI option.

To test this locally:

  1. Build client app passing local server URL
  2. Run local meteor server with --production flag
  3. Open index.html generated by meteor-build-client

To be quite honest — I'm not sure about if no.3 going to work without locally running nginx, this is my method for testing. Let me know if that worked for you

@technicalbirdVayuz
Copy link

I tried the and steps, it worked.

First of all, I have not used DDP connection as mentioned here but without this its working fine [Instead if I add that code, then I got DDP is not defined]

there is one error also, noe from where It is coming from

Please check:
image

Also, My idea of using this packageas as an micro Service but using it this way will make it highly dependent on the server, would be great if we can use it as a stand-alone build.

@technicalbirdVayuz
Copy link

technicalbirdVayuz commented Jun 22, 2022

& Also, If we have to run this way then why should we create a client build. In any case, We need to run the server & server ideally has the client code too.

Please correct me if I am wrong @dr-dimitru

@dr-dimitru
Copy link
Collaborator

if we can use it as a stand-alone build.

This isn't possible due to the way dynamic imports implemented. At the moment it serves compiled pieces of code in batches meaning multiple packages and files are served in the single request.

If there will be a way to request packages one-by-one, then we can place all code-pieces as static files under public directory.

Simply replacing dynamic imports with static imports during the building phase won't work either as "dynamic" pieces of code won't be a part of "bundle"

@dr-dimitru
Copy link
Collaborator

& Also, If we have to run this way then why should we create a client build. In any case, We need to run the server & server ideally has the client code too.

You can place client build into CDN, or have many servers serving static files and one handling server-part

@dr-dimitru
Copy link
Collaborator

there is one error also, noe from where It is coming from

Please check:

This means server return HTML instead of JSON, I'd start with inspecting "Network" tab

@technicalbirdVayuz
Copy link

Thanks for the quick response.

Just want to ask one more thing, will it be considered as a microservice if we use it this way as it is not loosely coupled now.

@dr-dimitru
Copy link
Collaborator

will it be considered as a microservice if we use it this way as it is not loosely coupled now.

Nope.

Microservice usually focused on performing single task, but doing it very well in the most efficient way. Nowadays this term has broad meaning, so some folks can call it microservice.

I'd just call it backend and static servers

@technicalbirdVayuz
Copy link

Ok. Do you have any references where we can use Meteor as a Micro Service?

@dr-dimitru
Copy link
Collaborator

@technicalbirdVayuz Meteor is great for:

  • Building front-end (compiling files and libs)
  • Transferring data between client and server (including reactively)

On the server it's just Node.js, so I'd suggest using pure "vanilla" Node.js to build microservice

@jankapunkt
Copy link
Contributor Author

@dr-dimitru a little bit off-topic but:

On the server it's just Node.js, so I'd suggest using pure "vanilla" Node.js to build microservice

I agree hre, unless you want some of the Meteor Magic, like Mongo Integration, DDP (to connect with other Meteor apps) or Accounts, then I suggest to watch my presentation on Microservices with Meteor

@dr-dimitru
Copy link
Collaborator

dr-dimitru commented Jun 24, 2022

@jankapunkt great mention. It seems to me that it only applicable if you're building ecosystem/infrastructure on top of Meteor — then it makes sense to keep micro services on top of Meteor.

In our case we always end up building vanilla Node.js micro services for efficiency and smallest footprint possible. Data between micro services usually exchanged via Database (MongoDB in our case).

Good references can be found at microservices.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants