@modern-js/app-tools 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.0",
14
+ "version": "1.4.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -20,13 +20,20 @@
20
20
  "exports": {
21
21
  ".": {
22
22
  "node": {
23
+ "jsnext:source": "./src/index.ts",
23
24
  "import": "./dist/js/modern/index.js",
24
25
  "require": "./dist/js/node/index.js"
25
26
  },
26
27
  "default": "./dist/js/treeshaking/index.js"
27
28
  },
28
- "./cli": "./dist/js/node/index.js",
29
- "./types": "./lib/types.d.ts"
29
+ "./cli": {
30
+ "jsnext:source": "./src/index.ts",
31
+ "default": "./dist/js/node/index.js"
32
+ },
33
+ "./type": {
34
+ "jsnext:source": "./lib/types.d.ts",
35
+ "default": "./lib/types.d.ts"
36
+ }
30
37
  },
31
38
  "typesVersions": {
32
39
  "*": {
@@ -43,18 +50,17 @@
43
50
  },
44
51
  "dependencies": {
45
52
  "@babel/runtime": "^7",
46
- "@modern-js/core": "^1.1.4",
47
- "@modern-js/types": "^1.1.5",
48
- "@modern-js/i18n-cli-language-detector": "^1.1.1",
49
- "@modern-js/new-action": "^1.2.1",
50
- "@modern-js/plugin": "^1.1.2",
51
- "@modern-js/plugin-analyze": "^1.1.1",
52
- "@modern-js/plugin-fast-refresh": "^1.1.1",
53
- "@modern-js/plugin-i18n": "^1.1.2",
54
- "@modern-js/plugin-polyfill": "^1.1.1",
55
- "@modern-js/server": "^1.2.1",
56
- "@modern-js/utils": "^1.1.6",
57
- "@modern-js/webpack": "^1.1.3",
53
+ "@modern-js/core": "^1.4.0",
54
+ "@modern-js/types": "^1.3.0",
55
+ "@modern-js/i18n-cli-language-detector": "^1.2.1",
56
+ "@modern-js/new-action": "^1.3.1",
57
+ "@modern-js/plugin": "^1.2.1",
58
+ "@modern-js/plugin-analyze": "^1.3.0",
59
+ "@modern-js/plugin-fast-refresh": "^1.2.1",
60
+ "@modern-js/plugin-i18n": "^1.2.1",
61
+ "@modern-js/server": "^1.4.0",
62
+ "@modern-js/utils": "^1.3.0",
63
+ "@modern-js/webpack": "^1.3.0",
58
64
  "webpack": "^5.54.0"
59
65
  },
60
66
  "devDependencies": {
@@ -63,8 +69,9 @@
63
69
  "@types/react": "^17",
64
70
  "@types/react-dom": "^17",
65
71
  "typescript": "^4",
66
- "@modern-js/plugin-testing": "^1.2.2",
67
- "@modern-js/module-tools": "^1.1.4"
72
+ "@scripts/build": "0.0.0",
73
+ "jest": "^27",
74
+ "@scripts/jest-config": "0.0.0"
68
75
  },
69
76
  "sideEffects": false,
70
77
  "modernConfig": {
@@ -74,12 +81,13 @@
74
81
  },
75
82
  "publishConfig": {
76
83
  "registry": "https://registry.npmjs.org/",
77
- "access": "public"
84
+ "access": "public",
85
+ "types": "./dist/types/index.d.ts"
78
86
  },
79
87
  "scripts": {
80
88
  "new": "modern new",
81
89
  "build": "modern build",
82
- "test": "modern test --passWithNoTests"
90
+ "test": "jest --passWithNoTests"
83
91
  },
84
92
  "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
85
93
  }
@@ -0,0 +1,37 @@
1
+ import { build } from '../../src/commands/build';
2
+
3
+ const mockBeforeBuild = jest.fn();
4
+ const mockAfterBuild = jest.fn();
5
+ const mockGenerateRoutes = jest.fn();
6
+
7
+ jest.mock('@modern-js/core', () => ({
8
+ __esModule: true,
9
+ mountHook() {
10
+ return {
11
+ beforeBuild: mockBeforeBuild,
12
+ afterBuild: mockAfterBuild,
13
+ };
14
+ },
15
+ useAppContext: jest.fn(() => ({
16
+ existSrc: false,
17
+ })),
18
+ useResolvedConfigContext: jest.fn(),
19
+ }));
20
+
21
+ jest.mock('../../src/utils/routes', () => ({
22
+ __esModule: true,
23
+ generateRoutes: () => mockGenerateRoutes(),
24
+ }));
25
+
26
+ describe('command build', () => {
27
+ afterAll(() => {
28
+ jest.resetAllMocks();
29
+ });
30
+
31
+ test('existSrc is false', async () => {
32
+ await build();
33
+ expect(mockBeforeBuild).toBeCalled();
34
+ expect(mockGenerateRoutes).toBeCalled();
35
+ expect(mockAfterBuild).toBeCalled();
36
+ });
37
+ });
@@ -0,0 +1,7 @@
1
+ import { dev } from '../../src/commands/dev';
2
+
3
+ describe('command', () => {
4
+ test('dev', () => {
5
+ expect(dev).toBeDefined();
6
+ });
7
+ });
@@ -0,0 +1,7 @@
1
+ import plugin from '../src';
2
+
3
+ describe('app-tools', () => {
4
+ it('default', () => {
5
+ expect(plugin).toBeDefined();
6
+ });
7
+ });
@@ -0,0 +1,27 @@
1
+ import { fs } from '@modern-js/utils';
2
+ import { generateRoutes } from '../src/utils/routes';
3
+
4
+ jest.mock('@modern-js/utils', () => {
5
+ const originalModule = jest.requireActual('@modern-js/utils');
6
+ return {
7
+ __esModule: true,
8
+ ...originalModule,
9
+ fs: {
10
+ outputFile: jest.fn((filename: string, output: string) => ({
11
+ filename,
12
+ output,
13
+ })),
14
+ },
15
+ };
16
+ });
17
+
18
+ describe('routes', () => {
19
+ test('generateRoutes', async () => {
20
+ const mockAppContext = {
21
+ serverRoutes: [],
22
+ distDirectory: './dist',
23
+ };
24
+ await generateRoutes(mockAppContext as any);
25
+ expect(fs.outputFile).toHaveBeenCalledTimes(1);
26
+ });
27
+ });
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "@modern-js/tsconfig/base",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "jsx": "preserve",
6
+ "baseUrl": "./",
7
+ "outDir": "./out",
8
+ "emitDeclarationOnly": true,
9
+ "isolatedModules": true,
10
+ "paths": {},
11
+ "types": ["node", "jest"]
12
+ }
13
+ }
package/tsconfig.json CHANGED
@@ -8,9 +8,7 @@
8
8
  "baseUrl": "./",
9
9
  "skipLibCheck": true,
10
10
  "isolatedModules": true,
11
- "paths": {
12
- "@/*": ["./src/*"]
13
- }
11
+ "paths": {}
14
12
  },
15
13
  "include": ["src"]
16
14
  }
@@ -1,129 +0,0 @@
1
- import { Configuration, webpack } from 'webpack';
2
- import { WebpackConfigTarget, getWebpackConfig } from '@modern-js/webpack';
3
- import {
4
- useAppContext,
5
- useResolvedConfigContext,
6
- mountHook,
7
- ResolvedConfigContext,
8
- manager,
9
- } from '@modern-js/core';
10
- import {
11
- fs,
12
- formatWebpackMessages,
13
- measureFileSizesBeforeBuild,
14
- printFileSizesAfterBuild,
15
- printBuildError,
16
- logger,
17
- isUseSSRBundle,
18
- } from '@modern-js/utils';
19
-
20
- // These sizes are pretty large. We'll warn for bundles exceeding them.
21
- const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
22
- const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
23
-
24
- interface CliOptions {
25
- analyze?: boolean;
26
- }
27
-
28
- export const build = async (options?: CliOptions) => {
29
- const webpackBuild = async (webpackConfig: Configuration, type?: string) => {
30
- const compiler = webpack(webpackConfig);
31
-
32
- return new Promise((resolve, reject) => {
33
- let label = process.env.NODE_ENV || '';
34
- if (type && type !== 'legacy') {
35
- label += ` ${type}`;
36
- }
37
- logger.info(`Creating a ${label} build...`);
38
-
39
- compiler.run((err, stats) => {
40
- let messages: {
41
- errors: any;
42
- warnings: any;
43
- };
44
- if (!err) {
45
- messages = formatWebpackMessages(
46
- stats!.toJson({ all: false, warnings: true, errors: true }),
47
- );
48
-
49
- if (messages.errors.length === 0) {
50
- logger.info(`File sizes after ${label} build:\n`);
51
- printFileSizesAfterBuild(
52
- stats,
53
- previousFileSizes,
54
- outputPath,
55
- WARN_AFTER_BUNDLE_GZIP_SIZE,
56
- WARN_AFTER_CHUNK_GZIP_SIZE,
57
- );
58
- logger.log();
59
- }
60
- }
61
-
62
- // When using run or watch, call close and wait for it to finish before calling run or watch again.
63
- // Concurrent compilations will corrupt the output files.
64
- compiler.close(closeErr => {
65
- if (closeErr) {
66
- logger.error(closeErr);
67
- }
68
- if (err) {
69
- reject(err);
70
- } else {
71
- if (messages.errors.length) {
72
- reject(new Error(messages.errors.join('\n\n')));
73
- return;
74
- }
75
- resolve({ warnings: messages.warnings });
76
- }
77
- });
78
- });
79
- });
80
- };
81
-
82
- /* eslint-disable react-hooks/rules-of-hooks */
83
- const resolvedConfig = useResolvedConfigContext();
84
- const appContext = useAppContext();
85
- /* eslint-enable react-hooks/rules-of-hooks */
86
-
87
- manager.run(() => {
88
- ResolvedConfigContext.set({ ...resolvedConfig, cliOptions: options });
89
- });
90
-
91
- const outputPath = appContext.distDirectory;
92
- const previousFileSizes = await measureFileSizesBeforeBuild(outputPath);
93
- fs.emptyDirSync(outputPath);
94
-
95
- const buildConfigs = [];
96
-
97
- buildConfigs.push({
98
- type: 'legacy',
99
- config: getWebpackConfig(WebpackConfigTarget.CLIENT)!,
100
- });
101
-
102
- if (resolvedConfig.output.enableModernMode) {
103
- buildConfigs.push({
104
- type: 'modern',
105
- config: getWebpackConfig(WebpackConfigTarget.MODERN)!,
106
- });
107
- }
108
-
109
- if (isUseSSRBundle(resolvedConfig)) {
110
- buildConfigs.push({
111
- type: 'ssr',
112
- config: getWebpackConfig(WebpackConfigTarget.NODE)!,
113
- });
114
- }
115
-
116
- await (mountHook() as any).beforeBuild({
117
- webpackConfigs: buildConfigs.map(({ config }) => config),
118
- });
119
-
120
- for (const buildConfig of buildConfigs) {
121
- const { type: buildType, config } = buildConfig;
122
- try {
123
- await webpackBuild(config, buildType);
124
- } catch (error) {
125
- printBuildError(error as Error);
126
- }
127
- }
128
- await (mountHook() as any).afterBuild();
129
- };
@@ -1,6 +0,0 @@
1
- import { mountHook } from '@modern-js/core';
2
-
3
- export const deploy = async (options: any) => {
4
- await (mountHook() as any).beforeDeploy(options);
5
- await (mountHook() as any).afterDeploy(options);
6
- };
@@ -1,80 +0,0 @@
1
- import {
2
- Configuration,
3
- getWebpackConfig,
4
- WebpackConfigTarget,
5
- } from '@modern-js/webpack';
6
- import {
7
- fs,
8
- logger,
9
- HMR_SOCK_PATH,
10
- clearConsole,
11
- chalk,
12
- isSSR,
13
- } from '@modern-js/utils';
14
- import {
15
- useAppContext,
16
- useResolvedConfigContext,
17
- mountHook,
18
- } from '@modern-js/core';
19
- import { createCompiler } from '../utils/createCompiler';
20
- import { createServer } from '../utils/createServer';
21
-
22
- export const dev = async () => {
23
- /* eslint-disable react-hooks/rules-of-hooks */
24
- const appContext = useAppContext();
25
- const userConfig = useResolvedConfigContext();
26
- /* eslint-enable react-hooks/rules-of-hooks */
27
-
28
- const { appDirectory, distDirectory, port } = appContext;
29
-
30
- fs.emptyDirSync(distDirectory);
31
-
32
- await (mountHook() as any).beforeDev();
33
-
34
- const webpackConfigs = [
35
- isSSR(userConfig) && getWebpackConfig(WebpackConfigTarget.NODE),
36
- getWebpackConfig(WebpackConfigTarget.CLIENT),
37
- ].filter(Boolean) as Configuration[];
38
-
39
- const compiler = await createCompiler({
40
- webpackConfigs,
41
- userConfig,
42
- appContext,
43
- });
44
-
45
- const app = await createServer({
46
- dev: {
47
- ...{
48
- client: {
49
- port: port!.toString(),
50
- overlay: false,
51
- logging: 'none',
52
- path: HMR_SOCK_PATH,
53
- host: 'localhost',
54
- },
55
- dev: { writeToDisk: (file: string) => !file.includes('.hot-update.') },
56
- hot: true,
57
- liveReload: true,
58
- port,
59
- https: userConfig.dev.https,
60
- },
61
- ...userConfig.tools.devServer,
62
- },
63
- compiler,
64
- pwd: appDirectory,
65
- config: userConfig as any,
66
- plugins: appContext.plugins
67
- .filter((p: any) => p.server)
68
- .map((p: any) => p.server),
69
- });
70
-
71
- app.listen(port, (err: Error) => {
72
- if (err) {
73
- throw err;
74
- }
75
-
76
- clearConsole();
77
-
78
- logger.log(chalk.cyan(`Starting the development server...`));
79
- });
80
- };
@@ -1,3 +0,0 @@
1
- export * from './dev';
2
- export * from './build';
3
- export * from './start';
@@ -1,30 +0,0 @@
1
- import { logger, chalk } from '@modern-js/utils';
2
- import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
3
- import server from '@modern-js/server';
4
- import { printInstructions } from '../utils/printInstructions';
5
-
6
- export const start = async () => {
7
- /* eslint-disable react-hooks/rules-of-hooks */
8
- const appContext = useAppContext();
9
- const userConfig = useResolvedConfigContext();
10
- /* eslint-enable react-hooks/rules-of-hooks */
11
-
12
- const { appDirectory, port } = appContext;
13
-
14
- logger.log(chalk.cyan(`Starting the modern server...`));
15
-
16
- const app = await server({
17
- pwd: appDirectory,
18
- config: userConfig as any,
19
- plugins: appContext.plugins
20
- .filter((p: any) => p.server)
21
- .map((p: any) => p.server),
22
- });
23
-
24
- app.listen(port, async (err: Error) => {
25
- if (err) {
26
- throw err;
27
- }
28
- await printInstructions(appContext, userConfig);
29
- });
30
- };
package/src/index.ts DELETED
@@ -1,100 +0,0 @@
1
- import { createPlugin, defineConfig, usePlugins, cli } from '@modern-js/core';
2
- import { lifecycle } from './lifecycle';
3
- import { i18n, localeKeys } from './locale';
4
- import { getLocaleLanguage } from './utils/language';
5
- import { start } from './commands/start';
6
- import { dev } from './commands/dev';
7
-
8
- export { defineConfig };
9
-
10
- // eslint-disable-next-line react-hooks/rules-of-hooks
11
- usePlugins([
12
- require.resolve('@modern-js/plugin-analyze/cli'),
13
- require.resolve('@modern-js/plugin-fast-refresh/cli'),
14
- require.resolve('@modern-js/plugin-polyfill/cli'),
15
- ]);
16
-
17
- export default createPlugin(
18
- (() => {
19
- const locale = getLocaleLanguage();
20
- i18n.changeLanguage({ locale });
21
-
22
- lifecycle();
23
-
24
- return {
25
- commands({ program }: any) {
26
- program
27
- .command('dev')
28
- .usage('[options]')
29
- .description(i18n.t(localeKeys.command.dev.describe))
30
- .option('-c --config <config>', i18n.t(localeKeys.command.dev.config))
31
- .action(async () => {
32
- await dev();
33
- });
34
-
35
- program
36
- .command('build')
37
- .usage('[options]')
38
- .description(i18n.t(localeKeys.command.build.describe))
39
- .option('--analyze', i18n.t(localeKeys.command.build.analyze))
40
- .action(async (options: any) => {
41
- const { build } = await import('./commands/build');
42
- await build(options);
43
- // force exit after build.
44
- // eslint-disable-next-line no-process-exit
45
- process.exit(0);
46
- });
47
-
48
- program
49
- .command('start')
50
- .usage('[options]')
51
- .description(i18n.t(localeKeys.command.start.describe))
52
- .action(async () => {
53
- await start();
54
- });
55
-
56
- program
57
- .command('deploy')
58
- .usage('[options]')
59
- .description(i18n.t(localeKeys.command.deploy.describe))
60
- .action(async (options: any) => {
61
- const { build } = await import('./commands/build');
62
- await build();
63
- const { deploy } = await import('./commands/deploy');
64
- await deploy(options);
65
- // eslint-disable-next-line no-process-exit
66
- process.exit(0);
67
- });
68
-
69
- program
70
- .command('new')
71
- .usage('[options]')
72
- .description(i18n.t(localeKeys.command.new.describe))
73
- .option('-d, --debug', i18n.t(localeKeys.command.new.debug), false)
74
- .option(
75
- '-c, --config <config>',
76
- i18n.t(localeKeys.command.new.config),
77
- )
78
- .option('--dist-tag <tag>', i18n.t(localeKeys.command.new.distTag))
79
- .option('--registry', i18n.t(localeKeys.command.new.registry))
80
- .action(async (options: any) => {
81
- const { MWANewAction } = await import('@modern-js/new-action');
82
- await MWANewAction({ ...options, locale });
83
- });
84
- },
85
- async fileChange() {
86
- await cli.restart();
87
- },
88
- };
89
- }) as any,
90
- {
91
- post: [
92
- '@modern-js/plugin-analyze',
93
- '@modern-js/plugin-fast-refresh',
94
- '@modern-js/plugin-ssr',
95
- '@modern-js/plugin-state',
96
- '@modern-js/plugin-router',
97
- '@modern-js/plugin-polyfill',
98
- ],
99
- },
100
- );
package/src/lifecycle.ts DELETED
@@ -1,43 +0,0 @@
1
- import { createAsyncWaterfall, createAsyncWorkflow } from '@modern-js/plugin';
2
- import { registerHook } from '@modern-js/core';
3
-
4
- import type { Compiler, Configuration, MultiCompiler } from '@modern-js/types';
5
-
6
- export const beforeDev = createAsyncWorkflow();
7
-
8
- export const afterDev = createAsyncWorkflow();
9
-
10
- export const beforeCreateCompiler = createAsyncWorkflow<{
11
- webpackConfigs: Configuration[];
12
- }>();
13
-
14
- export const afterCreateCompiler = createAsyncWorkflow<{
15
- compiler: Compiler | MultiCompiler | undefined;
16
- }>();
17
-
18
- export const beforePrintInstructions =
19
- createAsyncWaterfall<{ instructions: string }>();
20
-
21
- export const beforeBuild = createAsyncWorkflow<{
22
- webpackConfigs: Configuration[];
23
- }>();
24
-
25
- export const afterBuild = createAsyncWorkflow();
26
-
27
- export const beforeDeploy = createAsyncWorkflow<Record<string, any>>();
28
-
29
- export const afterDeploy = createAsyncWorkflow<Record<string, any>>();
30
-
31
- export const lifecycle = () => {
32
- registerHook({
33
- beforeDev,
34
- afterDev,
35
- beforeCreateCompiler,
36
- afterCreateCompiler,
37
- beforePrintInstructions,
38
- beforeBuild,
39
- afterBuild,
40
- beforeDeploy,
41
- afterDeploy,
42
- });
43
- };
package/src/locale/en.ts DELETED
@@ -1,18 +0,0 @@
1
- export const EN_LOCALE = {
2
- command: {
3
- dev: {
4
- describe: 'start dev server',
5
- config: 'specify config file',
6
- },
7
- build: { describe: 'build application', analyze: 'analyze bundle' },
8
- start: { describe: 'start server' },
9
- deploy: { describe: 'deploy application' },
10
- new: {
11
- describe: 'generator runner for MWA project',
12
- debug: 'using debug mode to log something',
13
- config: 'set default generator config(json string)',
14
- distTag: `use specified tag version for it's generator`,
15
- registry: 'set npm registry url to run npm command',
16
- },
17
- },
18
- };
@@ -1,9 +0,0 @@
1
- import { I18n } from '@modern-js/plugin-i18n';
2
- import { ZH_LOCALE } from './zh';
3
- import { EN_LOCALE } from './en';
4
-
5
- const i18n = new I18n();
6
-
7
- const localeKeys = i18n.init('zh', { zh: ZH_LOCALE, en: EN_LOCALE });
8
-
9
- export { i18n, localeKeys };
package/src/locale/zh.ts DELETED
@@ -1,18 +0,0 @@
1
- export const ZH_LOCALE = {
2
- command: {
3
- dev: {
4
- describe: '本地开发命令',
5
- config: '制定配置文件路径',
6
- },
7
- build: { describe: '构建应用命令', analyze: '分析构建产物' },
8
- start: { describe: '应用启动命令' },
9
- deploy: { describe: '部署应用命令' },
10
- new: {
11
- describe: 'MWA 项目中中执行生成器',
12
- debug: '开启 Debug 模式,打印调试日志信息',
13
- config: '生成器运行默认配置(JSON 字符串)',
14
- distTag: '生成器使用特殊的 npm Tag 版本',
15
- registry: '生成器运行过程中定制 npm Registry',
16
- },
17
- },
18
- };