@modern-js/server-utils 2.5.1-alpha.0 → 2.6.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,216 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
- mod
22
- ));
23
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
- var tsconfig_paths_plugin_exports = {};
25
- __export(tsconfig_paths_plugin_exports, {
26
- tsconfigPathsBeforeHookFactory: () => tsconfigPathsBeforeHookFactory
27
- });
28
- module.exports = __toCommonJS(tsconfig_paths_plugin_exports);
29
- var os = __toESM(require("os"));
30
- var import_path = __toESM(require("path"));
31
- var ts = __toESM(require("typescript"));
32
- var import_tsconfig_paths = require("@modern-js/utils/tsconfig-paths");
33
- const isRegExpKey = (str) => {
34
- return str.startsWith("^") || str.endsWith("$");
35
- };
36
- const resolveAliasPath = (baseUrl, filePath) => {
37
- if (filePath.startsWith(".") || filePath.startsWith("..")) {
38
- return import_path.default.resolve(baseUrl, filePath);
39
- }
40
- return filePath;
41
- };
42
- const createAliasMatcher = (baseUrl, alias) => {
43
- const aliasPairs = Object.keys(alias).reduce((o, key) => {
44
- if (isRegExpKey(key)) {
45
- const regexp = new RegExp(key);
46
- const aliasPath = resolveAliasPath(baseUrl, alias[key]);
47
- o.push([regexp, aliasPath]);
48
- } else {
49
- const aliasPath = resolveAliasPath(baseUrl, alias[key]);
50
- o.push([key, aliasPath]);
51
- }
52
- return o;
53
- }, []);
54
- const cacheMap = /* @__PURE__ */ new Map();
55
- return (requestedModule) => {
56
- if (cacheMap.has(requestedModule)) {
57
- return cacheMap.get(requestedModule);
58
- }
59
- for (const [key, value] of aliasPairs) {
60
- if (key instanceof RegExp) {
61
- if (key.test(requestedModule)) {
62
- cacheMap.set(requestedModule, value);
63
- return value;
64
- }
65
- }
66
- if (requestedModule === key) {
67
- cacheMap.set(requestedModule, value);
68
- return value;
69
- }
70
- }
71
- };
72
- };
73
- const isDynamicImport = (tsBinary, node) => {
74
- return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
75
- };
76
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
77
- const tsPaths = {};
78
- const alias = {};
79
- Object.keys(paths).forEach((key) => {
80
- if (Array.isArray(paths[key])) {
81
- tsPaths[key] = paths[key];
82
- } else {
83
- alias[key] = paths[key];
84
- }
85
- });
86
- const matchAliasPath = createAliasMatcher(baseUrl, alias);
87
- const matchTsPath = (0, import_tsconfig_paths.createMatchPath)(baseUrl, tsPaths, ["main"]);
88
- const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
89
- const result = matchTsPath(
90
- requestedModule,
91
- readJSONSync,
92
- fileExists,
93
- extensions
94
- );
95
- if (result) {
96
- return result;
97
- }
98
- return matchAliasPath(requestedModule);
99
- };
100
- if (Object.keys(paths).length === 0) {
101
- return void 0;
102
- }
103
- return (ctx) => {
104
- return (sf) => {
105
- const visitNode = (node) => {
106
- var _a;
107
- if (isDynamicImport(tsBinary, node)) {
108
- const importPathWithQuotes = node.arguments[0].getText(sf);
109
- const text = importPathWithQuotes.slice(
110
- 1,
111
- importPathWithQuotes.length - 1
112
- );
113
- const result = getNotAliasedPath(sf, matchPath, text);
114
- if (!result) {
115
- return node;
116
- }
117
- return tsBinary.factory.updateCallExpression(
118
- node,
119
- node.expression,
120
- node.typeArguments,
121
- tsBinary.factory.createNodeArray([
122
- tsBinary.factory.createStringLiteral(result)
123
- ])
124
- );
125
- }
126
- if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
127
- try {
128
- const importPathWithQuotes = (_a = node == null ? void 0 : node.moduleSpecifier) == null ? void 0 : _a.getText();
129
- if (!importPathWithQuotes) {
130
- return node;
131
- }
132
- const text = importPathWithQuotes.substring(
133
- 1,
134
- importPathWithQuotes.length - 1
135
- );
136
- const result = getNotAliasedPath(sf, matchPath, text);
137
- if (!result) {
138
- return node;
139
- }
140
- const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
141
- moduleSpecifier.parent = node.moduleSpecifier.parent;
142
- let newNode;
143
- if (tsBinary.isImportDeclaration(node)) {
144
- newNode = tsBinary.factory.updateImportDeclaration(
145
- node,
146
- node.decorators,
147
- node.modifiers,
148
- node.importClause,
149
- moduleSpecifier,
150
- node.assertClause
151
- );
152
- } else {
153
- newNode = tsBinary.factory.updateExportDeclaration(
154
- node,
155
- node.decorators,
156
- node.modifiers,
157
- node.isTypeOnly,
158
- node.exportClause,
159
- moduleSpecifier,
160
- node.assertClause
161
- );
162
- }
163
- newNode.flags = node.flags;
164
- return newNode;
165
- } catch (e) {
166
- return node;
167
- }
168
- }
169
- return tsBinary.visitEachChild(node, visitNode, ctx);
170
- };
171
- return tsBinary.visitNode(sf, visitNode);
172
- };
173
- };
174
- }
175
- function getNotAliasedPath(sf, matcher, text) {
176
- let result = matcher(text, void 0, void 0, [
177
- ".ts",
178
- ".tsx",
179
- ".js",
180
- ".jsx"
181
- ]);
182
- if (!result) {
183
- return;
184
- }
185
- if (os.platform() === "win32") {
186
- result = result.replace(/\\/g, "/");
187
- }
188
- if (!import_path.default.isAbsolute(result)) {
189
- if (!result.startsWith(".") && !result.startsWith("..")) {
190
- try {
191
- const packagePath = require.resolve(result, {
192
- paths: [process.cwd(), ...module.paths]
193
- });
194
- if (packagePath) {
195
- return result;
196
- }
197
- } catch (e) {
198
- }
199
- }
200
- try {
201
- const packagePath = require.resolve(text, {
202
- paths: [process.cwd(), ...module.paths]
203
- });
204
- if (packagePath) {
205
- return text;
206
- }
207
- } catch (e) {
208
- }
209
- }
210
- const resolvedPath = import_path.posix.relative((0, import_path.dirname)(sf.fileName), result) || "./";
211
- return resolvedPath[0] === "." ? resolvedPath : `./${resolvedPath}`;
212
- }
213
- // Annotate the CommonJS export names for ESM import in node:
214
- 0 && (module.exports = {
215
- tsconfigPathsBeforeHookFactory
216
- });
@@ -1,47 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var typescript_loader_exports = {};
19
- __export(typescript_loader_exports, {
20
- TypescriptLoader: () => TypescriptLoader
21
- });
22
- module.exports = __toCommonJS(typescript_loader_exports);
23
- class TypescriptLoader {
24
- constructor({ appDirectory }) {
25
- this.appDirectory = appDirectory;
26
- }
27
- load() {
28
- if (this.tsBinary) {
29
- return this.tsBinary;
30
- }
31
- try {
32
- const tsPath = require.resolve("typescript", {
33
- paths: [this.appDirectory || process.cwd()]
34
- });
35
- const ts = require(tsPath);
36
- return ts;
37
- } catch (error) {
38
- throw new Error(
39
- 'TypeScript could not be found! Please, install "typescript" package.'
40
- );
41
- }
42
- }
43
- }
44
- // Annotate the CommonJS export names for ESM import in node:
45
- 0 && (module.exports = {
46
- TypescriptLoader
47
- });
@@ -1,29 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var src_exports = {};
20
- __export(src_exports, {
21
- compile: () => import_common.compile
22
- });
23
- module.exports = __toCommonJS(src_exports);
24
- __reExport(src_exports, require("./compilers/babel"), module.exports);
25
- var import_common = require("./common");
26
- // Annotate the CommonJS export names for ESM import in node:
27
- 0 && (module.exports = {
28
- compile
29
- });
@@ -1,204 +0,0 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
8
- }
9
- if (info.done) {
10
- resolve(value);
11
- } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _asyncToGenerator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- var __generator = this && this.__generator || function(thisArg, body) {
31
- var f, y, t, g, _ = {
32
- label: 0,
33
- sent: function() {
34
- if (t[0] & 1) throw t[1];
35
- return t[1];
36
- },
37
- trys: [],
38
- ops: []
39
- };
40
- return(g = {
41
- next: verb(0),
42
- "throw": verb(1),
43
- "return": verb(2)
44
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
- return this;
46
- }), g);
47
- function verb(n) {
48
- return function(v) {
49
- return step([
50
- n,
51
- v
52
- ]);
53
- };
54
- }
55
- function step(op) {
56
- if (f) throw new TypeError("Generator is already executing.");
57
- while(_)try {
58
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
- if (y = 0, t) op = [
60
- op[0] & 2,
61
- t.value
62
- ];
63
- switch(op[0]){
64
- case 0:
65
- case 1:
66
- t = op;
67
- break;
68
- case 4:
69
- _.label++;
70
- return {
71
- value: op[1],
72
- done: false
73
- };
74
- case 5:
75
- _.label++;
76
- y = op[1];
77
- op = [
78
- 0
79
- ];
80
- continue;
81
- case 7:
82
- op = _.ops.pop();
83
- _.trys.pop();
84
- continue;
85
- default:
86
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
- _ = 0;
88
- continue;
89
- }
90
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
- _.label = op[1];
92
- break;
93
- }
94
- if (op[0] === 6 && _.label < t[1]) {
95
- _.label = t[1];
96
- t = op;
97
- break;
98
- }
99
- if (t && _.label < t[2]) {
100
- _.label = t[2];
101
- _.ops.push(op);
102
- break;
103
- }
104
- if (t[2]) _.ops.pop();
105
- _.trys.pop();
106
- continue;
107
- }
108
- op = body.call(thisArg, _);
109
- } catch (e) {
110
- op = [
111
- 6,
112
- e
113
- ];
114
- y = 0;
115
- } finally{
116
- f = t = 0;
117
- }
118
- if (op[0] & 5) throw op[1];
119
- return {
120
- value: op[0] ? op[1] : void 0,
121
- done: true
122
- };
123
- }
124
- };
125
- import * as path from "path";
126
- import { fs } from "@modern-js/utils";
127
- import { compileByTs } from "../compilers/typescript";
128
- import { compileByBabel } from "../compilers/babel";
129
- var FILE_EXTENSIONS = [
130
- ".js",
131
- ".ts",
132
- ".mjs",
133
- ".ejs"
134
- ];
135
- var validateAbsolutePath = function(filename, message) {
136
- if (!path.isAbsolute(filename)) {
137
- throw new Error(message);
138
- }
139
- };
140
- var validateAbsolutePaths = function(filenames, messageFunc) {
141
- filenames.forEach(function(filename) {
142
- return validateAbsolutePath(filename, messageFunc(filename));
143
- });
144
- };
145
- var compile = function() {
146
- var _ref = _asyncToGenerator(function(appDirectory, modernConfig, compileOptions) {
147
- var _modernConfig_server, sourceDirs, distDir, tsconfigPath, compiler, isTsProject, _tmp;
148
- return __generator(this, function(_state) {
149
- switch(_state.label){
150
- case 0:
151
- sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
152
- validateAbsolutePaths(sourceDirs, function(dir) {
153
- return "source dir ".concat(dir, " is not an absolute path.");
154
- });
155
- validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
156
- compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_server = modernConfig.server) === null || _modernConfig_server === void 0 ? void 0 : _modernConfig_server.compiler;
157
- _tmp = tsconfigPath;
158
- if (!_tmp) return [
159
- 3,
160
- 2
161
- ];
162
- return [
163
- 4,
164
- fs.pathExists(tsconfigPath)
165
- ];
166
- case 1:
167
- _tmp = _state.sent();
168
- _state.label = 2;
169
- case 2:
170
- isTsProject = _tmp;
171
- if (!(!isTsProject || compiler === "babel")) return [
172
- 3,
173
- 4
174
- ];
175
- return [
176
- 4,
177
- compileByBabel(appDirectory, modernConfig, compileOptions)
178
- ];
179
- case 3:
180
- _state.sent();
181
- return [
182
- 3,
183
- 6
184
- ];
185
- case 4:
186
- return [
187
- 4,
188
- compileByTs(appDirectory, modernConfig, compileOptions)
189
- ];
190
- case 5:
191
- _state.sent();
192
- _state.label = 6;
193
- case 6:
194
- return [
195
- 2
196
- ];
197
- }
198
- });
199
- });
200
- return function compile(appDirectory, modernConfig, compileOptions) {
201
- return _ref.apply(this, arguments);
202
- };
203
- }();
204
- export { FILE_EXTENSIONS, compile };