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. JavaScript_object(基于map和数组练习)

    JavaScript在编程过程中,不会像java那么方便,提供大量的API让开发这直接去调用,但底层也封装大量的工具让开发者写方法去实现, 以map为例,写简单的增删改查模拟java中的map: fu ...

  2. SSM框架入门和搭建 十部曲

    又快到毕业设计的时候了,有的学弟说想用ssm做毕业设计,在网上找到资料看不懂,基础差.我就帮他写了一个demo,顺便也整理一下. SSM框架,顾名思义,就是Spring+SpringMVC+mybat ...

  3. Python django admin 替换表单控件

    测试版本: Python 2.7 Django 1.6.2 models.py from django.db import models class Article(models.Model): ti ...

  4. Extjs中grid表格中去掉红三角

    在编辑Extjs的gridpanel的时候,数据有错误或是修改在每个单元格上都会出现红色的小三角,在每个列上面可以配置allowBlank: false来标识这个不可以为空 有的时候在保存数据时如果不 ...

  5. jquery 插件模版

    ;(function ($) { //插件的默认值属性 var defaults = { Weight: '300px', height: '230px', nextId: 'nextBtn', ne ...

  6. Python按行读取文件

    1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...

  7. Ajax表单提交插件jquery form

    jQuery Form插件是一个优秀的Ajax表单插件,我们可以非常容易的使用它处理表单控件的值,清空和复位表单控件,附件上传,以及完成Ajax表单提交. jQuery Form有两个核心方法ajax ...

  8. oracle中字符串的大小比较,字符串与数字的比较和运算

    Oracle比较字符串是根据ASCII码来的,第一个字母的ASCII大小比较如果相等再比较下一个,类推.字符串和数字进行操作是会报异常的,因为类型不一样不能进行比较. Oracle在执行SQL的时候有 ...

  9. asp.net 后台使用js弹窗失效问题

    1.这些事件输出来前后都变成JS代码了,看到到这样的代码的了.会变成<script>alert('合同号XXX已存在')</script>首先后台调试一下看看Page.Clie ...

  10. 【HDOJ】1158 Employment Planning

    简单DP. #include <cstdio> #include <cstring> #include <cstdlib> #include <climits ...