# 项目介绍

file-lane模块用于文件转换，可实现文件1对1的转换，也可实现1对多、多对1的文件转换。

<img src="./doc/flow-detail.png" style="max-width: 1000px; width: 100%"/>

## 安装使用

- 安装

  `npm i file-lane`

- 快速上手

  ```javascript
  const { FileLane } = require('file-lane')
  const { UxLoader } = require('aiot-toolkit/aiotpack')
  const Path = require('path')

  const projectPath = Path.join(__dirname, '../testProject')

  // 定义项目转换的配置参数，output和module为必要配置内容
  const projectConfig = {
    // output指定转换后项目的存储位置
    get output() {
      const name = Path.basename(projectPath)
      const result = `../.temp_${name}`
      return result
    },
    // 转换过程中使用的转换模块配置
    module: {
      rules: [
        {
          test: [/.+\.ux$/],
          exclude: [/app\.ux/],
          loader: [UxLoader]
        }
      ]
    }
  }

  // 开始执行文件转换
  new FileLane(projectConfig).start()
  // 开启watch模式
  // new FileLane(projectConfig).start({ watch: true })
  ```

## 参数配置

- 必填参数
  - [output](#output)
  - [module](#module)
- 可选参数
  - [fileCollector](#fileCollector)
  - [include](#include)
  - [exclude](#exclude)
  - [plugins](#plugins)
  - [preWorks](#preWorks)
  - [followWorks](#followWorks)
  - [watchIgnores](#watchIgnores)

<a id="output">output</a>

描述: 输出目录

参数类型: string

示例:

```
'temp_project'
```

<a id="module">module</a>

描述: 文件转换规则集

参数类型: { rules: IRule[] } // IRule：转换规则

```javascript
interface IRule {
  // 配置文件
  test: MatchType

  // 文件的 Loader，从前向后依次执行，前一个 loader 结果做为后一个 loader 的入参
  loader: ILoaderClass[]
  exclude?: MatchType
  include?: MatchType
}
```

示例:

```
{
  rules: [
    {
      test: [/.+\.ux$/],
      exclude: [/app\.ux/],
      loader: [UxLoader]
    }
  ]
}

```

<a id="fileCollector">fileCollector</a>

描述: 文件收集器，输入文件路径，返回待合并的文件路径，常用于多转1，默认值为直接使用源文件

参数类型:

```
(file: string) => string[]
```

示例:

```javascript
// src/a.hml -->[src/a.hml, src/a.js, src/a.css]
(file: string) => {
  const fileList = [file]
  const { dir, name, ext } = path.parse(file)

  if (ext === '.hml') {
    ['.js', '.css'].map((item) => {
      const collectFile = path.join(dir, `${name}${item}`)
      // 若路径真实存在，则push到文件列表中
      if (fs.existsSync(collectFile)) {
        fileList.push(collectFile)
      }
    })
    return fileList
  }
}
```

<a id="include">include</a>

描述: 指定文件范围

参数类型:

```
OneMatchType | OneMatchType[]
```

示例:

```javascript
// 匹配包含有src字符串的文件
include = ['src']
```

<a id="exclude">exclude</a>

描述: 需排除的文件范围

参数类型:

```
OneMatchType | OneMatchType[]
```

示例:

```javascript
// 排除路径中包含有node_modules字符串的文件
exclude = [/node_modules/]
```

<a id="plugins">plugins</a>

描述: 插件，在每个文件转换完成前后，所有文件转换完成前后，在打包完成前后会触发

参数类型:

```
IPlugin[]
```

示例:

```

```

<a id="preWorks">preWorks</a>

描述: 前置工作，所有文件转换前的工作

参数类型:

```
PreWork[]
```

示例:

```javascript
preWorks = [validateManifest]

const validateManifest: PreWork<IJavascriptCompileOption>  = async (context) => {
  // 校验manifest.json文件的内容
}
```

<a id="followWorks">followWorks</a>

描述: 后续工作，所有文件转换后的工作

参数类型:

```
FollowWoker<O>[]
```

示例:

```javascript
followWorks = [
  {
    worker: toRpk,
    workerDescribe: 'follow work'
  }
]

const toRpk: FollowWork<IJavascriptCompileOption> = async (context, config, compilerOption) => {
  // 生成rpk逻辑
}
```

<a id="watchIgnores">watchIgnores</a>

描述: 配置watch时忽略的文件或者文件夹

参数类型:

```
MatchType
```

示例:

```javascript
watchIgnores = [/node_modules/, '/build/', '/dist/']
```
