[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;
[Javascript] Add a browser build to an npm module的更多相关文章
- Xcode6 ADD Copy Files Build Phase 是灰色的
在学习的怎样写frameWork的时候,查看一个教程How to Create a Framework for iOS [一个中文翻译 创建自己的framework] 其中一个步骤就是添加一个Cop ...
- 开发发布npm module包
开发发布npm module包 问题 在项目开发过程中,每当进入一个新的业务项目,从零开始搭建一套前端项目结构是一件让人头疼的事情,就要重新复制一个上一个项目的前端框架和组件代码库.其中很多功能的模块 ...
- [Javascript] Redirect the browser using JavaScript
Three methods to preform redirection in browser: widnow.location.href window.location.assign window. ...
- 【Javascript】Windows下Node.js与npm的安装与配置
1:先下载Node.js,网站https://nodejs.org/en/,左侧为稳定版,右侧为最新版,推荐稳定版 2:Node.js安装,运行下载后的.msi文件,一路下一步就可以了,我选择的安 ...
- 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 ...
- 深入理解JavaScript系列(3):全面解析Module模式
简介 Module模式是JavaScript编程中一个非常通用的模式,一般情况下,大家都知道基本用法,本文尝试着给大家更多该模式的高级使用方式. 首先我们来看看Module模式的基本特征: 模块化,可 ...
- JavaScript基础对象创建模式之模块模式(Module Pattern)(025)
模块模式可以提供软件架构,为不断增长的代码提供组织形式.JavaScript没有提供package的语言表示,但我们可以通过模块模式来分解并组织 代码块,这些黑盒的代码块内的功能可以根据不断变化的软件 ...
- 自定义内建模块 - Python Build Your Own Built-In Module
在 python 中, 用户可以通过 py 文件创建自定义的 module, 也可以通过 C 创建 dll, 扩展 python module. 当用户在一个正在编辑的模块 module 中, 引入( ...
- 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 ...
随机推荐
- Linux下登陆mysql服务器不需要输入账号密码信息
linux下登录mysql服务器一般都是在命令行手动输入链接信息 [root@localhost ~]# mysql -hlocalhost -uroot -p11111 而在mysql 5.6之后版 ...
- Best Time to Buy and Sell sock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- underscorejs-reduceRight学习
2.4 reduceRight 2.4.1 语法: _.reduceRight(list, iteratee, memo, [context]) 2.3.2 说明: reduceRight和reduc ...
- spring获取properties
实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...
- .NET MVC执行过程
1.网址路由比对 2.执行Controller与Action 3.执行View并返回结果 在使用MVC中是由IgnoreRoute()辅助方法对比成功的,会导致程序直接跳离MVC的执行生命周期,将程序 ...
- java正则表达式一:基本使用
java正则表达式主要涉及三个类:java.util.regex.Matcher.java.util.regex.Pattern.java.util.regex.PatternSyntaxExcept ...
- 调用API函数,在窗口非客户区绘图(通过GetWindowDC获得整个窗口的DC,就可以随意作画了)
http://hi.baidu.com/3582077/item/77d3c1ff60f9fa5ec9f33754 调用API函数,在窗口非客户区绘图 GDI+的Graphics类里有个FromHdc ...
- Delphi新语法
http://www.cnblogs.com/hnxxcxg/category/456344.html
- VMware 11安装Mac OS X 10.10
http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html
- BZOJ 1003 [ZJOI2006]物流运输trans
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4242 Solved: 1765[Submit] ...