@modern-js/server-utils 2.69.4 → 3.0.0-alpha.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.
Files changed (44) hide show
  1. package/dist/cjs/common/index.js +58 -62
  2. package/dist/cjs/compilers/babel/index.js +145 -101
  3. package/dist/cjs/compilers/babel/preset/alias.js +123 -105
  4. package/dist/cjs/compilers/babel/preset/index.js +102 -68
  5. package/dist/cjs/compilers/babel/preset/types.js +17 -15
  6. package/dist/cjs/compilers/typescript/index.js +112 -119
  7. package/dist/cjs/compilers/typescript/tsconfigPathsPlugin.js +157 -191
  8. package/dist/cjs/compilers/typescript/typescriptLoader.js +47 -41
  9. package/dist/cjs/index.js +68 -28
  10. package/dist/esm/common/index.mjs +29 -0
  11. package/dist/esm/compilers/babel/index.mjs +57 -0
  12. package/dist/esm/compilers/babel/preset/alias.mjs +84 -0
  13. package/dist/esm/compilers/babel/preset/index.mjs +29 -0
  14. package/dist/esm/compilers/typescript/index.mjs +76 -0
  15. package/dist/esm/compilers/typescript/tsconfigPathsPlugin.mjs +126 -0
  16. package/dist/esm/compilers/typescript/typescriptLoader.mjs +20 -0
  17. package/dist/{esm-node/index.js → esm/index.mjs} +2 -4
  18. package/dist/esm-node/common/index.mjs +29 -0
  19. package/dist/esm-node/compilers/babel/index.mjs +57 -0
  20. package/dist/esm-node/compilers/babel/preset/alias.mjs +84 -0
  21. package/dist/esm-node/compilers/babel/preset/index.mjs +29 -0
  22. package/dist/esm-node/compilers/typescript/index.mjs +76 -0
  23. package/dist/esm-node/compilers/typescript/tsconfigPathsPlugin.mjs +126 -0
  24. package/dist/esm-node/compilers/typescript/typescriptLoader.mjs +20 -0
  25. package/dist/esm-node/index.mjs +3 -0
  26. package/package.json +33 -25
  27. package/rslib.config.mts +4 -0
  28. package/dist/esm/common/index.js +0 -97
  29. package/dist/esm/compilers/babel/index.js +0 -119
  30. package/dist/esm/compilers/babel/preset/alias.js +0 -70
  31. package/dist/esm/compilers/babel/preset/index.js +0 -36
  32. package/dist/esm/compilers/typescript/index.js +0 -202
  33. package/dist/esm/compilers/typescript/tsconfigPathsPlugin.js +0 -188
  34. package/dist/esm/compilers/typescript/typescriptLoader.js +0 -30
  35. package/dist/esm/index.js +0 -5
  36. package/dist/esm-node/common/index.js +0 -35
  37. package/dist/esm-node/compilers/babel/index.js +0 -66
  38. package/dist/esm-node/compilers/babel/preset/alias.js +0 -76
  39. package/dist/esm-node/compilers/babel/preset/index.js +0 -36
  40. package/dist/esm-node/compilers/typescript/index.js +0 -93
  41. package/dist/esm-node/compilers/typescript/tsconfigPathsPlugin.js +0 -170
  42. package/dist/esm-node/compilers/typescript/typescriptLoader.js +0 -24
  43. /package/dist/esm/compilers/babel/preset/{types.js → types.mjs} +0 -0
  44. /package/dist/esm-node/compilers/babel/preset/{types.js → types.mjs} +0 -0
@@ -1,204 +1,170 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var tsconfigPathsPlugin_exports = {};
30
- __export(tsconfigPathsPlugin_exports, {
31
- tsconfigPathsBeforeHookFactory: () => tsconfigPathsBeforeHookFactory
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module1)=>{
5
+ var getter = module1 && module1.__esModule ? ()=>module1['default'] : ()=>module1;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ tsconfigPathsBeforeHookFactory: ()=>tsconfigPathsBeforeHookFactory
32
37
  });
33
- module.exports = __toCommonJS(tsconfigPathsPlugin_exports);
34
- var os = __toESM(require("os"));
35
- var import_path = __toESM(require("path"));
36
- var import_tsconfig_paths = require("@modern-js/utils/tsconfig-paths");
37
- var ts = __toESM(require("typescript"));
38
- const isRegExpKey = (str) => {
39
- return str.startsWith("^") || str.endsWith("$");
40
- };
41
- const resolveAliasPath = (baseUrl, filePath) => {
42
- if (filePath.startsWith(".") || filePath.startsWith("..")) {
43
- return import_path.default.resolve(baseUrl, filePath);
44
- }
45
- return filePath;
46
- };
47
- const createAliasMatcher = (baseUrl, alias) => {
48
- const aliasPairs = Object.keys(alias).reduce((o, key) => {
49
- if (isRegExpKey(key)) {
50
- const regexp = new RegExp(key);
51
- const aliasPath = resolveAliasPath(baseUrl, alias[key]);
52
- o.push([
53
- regexp,
54
- aliasPath
55
- ]);
56
- } else {
57
- const aliasPath = resolveAliasPath(baseUrl, alias[key]);
58
- o.push([
59
- key,
60
- aliasPath
61
- ]);
62
- }
63
- return o;
64
- }, []);
65
- const cacheMap = /* @__PURE__ */ new Map();
66
- return (requestedModule) => {
67
- if (cacheMap.has(requestedModule)) {
68
- return cacheMap.get(requestedModule);
69
- }
70
- for (const [key, value] of aliasPairs) {
71
- if (key instanceof RegExp) {
72
- if (key.test(requestedModule)) {
73
- cacheMap.set(requestedModule, value);
74
- return value;
75
- }
76
- }
77
- if (requestedModule === key) {
78
- cacheMap.set(requestedModule, value);
79
- return value;
80
- }
81
- }
82
- };
38
+ const external_os_namespaceObject = require("os");
39
+ const external_path_namespaceObject = require("path");
40
+ var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
41
+ const tsconfig_paths_namespaceObject = require("@modern-js/utils/tsconfig-paths");
42
+ const external_typescript_namespaceObject = require("typescript");
43
+ const isRegExpKey = (str)=>str.startsWith('^') || str.endsWith('$');
44
+ const resolveAliasPath = (baseUrl, filePath)=>{
45
+ if (filePath.startsWith('.') || filePath.startsWith('..')) return external_path_default().resolve(baseUrl, filePath);
46
+ return filePath;
83
47
  };
84
- const isDynamicImport = (tsBinary, node) => {
85
- return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
86
- };
87
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
88
- const tsPaths = {};
89
- const alias = {};
90
- Object.keys(paths).forEach((key) => {
91
- if (Array.isArray(paths[key])) {
92
- tsPaths[key] = paths[key];
93
- } else {
94
- alias[key] = paths[key];
95
- }
96
- });
97
- const matchAliasPath = createAliasMatcher(baseUrl, alias);
98
- const matchTsPath = (0, import_tsconfig_paths.createMatchPath)(baseUrl, tsPaths, [
99
- "main"
100
- ]);
101
- const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
102
- const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
103
- if (result) {
104
- return result;
105
- }
106
- return matchAliasPath(requestedModule);
107
- };
108
- if (Object.keys(paths).length === 0) {
109
- return void 0;
110
- }
111
- return (ctx) => {
112
- return (sf) => {
113
- const visitNode = (node) => {
114
- if (isDynamicImport(tsBinary, node)) {
115
- const importPathWithQuotes = node.arguments[0].getText(sf);
116
- const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
117
- const result = getNotAliasedPath(sf, matchPath, text);
118
- if (!result) {
119
- return node;
120
- }
121
- return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
122
- tsBinary.factory.createStringLiteral(result)
123
- ]));
48
+ const createAliasMatcher = (baseUrl, alias)=>{
49
+ const aliasPairs = Object.keys(alias).reduce((o, key)=>{
50
+ if (isRegExpKey(key)) {
51
+ const regexp = new RegExp(key);
52
+ const aliasPath = resolveAliasPath(baseUrl, alias[key]);
53
+ o.push([
54
+ regexp,
55
+ aliasPath
56
+ ]);
57
+ } else {
58
+ const aliasPath = resolveAliasPath(baseUrl, alias[key]);
59
+ o.push([
60
+ key,
61
+ aliasPath
62
+ ]);
124
63
  }
125
- if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
126
- try {
127
- var _node_moduleSpecifier;
128
- const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node_moduleSpecifier = node.moduleSpecifier) === null || _node_moduleSpecifier === void 0 ? void 0 : _node_moduleSpecifier.getText();
129
- if (!importPathWithQuotes) {
130
- return node;
131
- }
132
- const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
133
- const result = getNotAliasedPath(sf, matchPath, text);
134
- if (!result) {
135
- return node;
64
+ return o;
65
+ }, []);
66
+ const cacheMap = new Map();
67
+ return (requestedModule)=>{
68
+ if (cacheMap.has(requestedModule)) return cacheMap.get(requestedModule);
69
+ for (const [key, value] of aliasPairs){
70
+ if (key instanceof RegExp) {
71
+ if (key.test(requestedModule)) {
72
+ cacheMap.set(requestedModule, value);
73
+ return value;
74
+ }
136
75
  }
137
- const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
138
- moduleSpecifier.parent = node.moduleSpecifier.parent;
139
- let newNode;
140
- if (tsBinary.isImportDeclaration(node)) {
141
- newNode = tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
142
- } else {
143
- newNode = tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
76
+ if (requestedModule === key) {
77
+ cacheMap.set(requestedModule, value);
78
+ return value;
144
79
  }
145
- newNode.flags = node.flags;
146
- return newNode;
147
- } catch {
148
- return node;
149
- }
150
80
  }
151
- return tsBinary.visitEachChild(node, visitNode, ctx);
152
- };
153
- return tsBinary.visitNode(sf, visitNode);
154
81
  };
155
- };
82
+ };
83
+ const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === external_typescript_namespaceObject.SyntaxKind.ImportKeyword;
84
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
85
+ const tsPaths = {};
86
+ const alias = {};
87
+ Object.keys(paths).forEach((key)=>{
88
+ if (Array.isArray(paths[key])) tsPaths[key] = paths[key];
89
+ else alias[key] = paths[key];
90
+ });
91
+ const matchAliasPath = createAliasMatcher(baseUrl, alias);
92
+ const matchTsPath = (0, tsconfig_paths_namespaceObject.createMatchPath)(baseUrl, tsPaths, [
93
+ 'main'
94
+ ]);
95
+ const matchPath = (requestedModule, readJSONSync, fileExists, extensions)=>{
96
+ const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
97
+ if (result) return result;
98
+ return matchAliasPath(requestedModule);
99
+ };
100
+ if (0 === Object.keys(paths).length) return;
101
+ return (ctx)=>(sf)=>{
102
+ const visitNode = (node)=>{
103
+ if (isDynamicImport(tsBinary, node)) {
104
+ const importPathWithQuotes = node.arguments[0].getText(sf);
105
+ const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
106
+ const result = getNotAliasedPath(sf, matchPath, text);
107
+ if (!result) return node;
108
+ return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
109
+ tsBinary.factory.createStringLiteral(result)
110
+ ]));
111
+ }
112
+ if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) try {
113
+ const importPathWithQuotes = node?.moduleSpecifier?.getText();
114
+ if (!importPathWithQuotes) return node;
115
+ const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
116
+ const result = getNotAliasedPath(sf, matchPath, text);
117
+ if (!result) return node;
118
+ const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
119
+ moduleSpecifier.parent = node.moduleSpecifier.parent;
120
+ let newNode;
121
+ newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
122
+ newNode.flags = node.flags;
123
+ return newNode;
124
+ } catch {
125
+ return node;
126
+ }
127
+ return tsBinary.visitEachChild(node, visitNode, ctx);
128
+ };
129
+ return tsBinary.visitNode(sf, visitNode);
130
+ };
156
131
  }
157
132
  function getNotAliasedPath(sf, matcher, text) {
158
- let result = matcher(text, void 0, void 0, [
159
- ".ts",
160
- ".tsx",
161
- ".js",
162
- ".jsx"
163
- ]);
164
- if (!result) {
165
- return;
166
- }
167
- if (os.platform() === "win32") {
168
- result = result.replace(/\\/g, "/");
169
- }
170
- if (!import_path.default.isAbsolute(result)) {
171
- if (!result.startsWith(".") && !result.startsWith("..")) {
172
- try {
173
- const packagePath = require.resolve(result, {
174
- paths: [
175
- process.cwd(),
176
- ...module.paths
177
- ]
178
- });
179
- if (packagePath) {
180
- return result;
181
- }
182
- } catch {
183
- }
184
- }
185
- try {
186
- const packagePath = require.resolve(text, {
187
- paths: [
188
- process.cwd(),
189
- ...module.paths
190
- ]
191
- });
192
- if (packagePath) {
193
- return text;
194
- }
195
- } catch {
133
+ let result = matcher(text, void 0, void 0, [
134
+ '.ts',
135
+ '.tsx',
136
+ '.js',
137
+ '.jsx'
138
+ ]);
139
+ if (!result) return;
140
+ if ('win32' === external_os_namespaceObject.platform()) result = result.replace(/\\/g, '/');
141
+ if (!external_path_default().isAbsolute(result)) {
142
+ if (!result.startsWith('.') && !result.startsWith('..')) try {
143
+ const packagePath = require.resolve(result, {
144
+ paths: [
145
+ process.cwd(),
146
+ ...module.paths
147
+ ]
148
+ });
149
+ if (packagePath) return result;
150
+ } catch {}
151
+ try {
152
+ const packagePath = require.resolve(text, {
153
+ paths: [
154
+ process.cwd(),
155
+ ...module.paths
156
+ ]
157
+ });
158
+ if (packagePath) return text;
159
+ } catch {}
196
160
  }
197
- }
198
- const resolvedPath = import_path.posix.relative((0, import_path.dirname)(sf.fileName), result) || "./";
199
- return resolvedPath[0] === "." ? resolvedPath : `./${resolvedPath}`;
161
+ const resolvedPath = external_path_namespaceObject.posix.relative((0, external_path_namespaceObject.dirname)(sf.fileName), result) || './';
162
+ return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
200
163
  }
201
- // Annotate the CommonJS export names for ESM import in node:
202
- 0 && (module.exports = {
203
- tsconfigPathsBeforeHookFactory
164
+ exports.tsconfigPathsBeforeHookFactory = __webpack_exports__.tsconfigPathsBeforeHookFactory;
165
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
166
+ "tsconfigPathsBeforeHookFactory"
167
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
168
+ Object.defineProperty(exports, '__esModule', {
169
+ value: true
204
170
  });
@@ -1,48 +1,54 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var typescriptLoader_exports = {};
20
- __export(typescriptLoader_exports, {
21
- TypescriptLoader: () => TypescriptLoader
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ TypescriptLoader: ()=>TypescriptLoader
22
28
  });
23
- module.exports = __toCommonJS(typescriptLoader_exports);
24
29
  class TypescriptLoader {
25
- load() {
26
- if (this.tsBinary) {
27
- return this.tsBinary;
30
+ load() {
31
+ if (this.tsBinary) return this.tsBinary;
32
+ try {
33
+ const tsPath = require.resolve("typescript", {
34
+ paths: [
35
+ this.appDirectory || process.cwd()
36
+ ]
37
+ });
38
+ const ts = require(tsPath);
39
+ return ts;
40
+ } catch (error) {
41
+ throw new Error('TypeScript could not be found! Please, install "typescript" package.');
42
+ }
28
43
  }
29
- try {
30
- const tsPath = require.resolve("typescript", {
31
- paths: [
32
- this.appDirectory || process.cwd()
33
- ]
34
- });
35
- const ts = require(tsPath);
36
- return ts;
37
- } catch (error) {
38
- throw new Error('TypeScript could not be found! Please, install "typescript" package.');
44
+ constructor({ appDirectory }){
45
+ this.appDirectory = appDirectory;
39
46
  }
40
- }
41
- constructor({ appDirectory }) {
42
- this.appDirectory = appDirectory;
43
- }
44
47
  }
45
- // Annotate the CommonJS export names for ESM import in node:
46
- 0 && (module.exports = {
47
- TypescriptLoader
48
+ exports.TypescriptLoader = __webpack_exports__.TypescriptLoader;
49
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
50
+ "TypescriptLoader"
51
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
52
+ Object.defineProperty(exports, '__esModule', {
53
+ value: true
48
54
  });
package/dist/cjs/index.js CHANGED
@@ -1,31 +1,71 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
2
+ var __webpack_modules__ = {
3
+ "./common" (module) {
4
+ module.exports = require("./common/index.js");
5
+ },
6
+ "./compilers/babel" (module) {
7
+ module.exports = require("./compilers/babel/index.js");
8
+ }
9
9
  };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
- var src_exports = {};
21
- __export(src_exports, {
22
- compile: () => import_common.compile
23
- });
24
- module.exports = __toCommonJS(src_exports);
25
- __reExport(src_exports, require("./compilers/babel"), module.exports);
26
- var import_common = require("./common");
27
- // Annotate the CommonJS export names for ESM import in node:
28
- 0 && (module.exports = {
29
- compile,
30
- ...require("./compilers/babel")
10
+ var __webpack_module_cache__ = {};
11
+ function __webpack_require__(moduleId) {
12
+ var cachedModule = __webpack_module_cache__[moduleId];
13
+ if (void 0 !== cachedModule) return cachedModule.exports;
14
+ var module = __webpack_module_cache__[moduleId] = {
15
+ exports: {}
16
+ };
17
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
18
+ return module.exports;
19
+ }
20
+ (()=>{
21
+ __webpack_require__.n = (module)=>{
22
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
23
+ __webpack_require__.d(getter, {
24
+ a: getter
25
+ });
26
+ return getter;
27
+ };
28
+ })();
29
+ (()=>{
30
+ __webpack_require__.d = (exports1, definition)=>{
31
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
32
+ enumerable: true,
33
+ get: definition[key]
34
+ });
35
+ };
36
+ })();
37
+ (()=>{
38
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
39
+ })();
40
+ (()=>{
41
+ __webpack_require__.r = (exports1)=>{
42
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
43
+ value: 'Module'
44
+ });
45
+ Object.defineProperty(exports1, '__esModule', {
46
+ value: true
47
+ });
48
+ };
49
+ })();
50
+ var __webpack_exports__ = {};
51
+ (()=>{
52
+ __webpack_require__.r(__webpack_exports__);
53
+ __webpack_require__.d(__webpack_exports__, {
54
+ compile: ()=>_common__rspack_import_1.compile
55
+ });
56
+ var _compilers_babel__rspack_import_0 = __webpack_require__("./compilers/babel");
57
+ var __rspack_reexport = {};
58
+ for(const __rspack_import_key in _compilers_babel__rspack_import_0)if ([
59
+ "compile",
60
+ "default"
61
+ ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_compilers_babel__rspack_import_0[__rspack_import_key];
62
+ __webpack_require__.d(__webpack_exports__, __rspack_reexport);
63
+ var _common__rspack_import_1 = __webpack_require__("./common");
64
+ })();
65
+ exports.compile = __webpack_exports__.compile;
66
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
67
+ "compile"
68
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
69
+ Object.defineProperty(exports, '__esModule', {
70
+ value: true
31
71
  });
@@ -0,0 +1,29 @@
1
+ import { isAbsolute } from "path";
2
+ import { fs } from "@modern-js/utils";
3
+ const FILE_EXTENSIONS = [
4
+ '.js',
5
+ '.ts',
6
+ '.mjs',
7
+ '.ejs'
8
+ ];
9
+ const validateAbsolutePath = (filename, message)=>{
10
+ if (!isAbsolute(filename)) throw new Error(message);
11
+ };
12
+ const validateAbsolutePaths = (filenames, messageFunc)=>{
13
+ filenames.forEach((filename)=>validateAbsolutePath(filename, messageFunc(filename)));
14
+ };
15
+ const compile = async (appDirectory, modernConfig, compileOptions)=>{
16
+ const { sourceDirs, distDir, tsconfigPath } = compileOptions;
17
+ validateAbsolutePaths(sourceDirs, (dir)=>`source dir ${dir} is not an absolute path.`);
18
+ validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
19
+ const compiler = modernConfig?.server?.compiler;
20
+ const isTsProject = tsconfigPath && await fs.pathExists(tsconfigPath);
21
+ if (isTsProject && 'babel' !== compiler) {
22
+ const { compileByTs } = await import("../compilers/typescript");
23
+ await compileByTs(appDirectory, modernConfig, compileOptions);
24
+ } else {
25
+ const { compileByBabel } = await import("../compilers/babel");
26
+ await compileByBabel(appDirectory, modernConfig, compileOptions);
27
+ }
28
+ };
29
+ export { FILE_EXTENSIONS, compile };
@@ -0,0 +1,57 @@
1
+ import { basename as external_path_basename, extname, join } from "path";
2
+ import { compiler } from "@modern-js/babel-compiler";
3
+ import { fs, getAliasConfig, json5 } from "@modern-js/utils";
4
+ import { FILE_EXTENSIONS } from "../../common";
5
+ import { applyUserBabelConfig, getBabelConfig } from "./preset";
6
+ export * from "@babel/core";
7
+ const readTsConfig = (tsconfigPath, noExistReturn = null)=>{
8
+ if (!fs.existsSync(tsconfigPath)) return noExistReturn;
9
+ const content = fs.readFileSync(tsconfigPath, 'utf-8');
10
+ return json5.parse(content);
11
+ };
12
+ const existTsConfigFile = (tsconfigAbsolutePath)=>{
13
+ const tsconfig = readTsConfig(tsconfigAbsolutePath);
14
+ return Boolean(tsconfig);
15
+ };
16
+ const resolveBabelConfig = (appDirectory, config, option, isEsm)=>{
17
+ const { alias, babelConfig } = config;
18
+ const aliasConfig = getAliasConfig(alias, {
19
+ appDirectory,
20
+ ...option
21
+ });
22
+ const defaultBabelConfig = getBabelConfig({
23
+ appDirectory,
24
+ alias: aliasConfig,
25
+ isEsm
26
+ });
27
+ return applyUserBabelConfig(defaultBabelConfig, babelConfig);
28
+ };
29
+ const compileByBabel = async (appDirectory, config, compileOptions)=>{
30
+ const { sourceDirs, distDir, tsconfigPath, moduleType } = compileOptions;
31
+ const isEsm = 'module' === moduleType;
32
+ const results = await Promise.all(sourceDirs.map(async (sourceDir)=>{
33
+ const babelConfig = resolveBabelConfig(appDirectory, config, {
34
+ tsconfigPath: tsconfigPath ? tsconfigPath : ''
35
+ }, isEsm);
36
+ if (await fs.pathExists(sourceDir)) {
37
+ const basename = external_path_basename(sourceDir);
38
+ const targetDir = join(distDir, basename);
39
+ await fs.copy(sourceDir, targetDir, {
40
+ filter: (src)=>![
41
+ '.ts',
42
+ '.js'
43
+ ].includes(extname(src)) && src !== tsconfigPath
44
+ });
45
+ }
46
+ return compiler({
47
+ rootDir: appDirectory,
48
+ distDir,
49
+ sourceDir,
50
+ extensions: FILE_EXTENSIONS
51
+ }, babelConfig);
52
+ }));
53
+ results.forEach((result)=>{
54
+ if (1 === result.code) throw new Error(result.message);
55
+ });
56
+ };
57
+ export { compileByBabel, existTsConfigFile, readTsConfig, resolveBabelConfig };