3.4.1 • Published 5 years ago

express-module-serv v3.4.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

express-module-serv

An express app booting module that sets up a frontend modular system service which supports AMD, CMD and UMD standards.

Install

$ npm install --save express-module-serv

Usage

Pass in express app instance and options to set up the service.

var express = require('express');
var moduleServ = require('express-module-serv');
var app = express();
var options = {
  routePath: '/m', //default
  loaderPath: '/mloader.js', //default
  // ex. [".css", ".txt" ...], when module path's extension is not one of these 
  // then use `defaultExtensions` to resolve its path
  acceptExtensions: [], // default
  defaultExtensions: ['js'], //default
  pathSettings: {
    // requried
    baseUrl: __dirname + '/scripts',
    // optional prefix path settings
    paths: {
      css: __dirname + '/styles' or '../styles' relative to `base`
    }
  },
  debug: true, //default is false, skip minifying loader script

  //optional, set globals on window
  globals: {
    version: '1.0'
  },

  cacheControlExpiration: 10800, //default 0, set duration for expiration in seconds

  //default is true, set false in case you don't need rebuild when src file is updated, for example in production environment.
  reloadOnChange: true
};

//customize transformers if you need support loading css or svg modules

options.transformers = [
  //support loading css files, requiring the similar settings as the static middleware needs
  //for resource url correction
  require('express-module-serv/transformers/css-wrapper')({
    staticPath: __dirname + '/public',
    routePath: '/'
  }),
  //support CommonJS standard
  require('express-module-serv/transformers/cmd-wrapper')(),
  //add comma and new line
  require('express-module-serv/transformers/add-comma')()
];

moduleServ(app, options);
//suppose the index.html is in "public" directory
app.use(express.static(__dirname + '/public'));
app.listen(3003);

Then in html, you can import the loader script like this,

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Express module serv Test Page</title>
    <script src="/mloader.js" data-main="app" async></script>
  </head>
  <body>
  </body>
  </html>

app stands for a entry module name, and that module is located in the base(__dirname + '/scripts') directory. This module will be executed after mloader.js is loaded.

Both AMD or CommonJS module format are supported. For example, a js file with a filename "add.js" and located in __dirname + '/scripts/util' could be written like,

//AMD module, must pass in a valid module name according to its filepath
define('util/add', function(){
  return function add(a, b){
    return a+b;
  };
});

//or CommonJS
module.exports = function add(a, b){
  return a + b;
};

Dependency identifier can be absolute or relative to the dependent module, and will be resolved referring to pathSettings.

The loader script in the page exposes these global methods:

define

(id, deps, factory) -> module

for define a amd module

requireAsync

(deps) -> Promise

require dependencies asynchronously

3.4.2

5 years ago

3.4.1

5 years ago

3.4.0

5 years ago

3.3.1

6 years ago

3.3.0

6 years ago

2.9.1

6 years ago

3.2.2

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.9.0

7 years ago

2.8.5

8 years ago

2.8.4

8 years ago

2.8.3

8 years ago

2.8.2

8 years ago

2.8.1

8 years ago

2.8.0

8 years ago

2.7.4

8 years ago

2.7.3

8 years ago

2.7.2

8 years ago

2.7.1

8 years ago

2.7.0

8 years ago

2.6.2

8 years ago

2.6.1

8 years ago

1.8.6

8 years ago

1.8.5

8 years ago

1.8.4

8 years ago

1.8.3

8 years ago

1.8.2

8 years ago

1.8.1

8 years ago

1.8.0

8 years ago

1.7.2

8 years ago

1.7.1

8 years ago

1.7.0

8 years ago

1.6.6

8 years ago

1.6.5

8 years ago

1.6.4

8 years ago

1.6.3

8 years ago

1.6.2

8 years ago

1.6.1

8 years ago

1.6.0

8 years ago

2.5.5

8 years ago

1.5.7

8 years ago

2.5.4

8 years ago

1.5.6

8 years ago

2.5.3

8 years ago

1.5.5

8 years ago

2.5.2

8 years ago

1.5.4

8 years ago

1.5.3

8 years ago

1.5.2

8 years ago

2.5.1

8 years ago

1.5.1

8 years ago

2.5.0

8 years ago

1.5.0

8 years ago

2.4.1

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

2.4.0

8 years ago

2.3.4

8 years ago

1.3.9

8 years ago

1.3.8

8 years ago

1.3.7

8 years ago

1.3.6

8 years ago

2.3.3

8 years ago

1.3.5

8 years ago

1.3.4

8 years ago

1.3.3

8 years ago

1.3.2

8 years ago

2.3.2

8 years ago

2.3.1

8 years ago

1.3.1

8 years ago

2.3.0

8 years ago

1.3.0

8 years ago

2.2.2

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

2.2.1

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

2.2.0

8 years ago

1.2.0

8 years ago

2.1.1

8 years ago

1.1.1

8 years ago

2.1.0

8 years ago

1.1.0

8 years ago

1.0.6

8 years ago

2.0.0

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago