webpack从零开始第3课: 作为node的一个模块来使用

webpack目录node

参考: https://webpack.js.org/api/node/webpack

更多的时间,咱们将webpack作为一个nodejs模块来使用web

一:新建本身的打包文件build.js

咱们在./build/新建一个文件 build.js,内容以下npm

const webpack = require('webpack')
const webpackConfig = require('./webpackfile.js');
webpack(webpackConfig, (err, stats) => {
  if (err) throw err
  console.log('已打包好了,咱们作点别的事')
})

打包segmentfault

D:\03www2018\study\webpack2017>node ./build/build.js

效果与执行webpack命令同样
写成npm srciptapi

"scripts": {
    "abc": "webpack --config ./build/webpackfile.js",
    "def": "webpack-dev-server",
    "tttt": "node build/build.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

执行
D:03www2018studywebpack2017>npm run ttttwebpack-dev-server

二:使用webpack编译器ui

/*const webpack = require('webpack')
const webpackConfig = require('./webpackfile.js');
webpack(webpackConfig, (err, stats) => {
  if (err) throw err
  console.log('已打包好了,咱们作点别的事')
})*/

const webpack = require('webpack')
const webpackConfig = require('./webpackfile.js');
const compiler = webpack(webpackConfig)
compiler.run( (err, stats) => {
  if (err) throw err
  console.log('已打包好了,咱们作点别的事...')
})
// 编译器运行比上面要快些,但它没有包括watch部分,没有监视,只是编译

既监视又编译webpack2

/*const webpack = require('webpack')
const webpackConfig = require('./webpackfile.js');
webpack(webpackConfig, (err, stats) => {
  if (err) throw err
  console.log('已打包好了,咱们作点别的事')
})*/

/*const webpack = require('webpack')
const webpackConfig = require('./webpackfile.js');
const compiler = webpack(webpackConfig)
compiler.run( (err, stats) => {
  if (err) throw err
  console.log('已打包好了,咱们作点别的事...')
})*/
// 编译器运行比上面要快些,但它没有包括watch部分,没有监视,只是编译,若是要编译,还要加上


const webpack = require('webpack')
const webpackConfig = require('./webpackfile.js');
const compiler = webpack(webpackConfig)
const watching = compiler.watch({
  /* watchOptions */
}, (err, stats) => {
  // Print watch/build result here...
  console.log('我一直在监视着呢');
});

三:配合其它node模块
上面的打包不够丰富,若是和其它node模块配置能够实现更多的功能
如,使用chalk模块来更好的显示提示信息插件

相关文章
相关标签/搜索