@modern-js/server-utils 1.15.1-beta.1 → 1.17.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 (27) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/js/modern/{compilers/babel/index.js → babel.js} +0 -37
  3. package/dist/js/modern/index.js +1 -2
  4. package/dist/js/node/{compilers/babel/index.js → babel.js} +3 -51
  5. package/dist/js/node/index.js +2 -14
  6. package/dist/js/treeshaking/babel.js +74 -0
  7. package/dist/js/treeshaking/index.js +1 -2
  8. package/dist/types/{compilers/babel/index.d.ts → babel.d.ts} +1 -3
  9. package/dist/types/index.d.ts +1 -2
  10. package/package.json +9 -17
  11. package/dist/js/modern/common/index.js +0 -32
  12. package/dist/js/modern/compilers/typescript/index.js +0 -101
  13. package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +0 -138
  14. package/dist/js/modern/compilers/typescript/typescript-loader.js +0 -32
  15. package/dist/js/node/common/index.js +0 -50
  16. package/dist/js/node/compilers/typescript/index.js +0 -123
  17. package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +0 -152
  18. package/dist/js/node/compilers/typescript/typescript-loader.js +0 -41
  19. package/dist/js/treeshaking/common/index.js +0 -76
  20. package/dist/js/treeshaking/compilers/babel/index.js +0 -159
  21. package/dist/js/treeshaking/compilers/typescript/index.js +0 -176
  22. package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +0 -155
  23. package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +0 -39
  24. package/dist/types/common/index.d.ts +0 -14
  25. package/dist/types/compilers/typescript/index.d.ts +0 -2
  26. package/dist/types/compilers/typescript/tsconfig-paths-plugin.d.ts +0 -2
  27. package/dist/types/compilers/typescript/typescript-loader.d.ts +0 -11
@@ -1,176 +0,0 @@
1
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
- import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
4
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
- import path from 'path';
6
- import { logger, getAlias, fs } from '@modern-js/utils';
7
- import { TypescriptLoader } from "./typescript-loader";
8
- import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
9
- import ts from 'typescript';
10
-
11
- var readTsConfigByFile = function readTsConfigByFile(tsConfigFile) {
12
- var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
13
- var _ref = parsedCmd,
14
- options = _ref.options,
15
- fileNames = _ref.fileNames,
16
- projectReferences = _ref.projectReferences;
17
- return {
18
- options: options,
19
- fileNames: fileNames,
20
- projectReferences: projectReferences
21
- };
22
- };
23
-
24
- var copyFiles = /*#__PURE__*/function () {
25
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(from, to, tsconfigPath) {
26
- var basename, targetDir;
27
- return _regeneratorRuntime().wrap(function _callee$(_context) {
28
- while (1) {
29
- switch (_context.prev = _context.next) {
30
- case 0:
31
- _context.next = 2;
32
- return fs.pathExists(from);
33
-
34
- case 2:
35
- if (!_context.sent) {
36
- _context.next = 7;
37
- break;
38
- }
39
-
40
- basename = path.basename(from);
41
- targetDir = path.join(to, basename);
42
- _context.next = 7;
43
- return fs.copy(from, targetDir, {
44
- filter: function filter(src) {
45
- return !['.ts'].includes(path.extname(src)) && src !== tsconfigPath;
46
- }
47
- });
48
-
49
- case 7:
50
- case "end":
51
- return _context.stop();
52
- }
53
- }
54
- }, _callee);
55
- }));
56
-
57
- return function copyFiles(_x, _x2, _x3) {
58
- return _ref2.apply(this, arguments);
59
- };
60
- }();
61
-
62
- export var compileByTs = /*#__PURE__*/function () {
63
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
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;
65
-
66
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
67
- while (1) {
68
- switch (_context2.prev = _context2.next) {
69
- case 0:
70
- logger.info("Running ts compile...");
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:
81
- ts = new TypescriptLoader({
82
- appDirectory: appDirectory
83
- }).load();
84
- createProgram = ts.createIncrementalProgram || ts.createProgram;
85
- formatHost = getFormatHost(ts);
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]
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;
119
-
120
- _iterator.s();
121
-
122
- case 21:
123
- if ((_step = _iterator.n()).done) {
124
- _context2.next = 27;
125
- break;
126
- }
127
-
128
- source = _step.value;
129
- _context2.next = 25;
130
- return copyFiles(source, distDir, tsconfigPath);
131
-
132
- case 25:
133
- _context2.next = 21;
134
- break;
135
-
136
- case 27:
137
- _context2.next = 32;
138
- break;
139
-
140
- case 29:
141
- _context2.prev = 29;
142
- _context2.t0 = _context2["catch"](19);
143
-
144
- _iterator.e(_context2.t0);
145
-
146
- case 32:
147
- _context2.prev = 32;
148
-
149
- _iterator.f();
150
-
151
- return _context2.finish(32);
152
-
153
- case 35:
154
- case "end":
155
- return _context2.stop();
156
- }
157
- }
158
- }, _callee2, null, [[19, 29, 32, 35]]);
159
- }));
160
-
161
- return function compileByTs(_x4, _x5, _x6) {
162
- return _ref3.apply(this, arguments);
163
- };
164
- }();
165
-
166
- var getFormatHost = function getFormatHost(ts) {
167
- return {
168
- getCanonicalFileName: function getCanonicalFileName(path) {
169
- return path;
170
- },
171
- getCurrentDirectory: ts.sys.getCurrentDirectory,
172
- getNewLine: function getNewLine() {
173
- return ts.sys.newLine;
174
- }
175
- };
176
- };
@@ -1,155 +0,0 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
4
- import * as os from 'os';
5
- import path, { dirname, posix } from 'path';
6
- import { createMatchPath } from 'tsconfig-paths';
7
-
8
- var isRegExpKey = function isRegExpKey(str) {
9
- return str.startsWith('^') || str.endsWith('$');
10
- };
11
-
12
- var resolveAliasPath = function resolveAliasPath(baseUrl, filePath) {
13
- if (path.isAbsolute(filePath)) {
14
- return filePath;
15
- }
16
-
17
- return path.resolve(baseUrl, filePath);
18
- };
19
-
20
- var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
21
- var aliasPairs = Object.keys(alias).reduce(function (o, key) {
22
- if (isRegExpKey(key)) {
23
- var regexp = new RegExp(key);
24
- var aliasPath = resolveAliasPath(baseUrl, alias[key]);
25
- o.push([regexp, aliasPath]);
26
- } else {
27
- var _aliasPath = resolveAliasPath(baseUrl, alias[key]);
28
-
29
- o.push([key, _aliasPath]);
30
- }
31
-
32
- return o;
33
- }, []);
34
- return function (requestedModule) {
35
- var _iterator = _createForOfIteratorHelper(aliasPairs),
36
- _step;
37
-
38
- try {
39
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
40
- var _step$value = _slicedToArray(_step.value, 2),
41
- key = _step$value[0],
42
- value = _step$value[1];
43
-
44
- if (key instanceof RegExp) {
45
- if (key.test(requestedModule)) {
46
- return value;
47
- }
48
- }
49
-
50
- if (requestedModule === key) {
51
- return value;
52
- }
53
- }
54
- } catch (err) {
55
- _iterator.e(err);
56
- } finally {
57
- _iterator.f();
58
- }
59
- };
60
- };
61
-
62
- export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
63
- var tsPaths = {};
64
- var alias = {};
65
- Object.keys(paths).forEach(function (key) {
66
- if (Array.isArray(paths[key])) {
67
- tsPaths[key] = paths[key];
68
- } else {
69
- alias[key] = paths[key];
70
- }
71
- });
72
- var matchAliasPath = createAliasMatcher(baseUrl, alias);
73
- var matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
74
-
75
- var matchPath = function matchPath(requestedModule, readJSONSync, fileExists, extensions) {
76
- var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
77
-
78
- if (result) {
79
- return result;
80
- }
81
-
82
- return matchAliasPath(requestedModule);
83
- };
84
-
85
- if (Object.keys(paths).length === 0) {
86
- return undefined;
87
- }
88
-
89
- return function (ctx) {
90
- return function (sf) {
91
- var visitNode = function visitNode(node) {
92
- if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
93
- try {
94
- var importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
95
-
96
- if (!importPathWithQuotes) {
97
- return node;
98
- }
99
-
100
- var text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
101
- var result = getNotAliasedPath(sf, matchPath, text);
102
-
103
- if (!result) {
104
- return node;
105
- }
106
-
107
- var moduleSpecifier = tsBinary.factory.createStringLiteral(result);
108
- moduleSpecifier.parent = node.moduleSpecifier.parent;
109
-
110
- if (tsBinary.isImportDeclaration(node)) {
111
- return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
112
- } else {
113
- return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
114
- }
115
- } catch (_unused) {
116
- return node;
117
- }
118
- }
119
-
120
- return tsBinary.visitEachChild(node, visitNode, ctx);
121
- };
122
-
123
- return tsBinary.visitNode(sf, visitNode);
124
- };
125
- };
126
- } // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
127
-
128
- function getNotAliasedPath(sf, matcher, text) {
129
- var result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
130
-
131
- if (!result) {
132
- return;
133
- }
134
-
135
- if (os.platform() === 'win32') {
136
- result = result.replace(/\\/g, '/');
137
- }
138
-
139
- if (!path.isAbsolute(result)) {
140
- try {
141
- // Installed packages (node modules) should take precedence over root files with the same name.
142
- // Ref: https://github.com/nestjs/nest-cli/issues/838
143
- var packagePath = require.resolve(text, {
144
- paths: [process.cwd()].concat(_toConsumableArray(module.paths))
145
- });
146
-
147
- if (packagePath) {
148
- return text;
149
- }
150
- } catch (_unused2) {}
151
- }
152
-
153
- var resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
154
- return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
155
- }
@@ -1,39 +0,0 @@
1
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/esm/createClass";
3
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
- export var TypescriptLoader = /*#__PURE__*/function () {
5
- function TypescriptLoader(_ref) {
6
- var appDirectory = _ref.appDirectory;
7
-
8
- _classCallCheck(this, TypescriptLoader);
9
-
10
- _defineProperty(this, "tsBinary", void 0);
11
-
12
- _defineProperty(this, "appDirectory", void 0);
13
-
14
- this.appDirectory = appDirectory;
15
- }
16
-
17
- _createClass(TypescriptLoader, [{
18
- key: "load",
19
- value: function load() {
20
- if (this.tsBinary) {
21
- return this.tsBinary;
22
- }
23
-
24
- try {
25
- var tsPath = require.resolve('typescript', {
26
- paths: [this.appDirectory || process.cwd()]
27
- });
28
-
29
- var _ts = require(tsPath);
30
-
31
- return _ts;
32
- } catch (error) {
33
- throw new Error('TypeScript could not be found! Please, install "typescript" package.');
34
- }
35
- }
36
- }]);
37
-
38
- return TypescriptLoader;
39
- }();
@@ -1,14 +0,0 @@
1
- import type { NormalizedConfig } from '@modern-js/core';
2
- export interface Pattern {
3
- from: string;
4
- to: string;
5
- tsconfigPath?: string;
6
- }
7
- export interface CompileOptions {
8
- sourceDirs: string[];
9
- distDir: string;
10
- tsconfigPath?: string;
11
- }
12
- export declare type CompileFunc = (appDirectory: string, modernConfig: NormalizedConfig, compileOptions: CompileOptions) => Promise<void>;
13
- export declare const FILE_EXTENSIONS: string[];
14
- export declare const compile: CompileFunc;
@@ -1,2 +0,0 @@
1
- import type { CompileFunc } from '../../common';
2
- export declare const compileByTs: CompileFunc;
@@ -1,2 +0,0 @@
1
- import * as ts from 'typescript';
2
- export declare function tsconfigPathsBeforeHookFactory(tsBinary: typeof ts, baseUrl: string, paths: Record<string, string[] | string>): ((ctx: ts.TransformationContext) => ts.Transformer<any>) | undefined;
@@ -1,11 +0,0 @@
1
- import type ts from 'typescript';
2
- export declare class TypescriptLoader {
3
- private tsBinary?;
4
- private appDirectory?;
5
- constructor({
6
- appDirectory
7
- }: {
8
- appDirectory: string;
9
- });
10
- load(): typeof ts;
11
- }