Modify the source code in node_modules through nodejs

When developing a project, when the default configuration in node_modules does not meet the current project requirements, you need to modify the corresponding source code in node_modules. However, if you directly operate the source code for manual modification, the code will not be synchronized to the warehouse. When other people pull the code for development, or after executing npm install, the source code is still the default configuration. At this time, you can operate the source code through nodejs, which can meet the current needs.

Step 1: Create a file directory for storing modified content

Create a folder change_modules to store the modified file content, but the modified file directory needs to be consistent with the modified file directory in node_modules . For example, the directory where the modified file xmllint.js is located is: node_modules/@wepy/cli/core/util/xmllint.js, then the newly created file directory should be: change_modules/@wepy/cli/core/util/xmllint.js, as shown in the picture

Step 2: Modify the content

Just paste all the content in the modified file into the corresponding directory in change_modules, and then modify the places that need to be changed.

Step Three: Perform Copy | Replace

Create a new change_modules.js file, which stores the relevant syntax for file replacement

const fs = require('fs')
const path = require('path')

// 将 change_modules 内的文件覆盖在node_modules中
const REAL_NODE_MODULES = path.resolve('./node_modules') // 旧node_modules
const MY_NODE_MODULES = path.resolve('./change_modules') // 新node_modules
copy(MY_NODE_MODULES, REAL_NODE_MODULES)

/**
*@param{string}需要复制的目录、文件
*@param{string}复制到指定的目录、文件
*@param{function}每次复制前,都会经过一次filterFn,若返回true,则复制。
*/
function copy(origin, target, filterFn = () => true) {
  if (fs.statSync(origin).isDirectory()) {
    if (!fs.existsSync(target)) {
      fs.mkdirSync(target)
    }
    fs.readdirSync(origin).forEach(originName => {
      const originFilePath = path.resolve(origin, originName)
      const targetFilePath = path.resolve(target, originName)
      copy(originFilePath, targetFilePath, filterFn)
    })
  } else if (filterFn(origin, target)) {
    //执行替换
    fs.copyFileSync(origin, target)
  }
}

Step 4: Modify package.json

Add the syntax to execute change_modules.js in package.json, so that the execution of change_modules.js replaces the corresponding file before the project runs, as shown in the figure: 

After performing the above operations, run the project and you're done!

Related nodejs syntax details

require('fs') : The fs module is one of the core modules of nodejs, which mainly handles operations such as reading and writing, copying, deleting, and renaming files. When you need to use this module, you need to import this file first.

path.resolve():  This method resolves a path/path segment into an absolute path.

fs.statsync(path): Receive a path variable to get the detailed information of the path.

isDirectory(): Determine whether it is a directory, the directory returns true, and the file returns false.

fs.existsSync(path): Check whether the specified path exists.

fs.mkdirSync(path): used to create a directory.

fs.readdirSpath(path): Get all file names in the specified directory.

fs.copyFileSpath(beforePath,newPath): used to synchronously copy files from the source path to the target path.

Guess you like

Origin blog.csdn.net/weixin_44594219/article/details/127206080