npm和webpack

npm是前端开发中常用的一种工具,对于普通开发者来说,便于管理依赖。

往大了说,便于共享代码。写完代码,使用npm发布以后,然后别人用npm可以方便地共享到你的代码。

 

npm的使用:

mac环境下的安装:brew install node //node自带npm

在前端工程的根文件下,npm init --yes 会在该文件夹下生成package.json//package.json 声明了该工程的名称、版本、依赖等信息。

在该工程的根文件夹下,npm install 依赖名 --save,会安装依赖,同时把改依赖信息写入package.json当中

npm install 依赖名 --save-dev 安装开发测试环境的依赖

npm update //更新依赖

npm uninstall 依赖名 //在本地去除依赖

npm uninstall --save 依赖名 //在package.json中删除该依赖的信息

npm install -g 依赖名 //全局安装(全局指该主机的全局),可以直接在命令行中使用

npm update -g 依赖名 //依赖的更新

npm uninstall -g 依赖名 //依赖的卸载

 

webpack:高效的打包工具,可以把一堆js合成为一个js文件,同时生成html

使用方法:

安装:

npm install webpack -g

配置文件:

webpack.config.js

var path = require('path');

module.exports = {
    entry: './app/index.js',     //入口文件
    output: {
        filename: 'bundle.js',   //生成的文件名
        path: path.resolve(__dirname, 'dist') //生成文件的路径
    }
};

通过npm使用webpack:在package.json内配置scripts build

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"  //关键代码
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.23.1",   // 大版本.次要版本.小版本   ~版本号:不低于小版本,不大于次要版本  ^版本号:不低于次要版本,不大于大版本
    "babel-loader": "^6.3.2",
    "babel-preset-es2015": "^6.22.0",
    "html-webpack-plugin": "^2.28.0",
    "webpack": "^2.2.1"
  },
  "dependencies": {
    "lodash": "^4.17.4"
  }
}

然后 npm run build就开始使用webpack进行打包

 

webpack把app/index.js为入口的一堆js打包合成为一个 bundle.js

更厉害的是,webpack可以将一个html模板和bundle.js合成在一起,生成一个引用了bundle.js的html文件。

生成html的方法:

安装:

npm install html-webpack-plugin --save-dev 

修改webpack.config.js

var path = require('path');
var HtmlWebpackPlugin= require('html-webpack-plugin'); //声明下插件

module.exports = {
    entry: './app/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins:[                              //使用插件
        new HtmlWebpackPlugin({
            filename :'index.html',    //要生成的html文件
            template:'index.html'     //html文件的模板
        })
    ]
};

修改后,

npm run build 就会在dist文件夹下生成一个html文件,bundle.js文件已经引入好了。

 

 

另外前段开发过程中存在一个这样的问题:

es6(es2015和es2016)已经出了很久了,但是市面上的浏览器还不能完全兼容。

使用es5写代码就好了,但是广大程序员就是喜欢装逼用es6。为了解决这个问题,babel应运而生。babel就是能把你es6写的代码转成es5,方便浏览器兼容。

 

babel使用方法:

安装:

npm install babel-core babel-loader --save-dev

修改webpack.config.js的配置文件

var path = require('path');
var HtmlWebpackPlugin= require('html-webpack-plugin');

module.exports = {
    entry: './app/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins:[
        new HtmlWebpackPlugin({
            filename :'index.html',
            template:'index.html'
        })
    ],
    module:{        //添加loader
        loaders:[
            {
                test: /.js$/,   //那些文件要转化
                exclude: /node_modules/, //屏蔽哪些
                loader:"babel-loader"       //指定loader
            }
        ]
    }
};    

最后npm run build就把文件生成好了。

 

posted @ 2017-02-15 21:24  swaggyC  阅读(6351)  评论(1编辑  收藏  举报