[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 ...
随机推荐
- Objective-C学习篇05—Foundation框架简介
iOS中所谓的框架,说到底就是一个目录,iOS提供了很多我们可以在应用程序中调用的框架.许多应用程序都使用了如Foundation.UIKit和Core Graphics这些框架.根据你为应用程序选择 ...
- sql批量插入数据之存储过程
-- ============================================= -- Author: jf_ou -- Create date: 2016/03/22 -- Desc ...
- 1 Two Sum(找和为target的两个数字下标Medium)
题目意思:给一个数组,找到和为target的两个元素的序号,并且只有一组这样的元素 思路:map<int,int>(nums[i],i+1),然后从后往前循环,用count找,比较i+1 ...
- Extjs之combobox联动
Ext.Loader.setConfig({ enabled : true }); Ext.Loader.setPath('Ext.ux', '../extjs/ux'); Ext.require([ ...
- @font-face扒站的步骤
今天模仿百度首页手机版的时候遇到的@font-face的问题,现在整理一下. 问题:图中红色区域,在拷贝F12样式的时候,并没有出现这些小图标. 图1:百度的效果 ...
- HTTP协议学习-02
HTTP 协议详解之 URL 的组成 http(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于 TCP 的连接方式,HTTP1.1 版本中给出一种持续连接的机制,绝大多数的 ...
- Delphi-UpperCase 函数
函数名称 UpperCase 所在单元 System.SysUtils 函数原型 function UpperCase(const S: string): string; 函数功能 将字符串中所有的小 ...
- 转:视觉中国的NoSQL之路:从MySQL到MongoDB
起因 视觉中国网站(www.chinavisual.com)是国内最大的创意人群的专业网站.2009年以前,同很多公司一样,我们的CMS和社区产品都构建于PHP+Nginx+MySQL之上:MySQL ...
- BZOJ 3065 带插入区间K小值
http://www.lydsy.com/JudgeOnline/problem.php?id=3065 思路:替罪羊树套权值线段树. 当替罪羊树某个子树大于某个比利(比例)时就暴力重构,本题时间复杂 ...
- Qt中添加背景图片的方法
工作似乎走上正轨了,上周五的工作是做一个界面,用到QFrame和QPushButton,QFrame做主面板,QPushButton为其子控件,需要在主面板上贴背景图片,还需要在QPushButton ...