[Javascript] ADD a browser build to an NPM module

Source: Internet
Author: User
Tags postcss

In this lesson, we ' re going to use to webpack 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 folderif(Process.env.NODE_ENV = = ='Production') {Config.output.path= Path.join (__dirname,'Dist' ); Config.plugins.push (NewWebpack.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.

_____

varWebpack = require ('Webpack');varPath = require ('Path');varExtracttextplugin = require ("Extract-text-webpack-plugin");varHtmlwebpackplugin = require ('Html-webpack-plugin');varCopywebpackplugin = require ('Copy-webpack-plugin');varPostcss = require ('Postcss-loader');varAutoprefixer = require ('Autoprefixer');varENV =Process.env.NODE_ENV;varConfig ={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: [NewWebpack.optimize.OccurenceOrderPlugin (true),        NewWebpack. Defineplugin ({MODE: {Production:process.env.NODE_ENV==='Production', Dev:process.env.NODE_ENV==='Development'            }        }),        NewExtracttextplugin ("Index.min.css")    ]};if(Process.env.NODE_ENV = = ='Production') {Config.output.path= Path.join (__dirname,'Dist' ); Config.plugins.push (NewWebpack.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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.