[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;
复制代码

 

posted @   Zhentiw  阅读(265)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-05-01 [ES6] 23. Rest Parameters & Spread Parameters
点击右上角即可分享
微信分享提示