diff --git a/packages/build-scripts/CHANGELOG.md b/packages/build-scripts/CHANGELOG.md index 299adad..fe860de 100644 --- a/packages/build-scripts/CHANGELOG.md +++ b/packages/build-scripts/CHANGELOG.md @@ -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 diff --git a/packages/build-scripts/package.json b/packages/build-scripts/package.json index 373ee78..2eb5eaf 100644 --- a/packages/build-scripts/package.json +++ b/packages/build-scripts/package.json @@ -1,6 +1,6 @@ { "name": "build-scripts", - "version": "1.1.2", + "version": "1.2.0", "license": "MIT", "description": "scripts core", "main": "lib/index.js", @@ -16,7 +16,7 @@ "build": "tsc", "prepublishOnly": "npm run build", "start": "tsc -w", - "test": "jest" + "test": "jest --runInBand" }, "engines": { "node": ">=8.6.0", @@ -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", diff --git a/packages/build-scripts/src/core/Context.ts b/packages/build-scripts/src/core/Context.ts index 6ed8cc0..f11d752 100644 --- a/packages/build-scripts/src/core/Context.ts +++ b/packages/build-scripts/src/core/Context.ts @@ -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, @@ -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', @@ -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); @@ -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(originalValue, modifiedValue[modifiedConfigKey]) : modifiedValue[modifiedConfigKey] ; diff --git a/packages/build-scripts/test/configFiles.test.ts b/packages/build-scripts/test/configFiles.test.ts new file mode 100644 index 0000000..4a8cf02 --- /dev/null +++ b/packages/build-scripts/test/configFiles.test.ts @@ -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'); + }); +}); \ No newline at end of file diff --git a/packages/build-scripts/test/fixtures/jsConfig/build.config.js b/packages/build-scripts/test/fixtures/jsConfig/build.config.js new file mode 100644 index 0000000..bf6873f --- /dev/null +++ b/packages/build-scripts/test/fixtures/jsConfig/build.config.js @@ -0,0 +1,3 @@ +module.exports = { + entry: 'src/index' +}; \ No newline at end of file diff --git a/packages/build-scripts/test/fixtures/mixConfig/build.config.js b/packages/build-scripts/test/fixtures/mixConfig/build.config.js new file mode 100644 index 0000000..33e524a --- /dev/null +++ b/packages/build-scripts/test/fixtures/mixConfig/build.config.js @@ -0,0 +1,3 @@ +module.exports = { + entry: 'src/index.js' +}; \ No newline at end of file diff --git a/packages/build-scripts/test/fixtures/mixConfig/build.json b/packages/build-scripts/test/fixtures/mixConfig/build.json new file mode 100644 index 0000000..e8ba052 --- /dev/null +++ b/packages/build-scripts/test/fixtures/mixConfig/build.json @@ -0,0 +1,3 @@ +{ + "entry": "src/index.ts" +} \ No newline at end of file diff --git a/packages/build-scripts/test/fixtures/tsConfig/build.config.ts b/packages/build-scripts/test/fixtures/tsConfig/build.config.ts new file mode 100644 index 0000000..7a03529 --- /dev/null +++ b/packages/build-scripts/test/fixtures/tsConfig/build.config.ts @@ -0,0 +1,9 @@ +interface Config { + entry: string; +} + +const config: Config = { + entry: 'src/index', +}; + +export default config; \ No newline at end of file