美文网首页
webpack与脚本中

webpack与脚本中

作者: 一点金光 | 来源:发表于2019-07-30 00:49 被阅读0次
---
title:webpack与脚本中
date: 2018-06-09 16:29:00
updated: 2018-06-10 12:00:00
categories:
- 编程开发
tags:
- webpack
---

安装软件

npm install --save-dev webpack

简单使用

const webpack = require('webpack');

webpack({
  // Configuration Object
}, (err, stats) => { // Stats Object
  if (err || stats.hasErrors()) {
    // Handle errors here
  }
  // Done processing
});

编器对象


执行回调

const webpack = require('webpack');
const compiler = webpack({
  // Configuration Object
});

compiler.run((err, stats) => { // Stats Object
  // ...
});

const compiler = webpack({
  // Configuration Object
});
compiler.run((err, stats) => { // Stats Object
  // ...
});

监控文件

const watching = compiler.watch({
  // Example watchOptions
  aggregateTimeout: 300,
  poll: undefined
}, (err, stats) => { // Stats Object
  // Print watch/build result here...
  console.log(stats);
});

//关闭监控
watching.close(() => {
  console.log('Watching Ended.');
});
//不校监控
watching.invalidate();

状态对象

//是否有出错
stats.hasErrors()

//是否有警告
stats.hasWarnings()

//转化为对象
stats.toJson(options)

//转化为字符
stats.toString(options)

多个编译

const webpack = require('webpack');

webpack([
  { entry: './index1.js', output: { filename: 'bundle1.js' } },
  { entry: './index2.js', output: { filename: 'bundle2.js' } }
], (err, stats) => { // Stats Object
  process.stdout.write(stats.toString() + '\n');
})

错误处理

const webpack = require('webpack');

webpack({
  // Configuration Object
}, (err, stats) => {
  if (err) {
    console.error(err.stack || err);
    if (err.details) {
      console.error(err.details);
    }
    return;
  }

  const info = stats.toJson();

  if (stats.hasErrors()) {
    console.error(info.errors);
  }

  if (stats.hasWarnings()) {
    console.warn(info.warnings);
  }

  // Log result...
});

文件系统
可以指定文件系统

const MemoryFS = require('memory-fs');
const webpack = require('webpack');

const fs = new MemoryFS();
const compiler = webpack({ / options / });

compiler.outputFileSystem = fs;
compiler.run((err, stats) => {
  // Read the output later:
  const content = fs.readFileSync('...');
});

参考文献
webpack-offical-node

相关文章

网友评论

      本文标题:webpack与脚本中

      本文链接:https://www.haomeiwen.com/subject/agpwrctx.html