{"id":20345659,"url":"https://github.com/kscript/file-loader","last_synced_at":"2025-03-04T15:44:11.597Z","repository":{"id":47925718,"uuid":"163146074","full_name":"kscript/file-loader","owner":"kscript","description":"文件加载器","archived":false,"fork":false,"pushed_at":"2021-08-11T20:17:35.000Z","size":33,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-15T05:04:52.659Z","etag":null,"topics":["file","loader","npm"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kscript.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-26T06:43:00.000Z","updated_at":"2023-03-08T03:02:01.000Z","dependencies_parsed_at":"2022-08-12T14:20:54.742Z","dependency_job_id":null,"html_url":"https://github.com/kscript/file-loader","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kscript%2Ffile-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kscript%2Ffile-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kscript%2Ffile-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kscript%2Ffile-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kscript","download_url":"https://codeload.github.com/kscript/file-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241876557,"owners_count":20035396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["file","loader","npm"],"created_at":"2024-11-14T22:09:18.363Z","updated_at":"2025-03-04T15:44:11.570Z","avatar_url":"https://github.com/kscript.png","language":"TypeScript","readme":"### ks-file-loader\n一个文件加载器\n\n#### 安装\n```npm\n  npm i ks-file-loader -D\n```\n\n#### Api\n\n| 属性 | 类型 | 默认值 | 说明 |\n|--|--|--|--|\n| path | string | './' | 要处理目录(相对于项目目录, 而非文件所在目录) |\n| name | string / RegExp | '' | 要处理文件名 |\n| fullName | string / RegExp | '' | 要处理文件 (文件名+扩展名) |\n| ext | string / RegExp | '' | 要处理文件类型 |\n| mode | string | 'BFS' | 遍历模式: 深度优先(DFS)/广度优先(BFS) |\n| exclude | string / RegExp | - | 排除路径. 没有被排除的文件才会被加载 |\n| include | string / RegExp | - | 包含路径. 默认排除 node_modules 这样的大文件夹, 如果要加载, 须在include中指定 |\n| deep | boolean | false | 是否深层遍历 |\n| async | boolean | false | 当不深层遍历时, 是否异步加载 (异步时使用Promise.all加载, 不采用异步可以保持顺序) |\n| showDir | boolean | false | 文件夹是否经过loader |\n| readFile | boolean | false | 是否读取文件内容 |\n| output | string / Function | - | 输出文件名 |\n| outputDir | string | - | 输出文件夹名 |\n| error | function | - | 处理出错时的回调 |\n| done | function | - | 处理完毕时的回调 |\n| loader | function | - | 加载器. 参数( stats: 文件信息 data: 文件内容 done: 处理完成时要执行的回调) |\n\n\u003e loader函数有异步操作时, 有两种方式: 1、回调函数 2、Promise(推荐).  \n  当执行loader函数后, 返回值  \n  为 Promise 时, loader会先等待Promise执行完毕  \n  为 false 时, 等待用户手动调用 done  \n  为其它值时, 则继续执行  \n\n\u003e 设置了fullName时, 不再验证文件名和扩展名\n\n\u003e output 有字符串和函数两种形式\n  - 值为字符串时\n    匹配规则为\\[(.*?)(path|dir|name|hash|ext)(.*?)\\](\\?|)\n    [{前缀}{精确匹配}{后缀}]{?表示精确匹配为空时, 输出空字符串}\n  - 值为函数时, 等同于loader, 但如果返回了一个字符串, 那么则会进入 值为字符串 时的流程\n    \n\n#### 如何使用?\n\n案例1: 用 hexo 生成的博客, 在源码中有大量的空行, 使用\n```js\n// 调用示例\nconst fs = require(\"fs\");\nconst fileLoader = require('ks-file-loader')\n\nfileLoader({\n\n  // 要进行转换的目录\n  // 相对于项目目录, 而非文件所在目录\n  path: './public',\n\n  // 文件扩展名, 支持正则\n  ext: 'html',\n\n  // 文件名, 支持正则\n  // name: '',\n\n  // 完整文件名, 支持正则 (验证文件名 + 扩展名)\n  // fullName: ''\n\n  // 包含目录\n  // 默认会排除 node_modules 这样的大文件夹, 如果要加载, 必须在include中指定\n  // include: /2018/,\n\n  // 排除目录\n  exclude: /lib/,\n\n  // 遍历模式: 广度优先(BFS) / 深度优先(DFS)\n  mode: 'DFS',\n\n  // 是否深层遍历\n  deep: true,\n\n  // 是否读取文件内容\n  readFile: true,\n  /**\n   * 加载器\n   * @param {object} stats 文件信息\n   * @param {string} data 文件内容 readFile 为 false 时返回空字符串\n   * @param {function} done 文件处理完毕的回调\n   * @return {Promise|false|any} Promise: 使用Promise处理异步  false: 使用回调函数处理异步(处理完需手动调用done) 其它值: 默认为同步\n   */\n  loader: function(stats, data, done) {\n    \n    // do something..\n    // 替换多余的空行\n    var content = data.replace(/\\n(\\s+)\\n+/g, '\\n');\n\n    // Promise方式处理异步操作\n    return new Promise((resolve, reject) {\n      fs.writeFile(stats.path, content, function(error){\n        if(error){\n          // 处理出错时, 即便通过reject抛出了错误, 也会被忽略, 因此需要用户自己先行处理错误\n          reject(error);\n        } else {\n          resolve();\n        }\n      });\n    });\n    // 回调函数方式处理异步操作\n    // 写入文件\n    fs.writeFile(stats.path, content, function(error){\n      if(error){\n        console.log(error);\n      }\n      done();\n    });\n    // 返回值必须全等于false\n    return false;\n  },\n  // 转换完毕\n  done: function(){\n    console.log('complete');\n  }\n});\n```\n\n案例2: 给指定类型的文件加hash\n```js\nconst fileLoader = require('ks-file-loader')\nfileLoader({\n  ext: /^(js|ts)$/,\n  // output: '[dir/][name-]?[hash][.ext]',\n  output: function(stats) {\n    let name = /\\.d\\.ts$/.test(stats.path) ? stats.name.slice(0, -2) + '-[hash].d' : '[name-]?[hash]'\n    return '[dir/]' + name + '[.ext]'\n  }\n})\n```\n\n#### lisence\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkscript%2Ffile-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkscript%2Ffile-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkscript%2Ffile-loader/lists"}