Skip to content
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: 7 additions & 0 deletions packages/build-scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.2.0

- [feat] auto load config of `build.config.(js|ts)`
- [fix] exit process when config is not found
- [chore] upgrade version of esbuild (up to `^0.13.12`)
- [chore] optimize message, add verbose message when modify user config

## 1.1.2

- [fix] missing type of hasRegistration
Expand Down
7 changes: 4 additions & 3 deletions packages/build-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-scripts",
"version": "1.1.2",
"version": "1.2.0",
"license": "MIT",
"description": "scripts core",
"main": "lib/index.js",
Expand All @@ -16,7 +16,7 @@
"build": "tsc",
"prepublishOnly": "npm run build",
"start": "tsc -w",
"test": "jest"
"test": "jest --runInBand"
},
"engines": {
"node": ">=8.6.0",
Expand All @@ -34,7 +34,8 @@
"commander": "^2.19.0",
"deepmerge": "^4.0.0",
"detect-port": "^1.3.0",
"esbuild": "^0.12.16",
"esbuild": "^0.13.2",
"fast-glob": "^3.2.7",
"fs-extra": "^8.1.0",
"inquirer": "^6.5.1",
"json5": "^2.1.3",
Expand Down
21 changes: 15 additions & 6 deletions packages/build-scripts/src/core/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import webpack, { MultiStats } from 'webpack';
import { Logger } from 'npmlog';
import { AggregatedResult } from '@jest/test-result';
import { GlobalConfig } from '@jest/types/build/Config';
import * as fg from 'fast-glob';
import type WebpackDevServer from 'webpack-dev-server';
import {
IHash,
Expand All @@ -25,7 +26,7 @@ import deepmerge = require('deepmerge');
import log = require('../utils/log');

const PKG_FILE = 'package.json';
const USER_CONFIG_FILE = 'build.json';
const USER_CONFIG_FILE = ['build.json', 'build.config.(js|ts)'];
const PLUGIN_CONTEXT_KEY = [
'command' as 'command',
'commandArgs' as 'commandArgs',
Expand Down Expand Up @@ -466,22 +467,29 @@ class Context {
? config
: path.resolve(this.rootDir, config);
} else {
configPath = path.resolve(this.rootDir, USER_CONFIG_FILE);
const [defaultUserConfig] = await fg(USER_CONFIG_FILE, { cwd: this.rootDir, absolute: true });
configPath = defaultUserConfig;
}
let userConfig: IUserConfig = {
plugins: [],
};
if (fs.existsSync(configPath)) {
if (configPath && fs.existsSync(configPath)) {
try {
userConfig = await loadConfig(configPath, log);
} catch (err) {
log.info(
'CONFIG',
`Fail to load config file ${configPath}, use default config instead`,
`Fail to load config file ${configPath}`,
);
log.error('CONFIG', err.stack || err.toString());
process.exit(1);
}
} else {
log.error(
'CONFIG',
`config file${`(${configPath})` || ''} is not exist`,
);
process.exit(1);
}

return this.mergeModeConfig(userConfig);
Expand Down Expand Up @@ -643,9 +651,10 @@ class Context {
const modifiedValue = configKey(this.userConfig);
if (_.isPlainObject(modifiedValue)) {
if (Object.prototype.hasOwnProperty.call(modifiedValue, 'plugins')) {
log.warn('[waring]', errorMsg);
// remove plugins while it is not support to be modified
log.verbose('[modifyUserConfig]', 'delete plugins of user config while it is not support to be modified');
delete modifiedValue.plugins;
}
delete modifiedValue.plugins;
Object.keys(modifiedValue).forEach(modifiedConfigKey => {
const originalValue = this.userConfig[modifiedConfigKey];
this.userConfig[modifiedConfigKey] = mergeInDeep ? mergeConfig<JsonValue>(originalValue, modifiedValue[modifiedConfigKey]) : modifiedValue[modifiedConfigKey] ;
Expand Down
41 changes: 41 additions & 0 deletions packages/build-scripts/test/configFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Context from '../src/core/Context'
import path = require('path')

describe('load js config', () => {
const context = new Context({
args: {},
command: 'start',
rootDir: path.join(__dirname, 'fixtures/jsConfig/')
});

it('combine basic config', async () => {
await context.resolveConfig();
expect(context.userConfig.entry).toEqual('src/index');
});
});

describe('load ts config', () => {
const context = new Context({
args: {},
command: 'start',
rootDir: path.join(__dirname, 'fixtures/tsConfig/')
});

it('combine basic config', async () => {
await context.resolveConfig();
expect(context.userConfig.entry).toEqual('src/index');
});
});

describe('load mix config', () => {
const context = new Context({
args: {},
command: 'start',
rootDir: path.join(__dirname, 'fixtures/mixConfig/')
});

it('combine basic config', async () => {
await context.resolveConfig();
expect(context.userConfig.entry).toEqual('src/index.ts');
});
});
3 changes: 3 additions & 0 deletions packages/build-scripts/test/fixtures/jsConfig/build.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: 'src/index'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: 'src/index.js'
};
3 changes: 3 additions & 0 deletions packages/build-scripts/test/fixtures/mixConfig/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"entry": "src/index.ts"
}
9 changes: 9 additions & 0 deletions packages/build-scripts/test/fixtures/tsConfig/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Config {
entry: string;
}

const config: Config = {
entry: 'src/index',
};

export default config;