@modern-js/server-utils 1.15.1-beta.0 → 1.15.1-beta.1

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.
@@ -1,26 +1,32 @@
1
1
  import * as path from 'path';
2
2
  import { fs } from '@modern-js/utils';
3
- import recursive from 'recursive-readdir';
4
3
  import { compileByTs } from "../compilers/typescript";
5
4
  import { compileByBabel } from "../compilers/babel";
6
5
  export const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
7
- export const TS_CONFIG_FILENAME = 'tsconfig.json';
6
+
7
+ const validateAbsolutePath = (filename, message) => {
8
+ if (!path.isAbsolute(filename)) {
9
+ throw new Error(message);
10
+ }
11
+ };
12
+
13
+ const validateAbsolutePaths = (filenames, messageFunc) => {
14
+ filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
15
+ };
16
+
8
17
  export const compile = async (appDirectory, modernConfig, compileOptions) => {
9
- const tsConifgPath = path.join(appDirectory, TS_CONFIG_FILENAME);
10
- const isTsProject = await fs.pathExists(tsConifgPath);
18
+ const {
19
+ sourceDirs,
20
+ distDir,
21
+ tsconfigPath
22
+ } = compileOptions;
23
+ validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
24
+ validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
25
+ const isTsProject = tsconfigPath && (await fs.pathExists(tsconfigPath));
11
26
 
12
27
  if (isTsProject) {
13
28
  await compileByTs(appDirectory, modernConfig, compileOptions);
14
29
  } else {
15
30
  await compileByBabel(appDirectory, modernConfig, compileOptions);
16
31
  }
17
- };
18
- export const recursiveReadTsFiles = async dirname => {
19
- const ignoreFunc = filename => {
20
- const extname = path.extname(filename);
21
- return !['.ts', '.d.ts'].includes(extname);
22
- };
23
-
24
- const files = await recursive(dirname, [ignoreFunc]);
25
- return files;
26
32
  };
@@ -86,32 +86,29 @@ export const resolveBabelConfig = (appDirectory, modernConfig, option // FIXME:
86
86
  };
87
87
  export const compileByBabel = async (appDirectory, modernConfig, compileOptions) => {
88
88
  const {
89
- patterns
89
+ sourceDirs,
90
+ distDir,
91
+ tsconfigPath
90
92
  } = compileOptions;
91
- const results = await Promise.all(patterns.map(async pattern => {
92
- const {
93
- from,
94
- to,
95
- tsconfigPath
96
- } = pattern;
93
+ const results = await Promise.all(sourceDirs.map(async sourceDir => {
97
94
  const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
98
95
  tsconfigPath: tsconfigPath ? tsconfigPath : '',
99
96
  syntax: 'es6+',
100
97
  type: 'commonjs'
101
98
  });
102
99
 
103
- if (await fs.pathExists(from)) {
104
- const basename = path.basename(from);
105
- const targetDir = path.join(to, basename);
106
- await fs.copy(from, targetDir, {
100
+ if (await fs.pathExists(sourceDir)) {
101
+ const basename = path.basename(sourceDir);
102
+ const targetDir = path.join(distDir, basename);
103
+ await fs.copy(sourceDir, targetDir, {
107
104
  filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
108
105
  });
109
106
  }
110
107
 
111
108
  return compiler({
112
109
  rootDir: appDirectory,
113
- distDir: to,
114
- sourceDir: from,
110
+ distDir: distDir,
111
+ sourceDir: sourceDir,
115
112
  extensions: FILE_EXTENSIONS
116
113
  }, babelConfig);
117
114
  }));
@@ -4,11 +4,8 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
4
4
 
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
7
- // from to tsconfigpath
8
- // root dir
9
7
  import path from 'path';
10
8
  import { logger, getAlias, fs } from '@modern-js/utils';
11
- import { TS_CONFIG_FILENAME } from "../../common";
12
9
  import { TypescriptLoader } from "./typescript-loader";
13
10
  import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
14
11
  import ts from 'typescript';
@@ -35,68 +32,63 @@ const copyFiles = async (from, to, tsconfigPath) => {
35
32
  filter: src => !['.ts'].includes(path.extname(src)) && src !== tsconfigPath
36
33
  });
37
34
  }
38
- }; // TODO: 分离特殊的业务逻辑,通用化
39
-
35
+ };
40
36
 
41
37
  export const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
42
38
  logger.info(`Running ts compile...`);
43
- const rootTsconfigPath = path.join(appDirectory, TS_CONFIG_FILENAME);
44
39
  const {
45
- patterns
40
+ sourceDirs,
41
+ distDir,
42
+ tsconfigPath
46
43
  } = compileOptions;
44
+ if (!tsconfigPath) return;
47
45
  const ts = new TypescriptLoader({
48
46
  appDirectory
49
47
  }).load();
50
48
  const createProgram = ts.createIncrementalProgram || ts.createProgram;
51
49
  const formatHost = getFormatHost(ts);
52
-
53
- for (const pattern of patterns) {
54
- const {
55
- tsconfigPath = rootTsconfigPath,
56
- from,
57
- to
58
- } = pattern;
59
- const basename = path.basename(from);
60
- const targetDir = path.join(to, basename);
61
- logger.info(`Compile ${from}`);
62
- const {
63
- alias
64
- } = modernConfig.source;
65
- const aliasOption = getAlias(alias || {}, {
66
- appDirectory,
67
- tsconfigPath
68
- });
69
- const {
70
- paths = {},
71
- absoluteBaseUrl = './'
72
- } = aliasOption;
73
- const {
74
- options,
75
- fileNames,
76
- projectReferences
77
- } = readTsConfigByFile(tsconfigPath);
78
- const rootNames = fileNames.filter(fileName => {
79
- return fileName.endsWith('.d.ts') || fileName.includes(from);
80
- });
81
- let program = createProgram.call(ts, {
82
- rootNames: rootNames,
83
- projectReferences,
84
- options: _objectSpread(_objectSpread({}, options), {}, {
85
- outDir: targetDir
86
- })
87
- });
88
- const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
89
- const emitResult = program.emit(undefined, undefined, undefined, undefined, {
90
- before: [tsconfigPathsPlugin]
50
+ const {
51
+ alias
52
+ } = modernConfig.source;
53
+ const aliasOption = getAlias(alias || {}, {
54
+ appDirectory,
55
+ tsconfigPath
56
+ });
57
+ const {
58
+ paths = {},
59
+ absoluteBaseUrl = './'
60
+ } = aliasOption;
61
+ const {
62
+ options,
63
+ fileNames,
64
+ projectReferences
65
+ } = readTsConfigByFile(tsconfigPath);
66
+ const rootNames = fileNames.filter(fileName => {
67
+ return fileName.endsWith('.d.ts') || sourceDirs.some(sourceDir => {
68
+ return fileName.includes(sourceDir);
91
69
  });
92
- const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
70
+ });
71
+ const targetDir = path.join(appDirectory, './dist');
72
+ let program = createProgram.call(ts, {
73
+ rootNames: rootNames,
74
+ projectReferences,
75
+ options: _objectSpread(_objectSpread({}, options), {}, {
76
+ outDir: targetDir
77
+ })
78
+ });
79
+ const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
80
+ const emitResult = program.emit(undefined, undefined, undefined, undefined, {
81
+ before: [tsconfigPathsPlugin]
82
+ });
83
+ const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
93
84
 
94
- if (allDiagnostics.length > 0) {
95
- logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
96
- process.exit(1);
97
- }
85
+ if (allDiagnostics.length > 0) {
86
+ logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
87
+ process.exit(1);
88
+ }
98
89
 
99
- await copyFiles(from, to, tsconfigPath);
90
+ for (const source of sourceDirs) {
91
+ await copyFiles(source, distDir, tsconfigPath);
100
92
  }
101
93
  };
102
94
 
@@ -3,32 +3,42 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.recursiveReadTsFiles = exports.compile = exports.TS_CONFIG_FILENAME = exports.FILE_EXTENSIONS = void 0;
6
+ exports.compile = exports.FILE_EXTENSIONS = void 0;
7
7
 
8
8
  var path = _interopRequireWildcard(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- var _recursiveReaddir = _interopRequireDefault(require("recursive-readdir"));
13
-
14
12
  var _typescript = require("../compilers/typescript");
15
13
 
16
14
  var _babel = require("../compilers/babel");
17
15
 
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
16
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21
17
 
22
18
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
19
 
24
20
  const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
25
21
  exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
26
- const TS_CONFIG_FILENAME = 'tsconfig.json';
27
- exports.TS_CONFIG_FILENAME = TS_CONFIG_FILENAME;
22
+
23
+ const validateAbsolutePath = (filename, message) => {
24
+ if (!path.isAbsolute(filename)) {
25
+ throw new Error(message);
26
+ }
27
+ };
28
+
29
+ const validateAbsolutePaths = (filenames, messageFunc) => {
30
+ filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
31
+ };
28
32
 
29
33
  const compile = async (appDirectory, modernConfig, compileOptions) => {
30
- const tsConifgPath = path.join(appDirectory, TS_CONFIG_FILENAME);
31
- const isTsProject = await _utils.fs.pathExists(tsConifgPath);
34
+ const {
35
+ sourceDirs,
36
+ distDir,
37
+ tsconfigPath
38
+ } = compileOptions;
39
+ validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
40
+ validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
41
+ const isTsProject = tsconfigPath && (await _utils.fs.pathExists(tsconfigPath));
32
42
 
33
43
  if (isTsProject) {
34
44
  await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
@@ -37,16 +47,4 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
37
47
  }
38
48
  };
39
49
 
40
- exports.compile = compile;
41
-
42
- const recursiveReadTsFiles = async dirname => {
43
- const ignoreFunc = filename => {
44
- const extname = path.extname(filename);
45
- return !['.ts', '.d.ts'].includes(extname);
46
- };
47
-
48
- const files = await (0, _recursiveReaddir.default)(dirname, [ignoreFunc]);
49
- return files;
50
- };
51
-
52
- exports.recursiveReadTsFiles = recursiveReadTsFiles;
50
+ exports.compile = compile;
@@ -135,32 +135,29 @@ exports.resolveBabelConfig = resolveBabelConfig;
135
135
 
136
136
  const compileByBabel = async (appDirectory, modernConfig, compileOptions) => {
137
137
  const {
138
- patterns
138
+ sourceDirs,
139
+ distDir,
140
+ tsconfigPath
139
141
  } = compileOptions;
140
- const results = await Promise.all(patterns.map(async pattern => {
141
- const {
142
- from,
143
- to,
144
- tsconfigPath
145
- } = pattern;
142
+ const results = await Promise.all(sourceDirs.map(async sourceDir => {
146
143
  const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
147
144
  tsconfigPath: tsconfigPath ? tsconfigPath : '',
148
145
  syntax: 'es6+',
149
146
  type: 'commonjs'
150
147
  });
151
148
 
152
- if (await _utils.fs.pathExists(from)) {
153
- const basename = path.basename(from);
154
- const targetDir = path.join(to, basename);
155
- await _utils.fs.copy(from, targetDir, {
149
+ if (await _utils.fs.pathExists(sourceDir)) {
150
+ const basename = path.basename(sourceDir);
151
+ const targetDir = path.join(distDir, basename);
152
+ await _utils.fs.copy(sourceDir, targetDir, {
156
153
  filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
157
154
  });
158
155
  }
159
156
 
160
157
  return (0, _babelCompiler.compiler)({
161
158
  rootDir: appDirectory,
162
- distDir: to,
163
- sourceDir: from,
159
+ distDir: distDir,
160
+ sourceDir: sourceDir,
164
161
  extensions: _common.FILE_EXTENSIONS
165
162
  }, babelConfig);
166
163
  }));
@@ -9,8 +9,6 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- var _common = require("../../common");
13
-
14
12
  var _typescriptLoader = require("./typescript-loader");
15
13
 
16
14
  var _tsconfigPathsPlugin = require("./tsconfig-paths-plugin");
@@ -50,75 +48,67 @@ const copyFiles = async (from, to, tsconfigPath) => {
50
48
  filter: src => !['.ts'].includes(_path.default.extname(src)) && src !== tsconfigPath
51
49
  });
52
50
  }
53
- }; // TODO: 分离特殊的业务逻辑,通用化
54
-
51
+ };
55
52
 
56
53
  const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
57
54
  _utils.logger.info(`Running ts compile...`);
58
55
 
59
- const rootTsconfigPath = _path.default.join(appDirectory, _common.TS_CONFIG_FILENAME);
60
-
61
56
  const {
62
- patterns
57
+ sourceDirs,
58
+ distDir,
59
+ tsconfigPath
63
60
  } = compileOptions;
61
+ if (!tsconfigPath) return;
64
62
  const ts = new _typescriptLoader.TypescriptLoader({
65
63
  appDirectory
66
64
  }).load();
67
65
  const createProgram = ts.createIncrementalProgram || ts.createProgram;
68
66
  const formatHost = getFormatHost(ts);
69
-
70
- for (const pattern of patterns) {
71
- const {
72
- tsconfigPath = rootTsconfigPath,
73
- from,
74
- to
75
- } = pattern;
76
-
77
- const basename = _path.default.basename(from);
78
-
79
- const targetDir = _path.default.join(to, basename);
80
-
81
- _utils.logger.info(`Compile ${from}`);
82
-
83
- const {
84
- alias
85
- } = modernConfig.source;
86
- const aliasOption = (0, _utils.getAlias)(alias || {}, {
87
- appDirectory,
88
- tsconfigPath
89
- });
90
- const {
91
- paths = {},
92
- absoluteBaseUrl = './'
93
- } = aliasOption;
94
- const {
95
- options,
96
- fileNames,
97
- projectReferences
98
- } = readTsConfigByFile(tsconfigPath);
99
- const rootNames = fileNames.filter(fileName => {
100
- return fileName.endsWith('.d.ts') || fileName.includes(from);
101
- });
102
- let program = createProgram.call(ts, {
103
- rootNames: rootNames,
104
- projectReferences,
105
- options: _objectSpread(_objectSpread({}, options), {}, {
106
- outDir: targetDir
107
- })
108
- });
109
- const tsconfigPathsPlugin = (0, _tsconfigPathsPlugin.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
110
- const emitResult = program.emit(undefined, undefined, undefined, undefined, {
111
- before: [tsconfigPathsPlugin]
67
+ const {
68
+ alias
69
+ } = modernConfig.source;
70
+ const aliasOption = (0, _utils.getAlias)(alias || {}, {
71
+ appDirectory,
72
+ tsconfigPath
73
+ });
74
+ const {
75
+ paths = {},
76
+ absoluteBaseUrl = './'
77
+ } = aliasOption;
78
+ const {
79
+ options,
80
+ fileNames,
81
+ projectReferences
82
+ } = readTsConfigByFile(tsconfigPath);
83
+ const rootNames = fileNames.filter(fileName => {
84
+ return fileName.endsWith('.d.ts') || sourceDirs.some(sourceDir => {
85
+ return fileName.includes(sourceDir);
112
86
  });
113
- const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
114
-
115
- if (allDiagnostics.length > 0) {
116
- _utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
117
-
118
- process.exit(1);
119
- }
87
+ });
88
+
89
+ const targetDir = _path.default.join(appDirectory, './dist');
90
+
91
+ let program = createProgram.call(ts, {
92
+ rootNames: rootNames,
93
+ projectReferences,
94
+ options: _objectSpread(_objectSpread({}, options), {}, {
95
+ outDir: targetDir
96
+ })
97
+ });
98
+ const tsconfigPathsPlugin = (0, _tsconfigPathsPlugin.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
99
+ const emitResult = program.emit(undefined, undefined, undefined, undefined, {
100
+ before: [tsconfigPathsPlugin]
101
+ });
102
+ const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
103
+
104
+ if (allDiagnostics.length > 0) {
105
+ _utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
106
+
107
+ process.exit(1);
108
+ }
120
109
 
121
- await copyFiles(from, to, tsconfigPath);
110
+ for (const source of sourceDirs) {
111
+ await copyFiles(source, distDir, tsconfigPath);
122
112
  }
123
113
  };
124
114
 
@@ -2,42 +2,67 @@ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
3
  import * as path from 'path';
4
4
  import { fs } from '@modern-js/utils';
5
- import recursive from 'recursive-readdir';
6
5
  import { compileByTs } from "../compilers/typescript";
7
6
  import { compileByBabel } from "../compilers/babel";
8
7
  export var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
9
- export var TS_CONFIG_FILENAME = 'tsconfig.json';
8
+
9
+ var validateAbsolutePath = function validateAbsolutePath(filename, message) {
10
+ if (!path.isAbsolute(filename)) {
11
+ throw new Error(message);
12
+ }
13
+ };
14
+
15
+ var validateAbsolutePaths = function validateAbsolutePaths(filenames, messageFunc) {
16
+ filenames.forEach(function (filename) {
17
+ return validateAbsolutePath(filename, messageFunc(filename));
18
+ });
19
+ };
20
+
10
21
  export var compile = /*#__PURE__*/function () {
11
22
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(appDirectory, modernConfig, compileOptions) {
12
- var tsConifgPath, isTsProject;
23
+ var sourceDirs, distDir, tsconfigPath, isTsProject;
13
24
  return _regeneratorRuntime().wrap(function _callee$(_context) {
14
25
  while (1) {
15
26
  switch (_context.prev = _context.next) {
16
27
  case 0:
17
- tsConifgPath = path.join(appDirectory, TS_CONFIG_FILENAME);
18
- _context.next = 3;
19
- return fs.pathExists(tsConifgPath);
28
+ sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
29
+ validateAbsolutePaths(sourceDirs, function (dir) {
30
+ return "source dir ".concat(dir, " is not an absolute path.");
31
+ });
32
+ validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
33
+ _context.t0 = tsconfigPath;
34
+
35
+ if (!_context.t0) {
36
+ _context.next = 8;
37
+ break;
38
+ }
20
39
 
21
- case 3:
22
- isTsProject = _context.sent;
40
+ _context.next = 7;
41
+ return fs.pathExists(tsconfigPath);
42
+
43
+ case 7:
44
+ _context.t0 = _context.sent;
45
+
46
+ case 8:
47
+ isTsProject = _context.t0;
23
48
 
24
49
  if (!isTsProject) {
25
- _context.next = 9;
50
+ _context.next = 14;
26
51
  break;
27
52
  }
28
53
 
29
- _context.next = 7;
54
+ _context.next = 12;
30
55
  return compileByTs(appDirectory, modernConfig, compileOptions);
31
56
 
32
- case 7:
33
- _context.next = 11;
57
+ case 12:
58
+ _context.next = 16;
34
59
  break;
35
60
 
36
- case 9:
37
- _context.next = 11;
61
+ case 14:
62
+ _context.next = 16;
38
63
  return compileByBabel(appDirectory, modernConfig, compileOptions);
39
64
 
40
- case 11:
65
+ case 16:
41
66
  case "end":
42
67
  return _context.stop();
43
68
  }
@@ -48,35 +73,4 @@ export var compile = /*#__PURE__*/function () {
48
73
  return function compile(_x, _x2, _x3) {
49
74
  return _ref.apply(this, arguments);
50
75
  };
51
- }();
52
- export var recursiveReadTsFiles = /*#__PURE__*/function () {
53
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(dirname) {
54
- var ignoreFunc, files;
55
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
56
- while (1) {
57
- switch (_context2.prev = _context2.next) {
58
- case 0:
59
- ignoreFunc = function ignoreFunc(filename) {
60
- var extname = path.extname(filename);
61
- return !['.ts', '.d.ts'].includes(extname);
62
- };
63
-
64
- _context2.next = 3;
65
- return recursive(dirname, [ignoreFunc]);
66
-
67
- case 3:
68
- files = _context2.sent;
69
- return _context2.abrupt("return", files);
70
-
71
- case 5:
72
- case "end":
73
- return _context2.stop();
74
- }
75
- }
76
- }, _callee2);
77
- }));
78
-
79
- return function recursiveReadTsFiles(_x4) {
80
- return _ref2.apply(this, arguments);
81
- };
82
76
  }();
@@ -79,53 +79,52 @@ export var resolveBabelConfig = function resolveBabelConfig(appDirectory, modern
79
79
  };
80
80
  export var compileByBabel = /*#__PURE__*/function () {
81
81
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
82
- var patterns, results;
82
+ var sourceDirs, distDir, tsconfigPath, results;
83
83
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
84
84
  while (1) {
85
85
  switch (_context2.prev = _context2.next) {
86
86
  case 0:
87
- patterns = compileOptions.patterns;
87
+ sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
88
88
  _context2.next = 3;
89
- return Promise.all(patterns.map( /*#__PURE__*/function () {
90
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern) {
91
- var from, to, tsconfigPath, babelConfig, basename, targetDir;
89
+ return Promise.all(sourceDirs.map( /*#__PURE__*/function () {
90
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(sourceDir) {
91
+ var babelConfig, basename, targetDir;
92
92
  return _regeneratorRuntime().wrap(function _callee$(_context) {
93
93
  while (1) {
94
94
  switch (_context.prev = _context.next) {
95
95
  case 0:
96
- from = pattern.from, to = pattern.to, tsconfigPath = pattern.tsconfigPath;
97
96
  babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
98
97
  tsconfigPath: tsconfigPath ? tsconfigPath : '',
99
98
  syntax: 'es6+',
100
99
  type: 'commonjs'
101
100
  });
102
- _context.next = 4;
103
- return fs.pathExists(from);
101
+ _context.next = 3;
102
+ return fs.pathExists(sourceDir);
104
103
 
105
- case 4:
104
+ case 3:
106
105
  if (!_context.sent) {
107
- _context.next = 9;
106
+ _context.next = 8;
108
107
  break;
109
108
  }
110
109
 
111
- basename = path.basename(from);
112
- targetDir = path.join(to, basename);
113
- _context.next = 9;
114
- return fs.copy(from, targetDir, {
110
+ basename = path.basename(sourceDir);
111
+ targetDir = path.join(distDir, basename);
112
+ _context.next = 8;
113
+ return fs.copy(sourceDir, targetDir, {
115
114
  filter: function filter(src) {
116
115
  return !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath;
117
116
  }
118
117
  });
119
118
 
120
- case 9:
119
+ case 8:
121
120
  return _context.abrupt("return", compiler({
122
121
  rootDir: appDirectory,
123
- distDir: to,
124
- sourceDir: from,
122
+ distDir: distDir,
123
+ sourceDir: sourceDir,
125
124
  extensions: FILE_EXTENSIONS
126
125
  }, babelConfig));
127
126
 
128
- case 10:
127
+ case 9:
129
128
  case "end":
130
129
  return _context.stop();
131
130
  }
@@ -1,12 +1,9 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
1
  import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
3
  import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
4
4
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
- // from to tsconfigpath
6
- // root dir
7
5
  import path from 'path';
8
6
  import { logger, getAlias, fs } from '@modern-js/utils';
9
- import { TS_CONFIG_FILENAME } from "../../common";
10
7
  import { TypescriptLoader } from "./typescript-loader";
11
8
  import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
12
9
  import ts from 'typescript';
@@ -60,115 +57,105 @@ var copyFiles = /*#__PURE__*/function () {
60
57
  return function copyFiles(_x, _x2, _x3) {
61
58
  return _ref2.apply(this, arguments);
62
59
  };
63
- }(); // TODO: 分离特殊的业务逻辑,通用化
64
-
60
+ }();
65
61
 
66
62
  export var compileByTs = /*#__PURE__*/function () {
67
63
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
68
- var rootTsconfigPath, patterns, ts, createProgram, formatHost, _iterator, _step, _loop;
64
+ var sourceDirs, distDir, tsconfigPath, ts, createProgram, formatHost, alias, aliasOption, _aliasOption$paths, paths, _aliasOption$absolute, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, rootNames, targetDir, program, tsconfigPathsPlugin, emitResult, allDiagnostics, _iterator, _step, source;
69
65
 
70
- return _regeneratorRuntime().wrap(function _callee2$(_context3) {
66
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
71
67
  while (1) {
72
- switch (_context3.prev = _context3.next) {
68
+ switch (_context2.prev = _context2.next) {
73
69
  case 0:
74
70
  logger.info("Running ts compile...");
75
- rootTsconfigPath = path.join(appDirectory, TS_CONFIG_FILENAME);
76
- patterns = compileOptions.patterns;
71
+ sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
72
+
73
+ if (tsconfigPath) {
74
+ _context2.next = 4;
75
+ break;
76
+ }
77
+
78
+ return _context2.abrupt("return");
79
+
80
+ case 4:
77
81
  ts = new TypescriptLoader({
78
82
  appDirectory: appDirectory
79
83
  }).load();
80
84
  createProgram = ts.createIncrementalProgram || ts.createProgram;
81
85
  formatHost = getFormatHost(ts);
82
- _iterator = _createForOfIteratorHelper(patterns);
83
- _context3.prev = 7;
84
- _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
85
- var pattern, _pattern$tsconfigPath, tsconfigPath, from, to, basename, targetDir, alias, aliasOption, _aliasOption$paths, paths, _aliasOption$absolute, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics;
86
-
87
- return _regeneratorRuntime().wrap(function _loop$(_context2) {
88
- while (1) {
89
- switch (_context2.prev = _context2.next) {
90
- case 0:
91
- pattern = _step.value;
92
- _pattern$tsconfigPath = pattern.tsconfigPath, tsconfigPath = _pattern$tsconfigPath === void 0 ? rootTsconfigPath : _pattern$tsconfigPath, from = pattern.from, to = pattern.to;
93
- basename = path.basename(from);
94
- targetDir = path.join(to, basename);
95
- logger.info("Compile ".concat(from));
96
- alias = modernConfig.source.alias;
97
- aliasOption = getAlias(alias || {}, {
98
- appDirectory: appDirectory,
99
- tsconfigPath: tsconfigPath
100
- });
101
- _aliasOption$paths = aliasOption.paths, paths = _aliasOption$paths === void 0 ? {} : _aliasOption$paths, _aliasOption$absolute = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption$absolute === void 0 ? './' : _aliasOption$absolute;
102
- _readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
103
- rootNames = fileNames.filter(function (fileName) {
104
- return fileName.endsWith('.d.ts') || fileName.includes(from);
105
- });
106
- program = createProgram.call(ts, {
107
- rootNames: rootNames,
108
- projectReferences: projectReferences,
109
- options: _objectSpread(_objectSpread({}, options), {}, {
110
- outDir: targetDir
111
- })
112
- });
113
- tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
114
- emitResult = program.emit(undefined, undefined, undefined, undefined, {
115
- before: [tsconfigPathsPlugin]
116
- });
117
- allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
118
-
119
- if (allDiagnostics.length > 0) {
120
- logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
121
- process.exit(1);
122
- }
123
-
124
- _context2.next = 17;
125
- return copyFiles(from, to, tsconfigPath);
126
-
127
- case 17:
128
- case "end":
129
- return _context2.stop();
130
- }
131
- }
132
- }, _loop);
86
+ alias = modernConfig.source.alias;
87
+ aliasOption = getAlias(alias || {}, {
88
+ appDirectory: appDirectory,
89
+ tsconfigPath: tsconfigPath
90
+ });
91
+ _aliasOption$paths = aliasOption.paths, paths = _aliasOption$paths === void 0 ? {} : _aliasOption$paths, _aliasOption$absolute = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption$absolute === void 0 ? './' : _aliasOption$absolute;
92
+ _readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
93
+ rootNames = fileNames.filter(function (fileName) {
94
+ return fileName.endsWith('.d.ts') || sourceDirs.some(function (sourceDir) {
95
+ return fileName.includes(sourceDir);
96
+ });
97
+ });
98
+ targetDir = path.join(appDirectory, './dist');
99
+ program = createProgram.call(ts, {
100
+ rootNames: rootNames,
101
+ projectReferences: projectReferences,
102
+ options: _objectSpread(_objectSpread({}, options), {}, {
103
+ outDir: targetDir
104
+ })
105
+ });
106
+ tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
107
+ emitResult = program.emit(undefined, undefined, undefined, undefined, {
108
+ before: [tsconfigPathsPlugin]
133
109
  });
110
+ allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
111
+
112
+ if (allDiagnostics.length > 0) {
113
+ logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
114
+ process.exit(1);
115
+ }
116
+
117
+ _iterator = _createForOfIteratorHelper(sourceDirs);
118
+ _context2.prev = 19;
134
119
 
135
120
  _iterator.s();
136
121
 
137
- case 10:
122
+ case 21:
138
123
  if ((_step = _iterator.n()).done) {
139
- _context3.next = 14;
124
+ _context2.next = 27;
140
125
  break;
141
126
  }
142
127
 
143
- return _context3.delegateYield(_loop(), "t0", 12);
128
+ source = _step.value;
129
+ _context2.next = 25;
130
+ return copyFiles(source, distDir, tsconfigPath);
144
131
 
145
- case 12:
146
- _context3.next = 10;
132
+ case 25:
133
+ _context2.next = 21;
147
134
  break;
148
135
 
149
- case 14:
150
- _context3.next = 19;
136
+ case 27:
137
+ _context2.next = 32;
151
138
  break;
152
139
 
153
- case 16:
154
- _context3.prev = 16;
155
- _context3.t1 = _context3["catch"](7);
140
+ case 29:
141
+ _context2.prev = 29;
142
+ _context2.t0 = _context2["catch"](19);
156
143
 
157
- _iterator.e(_context3.t1);
144
+ _iterator.e(_context2.t0);
158
145
 
159
- case 19:
160
- _context3.prev = 19;
146
+ case 32:
147
+ _context2.prev = 32;
161
148
 
162
149
  _iterator.f();
163
150
 
164
- return _context3.finish(19);
151
+ return _context2.finish(32);
165
152
 
166
- case 22:
153
+ case 35:
167
154
  case "end":
168
- return _context3.stop();
155
+ return _context2.stop();
169
156
  }
170
157
  }
171
- }, _callee2, null, [[7, 16, 19, 22]]);
158
+ }, _callee2, null, [[19, 29, 32, 35]]);
172
159
  }));
173
160
 
174
161
  return function compileByTs(_x4, _x5, _x6) {
@@ -5,10 +5,10 @@ export interface Pattern {
5
5
  tsconfigPath?: string;
6
6
  }
7
7
  export interface CompileOptions {
8
- patterns: Pattern[];
8
+ sourceDirs: string[];
9
+ distDir: string;
10
+ tsconfigPath?: string;
9
11
  }
10
12
  export declare type CompileFunc = (appDirectory: string, modernConfig: NormalizedConfig, compileOptions: CompileOptions) => Promise<void>;
11
13
  export declare const FILE_EXTENSIONS: string[];
12
- export declare const TS_CONFIG_FILENAME = "tsconfig.json";
13
- export declare const compile: CompileFunc;
14
- export declare const recursiveReadTsFiles: (dirname: string) => Promise<string[]>;
14
+ export declare const compile: CompileFunc;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.15.1-beta.0",
14
+ "version": "1.15.1-beta.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",