match-require

2018-10-14 admin

match-require是什么

什么是match-require,find require calls from string using regexp

match-require使用教程帮助文档

match-require

NPM version NPM downloads Build Status Coverage Status

find/replace dependencies using regexp

examples

const matchRequire = require('match-require');

  it('findAll works', () => {
    const content = ['// require("2")',
      'require("3");',
      '/* require("2") */',
      'require("4")'
    ].join('\n');

    const ret = matchRequire.findAll(content);

    expect(ret).to.eql(['3', '4']);
  });

  it('replaceAll works', () => {
    const content = ['// require("2")',
      'require("3");',
      '/* require("2") */',
      'require("4")'
    ].join('\n');

    const ret = matchRequire.replaceAll(content, (dep) => {
      return dep === '4' ? '5' : dep;
    });

    expect(ret).to.eql([
      'require("3");',
      '',
      'require("5")'
    ].join('\n'));
  });

  it('import works', () => {
    const content = ['// import "2"',
      'import x from "3";',
      'console.import("1")',
      '/* import "2" */',
      'import {z} from "4";',
      `import {
 x,
 y,
 z,
} from "5";`,
    ].join('\n');

    const ret = matchRequire.findAll(content);

    expect(ret).to.eql(['3', '4', '5']);
  });

history

2.1.0

  • add replaceAll

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-42638.html

文章标题:match-require

回到顶部