@modern-js/server-utils 2.15.0 → 2.16.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.
@@ -1,49 +1,43 @@
1
1
  import * as path from "path";
2
- import {
3
- getBabelChain,
4
- applyUserBabelConfig
5
- } from "@modern-js/babel-preset-lib";
2
+ import { getBabelChain, applyUserBabelConfig } from "@modern-js/babel-preset-lib";
6
3
  import { fs, json5, getAliasConfig } from "@modern-js/utils";
7
4
  import { compiler } from "@modern-js/babel-compiler";
8
5
  import { FILE_EXTENSIONS } from "../../common";
9
6
  export * from "@babel/core";
10
- const readTsConfig = (tsconfigPath, noExistReturn = null) => {
7
+ export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
11
8
  if (!fs.existsSync(tsconfigPath)) {
12
9
  return noExistReturn;
13
10
  }
14
11
  const content = fs.readFileSync(tsconfigPath, "utf-8");
15
12
  return json5.parse(content);
16
13
  };
17
- const existTsConfigFile = (tsconfigAbsolutePath) => {
14
+ export const existTsConfigFile = (tsconfigAbsolutePath) => {
18
15
  const tsconfig = readTsConfig(tsconfigAbsolutePath);
19
16
  return Boolean(tsconfig);
20
17
  };
21
- const getBabelConfig = (libPresetOption, syntaxOption) => {
18
+ export const getBabelConfig = (libPresetOption, syntaxOption) => {
22
19
  const chain = getBabelChain(libPresetOption, syntaxOption);
23
20
  return {
24
21
  sourceType: "unambiguous",
25
22
  ...chain.toJSON()
26
23
  };
27
24
  };
28
- const resolveBabelConfig = (appDirectory, config, option) => {
25
+ export const resolveBabelConfig = (appDirectory, config, option) => {
29
26
  const { alias, babelConfig } = config;
30
27
  const aliasConfig = getAliasConfig(alias, {
31
28
  appDirectory,
32
29
  ...option
33
30
  });
34
- const babelChain = getBabelChain(
35
- {
36
- appDirectory,
37
- enableReactPreset: true,
38
- enableTypescriptPreset: true,
39
- alias: aliasConfig,
40
- envVars: []
41
- },
42
- {
43
- type: option.type,
44
- syntax: option.syntax
45
- }
46
- );
31
+ const babelChain = getBabelChain({
32
+ appDirectory,
33
+ enableReactPreset: true,
34
+ enableTypescriptPreset: true,
35
+ alias: aliasConfig,
36
+ envVars: []
37
+ }, {
38
+ type: option.type,
39
+ syntax: option.syntax
40
+ });
47
41
  const envOptions = babelChain.preset("@babel/preset-env").options();
48
42
  babelChain.preset("@babel/preset-env").use(require.resolve("@babel/preset-env"), [
49
43
  {
@@ -51,58 +45,50 @@ const resolveBabelConfig = (appDirectory, config, option) => {
51
45
  loose: true
52
46
  }
53
47
  ]);
54
- babelChain.plugin("babel-plugin-transform-typescript-metadata").use(
55
- require.resolve("babel-plugin-transform-typescript-metadata"),
56
- []
57
- );
48
+ babelChain.plugin("babel-plugin-transform-typescript-metadata").use(require.resolve("babel-plugin-transform-typescript-metadata"), []);
58
49
  babelChain.plugin("@babel/plugin-proposal-decorators").use(require.resolve("@babel/plugin-proposal-decorators"), [
59
- { legacy: true }
50
+ {
51
+ legacy: true
52
+ }
60
53
  ]);
61
54
  babelChain.plugin("@babel/plugin-proposal-class-properties").use(require.resolve("@babel/plugin-proposal-class-properties"), [
62
55
  {
63
56
  loose: true
64
57
  }
65
58
  ]);
66
- const internalBabelConfig = { ...babelChain.toJSON() };
59
+ const internalBabelConfig = {
60
+ ...babelChain.toJSON()
61
+ };
67
62
  return applyUserBabelConfig(internalBabelConfig, babelConfig);
68
63
  };
69
- const compileByBabel = async (appDirectory, config, compileOptions) => {
64
+ export const compileByBabel = async (appDirectory, config, compileOptions) => {
70
65
  const { sourceDirs, distDir, tsconfigPath } = compileOptions;
71
- const results = await Promise.all(
72
- sourceDirs.map(async (sourceDir) => {
73
- const babelConfig = resolveBabelConfig(appDirectory, config, {
74
- tsconfigPath: tsconfigPath ? tsconfigPath : "",
75
- syntax: "es6+",
76
- type: "commonjs"
66
+ const results = await Promise.all(sourceDirs.map(async (sourceDir) => {
67
+ const babelConfig = resolveBabelConfig(appDirectory, config, {
68
+ tsconfigPath: tsconfigPath ? tsconfigPath : "",
69
+ syntax: "es6+",
70
+ type: "commonjs"
71
+ });
72
+ if (await fs.pathExists(sourceDir)) {
73
+ const basename = path.basename(sourceDir);
74
+ const targetDir = path.join(distDir, basename);
75
+ await fs.copy(sourceDir, targetDir, {
76
+ filter: (src) => ![
77
+ ".ts",
78
+ ".js"
79
+ ].includes(path.extname(src)) && src !== tsconfigPath
77
80
  });
78
- if (await fs.pathExists(sourceDir)) {
79
- const basename = path.basename(sourceDir);
80
- const targetDir = path.join(distDir, basename);
81
- await fs.copy(sourceDir, targetDir, {
82
- filter: (src) => ![".ts", ".js"].includes(path.extname(src)) && src !== tsconfigPath
83
- });
84
- }
85
- return compiler(
86
- {
87
- rootDir: appDirectory,
88
- distDir,
89
- sourceDir,
90
- extensions: FILE_EXTENSIONS
91
- },
92
- babelConfig
93
- );
94
- })
95
- );
81
+ }
82
+ return compiler({
83
+ rootDir: appDirectory,
84
+ distDir,
85
+ sourceDir,
86
+ extensions: FILE_EXTENSIONS
87
+ }, babelConfig);
88
+ }));
96
89
  results.forEach((result) => {
97
90
  if (result.code === 1) {
98
91
  throw new Error(result.message);
99
92
  }
100
93
  });
101
94
  };
102
- export {
103
- compileByBabel,
104
- existTsConfigFile,
105
- getBabelConfig,
106
- readTsConfig,
107
- resolveBabelConfig
108
- };
@@ -4,24 +4,26 @@ import ts from "typescript";
4
4
  import { TypescriptLoader } from "./typescriptLoader";
5
5
  import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin";
6
6
  const readTsConfigByFile = (tsConfigFile) => {
7
- const parsedCmd = ts.getParsedCommandLineOfConfigFile(
8
- tsConfigFile,
9
- void 0,
10
- ts.sys
11
- );
7
+ const parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, ts.sys);
12
8
  const { options, fileNames, projectReferences } = parsedCmd;
13
- return { options, fileNames, projectReferences };
9
+ return {
10
+ options,
11
+ fileNames,
12
+ projectReferences
13
+ };
14
14
  };
15
15
  const copyFiles = async (from, to, appDirectory, tsconfigPath) => {
16
16
  if (await fs.pathExists(from)) {
17
17
  const relativePath = path.relative(appDirectory, from);
18
18
  const targetDir = path.join(to, relativePath);
19
19
  await fs.copy(from, targetDir, {
20
- filter: (src) => ![".ts"].includes(path.extname(src)) && src !== tsconfigPath
20
+ filter: (src) => ![
21
+ ".ts"
22
+ ].includes(path.extname(src)) && src !== tsconfigPath
21
23
  });
22
24
  }
23
25
  };
24
- const compileByTs = async (appDirectory, config, compileOptions) => {
26
+ export const compileByTs = async (appDirectory, config, compileOptions) => {
25
27
  logger.info(`Running ts compile...`);
26
28
  const { sourceDirs, distDir, tsconfigPath } = compileOptions;
27
29
  if (!tsconfigPath) {
@@ -39,9 +41,7 @@ const compileByTs = async (appDirectory, config, compileOptions) => {
39
41
  });
40
42
  const { paths = {}, absoluteBaseUrl = "./" } = aliasOption;
41
43
  const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath);
42
- const sourcePosixPaths = sourceDirs.map(
43
- (sourceDir) => sourceDir.split(path.sep).join(path.posix.sep)
44
- );
44
+ const sourcePosixPaths = sourceDirs.map((sourceDir) => sourceDir.split(path.sep).join(path.posix.sep));
45
45
  const rootNames = fileNames.filter((fileName) => {
46
46
  return fileName.endsWith(".d.ts") || sourcePosixPaths.some((sourceDir) => {
47
47
  return fileName.includes(sourceDir);
@@ -56,23 +56,18 @@ const compileByTs = async (appDirectory, config, compileOptions) => {
56
56
  ...options
57
57
  }
58
58
  });
59
- const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(
60
- ts2,
61
- absoluteBaseUrl,
62
- paths
63
- );
59
+ const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts2, absoluteBaseUrl, paths);
64
60
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
65
- before: [tsconfigPathsPlugin]
61
+ before: [
62
+ tsconfigPathsPlugin
63
+ ]
66
64
  });
67
65
  const allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
68
66
  const { noEmitOnError } = options;
69
67
  if (allDiagnostics.length > 0) {
70
- logger.error(
71
- ts2.formatDiagnosticsWithColorAndContext(
72
- [...new Set(allDiagnostics)],
73
- formatHost
74
- )
75
- );
68
+ logger.error(ts2.formatDiagnosticsWithColorAndContext([
69
+ ...new Set(allDiagnostics)
70
+ ], formatHost));
76
71
  if (typeof noEmitOnError === "undefined" || noEmitOnError === true) {
77
72
  process.exit(1);
78
73
  }
@@ -89,6 +84,3 @@ const getFormatHost = (ts2) => {
89
84
  getNewLine: () => ts2.sys.newLine
90
85
  };
91
86
  };
92
- export {
93
- compileByTs
94
- };
@@ -16,10 +16,16 @@ const createAliasMatcher = (baseUrl, alias) => {
16
16
  if (isRegExpKey(key)) {
17
17
  const regexp = new RegExp(key);
18
18
  const aliasPath = resolveAliasPath(baseUrl, alias[key]);
19
- o.push([regexp, aliasPath]);
19
+ o.push([
20
+ regexp,
21
+ aliasPath
22
+ ]);
20
23
  } else {
21
24
  const aliasPath = resolveAliasPath(baseUrl, alias[key]);
22
- o.push([key, aliasPath]);
25
+ o.push([
26
+ key,
27
+ aliasPath
28
+ ]);
23
29
  }
24
30
  return o;
25
31
  }, []);
@@ -45,7 +51,7 @@ const createAliasMatcher = (baseUrl, alias) => {
45
51
  const isDynamicImport = (tsBinary, node) => {
46
52
  return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
47
53
  };
48
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
54
+ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
49
55
  const tsPaths = {};
50
56
  const alias = {};
51
57
  Object.keys(paths).forEach((key) => {
@@ -56,14 +62,11 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
56
62
  }
57
63
  });
58
64
  const matchAliasPath = createAliasMatcher(baseUrl, alias);
59
- const matchTsPath = createMatchPath(baseUrl, tsPaths, ["main"]);
65
+ const matchTsPath = createMatchPath(baseUrl, tsPaths, [
66
+ "main"
67
+ ]);
60
68
  const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
61
- const result = matchTsPath(
62
- requestedModule,
63
- readJSONSync,
64
- fileExists,
65
- extensions
66
- );
69
+ const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
67
70
  if (result) {
68
71
  return result;
69
72
  }
@@ -75,36 +78,25 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
75
78
  return (ctx) => {
76
79
  return (sf) => {
77
80
  const visitNode = (node) => {
78
- var _a;
79
81
  if (isDynamicImport(tsBinary, node)) {
80
82
  const importPathWithQuotes = node.arguments[0].getText(sf);
81
- const text = importPathWithQuotes.slice(
82
- 1,
83
- importPathWithQuotes.length - 1
84
- );
83
+ const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
85
84
  const result = getNotAliasedPath(sf, matchPath, text);
86
85
  if (!result) {
87
86
  return node;
88
87
  }
89
- return tsBinary.factory.updateCallExpression(
90
- node,
91
- node.expression,
92
- node.typeArguments,
93
- tsBinary.factory.createNodeArray([
94
- tsBinary.factory.createStringLiteral(result)
95
- ])
96
- );
88
+ return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
89
+ tsBinary.factory.createStringLiteral(result)
90
+ ]));
97
91
  }
98
92
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
99
93
  try {
100
- const importPathWithQuotes = (_a = node == null ? void 0 : node.moduleSpecifier) == null ? void 0 : _a.getText();
94
+ var _node_moduleSpecifier;
95
+ const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node_moduleSpecifier = node.moduleSpecifier) === null || _node_moduleSpecifier === void 0 ? void 0 : _node_moduleSpecifier.getText();
101
96
  if (!importPathWithQuotes) {
102
97
  return node;
103
98
  }
104
- const text = importPathWithQuotes.substring(
105
- 1,
106
- importPathWithQuotes.length - 1
107
- );
99
+ const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
108
100
  const result = getNotAliasedPath(sf, matchPath, text);
109
101
  if (!result) {
110
102
  return node;
@@ -113,22 +105,9 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
113
105
  moduleSpecifier.parent = node.moduleSpecifier.parent;
114
106
  let newNode;
115
107
  if (tsBinary.isImportDeclaration(node)) {
116
- newNode = tsBinary.factory.updateImportDeclaration(
117
- node,
118
- node.modifiers,
119
- node.importClause,
120
- moduleSpecifier,
121
- node.assertClause
122
- );
108
+ newNode = tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
123
109
  } else {
124
- newNode = tsBinary.factory.updateExportDeclaration(
125
- node,
126
- node.modifiers,
127
- node.isTypeOnly,
128
- node.exportClause,
129
- moduleSpecifier,
130
- node.assertClause
131
- );
110
+ newNode = tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
132
111
  }
133
112
  newNode.flags = node.flags;
134
113
  return newNode;
@@ -159,7 +138,10 @@ function getNotAliasedPath(sf, matcher, text) {
159
138
  if (!result.startsWith(".") && !result.startsWith("..")) {
160
139
  try {
161
140
  const packagePath = require.resolve(result, {
162
- paths: [process.cwd(), ...module.paths]
141
+ paths: [
142
+ process.cwd(),
143
+ ...module.paths
144
+ ]
163
145
  });
164
146
  if (packagePath) {
165
147
  return result;
@@ -169,7 +151,10 @@ function getNotAliasedPath(sf, matcher, text) {
169
151
  }
170
152
  try {
171
153
  const packagePath = require.resolve(text, {
172
- paths: [process.cwd(), ...module.paths]
154
+ paths: [
155
+ process.cwd(),
156
+ ...module.paths
157
+ ]
173
158
  });
174
159
  if (packagePath) {
175
160
  return text;
@@ -180,6 +165,3 @@ function getNotAliasedPath(sf, matcher, text) {
180
165
  const resolvedPath = posix.relative(dirname(sf.fileName), result) || "./";
181
166
  return resolvedPath[0] === "." ? resolvedPath : `./${resolvedPath}`;
182
167
  }
183
- export {
184
- tsconfigPathsBeforeHookFactory
185
- };
@@ -1,24 +1,36 @@
1
- class TypescriptLoader {
2
- constructor({ appDirectory }) {
3
- this.appDirectory = appDirectory;
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
4
11
  }
12
+ return obj;
13
+ }
14
+ export class TypescriptLoader {
5
15
  load() {
6
16
  if (this.tsBinary) {
7
17
  return this.tsBinary;
8
18
  }
9
19
  try {
10
20
  const tsPath = require.resolve("typescript", {
11
- paths: [this.appDirectory || process.cwd()]
21
+ paths: [
22
+ this.appDirectory || process.cwd()
23
+ ]
12
24
  });
13
25
  const ts = require(tsPath);
14
26
  return ts;
15
27
  } catch (error) {
16
- throw new Error(
17
- 'TypeScript could not be found! Please, install "typescript" package.'
18
- );
28
+ throw new Error('TypeScript could not be found! Please, install "typescript" package.');
19
29
  }
20
30
  }
31
+ constructor({ appDirectory }) {
32
+ _define_property(this, "tsBinary", void 0);
33
+ _define_property(this, "appDirectory", void 0);
34
+ this.appDirectory = appDirectory;
35
+ }
21
36
  }
22
- export {
23
- TypescriptLoader
24
- };
@@ -1,5 +1,2 @@
1
1
  export * from "./compilers/babel";
2
- import { compile } from "./common";
3
- export {
4
- compile
5
- };
2
+ export { compile } from "./common";
package/package.json CHANGED
@@ -3,7 +3,11 @@
3
3
  "description": "A Progressive React Framework for modern web development.",
4
4
  "homepage": "https://modernjs.dev",
5
5
  "bugs": "https://github.com/web-infra-dev/modern.js/issues",
6
- "repository": "web-infra-dev/modern.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/web-infra-dev/modern.js",
9
+ "directory": "packages/server/utils"
10
+ },
7
11
  "license": "MIT",
8
12
  "keywords": [
9
13
  "react",
@@ -11,7 +15,7 @@
11
15
  "modern",
12
16
  "modern.js"
13
17
  ],
14
- "version": "2.15.0",
18
+ "version": "2.16.0",
15
19
  "jsnext:source": "./src/index.ts",
16
20
  "types": "./dist/types/index.d.ts",
17
21
  "main": "./dist/cjs/index.js",
@@ -35,9 +39,9 @@
35
39
  "@babel/runtime": "^7.18.0",
36
40
  "babel-plugin-module-resolver": "^4.1.0",
37
41
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
38
- "@modern-js/babel-compiler": "2.15.0",
39
- "@modern-js/babel-preset-lib": "2.15.0",
40
- "@modern-js/utils": "2.15.0"
42
+ "@modern-js/babel-compiler": "2.16.0",
43
+ "@modern-js/babel-preset-lib": "2.16.0",
44
+ "@modern-js/utils": "2.16.0"
41
45
  },
42
46
  "devDependencies": {
43
47
  "@types/babel__core": "^7.1.15",
@@ -46,9 +50,9 @@
46
50
  "jest": "^29",
47
51
  "ts-jest": "^29.0.5",
48
52
  "typescript": "^4",
49
- "@modern-js/server-core": "2.15.0",
50
- "@scripts/build": "2.15.0",
51
- "@scripts/jest-config": "2.15.0"
53
+ "@modern-js/server-core": "2.16.0",
54
+ "@scripts/build": "2.16.0",
55
+ "@scripts/jest-config": "2.16.0"
52
56
  },
53
57
  "sideEffects": false,
54
58
  "publishConfig": {