0.13.0 • Published 5 years ago

install v0.13.0

Weekly downloads
273,282
License
MIT
Repository
github
Last release
5 years ago

install Build Status Greenkeeper badge

The CommonJS module syntax is one of the most widely accepted conventions in the JavaScript ecosystem. Everyone seems to agree that require and exports are a reasonable way of expressing module dependencies and interfaces, and the tools for managing modular code are getting better all the time.

Much less of a consensus has developed around the best way to deliver CommonJS modules to a web browser, where the synchronous semantics of require pose a non-trivial implementation challenge. This module loader contributes to that confusion, yet also demonstrates that an amply-featured module loader need not stretch into the hundreds or thousands of lines.

Installation

From NPM:

npm install install

From GitHub:

cd path/to/node_modules
git clone git://github.com/benjamn/install.git
cd install
npm install .

Usage

The first step is to create an install function by calling the makeInstaller method. Note that all of the options described below are optional:

var install = require("install").makeInstaller({
  // Optional list of file extensions to be appended to required module
  // identifiers if they do not exactly match an installed module.
  extensions: [".js", ".json"],

  // If defined, the options.fallback function will be called when no
  // installed module is found for a required module identifier. Often
  // options.fallback will be implemented in terms of the native Node
  // require function, which has the ability to load binary modules.
  fallback,

  // Boolean flag indicating whether the installed code will be running in
  // a web browser.
  browser,

  // List of fields to look for in package.json files to determine the
  // main entry module of the package. The first field listed here whose
  // value is a string will be used to resolve the entry module. Defaults
  // to just ["main"], or ["browser", "main"] if options.browser is true.
  mainFields: ["browser", "main"],
});

The second step is to install some modules by passing a nested tree of objects and functions to the install function:

var require = install({
  "main.js"(require, exports, module) {
    // On the client, the "assert" module should be install-ed just like
    // any other module. On the server, since "assert" is a built-in Node
    // module, it may make sense to let the options.fallback function
    // handle such requirements. Both ways work equally well.
    var assert = require("assert");

    assert.strictEqual(
      // This require function uses the same lookup rules as Node, so it
      // will find "package" in the "node_modules" directory below.
      require("package").name,
      "/node_modules/package/entry.js"
    );

    exports.name = module.id;
  },

  node_modules: {
    package: {
      // If package.json is not defined, a module called "index.js" will
      // be used as the main entry point for the package. Otherwise the
      // exports.main property will identify the entry point.
      "package.json"(require, exports, module) {
        exports.name = "package";
        exports.version = "0.1.0";
        exports.main = "entry.js";
      },

      "entry.js"(require, exports, module) {
        exports.name = module.id;
      }
    }
  }
});

Note that the install function merely installs modules without evaluating them, so the third and final step is to require any entry point modules that you wish to evaluate:

console.log(require("./main").name);
// => "/main.js"

This is the "root" require function returned by the install function. If you're using the install package in a CommonJS environment like Node, be careful that you don't overwrite the require function provided by that system.

If you need to change the behavior of the module object that each module function receives as its third parameter, the shared Module constructor is exposed as a property of the install function returned by the makeInstaller factory:

var install = makeInstaller(options);
var proto = install.Module.prototype;

// Wrap all Module.prototype.require calls with some sort of logging.
proto.require = wrapWithLogging(proto.require);

// Add a new method available to all modules via module.newMethod(...).
proto.newMethod = function () {...};

Many more examples of how to use the install package can be found in the tests.

w-imgr-public@bufang/skeleton-webpack-plugin@microsoft/teamsfx-core@nickordoodle/social_media_clients@avocad0/sdkspotl-draw-tmpreact-native-sdaf@ptit-naud/npx-card@ptit-naud/npx-card-2.0.0trs-vue-cliupgradeable-toolkitvf-playertarnet-jumbo-reactheader-demo-trialwalrus-apimining-ingestiongloballistings-test-componentsweb-socket-handler@crazvansandu/crestron-components-sasscrestron-components-sasskula-calculatorec-components-reactphonegap-plugin-push-mslobodanreact-hoc-factorytypescript-cxxcsscorelibrarybagua.jscustom-dashboard-corecomic-package@cloudie/droplets@rappopo/cukcutnut-serverlunchcatvulcan-cra-startercomac_testinkstatus-v2oscar-ui-angularreact-3d-squareeviewer-libckaix_coreckaix_core_1xr3-spoke@nrnsport/commonyjs-linkorbit-widgethighwave-rtc_sdktowify-data-enginedtrf_formapollo-server-startertq-richeditor-testtq.richeditor.modulepwa-kit-corelowcode-ui-plusmomenta-backendexpress-typescript-mongo-restful-apiooxx_uidemodel-generator-stepperds-actionsgit-files-changed-reporter@lanetix/odata-ast-transformationsti-ui-aczhanshaoweitop-t-ui@giosg/bolivar-mailernode-kinozaltv-apiteranos-0010kx-buttonpcari-designbyidaily-demos@plum-xoxo/stores-dsm@vivek.kv/testnpmjsawsmanfingerprintnpmpkginkstats-v2reactfingerprintpkgxmeerkatlibra-wallet@arrempee/gatsby-theme-mdx-collections@arrempee/gatsby-theme-mdx-pagesgo-framework@infinitebrahmanuniverse/nolb-inst@mslobodan/phonegap-plugin-pushpage-skeleton-j7@diceforge/componentsreact-native-components-textinputsmobo-component-builderunily-connect-client-dev-uploadermzinsights-backend-utilsit-board-mobilereee@everything-registry/sub-chunk-1919payment-p2eplaviui@avihang-ui-design-system/react-componentseviewer7-lib@mstechusa/eviewer7-clitehreem-calculator-project12tlsclient-shared-moduleseddie-material-dashboard
0.13.0

5 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.4

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

7 years ago

0.10.0

7 years ago

0.9.6

7 years ago

0.9.5

7 years ago

0.9.4

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.9

7 years ago

0.8.8

7 years ago

0.8.7

7 years ago

0.8.6

7 years ago

0.8.5

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.3

8 years ago

0.7.2

8 years ago

0.7.1

8 years ago

0.7.0

8 years ago

0.6.2

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.7

8 years ago

0.5.6

8 years ago

0.5.5

8 years ago

0.5.4

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.1.8

9 years ago

0.1.7

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago