Skip to content

Commit

Permalink
feat: The exposed module reads the options. (#112)
Browse files Browse the repository at this point in the history
* feat: The exposed module reads the options.

* test: Add the readModuleOptions test case.
  • Loading branch information
jiuhuan authored Apr 3, 2023
1 parent 2a94a57 commit a52b44b
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ exports.tegg = {
};
```

```js
// config/config.default.js
{
tegg: {
// 读取模块支持自定义配置,可用于扩展或过滤不需要的模块文件
readModuleOptions: {
extraFilePattern: ['!**/dist', '!**/release'],
},
};
}
```

## Usage

### 原型
Expand Down
4 changes: 3 additions & 1 deletion core/common-util/src/ModuleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export interface ModuleConfig {
export interface ReadModuleReferenceOptions {
// module dir deep for globby when use auto scan module
// default is 10
deep?: number,
deep?: number;
cwd?: string;
extraFilePattern?: string[];
}

const DEFAULT_READ_MODULE_REF_OPTS = {
Expand Down Expand Up @@ -99,6 +100,7 @@ export class ModuleConfigUtil {
'!**/+(.*)/**',
// not load coverage
'!**/coverage',
...(realOptions.extraFilePattern || []),
], {
cwd: baseDir,
deep: realOptions.deep,
Expand Down
14 changes: 14 additions & 0 deletions core/common-util/test/ModuleConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ describe('test/ModuleConfig.test.ts', () => {
]);
});
});

describe('filter module', () => {
it('should work', () => {
const fixturesPath = path.join(__dirname, './fixtures/apps/app-with-modules');
const readModuleOptions = {
cwd: fixturesPath,
extraFilePattern: [ '!**/dist' ],
};
const ref = ModuleConfigUtil.readModuleReference(fixturesPath, readModuleOptions);
assert.deepStrictEqual(ref, [
{ path: path.join(fixturesPath, 'app/module-a') },
]);
});
});
});

describe('read package dependencies', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-a",
"eggModule": {
"name": "moduleA"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
3 changes: 2 additions & 1 deletion plugin/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default class App {
}

configWillLoad() {
this.app.moduleReferences = ModuleConfigUtil.readModuleReference(this.app.baseDir);
const { readModuleOptions } = this.app.config.tegg || {};
this.app.moduleReferences = ModuleConfigUtil.readModuleReference(this.app.baseDir, readModuleOptions || {});
this.app.moduleConfigs = {};
for (const reference of this.app.moduleReferences) {
const absoluteRef = {
Expand Down
1 change: 1 addition & 0 deletions plugin/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
],
"types": "typings/index.d.ts",
"scripts": {
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
"clean": "tsc -b --clean",
"tsc": "npm run clean && tsc -p ./tsconfig.json",
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
Expand Down
40 changes: 40 additions & 0 deletions plugin/config/test/ReadModule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import mm from 'egg-mock';
import assert from 'assert';
import path from 'path';

describe('test/ReadModule.test.ts', () => {
let app;
const fixturesPath = path.join(__dirname, './fixtures/apps/app-with-modules');

after(async () => {
await app.close();
});

afterEach(() => {
mm.restore();
});

before(async () => {
mm(process.env, 'EGG_TYPESCRIPT', true);
mm(process, 'cwd', () => {
return path.join(__dirname, '..');
});
app = mm.app({
baseDir: fixturesPath,
framework: require.resolve('egg'),
});
await app.ready();
});

it('should work', async () => {
assert.deepStrictEqual(app.moduleConfigs, {
moduleA: {
config: {},
name: 'moduleA',
reference: {
path: path.join(fixturesPath, 'app/module-a'),
},
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-a",
"eggModule": {
"name": "moduleA"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return {
tegg: {
readModuleOptions: {
extraFilePattern: [ '!**/dist' ],
},
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}

0 comments on commit a52b44b

Please sign in to comment.