Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

fix: 修复构建配置路径不一致的问题 #1379

Merged
merged 5 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/remax-cli/src/__tests__/integration/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import getConfig from '../../getConfig';
import { testBuildApp } from './helpers/runTest';

describe('remax config', () => {
it('override output', () => {
process.chdir(path.join(__dirname, 'fixtures/config'));
it('override output and normalize path', () => {
const cwd = path.join(__dirname, 'fixtures/config');
process.chdir(cwd);
const result = getConfig();
expect(result.output).toEqual('build');
expect(result.cwd).toEqual(cwd);
expect(result.rootDir).toEqual('src');
});

testBuildApp('config-add-css-rule');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const path = require('path');

module.exports = {
output: 'build',
cwd: path.join(process.cwd(), './'),
rootDir: 'src/',
};
16 changes: 14 additions & 2 deletions packages/remax-cli/src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ function readJavascriptConfig(path: string) {
return config || {};
}

function normalizeConfigPath(options: Options): Options {
const pathKeys: Array<keyof Options> = ['cwd', 'rootDir', 'output'];
pathKeys.forEach(key => {
const value = options[key];
// @ts-ignore string-type
options[key] = path.normalize(value).replace(/(\\|\/)$/, '');
});
return options;
}

export default function getConfig(validate = true): Options {
const configPath: string = path.join(process.cwd(), './remax.config');

let options = {};
let options = {} as Options;

if (fs.existsSync(configPath + '.js')) {
options = readJavascriptConfig(configPath + '.js');
Expand All @@ -31,8 +41,10 @@ export default function getConfig(validate = true): Options {
});
}

return {
const remaxConfig = {
...getDefaultOptions(),
...options,
};

return normalizeConfigPath(remaxConfig);
}