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;

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

  1. Xcode6 ADD Copy Files Build Phase 是灰色的

    在学习的怎样写frameWork的时候,查看一个教程How to Create a Framework for iOS  [一个中文翻译 创建自己的framework] 其中一个步骤就是添加一个Cop ...

  2. 开发发布npm module包

    开发发布npm module包 问题 在项目开发过程中,每当进入一个新的业务项目,从零开始搭建一套前端项目结构是一件让人头疼的事情,就要重新复制一个上一个项目的前端框架和组件代码库.其中很多功能的模块 ...

  3. [Javascript] Redirect the browser using JavaScript

    Three methods to preform redirection in browser: widnow.location.href window.location.assign window. ...

  4. 【Javascript】Windows下Node.js与npm的安装与配置

      1:先下载Node.js,网站https://nodejs.org/en/,左侧为稳定版,右侧为最新版,推荐稳定版 2:Node.js安装,运行下载后的.msi文件,一路下一步就可以了,我选择的安 ...

  5. Build OpenCV text(OCR) module on windows

    Background. AOI software needs to use the OCR feature to recognize the texts on the chips. Because o ...

  6. 深入理解JavaScript系列(3):全面解析Module模式

    简介 Module模式是JavaScript编程中一个非常通用的模式,一般情况下,大家都知道基本用法,本文尝试着给大家更多该模式的高级使用方式. 首先我们来看看Module模式的基本特征: 模块化,可 ...

  7. JavaScript基础对象创建模式之模块模式(Module Pattern)(025)

    模块模式可以提供软件架构,为不断增长的代码提供组织形式.JavaScript没有提供package的语言表示,但我们可以通过模块模式来分解并组织 代码块,这些黑盒的代码块内的功能可以根据不断变化的软件 ...

  8. 自定义内建模块 - Python Build Your Own Built-In Module

    在 python 中, 用户可以通过 py 文件创建自定义的 module, 也可以通过 C 创建 dll, 扩展 python module. 当用户在一个正在编辑的模块 module 中, 引入( ...

  9. npm run dev/build/serve

    1.ERR引发的思考 npm run dev npm ERR! missing script: dev npm ERR! A complete log of this run can be found ...

随机推荐

  1. WPF Binding

    winform有binding, WPF也有binding,区别在哪呢?这里暂时不提.以前也检查接触WPF binding, 但为什么过段时间就忘记了呢? 可能主要原因自己的知识体系不够完善吧,下面我 ...

  2. [转]Delphi 关键字详解

    全文链接地址:http://www.cnblogs.com/del/archive/2008/06/23/1228562.html

  3. vs2012快捷键

    (1)自己整理的使用频率最高的快捷键(建议大家亲身体验一下!这样才会在潜移默化中运用得到!) (这里省去了很多大家闭上眼都会操作的什么Ctrl+S 等等操作 给出的大多是不常用但是很有用的快捷键组合! ...

  4. 【USACO 2.1.3】三值的排序

    [题目描述] 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排 ...

  5. js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置

    <script> //得到页面高度 var yScroll = (document.documentElement.scrollHeight >document.documentEl ...

  6. js中的 window.location、document.location、document.URL 对像的区别(转载)

    原文:http://www.cr173.com/html/18417_1.html 当我们需要对html网页进行转向的时候或是读取当前网页的时候可以用到下面三个对像: window.location. ...

  7. D题(贪心)

    D - D Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u     Descripti ...

  8. HDU 5224 Tom and paper(最小周长)

    HDU 5224 Tom and paper(最小周长) Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  9. HDU 2553 n皇后问题(回溯法)

     DFS Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  10. raspberry pi 3 截图及查看

    RASPBIAN JESSIE WITH PIXEL Image with PIXEL desktop based on Debian Jessie Version:November 2016 Rel ...