Skip to content

Commit

Permalink
feat(settings): 支持且仅支持.mjs和.ts类型的项目配置 (#225)
Browse files Browse the repository at this point in the history
可使用`import {configure} from '@reskript/settings'`获得类型支持

BREAKING CHANGE: 项目配置必须为`reskript.config.{mjs|ts}`,且格式为ESM

BREAKING CHANGE: 应用入口配置必须为`xxx.config.{mjs|ts}`,且格式为ESM
  • Loading branch information
otakustay committed Jan 23, 2022
1 parent a4048a0 commit d10eaf9
Show file tree
Hide file tree
Showing 69 changed files with 790 additions and 484 deletions.
2 changes: 2 additions & 0 deletions packages/babel-plugin-add-react-display-name/package.json
Expand Up @@ -6,6 +6,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
Expand Up @@ -7,6 +7,8 @@
".": "./dist/index.js",
"./useComponentFile": "./dist/useComponentFile.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-utils/package.json
Expand Up @@ -6,6 +6,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-babel/package.json
Expand Up @@ -7,6 +7,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-build/package.json
Expand Up @@ -7,6 +7,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-dev/package.json
Expand Up @@ -7,6 +7,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-lint/package.json
Expand Up @@ -7,6 +7,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-play/package.json
Expand Up @@ -7,6 +7,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-test/package.json
Expand Up @@ -7,6 +7,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/package.json
Expand Up @@ -6,6 +6,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/config-babel/package.json
Expand Up @@ -7,6 +7,8 @@
".": "./dist/index.js",
"./willBreakingInternalUseOnly": "./dist/internal.cjs"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/config-img-loader/package.json
Expand Up @@ -3,7 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "commonjs",
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/config-jest/package.json
Expand Up @@ -2,7 +2,8 @@
"name": "@reskript/config-jest",
"version": "3.0.6",
"license": "MIT",
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/config-lint/package.json
Expand Up @@ -8,6 +8,8 @@
"./patch": "./config/patch.cjs",
"./config/*": "./config/*.cjs"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/config-webpack-dev-server/package.json
Expand Up @@ -6,6 +6,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/config-webpack/package.json
Expand Up @@ -6,6 +6,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/config-webpack/src/__tests__/compiler.ts
Expand Up @@ -13,8 +13,8 @@ interface CompileResult {
assets?: StatsAsset[];
}

export default async (entry: string, partialProjectSettings?: PartialProjectSettings) => {
const projectSettings = fillProjectSettings(partialProjectSettings);
export default async (entry: string, partialProjectSettings?: Omit<PartialProjectSettings, 'provider'>) => {
const projectSettings = fillProjectSettings({provider: 'webpack', ...partialProjectSettings});
const context: BuildContext = {
cwd: path.resolve(currentDirectory, 'fixtures'),
mode: 'development',
Expand Down
@@ -1,4 +1,4 @@
exports.html = {
export const html = {
meta: {
title: 'bob',
},
Expand Down
@@ -1,3 +1,3 @@
exports.entry = {
export const entry = {
filename: 'foo.dist.js',
};
16 changes: 8 additions & 8 deletions packages/config-webpack/src/partials/base.ts
Expand Up @@ -34,11 +34,14 @@ const readFileIfExists = async (filename: string) => {
return Buffer.from('');
};

const updateHashFromFile = async (hash: crypto.Hash, filename: string) => {
const updateHashFromFile = async (hash: crypto.Hash, filename: string): Promise<boolean> => {
if (existsSync(filename)) {
const buffer = await fs.readFile(filename);
hash.update(buffer);
return true;
}

return false;
};

const computeCacheKey = async (entry: BuildContext): Promise<string> => {
Expand All @@ -50,12 +53,9 @@ const computeCacheKey = async (entry: BuildContext): Promise<string> => {
// `reSKRipt`自己的版本信息等
await updateHashFromFile(hash, path.join(dirFromImportMeta(import.meta.url), '..', '..', 'package.json'));

const settingsLocation = path.join(entry.cwd, 'reskript.config.js');
if (existsSync(settingsLocation)) {
const buffer = await fs.readFile(settingsLocation);
hash.update(buffer);
}
else {
const hasTypeScriptSetting = await updateHashFromFile(hash, path.join(entry.cwd, 'reskript.config.ts'));
const hasJavaScriptSetting = await updateHashFromFile(hash, path.join(entry.cwd, 'reskript.config.mjs'));
if (!hasTypeScriptSetting && !hasJavaScriptSetting) {
hash.update(JSON.stringify(entry.projectSettings));
hash.update(entry.projectSettings.build.script.finalize.toString());
hash.update(entry.projectSettings.build.finalize.toString());
Expand Down Expand Up @@ -127,7 +127,7 @@ const factory: ConfigurationFactory = async entry => {
eslintPath: resolveSync('eslint'),
baseConfig: getScriptLintBaseConfig({cwd}),
exclude: ['node_modules', 'externals'],
extensions: ['js', 'jsx', 'ts', 'tsx'],
extensions: ['js', 'cjs', 'mjs', 'jsx', 'ts', 'tsx'],
emitError: true,
emitWarning: usage === 'devServer',
};
Expand Down
2 changes: 1 addition & 1 deletion packages/config-webpack/src/utils/entry.ts
Expand Up @@ -18,7 +18,7 @@ const validateEntryConfig = (config: EntryConfig, file: string) => {

const readEntryConfig = async (fileBaseName: string): Promise<EntryConfig> => {
try {
const {default: config} = await importUserModule<{default: EntryConfig}>(fileBaseName, {default: {}});
const config = await importUserModule<EntryConfig>(fileBaseName, {});
validateEntryConfig(config, fileBaseName);
return config;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/config-webpack/src/utils/validate.ts
Expand Up @@ -46,7 +46,7 @@ export const checkPreCommitHookWhenLintDisabled = async (cwd: string) => {

if (!packageConfig?.husky?.hooks?.['pre-commit']) {
const warning = `
This project has reportLintErrors option disabled in reskript.config.js,
This project has reportLintErrors option disabled in reSKRipt config,
and there is no pre-commit hook to lint your files,
you should at least install husky to enable pre-commit hook.
Expand Down
4 changes: 4 additions & 0 deletions packages/core/package.json
Expand Up @@ -6,6 +6,8 @@
"exports": {
".": "./dist/index.js"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down Expand Up @@ -35,10 +37,12 @@
"vitest": "^0.0.115"
},
"dependencies": {
"bundle-require": "^2.2.0",
"caller": "^1.0.1",
"dedent": "^0.7.0",
"dotenv": "^10.0.0",
"dotenv-expand": "^5.1.0",
"esbuild": "^0.14.8",
"find-up": "^6.2.0",
"g-status": "^2.0.2",
"globby": "^12.2.0",
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/resolve.ts
@@ -1,6 +1,7 @@
import path from 'path';
import fs from 'fs';
import {fileURLToPath} from 'url';
import {bundleRequire} from 'bundle-require';
import resolveCore from 'resolve';
import caller from 'caller';

Expand Down Expand Up @@ -28,16 +29,14 @@ export const resolveSync = (id: string) => {
return resolveCore.sync(id, {basedir: callerPath});
};

const USER_MODULES_EXTENSIONS = ['.js', '.cjs'];
const USER_MODULES_EXTENSIONS = ['.ts', '.mjs'];

export const importUserModule = async <T>(moduleName: string, defaultValue?: T): Promise<T> => {
const target = USER_MODULES_EXTENSIONS.map(v => moduleName + v).find(fs.existsSync);

if (target) {
// NOTE: 如果要改成原生ESM的话,这里得想个别的办法
delete require.cache[target];
const value = await import(target);
return value;
const {mod} = await bundleRequire({filepath: target});
return mod;
}

if (defaultValue) {
Expand Down
2 changes: 2 additions & 0 deletions packages/doctor/package.json
Expand Up @@ -3,6 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/init/package.json
Expand Up @@ -3,6 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/init/templates/normal-app/package.json
@@ -1,6 +1,8 @@
{
"name": "{{packageName}}",
"version": "1.0.0",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/less-safe-loader/package.json
Expand Up @@ -3,7 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "commonjs",
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-qiankun/package.json
Expand Up @@ -3,7 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "commonjs",
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-qiankun/src/__tests__/index.test.ts
Expand Up @@ -2,15 +2,15 @@ import {fillProjectSettings} from '@reskript/settings';
import plugin from '../index';

test('build to umd', async () => {
const projectSettings = fillProjectSettings();
const projectSettings = fillProjectSettings({provider: 'webpack'});
const settings = await plugin('test-app')(projectSettings, {cwd: process.cwd(), command: 'dev'});
const webpackConfig = settings.build.finalize({}, {} as any, {} as any);
expect(webpackConfig.output?.libraryTarget).toBe('umd');
expect(webpackConfig.output?.globalObject).toBe('window');
});

test('dev server container', async () => {
const projectSettings = fillProjectSettings();
const projectSettings = fillProjectSettings({provider: 'webpack'});
const settings = await plugin('test-app')(projectSettings, {cwd: process.cwd(), command: 'dev'});
expect(typeof settings.devServer.finalize).toBe('function');
});
3 changes: 2 additions & 1 deletion packages/plugin-sass/package.json
Expand Up @@ -3,7 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "commonjs",
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-workspace-no-build/package.json
Expand Up @@ -3,7 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "commonjs",
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/settings/package.json
Expand Up @@ -3,7 +3,8 @@
"version": "3.0.6",
"license": "MIT",
"type": "commonjs",
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.18.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/settings/src/__tests__/plugins.test.ts
Expand Up @@ -5,7 +5,7 @@ import {ProjectSettings, SettingsPlugin} from '../interface';
import {applyPlugins} from '../plugins';

test('one plugin', async () => {
const settings: ProjectSettings = fillProjectSettings({devServer: {}});
const settings: ProjectSettings = fillProjectSettings({provider: 'webpack', devServer: {}});
const plugin = jest.fn((settings: ProjectSettings, cmd: ProjectAware): ProjectSettings => {
return {
...settings,
Expand All @@ -25,7 +25,7 @@ test('one plugin', async () => {
});

test('multiple plugins', async () => {
const settings: ProjectSettings = fillProjectSettings({devServer: {}});
const settings: ProjectSettings = fillProjectSettings({provider: 'webpack', devServer: {}});
const port = (settings: ProjectSettings): ProjectSettings => {
return {
...settings,
Expand All @@ -50,7 +50,7 @@ test('multiple plugins', async () => {
});

test('plugins factory', async () => {
const settings: ProjectSettings = fillProjectSettings({devServer: {}});
const settings: ProjectSettings = fillProjectSettings({provider: 'webpack', devServer: {}});
const port: SettingsPlugin = settings => {
return {
...settings,
Expand Down

0 comments on commit d10eaf9

Please sign in to comment.