@modern-js/module-tools 1.3.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/CHANGELOG.md +55 -0
- package/dist/js/modern/features/dev/index.js +1 -1
- package/dist/js/node/features/dev/index.js +1 -1
- package/dist/types/index.d.ts +2 -0
- package/lib/types.d.ts +0 -10
- package/package.json +16 -17
- package/tests/dev-feature.test.ts +17 -1
- package/src/.eslintrc.json +0 -5
- package/src/cli/build.ts +0 -39
- package/src/cli/dev.ts +0 -24
- package/src/cli/index.ts +0 -3
- package/src/cli/new.ts +0 -32
- package/src/commands/build.ts +0 -74
- package/src/commands/dev.ts +0 -52
- package/src/commands/index.ts +0 -2
- package/src/features/build/build-platform.ts +0 -83
- package/src/features/build/build-watch.ts +0 -99
- package/src/features/build/build.ts +0 -111
- package/src/features/build/constants.ts +0 -52
- package/src/features/build/index.ts +0 -54
- package/src/features/build/logger/index.ts +0 -2
- package/src/features/build/logger/logText.ts +0 -80
- package/src/features/build/logger/loggerManager.ts +0 -132
- package/src/features/build/utils.ts +0 -235
- package/src/features/dev/index.ts +0 -74
- package/src/index.ts +0 -55
- package/src/locale/en.ts +0 -21
- package/src/locale/index.ts +0 -15
- package/src/locale/zh.ts +0 -21
- package/src/schema/index.ts +0 -4
- package/src/schema/output.ts +0 -41
- package/src/schema/schema.d.ts +0 -13
- package/src/schema/source.ts +0 -16
- package/src/tasks/build-source-code.ts +0 -234
- package/src/tasks/build-style.ts +0 -194
- package/src/tasks/build-watch-source-code.ts +0 -186
- package/src/tasks/build-watch-style.ts +0 -271
- package/src/tasks/constants.ts +0 -1
- package/src/tasks/copy-assets.ts +0 -123
- package/src/tasks/generator-dts.ts +0 -277
- package/src/type.d.ts +0 -1
- package/src/types.ts +0 -65
- package/src/utils/babel.ts +0 -104
- package/src/utils/color.ts +0 -3
- package/src/utils/copy.ts +0 -71
- package/src/utils/init-env.ts +0 -31
- package/src/utils/json.ts +0 -13
- package/src/utils/language.ts +0 -9
- package/src/utils/logger.ts +0 -141
- package/src/utils/readline.ts +0 -28
- package/src/utils/tsconfig.ts +0 -37
- package/src/utils/tspaths-transform/constants.ts +0 -19
- package/src/utils/tspaths-transform/index.ts +0 -139
- package/src/utils/tspaths-transform/utils.ts +0 -25
- package/src/utils/valide.ts +0 -37
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { Import } from '@modern-js/utils';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
|
|
4
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
5
|
-
'@modern-js/core',
|
|
6
|
-
require,
|
|
7
|
-
);
|
|
8
|
-
const inquirer: typeof import('inquirer') = Import.lazy('inquirer', require);
|
|
9
|
-
const color: typeof import('../../utils/color') = Import.lazy(
|
|
10
|
-
'../../utils/color',
|
|
11
|
-
require,
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
export interface IDevConfig {
|
|
15
|
-
appDirectory: string;
|
|
16
|
-
isTsProject: boolean;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type DevTaskType = 'storybook' | 'docsite' | 'unknow';
|
|
20
|
-
|
|
21
|
-
export const showMenu = async (config: IDevConfig) => {
|
|
22
|
-
const metas = await (core.mountHook() as any).moduleToolsMenu(undefined);
|
|
23
|
-
if (metas.length <= 0) {
|
|
24
|
-
console.info(
|
|
25
|
-
chalk.yellow(
|
|
26
|
-
'No runnable development features found.\nYou can use the `new` command to enable the development features',
|
|
27
|
-
),
|
|
28
|
-
);
|
|
29
|
-
// eslint-disable-next-line no-process-exit
|
|
30
|
-
process.exit(0);
|
|
31
|
-
}
|
|
32
|
-
const menuMessage = color.devMenuTitle('Select the debug mode:');
|
|
33
|
-
const { type } = await inquirer.prompt([
|
|
34
|
-
{
|
|
35
|
-
name: 'type',
|
|
36
|
-
message: menuMessage,
|
|
37
|
-
type: 'list',
|
|
38
|
-
choices: metas,
|
|
39
|
-
},
|
|
40
|
-
]);
|
|
41
|
-
|
|
42
|
-
const devMeta = metas.find((meta: any) => meta.value === type);
|
|
43
|
-
if (devMeta) {
|
|
44
|
-
await devMeta.runTask(config);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export const devStorybook = async (config: IDevConfig) => {
|
|
49
|
-
const metas = await (core.mountHook() as any).moduleToolsMenu(undefined);
|
|
50
|
-
const findStorybook = metas.find((meta: any) => meta.value === 'storybook');
|
|
51
|
-
if (findStorybook) {
|
|
52
|
-
await findStorybook.runTask(config);
|
|
53
|
-
} else {
|
|
54
|
-
console.info(
|
|
55
|
-
chalk.yellow(
|
|
56
|
-
'No development features found.\nYou can use the `new` command to enable the development features',
|
|
57
|
-
),
|
|
58
|
-
);
|
|
59
|
-
// eslint-disable-next-line no-process-exit
|
|
60
|
-
process.exit(0);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export const runSubCmd = async (subCmd: string, config: IDevConfig) => {
|
|
65
|
-
const metas = await (core.mountHook() as any).moduleToolsMenu(undefined);
|
|
66
|
-
|
|
67
|
-
const devMeta = metas.find((meta: any) => meta.value === subCmd);
|
|
68
|
-
if (devMeta) {
|
|
69
|
-
await devMeta.runTask(config);
|
|
70
|
-
} else {
|
|
71
|
-
// eslint-disable-next-line no-process-exit
|
|
72
|
-
process.exit(0);
|
|
73
|
-
}
|
|
74
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Import } from '@modern-js/utils';
|
|
2
|
-
|
|
3
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
4
|
-
'@modern-js/core',
|
|
5
|
-
require,
|
|
6
|
-
);
|
|
7
|
-
// const { createPlugin, usePlugins, defineConfig } = core;
|
|
8
|
-
const hooks: typeof import('@modern-js/module-tools-hooks') = Import.lazy(
|
|
9
|
-
'@modern-js/module-tools-hooks',
|
|
10
|
-
require,
|
|
11
|
-
);
|
|
12
|
-
const cli: typeof import('./cli') = Import.lazy('./cli', require);
|
|
13
|
-
const local: typeof import('./locale') = Import.lazy('./locale', require);
|
|
14
|
-
const schema: typeof import('./schema') = Import.lazy('./schema', require);
|
|
15
|
-
const lang: typeof import('./utils/language') = Import.lazy(
|
|
16
|
-
'./utils/language',
|
|
17
|
-
require,
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
export const { defineConfig } = core;
|
|
21
|
-
|
|
22
|
-
core.usePlugins([
|
|
23
|
-
require.resolve('@modern-js/plugin-changeset/cli'),
|
|
24
|
-
require.resolve('@modern-js/plugin-analyze/cli'),
|
|
25
|
-
]);
|
|
26
|
-
|
|
27
|
-
export default core.createPlugin(
|
|
28
|
-
(() => {
|
|
29
|
-
const locale = lang.getLocaleLanguage();
|
|
30
|
-
local.i18n.changeLanguage({ locale });
|
|
31
|
-
hooks.lifecycle();
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
validateSchema() {
|
|
35
|
-
return schema.addSchema();
|
|
36
|
-
},
|
|
37
|
-
config() {
|
|
38
|
-
return {
|
|
39
|
-
output: {
|
|
40
|
-
enableSourceMap: false,
|
|
41
|
-
jsPath: 'js',
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
},
|
|
45
|
-
commands({ program }: any) {
|
|
46
|
-
cli.devCli(program);
|
|
47
|
-
cli.buildCli(program);
|
|
48
|
-
cli.newCli(program, locale);
|
|
49
|
-
// 便于其他插件辨别
|
|
50
|
-
program.$$libraryName = 'module-tools';
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
}) as any,
|
|
54
|
-
{ post: ['@modern-js/plugin-analyze', '@modern-js/plugin-changeset'] },
|
|
55
|
-
);
|
package/src/locale/en.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const EN_LOCALE = {
|
|
2
|
-
command: {
|
|
3
|
-
build: {
|
|
4
|
-
describe: 'command for building module',
|
|
5
|
-
watch: 'building module in watch mode',
|
|
6
|
-
tsconfig: 'Specify a path to the tsconfig.json file',
|
|
7
|
-
style_only: 'only build style',
|
|
8
|
-
platform: 'building for other platforms',
|
|
9
|
-
no_tsc: 'close tsc compiler to emit d.ts',
|
|
10
|
-
no_clear: 'disable auto clear dist dir',
|
|
11
|
-
},
|
|
12
|
-
dev: { describe: 'start dev server' },
|
|
13
|
-
new: {
|
|
14
|
-
describe: 'generator runner for modern project',
|
|
15
|
-
debug: 'using debug mode to log something',
|
|
16
|
-
config: 'set default generator config(json string)',
|
|
17
|
-
distTag: `use specified tag version for it's generator`,
|
|
18
|
-
registry: 'set npm registry url to run npm command',
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
package/src/locale/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Import } from '@modern-js/utils';
|
|
2
|
-
|
|
3
|
-
import { ZH_LOCALE } from './zh';
|
|
4
|
-
import { EN_LOCALE } from './en';
|
|
5
|
-
|
|
6
|
-
const i18nPlugin: typeof import('@modern-js/plugin-i18n') = Import.lazy(
|
|
7
|
-
'@modern-js/plugin-i18n',
|
|
8
|
-
require,
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
const i18n = new i18nPlugin.I18n();
|
|
12
|
-
|
|
13
|
-
const localeKeys = i18n.init('zh', { zh: ZH_LOCALE, en: EN_LOCALE });
|
|
14
|
-
|
|
15
|
-
export { i18n, localeKeys };
|
package/src/locale/zh.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const ZH_LOCALE = {
|
|
2
|
-
command: {
|
|
3
|
-
build: {
|
|
4
|
-
describe: '构建模块命令',
|
|
5
|
-
watch: '使用 Watch 模式构建模块',
|
|
6
|
-
tsconfig: '指定 tsconfig.json 文件的路径',
|
|
7
|
-
style_only: '只构建样式文件',
|
|
8
|
-
platform: '构建其他平台产物',
|
|
9
|
-
no_tsc: '关闭 tsc 编译',
|
|
10
|
-
no_clear: '不清理产物目录',
|
|
11
|
-
},
|
|
12
|
-
dev: { describe: '本地开发命令' },
|
|
13
|
-
new: {
|
|
14
|
-
describe: '模块化工程方案中执行生成器',
|
|
15
|
-
debug: '开启 Debug 模式,打印调试日志信息',
|
|
16
|
-
config: '生成器运行默认配置(JSON 字符串)',
|
|
17
|
-
distTag: '生成器使用特殊的 npm Tag 版本',
|
|
18
|
-
registry: '生成器运行过程中定制 npm Registry',
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
package/src/schema/index.ts
DELETED
package/src/schema/output.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const PACKAGE_MODE_LIST = [
|
|
2
|
-
'universal-js',
|
|
3
|
-
'universal-js-lite',
|
|
4
|
-
'browser-js',
|
|
5
|
-
'browser-js-lite',
|
|
6
|
-
'node-js',
|
|
7
|
-
];
|
|
8
|
-
|
|
9
|
-
export const outputSchema = [
|
|
10
|
-
{
|
|
11
|
-
target: 'output.packageMode',
|
|
12
|
-
schema: { enum: PACKAGE_MODE_LIST },
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
target: 'output.packageFields',
|
|
16
|
-
schema: { typeof: 'object' },
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
target: 'output.disableTsChecker',
|
|
20
|
-
schema: { typeof: 'boolean' },
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
target: 'output.enableSourceMap',
|
|
24
|
-
schema: { typeof: 'boolean' },
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
target: 'output.importStyle',
|
|
28
|
-
schema: {
|
|
29
|
-
enum: ['compiled-code', 'source-code'],
|
|
30
|
-
// TODO: 目前default是无效的
|
|
31
|
-
default: 'source-code',
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
target: 'output.assetsPath',
|
|
36
|
-
schema: {
|
|
37
|
-
typeof: 'string',
|
|
38
|
-
default: 'styles',
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
];
|
package/src/schema/schema.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// import '@modern-js/core';
|
|
2
|
-
// import { PackageModeType, IPackageFields } from '../types';
|
|
3
|
-
|
|
4
|
-
// declare module '@modern-js/core' {
|
|
5
|
-
// interface OutputConfig {
|
|
6
|
-
// packageMode: PackageModeType;
|
|
7
|
-
// packageFields: IPackageFields;
|
|
8
|
-
// }
|
|
9
|
-
// interface NormalizedSourceConfig {
|
|
10
|
-
// enableBlockRuntime: boolean;
|
|
11
|
-
// jsxTransformRuntime: 'classic' | 'automatic';
|
|
12
|
-
// }
|
|
13
|
-
// }
|
package/src/schema/source.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export const sourceSchema = [
|
|
2
|
-
// 启动区块运行时
|
|
3
|
-
{
|
|
4
|
-
target: 'source.enableBlockRuntime',
|
|
5
|
-
schema: { type: 'boolean' },
|
|
6
|
-
},
|
|
7
|
-
{
|
|
8
|
-
target: 'source.jsxTransformRuntime',
|
|
9
|
-
schema: {
|
|
10
|
-
// https://babeljs.io/docs/en/babel-preset-react#runtime
|
|
11
|
-
enum: ['classic', 'automatic'],
|
|
12
|
-
// TODO: 目前default是无效的
|
|
13
|
-
default: 'automatic',
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
];
|
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { Import, fs } from '@modern-js/utils';
|
|
2
|
-
import type { NormalizedConfig, CoreOptions } from '@modern-js/core';
|
|
3
|
-
import type { BabelOptions, IVirtualDist } from '@modern-js/babel-compiler';
|
|
4
|
-
import type { ITsconfig } from '../types';
|
|
5
|
-
import { initEnv } from '../utils/init-env';
|
|
6
|
-
|
|
7
|
-
const babelCompiler: typeof import('@modern-js/babel-compiler') = Import.lazy(
|
|
8
|
-
'@modern-js/babel-compiler',
|
|
9
|
-
require,
|
|
10
|
-
);
|
|
11
|
-
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
12
|
-
const argv: typeof import('process.argv').default = Import.lazy(
|
|
13
|
-
'process.argv',
|
|
14
|
-
require,
|
|
15
|
-
);
|
|
16
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
17
|
-
'@modern-js/core',
|
|
18
|
-
require,
|
|
19
|
-
);
|
|
20
|
-
const bc: typeof import('../utils/babel') = Import.lazy(
|
|
21
|
-
'../utils/babel',
|
|
22
|
-
require,
|
|
23
|
-
);
|
|
24
|
-
const ts: typeof import('../utils/tsconfig') = Import.lazy(
|
|
25
|
-
'../utils/tsconfig',
|
|
26
|
-
require,
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
export enum Compiler {
|
|
30
|
-
babel,
|
|
31
|
-
esbuild,
|
|
32
|
-
swc,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface IBuildSourceCodeConfig {
|
|
36
|
-
babelConfig: BabelOptions;
|
|
37
|
-
srcRootDir: string;
|
|
38
|
-
willCompilerDirOrFile: string;
|
|
39
|
-
distDir: string;
|
|
40
|
-
compiler: Compiler;
|
|
41
|
-
projectData: {
|
|
42
|
-
appDirectory: string;
|
|
43
|
-
};
|
|
44
|
-
sourceMaps?: boolean;
|
|
45
|
-
tsconfigPath: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const runBabelCompiler = async (
|
|
49
|
-
willCompilerFiles: string[],
|
|
50
|
-
config: IBuildSourceCodeConfig,
|
|
51
|
-
babelConfig: BabelOptions = {},
|
|
52
|
-
) => {
|
|
53
|
-
const { srcRootDir, distDir } = config;
|
|
54
|
-
// TODO: 判断lynx模式下,修改distFileExtMap: {'js': 'js', 'jsx': 'jsx', 'ts': 'js', 'tsx': 'jsx'}
|
|
55
|
-
return babelCompiler.compiler(
|
|
56
|
-
{
|
|
57
|
-
quiet: true,
|
|
58
|
-
enableVirtualDist: true,
|
|
59
|
-
rootDir: srcRootDir,
|
|
60
|
-
filenames: willCompilerFiles,
|
|
61
|
-
distDir,
|
|
62
|
-
},
|
|
63
|
-
babelConfig,
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const getWillCompilerCode = (
|
|
68
|
-
srcDirOrFile: string,
|
|
69
|
-
option: { tsconfig: ITsconfig | null; isTsProject: boolean },
|
|
70
|
-
) => {
|
|
71
|
-
const { tsconfig, isTsProject } = option;
|
|
72
|
-
// 如果是一个文件路径,则直接返回
|
|
73
|
-
if (fs.existsSync(srcDirOrFile) && fs.lstatSync(srcDirOrFile).isFile()) {
|
|
74
|
-
return [srcDirOrFile];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const getExts = (isTs: boolean) => {
|
|
78
|
-
// TODO: 是否受控tsconfig.json 里的jsx配置
|
|
79
|
-
let exts = [];
|
|
80
|
-
if (isTs) {
|
|
81
|
-
exts = tsconfig?.compilerOptions?.allowJs
|
|
82
|
-
? ['.ts', '.tsx', '.js', '.jsx']
|
|
83
|
-
: ['.ts', '.tsx'];
|
|
84
|
-
} else {
|
|
85
|
-
exts = ['.js', '.jsx'];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return exts;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const exts = getExts(isTsProject);
|
|
92
|
-
const globPattern = `${srcDirOrFile}/**/*{${exts.join(',')}}`;
|
|
93
|
-
const files = glob.sync(globPattern, {
|
|
94
|
-
ignore: [`${srcDirOrFile}/**/*.d.ts`],
|
|
95
|
-
absolute: true,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
return files;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export const buildSourceCode = async (config: IBuildSourceCodeConfig) => {
|
|
102
|
-
const {
|
|
103
|
-
compiler,
|
|
104
|
-
willCompilerDirOrFile,
|
|
105
|
-
tsconfigPath,
|
|
106
|
-
sourceMaps,
|
|
107
|
-
babelConfig,
|
|
108
|
-
} = config;
|
|
109
|
-
const tsconfig = ts.readTsConfig(tsconfigPath);
|
|
110
|
-
const willCompilerFiles = getWillCompilerCode(willCompilerDirOrFile, {
|
|
111
|
-
tsconfig,
|
|
112
|
-
isTsProject: Boolean(tsconfig),
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
let distSet = null;
|
|
116
|
-
if (compiler === Compiler.babel) {
|
|
117
|
-
distSet = await runBabelCompiler(willCompilerFiles, config, {
|
|
118
|
-
...babelConfig,
|
|
119
|
-
sourceMaps,
|
|
120
|
-
});
|
|
121
|
-
} else {
|
|
122
|
-
distSet = {
|
|
123
|
-
code: 1,
|
|
124
|
-
message: 'no compiler',
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return distSet;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const generatorRealFiles = (virtualDists: IVirtualDist[]) => {
|
|
132
|
-
for (const virtualDist of virtualDists) {
|
|
133
|
-
const { distPath, code, sourcemap, sourceMapPath } = virtualDist;
|
|
134
|
-
fs.ensureFileSync(distPath);
|
|
135
|
-
fs.writeFileSync(distPath, code);
|
|
136
|
-
if (sourcemap.length > 0) {
|
|
137
|
-
fs.ensureFileSync(sourceMapPath);
|
|
138
|
-
fs.writeFileSync(sourceMapPath, sourcemap);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
interface ITaskConfig {
|
|
144
|
-
srcRootDir: string; // 源码的根目录
|
|
145
|
-
willCompilerDirOrFile: string; // 用于编译的源码文件或者源码目录
|
|
146
|
-
distDir: string;
|
|
147
|
-
appDirectory: string;
|
|
148
|
-
sourceMaps: boolean;
|
|
149
|
-
syntax: 'es5' | 'es6+';
|
|
150
|
-
type: 'module' | 'commonjs';
|
|
151
|
-
tsconfigPath: string;
|
|
152
|
-
copyDirs?: string;
|
|
153
|
-
compiler?: 'babel' | 'esbuild' | 'swc';
|
|
154
|
-
watch: boolean;
|
|
155
|
-
}
|
|
156
|
-
const defaultConfig: ITaskConfig = {
|
|
157
|
-
srcRootDir: `${process.cwd()}/src`,
|
|
158
|
-
willCompilerDirOrFile: `${process.cwd()}/src`,
|
|
159
|
-
distDir: './dist/js/temp',
|
|
160
|
-
compiler: 'babel',
|
|
161
|
-
appDirectory: '',
|
|
162
|
-
sourceMaps: false,
|
|
163
|
-
tsconfigPath: '',
|
|
164
|
-
syntax: 'es5',
|
|
165
|
-
type: 'module',
|
|
166
|
-
watch: false,
|
|
167
|
-
};
|
|
168
|
-
const taskMain = async ({
|
|
169
|
-
modernConfig,
|
|
170
|
-
}: {
|
|
171
|
-
modernConfig: NormalizedConfig;
|
|
172
|
-
}) => {
|
|
173
|
-
// Execution of the script's parameter handling and related required configuration acquisition
|
|
174
|
-
const processArgv = argv(process.argv.slice(2));
|
|
175
|
-
const config = processArgv<ITaskConfig>(defaultConfig);
|
|
176
|
-
process.env.BUILD_FORMAT = initEnv(config);
|
|
177
|
-
const compiler = Compiler.babel; // Currently, only babel is supported.
|
|
178
|
-
const babelConfig = bc.resolveBabelConfig(config.appDirectory, modernConfig, {
|
|
179
|
-
sourceAbsDir: config.srcRootDir,
|
|
180
|
-
tsconfigPath: config.tsconfigPath,
|
|
181
|
-
syntax: config.syntax,
|
|
182
|
-
type: config.type,
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const {
|
|
186
|
-
code,
|
|
187
|
-
message,
|
|
188
|
-
messageDetails,
|
|
189
|
-
virtualDists = [],
|
|
190
|
-
} = await buildSourceCode({
|
|
191
|
-
distDir: config.distDir,
|
|
192
|
-
srcRootDir: config.srcRootDir,
|
|
193
|
-
willCompilerDirOrFile: config.willCompilerDirOrFile,
|
|
194
|
-
sourceMaps: config.sourceMaps,
|
|
195
|
-
compiler,
|
|
196
|
-
projectData: { appDirectory: config.appDirectory },
|
|
197
|
-
tsconfigPath: config.tsconfigPath,
|
|
198
|
-
babelConfig,
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
if (code === 0) {
|
|
202
|
-
generatorRealFiles(virtualDists);
|
|
203
|
-
// 执行成功log使用 console.info
|
|
204
|
-
console.info('[Babel Compiler]: Successfully');
|
|
205
|
-
} else if (messageDetails && messageDetails.length > 0) {
|
|
206
|
-
console.error(message);
|
|
207
|
-
for (const detail of messageDetails || []) {
|
|
208
|
-
console.error(detail.content);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (code === 0 && config.copyDirs) {
|
|
213
|
-
const copyList = config.copyDirs.split(',');
|
|
214
|
-
for (const copyDir of copyList) {
|
|
215
|
-
fs.ensureDirSync(copyDir);
|
|
216
|
-
fs.copySync(config.distDir, copyDir);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
(async () => {
|
|
222
|
-
let options: CoreOptions | undefined;
|
|
223
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
224
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
225
|
-
}
|
|
226
|
-
const { resolved } = await core.cli.init([], options);
|
|
227
|
-
await core.manager.run(async () => {
|
|
228
|
-
try {
|
|
229
|
-
await taskMain({ modernConfig: resolved });
|
|
230
|
-
} catch (e) {
|
|
231
|
-
console.error(e);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
})();
|
package/src/tasks/build-style.ts
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { fs, Import } from '@modern-js/utils';
|
|
3
|
-
import type {
|
|
4
|
-
NormalizedConfig,
|
|
5
|
-
IAppContext,
|
|
6
|
-
CoreOptions,
|
|
7
|
-
} from '@modern-js/core';
|
|
8
|
-
import type { ICompilerResult, PostcssOption } from '@modern-js/style-compiler';
|
|
9
|
-
import type { ModuleToolsOutput } from '../types';
|
|
10
|
-
|
|
11
|
-
const cssConfig: typeof import('@modern-js/css-config') = Import.lazy(
|
|
12
|
-
'@modern-js/css-config',
|
|
13
|
-
require,
|
|
14
|
-
);
|
|
15
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
16
|
-
'@modern-js/core',
|
|
17
|
-
require,
|
|
18
|
-
);
|
|
19
|
-
const compiler: typeof import('@modern-js/style-compiler') = Import.lazy(
|
|
20
|
-
'@modern-js/style-compiler',
|
|
21
|
-
require,
|
|
22
|
-
);
|
|
23
|
-
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
24
|
-
const hooks: typeof import('@modern-js/module-tools-hooks') = Import.lazy(
|
|
25
|
-
'@modern-js/module-tools-hooks',
|
|
26
|
-
require,
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
const STYLE_DIRS = 'styles';
|
|
30
|
-
const SRC_STYLE_DIRS = 'src';
|
|
31
|
-
|
|
32
|
-
const checkStylesDirExist = (option: { appDirectory: string }) => {
|
|
33
|
-
const { appDirectory } = option;
|
|
34
|
-
return fs.existsSync(path.join(appDirectory, STYLE_DIRS));
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const generatorFileOrLogError = (
|
|
38
|
-
result: ICompilerResult,
|
|
39
|
-
successMessage = '',
|
|
40
|
-
) => {
|
|
41
|
-
if (result.code === 0 && result.dists.length > 0) {
|
|
42
|
-
for (const file of result.dists) {
|
|
43
|
-
fs.ensureFileSync(file.filename);
|
|
44
|
-
fs.writeFileSync(file.filename, file.content);
|
|
45
|
-
}
|
|
46
|
-
if (successMessage) {
|
|
47
|
-
console.info(successMessage);
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
for (const file of result.errors) {
|
|
51
|
-
console.error(file.error);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const getPostcssOption = (
|
|
57
|
-
appDirectory: string,
|
|
58
|
-
modernConfig: NormalizedConfig,
|
|
59
|
-
): PostcssOption => {
|
|
60
|
-
const postcssOption = cssConfig.getPostcssConfig(
|
|
61
|
-
appDirectory,
|
|
62
|
-
modernConfig,
|
|
63
|
-
false,
|
|
64
|
-
);
|
|
65
|
-
return {
|
|
66
|
-
plugins: postcssOption?.postcssOptions?.plugins || [],
|
|
67
|
-
enableSourceMap: (postcssOption as any)?.sourceMap || false,
|
|
68
|
-
options: {},
|
|
69
|
-
} as any;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const copyOriginStyleFiles = ({
|
|
73
|
-
targetDir,
|
|
74
|
-
outputDir,
|
|
75
|
-
}: {
|
|
76
|
-
targetDir: string;
|
|
77
|
-
outputDir: string;
|
|
78
|
-
}) => {
|
|
79
|
-
const styleFiles = glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
|
|
80
|
-
if (styleFiles.length > 0) {
|
|
81
|
-
fs.ensureDirSync(outputDir);
|
|
82
|
-
}
|
|
83
|
-
for (const styleFile of styleFiles) {
|
|
84
|
-
const file = path.relative(targetDir, styleFile);
|
|
85
|
-
fs.ensureDirSync(path.dirname(path.join(outputDir, file)));
|
|
86
|
-
fs.copyFileSync(styleFile, path.join(outputDir, file));
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const taskMain = async ({
|
|
91
|
-
modernConfig,
|
|
92
|
-
appContext,
|
|
93
|
-
}: {
|
|
94
|
-
modernConfig: NormalizedConfig;
|
|
95
|
-
appContext: IAppContext;
|
|
96
|
-
}) => {
|
|
97
|
-
const {
|
|
98
|
-
assetsPath = 'styles',
|
|
99
|
-
path: outputPath = 'dist',
|
|
100
|
-
jsPath = 'js',
|
|
101
|
-
} = modernConfig.output as ModuleToolsOutput;
|
|
102
|
-
const { appDirectory } = appContext;
|
|
103
|
-
|
|
104
|
-
const lessOption = await core
|
|
105
|
-
.mountHook()
|
|
106
|
-
.moduleLessConfig(
|
|
107
|
-
{ modernConfig },
|
|
108
|
-
{ onLast: async (_: any) => undefined },
|
|
109
|
-
);
|
|
110
|
-
const sassOption = await core
|
|
111
|
-
.mountHook()
|
|
112
|
-
.moduleSassConfig(
|
|
113
|
-
{ modernConfig },
|
|
114
|
-
{ onLast: async (_: any) => undefined },
|
|
115
|
-
);
|
|
116
|
-
const tailwindPlugin = await core
|
|
117
|
-
.mountHook()
|
|
118
|
-
.moduleTailwindConfig(
|
|
119
|
-
{ modernConfig },
|
|
120
|
-
{ onLast: async (_: any) => undefined },
|
|
121
|
-
);
|
|
122
|
-
const postcssOption = getPostcssOption(appDirectory, modernConfig);
|
|
123
|
-
if (tailwindPlugin) {
|
|
124
|
-
postcssOption.plugins?.push(tailwindPlugin);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const { importStyle } = modernConfig.output as ModuleToolsOutput;
|
|
128
|
-
const existStylesDir = checkStylesDirExist({ appDirectory });
|
|
129
|
-
// 编译 styles 目录样式
|
|
130
|
-
if (existStylesDir) {
|
|
131
|
-
const styleResult = await compiler.styleCompiler({
|
|
132
|
-
projectDir: appDirectory,
|
|
133
|
-
stylesDir: path.resolve(appDirectory, STYLE_DIRS),
|
|
134
|
-
outDir: path.join(appDirectory, outputPath, assetsPath),
|
|
135
|
-
enableVirtualDist: true,
|
|
136
|
-
compilerOption: {
|
|
137
|
-
less: lessOption,
|
|
138
|
-
sass: sassOption,
|
|
139
|
-
postcss: postcssOption,
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
generatorFileOrLogError(
|
|
143
|
-
styleResult!,
|
|
144
|
-
`[Style Compiler] Successfully for 'styles' dir`,
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// 编译 src 内的样式代码
|
|
149
|
-
const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
|
|
150
|
-
const outputDirtoSrc = path.join(
|
|
151
|
-
appDirectory,
|
|
152
|
-
outputPath,
|
|
153
|
-
jsPath,
|
|
154
|
-
assetsPath,
|
|
155
|
-
);
|
|
156
|
-
if (importStyle === 'compiled-code') {
|
|
157
|
-
const srcStyleResult = await compiler.styleCompiler({
|
|
158
|
-
projectDir: appDirectory,
|
|
159
|
-
stylesDir: srcDir,
|
|
160
|
-
outDir: outputDirtoSrc,
|
|
161
|
-
enableVirtualDist: true,
|
|
162
|
-
compilerOption: {
|
|
163
|
-
less: lessOption,
|
|
164
|
-
sass: sassOption,
|
|
165
|
-
postcss: postcssOption,
|
|
166
|
-
},
|
|
167
|
-
});
|
|
168
|
-
generatorFileOrLogError(
|
|
169
|
-
srcStyleResult!,
|
|
170
|
-
`[Style Compiler] Successfully for 'src' dir`,
|
|
171
|
-
);
|
|
172
|
-
} else {
|
|
173
|
-
copyOriginStyleFiles({ targetDir: srcDir, outputDir: outputDirtoSrc });
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
(async () => {
|
|
178
|
-
let options: CoreOptions | undefined;
|
|
179
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
180
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
181
|
-
}
|
|
182
|
-
hooks.buildLifeCycle();
|
|
183
|
-
const { resolved: modernConfig, appContext } = await core.cli.init(
|
|
184
|
-
[],
|
|
185
|
-
options,
|
|
186
|
-
);
|
|
187
|
-
await core.manager.run(async () => {
|
|
188
|
-
try {
|
|
189
|
-
await taskMain({ modernConfig, appContext });
|
|
190
|
-
} catch (e: any) {
|
|
191
|
-
console.error(e.toString());
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
})();
|