博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] Add a browser build to an npm module
阅读量:6575 次
发布时间:2019-06-24

本文共 3410 字,大约阅读时间需要 11 分钟。

In this lesson, we're going to use  to create a  (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 folderif (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;

 

转载地址:http://qsgjo.baihongyu.com/

你可能感兴趣的文章
Ruby小白入门笔记之<Rubymine工具的快捷键>
查看>>
Media Session API 为当前正在播放的视频,音频,提供元数据来自定义媒体通知
查看>>
yum -y install php-mysql 版本冲突
查看>>
【7.17总结】 匈牙利算法(二分图最大匹配)
查看>>
JDBC(转)
查看>>
大端小段详解—转载
查看>>
告别LVS:使用keepalived+nginx实现负载均衡代理多个https
查看>>
征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)
查看>>
[转载]触摸屏网站制作的小细节
查看>>
[转载]INNO Setup 使用笔记
查看>>
Servlet--HttpSession接口,HttpSessionContext接口,Cookie类
查看>>
Android世界第一个activity启动过程
查看>>
RR调度(Round-robin scheduling)简单介绍
查看>>
重载函数编译后的新名字
查看>>
oracle resetlog与noresetlog的作用(转载)
查看>>
linux服务器内存占用太高-释放内存
查看>>
Linux下长时间ping网络加时间戳并记录到文本(转)
查看>>
在Unity中实现屏幕空间反射Screen Space Reflection(2)
查看>>
目录的操作
查看>>
进程简述
查看>>