Giter VIP home page Giter VIP logo

meteor-live-modules's Introduction

meteor/itgenio:live-modules

Deploy any code to client/server runtime in any time.

Installation

  1. Add to your meteor project:
meteor add itgenio:live-modules
  1. (server side) Register modules:
  • by inserting documents into LiveModulesCollection;
  • by using helper class LiveModulesServiceDefault.
  1. (client/server side) Use LiveModules.importModule(...) or LiveModules.importModules(..) anywhere to import a code.

See an example

Features

  • Enable/disable modules
  • Import modules by specific tags or name
  • importTimeout to control imports time
  • Works with JS & CSS
  • Server and Client

Configuration via Meteor.settings

{
  "public": {
    "liveModules": {
      "collName": "meteor_itgenio_live-modules",
      "subName": "itgenio.live-modules",
      "logging": true,
      "importTimeout": 5000,
      "subOnStartup": true,
      "jsMarker": "/*js-content*/"
    }
  }
}

Example

  1. Add the package:
meteor add itgenio:live-modules
  1. Register live module (server side):

server/index.js:

import { LiveModulesConfig, LiveModulesCollection } from 'meteor/itgenio:live-modules';

LiveModulesCollection.remove({}); //clean all modules

LiveModulesCollection.insert({
  name: 'test-module1',
  v: 0,
  tags: ['startup'],
  enabled: true,
  required: false,
  source: `${LiveModulesConfig.jsMarker} console.log('Hello from test-module1');`,
});

LiveModulesCollection.insert({
  name: 'test-module2',
  v: 0,
  tags: ['startup'],
  enabled: true,
  required: false,
  source: `${LiveModulesConfig.jsMarker} console.log('Hello from test-module2');`,
});
  1. Import live module (client side):

client/index.js:

import { LiveModules } from 'meteor/itgenio:live-modules';

(async () => {
  await LiveModules.importModule('test-module1'); //by name
})();
  1. Done! Check your console in the browser for a line Hello from test-module1

Use cases

Import by tag on startup

import { LiveModules } from 'meteor/itgenio:live-modules';

Meteor.startup(async () => {
  //...

  //
  // Import all modules with tag `startup`
  //
  try {
    await LiveModules.importModules(['startup']);
  } catch (e) {
    console.error(e);
  }
  //...
});

Import by name in specific page

import { LiveModules } from 'meteor/itgenio:live-modules';

const lazy = () => LiveModules.importModule('my-lazy-component');

//suspense is HOC for <Suspense/> from react
const SubComponent = suspense(() => {
  //Some magic...
  //But you just waiting for resolving promise
  const { LazyComponent } = useService(lazy);

  return LazyComponent;
});

export function MyPage() {
  return <SubComponent />;
}

Evaluation

evaluate as JS

If the url ends with .js/.cjs or the source field starts with LiveModulesConfig.jsMarker (by default, /*js-content*/).

It's not safe, so be careful to use source field.

evaluate as CSS

If the url ends with .css or the source field doesn't start with LiveModulesConfig.jsMarker.

Only works on the Client side.

Types

See src/types.ts

Links

Tests

npm run test

meteor-live-modules's People

Contributors

afrokick avatar

Stargazers

Ivan avatar  avatar  avatar Jordan Brant Baker avatar  avatar  avatar

meteor-live-modules's Issues

[Feature] Load modules on the server side

A server runtime also uses meteorInstall for build a modules' graph, so, it should be not so hard to support LiveModules on the server.

Some tasks:

  • Add a field archs: string[]. Valid values: client, server
  • How to listen for changes from DB on server side? Fetch?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.