@modern-js/server-utils 2.0.0-beta.0 → 2.0.0-beta.2

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 CHANGED
@@ -1,5 +1,81 @@
1
1
  # @modern-js/server-utils
2
2
 
3
+ ## 2.0.0-beta.2
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9c3e: chore: v2
8
+
9
+ ### Patch Changes
10
+
11
+ - 9b915e0c10: fix: tsconfig-paths plugin's new node use old node flag
12
+ fix: tsconfig-paths 插件转换的新节点使用旧节点的 flag
13
+ - a8642da58f: fix(server-utils): incorrect babel-compiler version
14
+
15
+ fix(server-utils): 修复引用错误的 babel-compiler 版本的问题
16
+
17
+ - c2bb0f1: chore(server-utils): using pre-bundled tsconfig-paths
18
+
19
+ chore(server-utils): 使用预打包的 tsconfig-paths 依赖
20
+
21
+ - 14b712da84: fix: use consistent alias type and default value across packages
22
+
23
+ fix: 在各个包中使用一致的 alias 类型定义和默认值
24
+
25
+ - Updated dependencies [92f0ead]
26
+ - Updated dependencies [edd1cfb1af]
27
+ - Updated dependencies [cc971eabfc]
28
+ - Updated dependencies [5b9049f2e9]
29
+ - Updated dependencies [92004d1]
30
+ - Updated dependencies [b8bbe036c7]
31
+ - Updated dependencies [d5a31df781]
32
+ - Updated dependencies [dda38c9c3e]
33
+ - Updated dependencies [3bbea92b2a]
34
+ - Updated dependencies [f179749]
35
+ - Updated dependencies [abf3421a75]
36
+ - Updated dependencies [543be9558e]
37
+ - Updated dependencies [14b712da84]
38
+ - @modern-js/utils@2.0.0-beta.2
39
+ - @modern-js/plugin@2.0.0-beta.2
40
+ - @modern-js/babel-preset-lib@2.0.0-beta.2
41
+ - @modern-js/babel-compiler@2.0.0-beta.2
42
+
43
+ ## 2.0.0-beta.1
44
+
45
+ ### Major Changes
46
+
47
+ - dda38c9: chore: v2
48
+
49
+ ### Patch Changes
50
+
51
+ - 9b915e0c10: fix: tsconfig-paths plugin's new node use old node flag
52
+ fix: tsconfig-paths 插件转换的新节点使用旧节点的 flag
53
+ - a8642da: fix(server-utils): incorrect babel-compiler version
54
+
55
+ fix(server-utils): 修复引用错误的 babel-compiler 版本的问题
56
+
57
+ - 14b712d: fix: use consistent alias type and default value across packages
58
+
59
+ fix: 在各个包中使用一致的 alias 类型定义和默认值
60
+
61
+ - Updated dependencies [92f0ead]
62
+ - Updated dependencies [edd1cfb1af]
63
+ - Updated dependencies [cc971eabfc]
64
+ - Updated dependencies [5b9049f]
65
+ - Updated dependencies [92004d1]
66
+ - Updated dependencies [b8bbe036c7]
67
+ - Updated dependencies [d5a31df781]
68
+ - Updated dependencies [dda38c9]
69
+ - Updated dependencies [3bbea92b2a]
70
+ - Updated dependencies [f179749]
71
+ - Updated dependencies [abf3421]
72
+ - Updated dependencies [543be9558e]
73
+ - Updated dependencies [14b712d]
74
+ - @modern-js/utils@2.0.0-beta.1
75
+ - @modern-js/plugin@2.0.0-beta.1
76
+ - @modern-js/babel-preset-lib@2.0.0-beta.1
77
+ - @modern-js/babel-compiler@2.0.0-beta.1
78
+
3
79
  ## 2.0.0-beta.0
4
80
 
5
81
  ### Major Changes
@@ -3,20 +3,16 @@ import { fs } from '@modern-js/utils';
3
3
  import { compileByTs } from "../compilers/typescript";
4
4
  import { compileByBabel } from "../compilers/babel";
5
5
  export const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
6
-
7
6
  const validateAbsolutePath = (filename, message) => {
8
7
  if (!path.isAbsolute(filename)) {
9
8
  throw new Error(message);
10
9
  }
11
10
  };
12
-
13
11
  const validateAbsolutePaths = (filenames, messageFunc) => {
14
12
  filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
15
13
  };
16
-
17
14
  export const compile = async (appDirectory, modernConfig, compileOptions) => {
18
15
  var _modernConfig$server;
19
-
20
16
  const {
21
17
  sourceDirs,
22
18
  distDir,
@@ -26,7 +22,6 @@ export const compile = async (appDirectory, modernConfig, compileOptions) => {
26
22
  validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
27
23
  const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
28
24
  const isTsProject = tsconfigPath && (await fs.pathExists(tsconfigPath));
29
-
30
25
  if (!isTsProject || compiler === 'babel') {
31
26
  await compileByBabel(appDirectory, modernConfig, compileOptions);
32
27
  } else {
@@ -1,9 +1,6 @@
1
1
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
-
3
2
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
-
5
3
  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
-
7
4
  import * as path from 'path';
8
5
  import { getBabelChain, applyUserBabelConfig } from '@modern-js/babel-preset-lib';
9
6
  import { fs, json5, getAliasConfig } from '@modern-js/utils';
@@ -15,7 +12,6 @@ export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
15
12
  if (!fs.existsSync(tsconfigPath)) {
16
13
  return noExistReturn;
17
14
  }
18
-
19
15
  const content = fs.readFileSync(tsconfigPath, 'utf-8');
20
16
  return json5.parse(content);
21
17
  };
@@ -29,26 +25,30 @@ export const getBabelConfig = (libPresetOption, syntaxOption) => {
29
25
  sourceType: 'unambiguous'
30
26
  }, chain.toJSON());
31
27
  };
32
- export const resolveBabelConfig = (appDirectory, config, option // FIXME: babel type can't pass type checking
28
+ export const resolveBabelConfig = (appDirectory, config, option
29
+ // FIXME: babel type can't pass type checking
33
30
  ) => {
34
31
  const {
35
- envVars,
32
+ define,
36
33
  globalVars,
37
34
  alias,
38
35
  babelConfig
39
- } = config; // alias config
36
+ } = config;
40
37
 
38
+ // alias config
41
39
  const aliasConfig = getAliasConfig(alias, _objectSpread({
42
40
  appDirectory
43
- }, option)); // babel config
41
+ }, option));
44
42
 
43
+ // babel config
45
44
  const babelChain = getBabelChain({
46
45
  appDirectory,
47
46
  enableReactPreset: true,
48
47
  enableTypescriptPreset: true,
49
48
  alias: aliasConfig,
50
- envVars,
51
- globalVars
49
+ // FIXME: handle the define envVars
50
+ envVars: define,
51
+ globalVars: globalVars
52
52
  }, {
53
53
  type: option.type,
54
54
  syntax: option.syntax
@@ -60,14 +60,13 @@ export const resolveBabelConfig = (appDirectory, config, option // FIXME: babel
60
60
  babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
61
61
  babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
62
62
  legacy: true
63
- }]); // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
63
+ }]);
64
64
 
65
+ // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
65
66
  babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
66
67
  loose: true
67
68
  }]);
68
-
69
69
  const internalBabelConfig = _objectSpread({}, babelChain.toJSON());
70
-
71
70
  return applyUserBabelConfig(internalBabelConfig, babelConfig);
72
71
  };
73
72
  export const compileByBabel = async (appDirectory, config, compileOptions) => {
@@ -82,7 +81,6 @@ export const compileByBabel = async (appDirectory, config, compileOptions) => {
82
81
  syntax: 'es6+',
83
82
  type: 'commonjs'
84
83
  });
85
-
86
84
  if (await fs.pathExists(sourceDir)) {
87
85
  const basename = path.basename(sourceDir);
88
86
  const targetDir = path.join(distDir, basename);
@@ -90,7 +88,6 @@ export const compileByBabel = async (appDirectory, config, compileOptions) => {
90
88
  filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
91
89
  });
92
90
  }
93
-
94
91
  return compiler({
95
92
  rootDir: appDirectory,
96
93
  distDir,
@@ -1,15 +1,11 @@
1
1
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
-
3
2
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
-
5
3
  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
-
7
4
  import path from 'path';
8
5
  import { logger, getAliasConfig, fs } from '@modern-js/utils';
9
6
  import ts from 'typescript';
10
7
  import { TypescriptLoader } from "./typescript-loader";
11
8
  import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
12
-
13
9
  const readTsConfigByFile = tsConfigFile => {
14
10
  const parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
15
11
  const {
@@ -23,7 +19,6 @@ const readTsConfigByFile = tsConfigFile => {
23
19
  projectReferences
24
20
  };
25
21
  };
26
-
27
22
  const copyFiles = async (from, to, tsconfigPath) => {
28
23
  if (await fs.pathExists(from)) {
29
24
  const basename = path.basename(from);
@@ -33,7 +28,6 @@ const copyFiles = async (from, to, tsconfigPath) => {
33
28
  });
34
29
  }
35
30
  };
36
-
37
31
  export const compileByTs = async (appDirectory, config, compileOptions) => {
38
32
  logger.info(`Running ts compile...`);
39
33
  const {
@@ -41,11 +35,9 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
41
35
  distDir,
42
36
  tsconfigPath
43
37
  } = compileOptions;
44
-
45
38
  if (!tsconfigPath) {
46
39
  return;
47
40
  }
48
-
49
41
  const ts = new TypescriptLoader({
50
42
  appDirectory
51
43
  }).load();
@@ -86,20 +78,16 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
86
78
  before: [tsconfigPathsPlugin]
87
79
  });
88
80
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
89
-
90
81
  if (allDiagnostics.length > 0) {
91
- logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
92
-
82
+ logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
83
+ // eslint-disable-next-line no-process-exit
93
84
  process.exit(1);
94
85
  }
95
-
96
86
  for (const source of sourceDirs) {
97
87
  await copyFiles(source, distDir, tsconfigPath);
98
88
  }
99
-
100
89
  logger.info(`Ts compile succeed`);
101
90
  };
102
-
103
91
  const getFormatHost = ts => {
104
92
  return {
105
93
  getCanonicalFileName: path => path,
@@ -1,21 +1,17 @@
1
1
  import * as os from 'os';
2
2
  import path, { dirname, posix } from 'path';
3
3
  import * as ts from 'typescript';
4
- import { createMatchPath } from 'tsconfig-paths';
5
-
4
+ import { createMatchPath } from '@modern-js/utils/tsconfig-paths';
6
5
  const isRegExpKey = str => {
7
6
  return str.startsWith('^') || str.endsWith('$');
8
7
  };
9
-
10
8
  const resolveAliasPath = (baseUrl, filePath) => {
11
9
  // exclude absolute path and alias
12
10
  if (filePath.startsWith('.') || filePath.startsWith('..')) {
13
11
  return path.resolve(baseUrl, filePath);
14
12
  }
15
-
16
13
  return filePath;
17
14
  };
18
-
19
15
  const createAliasMatcher = (baseUrl, alias) => {
20
16
  const aliasPairs = Object.keys(alias).reduce((o, key) => {
21
17
  if (isRegExpKey(key)) {
@@ -26,16 +22,15 @@ const createAliasMatcher = (baseUrl, alias) => {
26
22
  const aliasPath = resolveAliasPath(baseUrl, alias[key]);
27
23
  o.push([key, aliasPath]);
28
24
  }
29
-
30
25
  return o;
31
26
  }, []);
32
- const cacheMap = new Map(); // eslint-disable-next-line consistent-return
27
+ const cacheMap = new Map();
33
28
 
29
+ // eslint-disable-next-line consistent-return
34
30
  return requestedModule => {
35
31
  if (cacheMap.has(requestedModule)) {
36
32
  return cacheMap.get(requestedModule);
37
33
  }
38
-
39
34
  for (const [key, value] of aliasPairs) {
40
35
  if (key instanceof RegExp) {
41
36
  if (key.test(requestedModule)) {
@@ -43,7 +38,6 @@ const createAliasMatcher = (baseUrl, alias) => {
43
38
  return value;
44
39
  }
45
40
  }
46
-
47
41
  if (requestedModule === key) {
48
42
  cacheMap.set(requestedModule, value);
49
43
  return value;
@@ -51,11 +45,9 @@ const createAliasMatcher = (baseUrl, alias) => {
51
45
  }
52
46
  };
53
47
  };
54
-
55
48
  const isDynamicImport = (tsBinary, node) => {
56
49
  return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
57
50
  };
58
-
59
51
  export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
60
52
  const tsPaths = {};
61
53
  const alias = {};
@@ -68,21 +60,16 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
68
60
  });
69
61
  const matchAliasPath = createAliasMatcher(baseUrl, alias);
70
62
  const matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
71
-
72
63
  const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
73
64
  const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
74
-
75
65
  if (result) {
76
66
  return result;
77
67
  }
78
-
79
68
  return matchAliasPath(requestedModule);
80
69
  };
81
-
82
70
  if (Object.keys(paths).length === 0) {
83
71
  return undefined;
84
72
  }
85
-
86
73
  return ctx => {
87
74
  return sf => {
88
75
  const visitNode = node => {
@@ -90,67 +77,53 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
90
77
  const importPathWithQuotes = node.arguments[0].getText(sf);
91
78
  const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
92
79
  const result = getNotAliasedPath(sf, matchPath, text);
93
-
94
80
  if (!result) {
95
81
  return node;
96
82
  }
97
-
98
83
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
99
84
  }
100
-
101
85
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
102
86
  try {
103
87
  var _node$moduleSpecifier;
104
-
105
88
  const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
106
-
107
89
  if (!importPathWithQuotes) {
108
90
  return node;
109
91
  }
110
-
111
92
  const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
112
93
  const result = getNotAliasedPath(sf, matchPath, text);
113
-
114
94
  if (!result) {
115
95
  return node;
116
96
  }
117
-
118
97
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
119
98
  moduleSpecifier.parent = node.moduleSpecifier.parent;
120
99
  let newNode;
121
-
122
100
  if (tsBinary.isImportDeclaration(node)) {
123
101
  newNode = tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
124
102
  } else {
125
103
  newNode = tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
126
104
  }
127
-
128
105
  newNode.flags = node.flags;
129
106
  return newNode;
130
107
  } catch (_unused) {
131
108
  return node;
132
109
  }
133
110
  }
134
-
135
111
  return tsBinary.visitEachChild(node, visitNode, ctx);
136
112
  };
137
-
138
113
  return tsBinary.visitNode(sf, visitNode);
139
114
  };
140
115
  };
141
- } // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
116
+ }
142
117
 
118
+ // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
143
119
  function getNotAliasedPath(sf, matcher, text) {
144
120
  let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
145
-
146
121
  if (!result) {
147
122
  return;
148
123
  }
149
-
150
124
  if (os.platform() === 'win32') {
151
125
  result = result.replace(/\\/g, '/');
152
126
  }
153
-
154
127
  if (!path.isAbsolute(result)) {
155
128
  // handle alias to alias
156
129
  if (!result.startsWith('.') && !result.startsWith('..')) {
@@ -160,29 +133,25 @@ function getNotAliasedPath(sf, matcher, text) {
160
133
  const packagePath = require.resolve(result, {
161
134
  paths: [process.cwd(), ...module.paths]
162
135
  });
163
-
164
136
  if (packagePath) {
165
137
  // eslint-disable-next-line consistent-return
166
138
  return result;
167
139
  }
168
140
  } catch (_unused2) {}
169
141
  }
170
-
171
142
  try {
172
143
  // Installed packages (node modules) should take precedence over root files with the same name.
173
144
  // Ref: https://github.com/nestjs/nest-cli/issues/838
174
145
  const packagePath = require.resolve(text, {
175
146
  paths: [process.cwd(), ...module.paths]
176
147
  });
177
-
178
148
  if (packagePath) {
179
149
  // eslint-disable-next-line consistent-return
180
150
  return text;
181
151
  }
182
152
  } catch (_unused3) {}
183
153
  }
184
-
185
- const resolvedPath = posix.relative(dirname(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
186
-
154
+ const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
155
+ // eslint-disable-next-line consistent-return
187
156
  return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
188
157
  }
@@ -1,32 +1,24 @@
1
1
  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; }
2
-
3
2
  export class TypescriptLoader {
4
3
  constructor({
5
4
  appDirectory
6
5
  }) {
7
6
  _defineProperty(this, "tsBinary", void 0);
8
-
9
7
  _defineProperty(this, "appDirectory", void 0);
10
-
11
8
  this.appDirectory = appDirectory;
12
9
  }
13
-
14
10
  load() {
15
11
  if (this.tsBinary) {
16
12
  return this.tsBinary;
17
13
  }
18
-
19
14
  try {
20
15
  const tsPath = require.resolve('typescript', {
21
16
  paths: [this.appDirectory || process.cwd()]
22
17
  });
23
-
24
18
  const ts = require(tsPath);
25
-
26
19
  return ts;
27
20
  } catch (error) {
28
21
  throw new Error('TypeScript could not be found! Please, install "typescript" package.');
29
22
  }
30
23
  }
31
-
32
24
  }
@@ -4,35 +4,24 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.compile = exports.FILE_EXTENSIONS = void 0;
7
-
8
7
  var path = _interopRequireWildcard(require("path"));
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
9
  var _typescript = require("../compilers/typescript");
13
-
14
10
  var _babel = require("../compilers/babel");
15
-
16
11
  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); }
17
-
18
12
  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; }
19
-
20
13
  const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
21
14
  exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
22
-
23
15
  const validateAbsolutePath = (filename, message) => {
24
16
  if (!path.isAbsolute(filename)) {
25
17
  throw new Error(message);
26
18
  }
27
19
  };
28
-
29
20
  const validateAbsolutePaths = (filenames, messageFunc) => {
30
21
  filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
31
22
  };
32
-
33
23
  const compile = async (appDirectory, modernConfig, compileOptions) => {
34
24
  var _modernConfig$server;
35
-
36
25
  const {
37
26
  sourceDirs,
38
27
  distDir,
@@ -42,12 +31,10 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
42
31
  validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
43
32
  const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
44
33
  const isTsProject = tsconfigPath && (await _utils.fs.pathExists(tsconfigPath));
45
-
46
34
  if (!isTsProject || compiler === 'babel') {
47
35
  await (0, _babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
48
36
  } else {
49
37
  await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
50
38
  }
51
39
  };
52
-
53
40
  exports.compile = compile;
@@ -11,19 +11,12 @@ var _exportNames = {
11
11
  compileByBabel: true
12
12
  };
13
13
  exports.resolveBabelConfig = exports.readTsConfig = exports.getBabelConfig = exports.existTsConfigFile = exports.compileByBabel = void 0;
14
-
15
14
  var path = _interopRequireWildcard(require("path"));
16
-
17
15
  var _babelPresetLib = require("@modern-js/babel-preset-lib");
18
-
19
16
  var _utils = require("@modern-js/utils");
20
-
21
17
  var _babelCompiler = require("@modern-js/babel-compiler");
22
-
23
18
  var _common = require("../../common");
24
-
25
19
  var _core = require("@babel/core");
26
-
27
20
  Object.keys(_core).forEach(function (key) {
28
21
  if (key === "default" || key === "__esModule") return;
29
22
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -35,66 +28,56 @@ Object.keys(_core).forEach(function (key) {
35
28
  }
36
29
  });
37
30
  });
38
-
39
31
  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); }
40
-
41
32
  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; }
42
-
43
33
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
44
-
45
34
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
46
-
47
35
  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; }
48
-
49
36
  const readTsConfig = (tsconfigPath, noExistReturn = null) => {
50
37
  // 如果不存在,则返回 noExistReturn
51
38
  if (!_utils.fs.existsSync(tsconfigPath)) {
52
39
  return noExistReturn;
53
40
  }
54
-
55
41
  const content = _utils.fs.readFileSync(tsconfigPath, 'utf-8');
56
-
57
42
  return _utils.json5.parse(content);
58
43
  };
59
-
60
44
  exports.readTsConfig = readTsConfig;
61
-
62
45
  const existTsConfigFile = tsconfigAbsolutePath => {
63
46
  const tsconfig = readTsConfig(tsconfigAbsolutePath);
64
47
  return Boolean(tsconfig);
65
48
  };
66
-
67
49
  exports.existTsConfigFile = existTsConfigFile;
68
-
69
50
  const getBabelConfig = (libPresetOption, syntaxOption) => {
70
51
  const chain = (0, _babelPresetLib.getBabelChain)(libPresetOption, syntaxOption);
71
52
  return _objectSpread({
72
53
  sourceType: 'unambiguous'
73
54
  }, chain.toJSON());
74
55
  };
75
-
76
56
  exports.getBabelConfig = getBabelConfig;
77
-
78
- const resolveBabelConfig = (appDirectory, config, option // FIXME: babel type can't pass type checking
57
+ const resolveBabelConfig = (appDirectory, config, option
58
+ // FIXME: babel type can't pass type checking
79
59
  ) => {
80
60
  const {
81
- envVars,
61
+ define,
82
62
  globalVars,
83
63
  alias,
84
64
  babelConfig
85
- } = config; // alias config
65
+ } = config;
86
66
 
67
+ // alias config
87
68
  const aliasConfig = (0, _utils.getAliasConfig)(alias, _objectSpread({
88
69
  appDirectory
89
- }, option)); // babel config
70
+ }, option));
90
71
 
72
+ // babel config
91
73
  const babelChain = (0, _babelPresetLib.getBabelChain)({
92
74
  appDirectory,
93
75
  enableReactPreset: true,
94
76
  enableTypescriptPreset: true,
95
77
  alias: aliasConfig,
96
- envVars,
97
- globalVars
78
+ // FIXME: handle the define envVars
79
+ envVars: define,
80
+ globalVars: globalVars
98
81
  }, {
99
82
  type: option.type,
100
83
  syntax: option.syntax
@@ -106,19 +89,16 @@ const resolveBabelConfig = (appDirectory, config, option // FIXME: babel type ca
106
89
  babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
107
90
  babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
108
91
  legacy: true
109
- }]); // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
92
+ }]);
110
93
 
94
+ // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
111
95
  babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
112
96
  loose: true
113
97
  }]);
114
-
115
98
  const internalBabelConfig = _objectSpread({}, babelChain.toJSON());
116
-
117
99
  return (0, _babelPresetLib.applyUserBabelConfig)(internalBabelConfig, babelConfig);
118
100
  };
119
-
120
101
  exports.resolveBabelConfig = resolveBabelConfig;
121
-
122
102
  const compileByBabel = async (appDirectory, config, compileOptions) => {
123
103
  const {
124
104
  sourceDirs,
@@ -131,7 +111,6 @@ const compileByBabel = async (appDirectory, config, compileOptions) => {
131
111
  syntax: 'es6+',
132
112
  type: 'commonjs'
133
113
  });
134
-
135
114
  if (await _utils.fs.pathExists(sourceDir)) {
136
115
  const basename = path.basename(sourceDir);
137
116
  const targetDir = path.join(distDir, basename);
@@ -139,7 +118,6 @@ const compileByBabel = async (appDirectory, config, compileOptions) => {
139
118
  filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
140
119
  });
141
120
  }
142
-
143
121
  return (0, _babelCompiler.compiler)({
144
122
  rootDir: appDirectory,
145
123
  distDir,
@@ -153,5 +131,4 @@ const compileByBabel = async (appDirectory, config, compileOptions) => {
153
131
  }
154
132
  });
155
133
  };
156
-
157
134
  exports.compileByBabel = compileByBabel;