npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-preset-p2m-library

v1.1.0

Published

A babel preset used by Shanghai PTM company to build shared library.

Downloads

10

Readme

babel-preset-p2m-library

A babel preset used by Shanghai PTM company to build shared library.

Usage

Include this preset into .babelrc as following:

{
  "presets": [
    ["p2m-library",
       {
         "browser": false, // generator library for browser, otherwise, it is for node.js
         "moduleId": "your/module/id", 
         "react": false, // support react jsx syntax
         "globals": {
           "jquery": "$"
         }
       }
    ]]
}

Features

Support ES version

This preset include ES2015, ES2016 and ES2017 presets.

Support Flow syntax

This preset include babel-preset-flow preset. So you can use all flow syntex in your js file.

Build environment

This preset use babel-plugin-dotenv to transform pre-define variable on build time. When you use this function, please ensure that you had add dotenv as dependency of your project.

You can add .env file in root folder of your project as following:

# .build.env
API_HOST=http://localhost:8080

then, you can import API_HOST from dotenv as following:

import {API_HOST} from 'dotenv';

console.log(API_HOST);

Variable API_HOST will be replaced when build this project. the output could be like follow.

console.log('http://localhost:8080');

In addition, this plugin support multiple env file for different environment. By default, it load .env.development file if it existed. If you set NODE_ENV or BABEL_ENV to production this plugin will load .env.production file.

Production version

This preset use BABEL_ENV or NODE_ENV to determinate whether production version or not. by default, it is development version, All js files are not minified in this case. If you change the environment to production, for browser style output, the babel engine will minified all js files. But for node.js style output, they still are unminified.

In addition, you can use .env.production to do more control in code.

Browser support

If you turn on browser option, this preset will build js file as an UMD module. In this case you must specified moduleId option. This option should be a / separated string. For example:

["p2m-library", {
  "browser": true,
  "moduleId": "p2m/message/client"
}]

Thus, the following javascript file

export default 'abc';

will be compiled as fillowing:

(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define("p2m/message/client", ["exports"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports);
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports);
    global.p2m = global.p2m || {};
    global.p2m.message = global.p2m.message || {};
    global.p2m.message.client = mod.exports;
  }
})(this, function (exports) {
  "use strict";

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = "abc";
});

If you use some external modules, you can use globals option to specified global variable for these libraries. For example, if you want use jquery. You can add following options.

["p2m-library", {
  "browser": true,
  "moduleId": "p2m/message/client",
  "globals": {
    "jquery": "$"
  }
}]

Result is just like following:

source:

const jq = require('jquery');
export default jq('div');

output:

(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define('p2m/message/client', ['exports', 'jquery'], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require('jquery'));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.$);
    global.p2m = global.p2m || {};
    global.p2m.message = global.p2m.message || {};
    global.p2m.message.client = mod.exports;
  }
})(this, function (exports, jq) {
  'use strict';

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = jq('div');
});