[Javascript] Add a browser build to an npm module

In this lesson, we‘re going to use webpack to create a UMD (Universal Module Definition) build of our module so users can consume it in a browser.

Install:

npm install --save-dev npm-run-all cross-env rimraf webpack

Package.json:

  "scripts": {
    "build": "npm-run-all --parallel build:*",
    "prebuild": "rimraf dist",
    "build:main": "cross-env NODE_ENV=production webpack",
    "build:umd": "cross-env NODE_ENV=umd webpack --output-filename index.umd.js",
    "build:umd.min": "cross-env NODE_ENV=umd webpack --output-filename index.umd.min.js -p"
  },

webpack.config.js:

// Modify the production path to dist folder
if (process.env.NODE_ENV === ‘production‘) {
    config.output.path = path.join( __dirname, ‘dist‘ );
    config.plugins.push( new webpack.optimize.UglifyJsPlugin( { output: { comments: false } } ) );
    config.devtool = ‘source-map‘;
}

if (process.env.NODE_ENV === ‘umd‘) {
    config.output.path = path.join( __dirname, ‘dist‘ );
    config.output.libraryTarget = ‘umd‘;
    config.output.library = ‘TtmdTable‘;
    config.devtool = ‘source-map‘;
}

After publish the module, can download the file from npmcdn.com.

_____

var webpack = require(‘webpack‘);
var path = require(‘path‘);
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require(‘html-webpack-plugin‘);
var CopyWebpackPlugin = require(‘copy-webpack-plugin‘);
var postcss = require(‘postcss-loader‘);
var autoprefixer = require(‘autoprefixer‘);
var ENV = process.env.NODE_ENV;

var config = {
    debug: true,
    devtool: ‘cheap-source-map‘,
    context: __dirname,
    output: {
        path: __dirname,
        filename: ‘angular-md-table.min.js‘,
        sourceMapFilename: ‘angular-md-table.min.js.map‘,
        publicPath: ‘./‘
    },
    entry: ‘./index.js‘,
    module: {
        loaders: [{
            test: /\.css$/,
            loader: ExtractTextPlugin.extract(‘style-loader‘, ‘css-loader!postcss-loader‘)
        }, {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract(‘style-loader‘, ‘css-loader!postcss-loader!sass-loader‘)
        }, {
            test: /\.js$/,
            loaders: [‘ng-annotate‘, ‘babel?presets[]=es2015,plugins[]=transform-runtime‘],
            exclude: /node_modules|bower_components/
        }, {
            test: /\.(woff|woff2|ttf|eot|svg|jpg|png)(\?]?.*)?$/,
            loader: ‘file-loader?name=res/[path][name].[ext]?[hash]‘
        }, {
            test: /\.html$/,
            loader: ‘html?removeEmptyAttributes=false&collapseWhitespace=false‘
        }, {
            test: /\.json$/,
            loader: ‘json‘
        }]
    },
    postcss: function() {
        return [autoprefixer];
    },
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(true),
        new webpack.DefinePlugin({
            MODE: {
                production: process.env.NODE_ENV === ‘production‘,
                dev: process.env.NODE_ENV === ‘development‘
            }
        }),
        new ExtractTextPlugin("index.min.css")
    ]
};

if (process.env.NODE_ENV === ‘production‘) {
    config.output.path = path.join( __dirname, ‘dist‘ );
    config.plugins.push( new webpack.optimize.UglifyJsPlugin( { output: { comments: false } } ) );
    config.devtool = ‘source-map‘;
}

if (process.env.NODE_ENV === ‘umd‘) {
    config.output.path = path.join( __dirname, ‘dist‘ );
    config.output.libraryTarget = ‘umd‘;
    config.output.library = ‘TtmdTable‘;
    config.devtool = ‘source-map‘;
}

module.exports = config;
时间: 2024-10-13 16:15:25

[Javascript] Add a browser build to an npm module的相关文章

javascript篇:Browser对象

WindowWindow对象表示浏览器打开的窗口.如果文档包含框架(frame或ifame标签),浏览器会为HTML文档创建一个Window对象——window,并为每个框架额外创建一个window对象.在客户端js中,window对象是全局的,所有表达式都在当前环境中计算,可以把窗口的属性作为全局变量来使用. Window对象属性 closed:只读返回窗口是否已经被关闭.当浏览器窗口关闭时,表示该窗口的对象并不会消失,它的closed将会设为true.通常可用于子窗口查询. 1 <html>

Build Android Kernel &amp;&amp; Kernel Module

I Build Android Kernel (with Module enabled) 1. Kernel source is under android_src/kernel folder 2. Config file is under kernel_src/arch/arm/configs folder xxx_deconfig 3. setup config: $make ARCH=arm CROSS_COMPILE=arm-eabi- xxx_defconfig (.config fi

开发发布npm module包

开发发布npm module包 问题 在项目开发过程中,每当进入一个新的业务项目,从零开始搭建一套前端项目结构是一件让人头疼的事情,就要重新复制一个上一个项目的前端框架和组件代码库.其中很多功能的模块组件都要重复拷贝,可以统一将这些组件类的模块统一打包上传至npm,以后每次都只需要install一下就可以了. 前期准备工作 安装nodejs github上新建一个repository用于托管组件代码 新建一个npm账户用于发布包 这里以工具组件库中的时间格式转换工具为例,主要用于对Date时间进

Xcode6 ADD Copy Files Build Phase 是灰色的,不能点问题

There's a bug with Xcode where if the Build Phases tab loses focus all the options will be grayed out. You need to do the following: Click Target Name > Build Phases > Click on whitespace below phases > Editor > Add Build Phase –  Paul Solt ?O

[Javascript] Redirect the browser using JavaScript

Three methods to preform redirection in browser: widnow.location.href window.location.assign window.location.replace 1 & 2, they are pretty much the same, just 1 is the property and 2 is the function, some people say use property is a little bit fast

[LeetCode][JavaScript]Add Digits

Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

[LeetCode][JavaScript]Add Binary

https://leetcode.com/problems/add-binary/ Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 长整型二进制加法. 1 /** 2 * @param {string} a 3 * @param {string} b 4 *

[LeetCode][JavaScript]Add Two Numbers

Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (