susee 0.1.0 → 0.1.1
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/LICENSE +201 -19
- package/README.md +96 -44
- package/bin/index.mjs +3 -0
- package/dist/index.cjs +985 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -21
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +17 -21
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +947 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +37 -28
- package/dist/banner-text/index.cjs +0 -6
- package/dist/banner-text/index.cjs.map +0 -1
- package/dist/banner-text/index.d.cts +0 -9
- package/dist/banner-text/index.d.mts +0 -9
- package/dist/banner-text/index.mjs +0 -6
- package/dist/banner-text/index.mjs.map +0 -1
- package/dist/minify/index.cjs +0 -6
- package/dist/minify/index.cjs.map +0 -1
- package/dist/minify/index.d.cts +0 -10
- package/dist/minify/index.d.mts +0 -10
- package/dist/minify/index.mjs +0 -6
- package/dist/minify/index.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,988 @@
|
|
|
1
1
|
/*! *****************************************************************************
|
|
2
|
-
Copyright
|
|
2
|
+
Copyright (c) Pho Thin Mg <phothinmg@disroot.org>
|
|
3
3
|
|
|
4
|
-
Licensed under the
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
5
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
6
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
5
7
|
***************************************************************************** */
|
|
6
|
-
"use strict";var __assign=this&&this.__assign||function(){return __assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},__assign.apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(i,s){function o(e){try{c(n.next(e))}catch(e){s(e)}}function a(e){try{c(n.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(o,a)}c((n=n.apply(e,t||[])).next())})},__generator=this&&this.__generator||function(e,t){var r,n,i,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=a(0),o.throw=a(1),o.return=a(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(c){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(s=0)),s;)try{if(r=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,n=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=t.call(e,s)}catch(e){a=[6,e],n=0}finally{r=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}},__rest=this&&this.__rest||function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(e);i<n.length;i++)t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i])&&(r[n[i]]=e[n[i]])}return r},__spreadArray=this&&this.__spreadArray||function(e,t,r){if(r||2===arguments.length)for(var n,i=0,s=t.length;i<s;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});var dependensia_1=__importDefault(require("dependensia")),node_fs_1=__importDefault(require("node:fs")),node_path_1=__importDefault(require("node:path")),typescript_1=__importDefault(require("typescript")),resolvePath=typescript_1.default.sys.resolvePath,fileExists=typescript_1.default.sys.fileExists,deleteFile=typescript_1.default.sys.deleteFile,directoryExists=typescript_1.default.sys.directoryExists,createDirectory=typescript_1.default.sys.createDirectory,writeFile=typescript_1.default.sys.writeFile,readFile=typescript_1.default.sys.readFile,wait=function(e){return new Promise(function(t){return setTimeout(t,e)})},writeOutFile=function(e,t){var r=resolvePath(e),n=node_path_1.default.dirname(r);fileExists(r)&&"function"==typeof deleteFile?deleteFile(r):directoryExists(n)?"function"==typeof deleteFile&&deleteFile(r):createDirectory(n),writeFile(r,t)};function getEntryPath(e){var t=e.match(/^\.+/);if(t){var r=t[0].length+1;return e.slice(r).trim()}return e}function clearFolder(e){return __awaiter(this,void 0,void 0,function(){var t,r;return __generator(this,function(n){switch(n.label){case 0:e=node_path_1.default.resolve(process.cwd(),e),n.label=1;case 1:return n.trys.push([1,4,,5]),[4,node_fs_1.default.promises.readdir(e,{withFileTypes:!0})];case 2:return t=n.sent(),[4,Promise.all(t.map(function(t){return node_fs_1.default.promises.rm(node_path_1.default.join(e,t.name),{recursive:!0})}))];case 3:return n.sent(),[3,5];case 4:if("ENOENT"!==(r=n.sent()).code)throw r;return[3,5];case 5:return[2]}})})}function mergeImports(e){for(var t,r,n,i=new Map,s=new Map,o=new Map,a=new Map,c=new Map,u=function(e){var u=e.match(/import\s+(?:type\s+)?(?:(.*?)\s+from\s+)?["']([^"']+)["'];?/);if(!u)return"continue";var l=u[1],p=u[2],f=e.includes("import type");if(!l){var d=e.match(/import\s+(?:type\s+)?(\w+)/);if(d){var m=d[1];(v=f?a:o).has(p)||v.set(p,new Set),null===(t=v.get(p))||void 0===t||t.add(m)}return"continue"}if(l.startsWith("{")){var _=f?s:i;_.has(p)||_.set(p,new Set),l.replace(/[{}]/g,"").split(",").map(function(e){return e.trim()}).filter(Boolean).forEach(function(e){var t;return null===(t=_.get(p))||void 0===t?void 0:t.add(e)})}else if(l.startsWith("* as")){var h=l.match(/\*\s+as\s+(\w+)/);if(h){var y=h[1];c.has(p)||c.set(p,new Set),null===(r=c.get(p))||void 0===r||r.add(y)}}else{var v;(v=f?a:o).has(p)||v.set(p,new Set),null===(n=v.get(p))||void 0===n||n.add(l.trim())}},l=0,p=e;l<p.length;l++){u(p[l])}for(var f=[],d=0,m=i;d<m.length;d++){for(var _=m[d],h=_[0],y=_[1],v=s.get(h)||new Set,g=new Set(__spreadArray([],y,!0)),w=0,j=v;w<j.length;w++){var b=j[w];y.has(b)||g.add(b)}if(g.size>0){var E=Array.from(g).sort().join(", ");f.push("import { ".concat(E,' } from "').concat(h,'";'))}}for(var x=0,F=s;x<F.length;x++){var S=F[x];h=S[0],v=S[1];if(!i.has(h)&&v.size>0){E=Array.from(v).sort().join(", ");f.push("import type { ".concat(E,' } from "').concat(h,'";'))}}for(var D=0,C=o;D<C.length;D++){for(var O=C[D],P=(h=O[0],O[1]),T=a.get(h)||new Set,A=(g=new Set(__spreadArray([],P,!0)),0),N=T;A<N.length;A++){b=N[A];P.has(b)||g.add(b)}if(g.size>0){E=Array.from(g).join(", ");f.push("import ".concat(E,' from "').concat(h,'";'))}}for(var M=0,I=a;M<I.length;M++){var k=I[M];h=k[0],T=k[1];if(!o.has(h)&&T.size>0){E=Array.from(T).join(", ");f.push("import type ".concat(E,' from "').concat(h,'";'))}}for(var L=0,q=c;L<q.length;L++){var z=q[L],W=(h=z[0],z[1]);if(W.size>0){E=Array.from(W).join(", ");f.push("import * as ".concat(E,' from "').concat(h,'";'))}}return f.sort()}function bundle(e){return __awaiter(this,arguments,Promise,function(e,t){var r,n,i,s,o,a,c,u,l,p,f,d,m;return void 0===t&&(t=!1),__generator(this,function(_){switch(_.label){case 0:return r=!1,n=!1,[4,(0,dependensia_1.default)(e)];case 1:return i=_.sent(),s=i.sort(),o=s.map(function(e){return{filePath:e,fileContent:readFile(resolvePath(e),"utf8")}}),a=[],c=function(e,i){void 0===i&&(i=!0);var s=typescript_1.default.getDefaultCompilerOptions(),o=typescript_1.default.createSourceFile(e.filePath,e.fileContent,typescript_1.default.ScriptTarget.Latest,!0),c=typescript_1.default.transform(o,[function(e){var s=e.factory;function c(t){if(typescript_1.default.isImportDeclaration(t)||typescript_1.default.isImportEqualsDeclaration(t)){var u=t.getText(o);return a.push(u),s.createEmptyStatement()}if(i){if(typescript_1.default.isExportDeclaration(t))return s.createEmptyStatement();if(typescript_1.default.isExportAssignment(t)){var l=t.expression;if(typescript_1.default.isIdentifier(l))return s.createEmptyStatement()}}else if(typescript_1.default.isExportDeclaration(t)&&(r=!0),typescript_1.default.isExportAssignment(t)){l=t.expression;typescript_1.default.isIdentifier(l)&&(n=!0)}return typescript_1.default.visitEachChild(t,c,e)}function u(i){return r&&n&&!t&&typescript_1.default.isExportDeclaration(i)?s.createEmptyStatement():typescript_1.default.visitEachChild(i,u,e)}return function(e){return typescript_1.default.visitNode(e,function(e){var i=c(e);return r&&n&&!t?u(i):i})}}],s),u=c.transformed[0],l=typescript_1.default.createPrinter({newLine:typescript_1.default.NewLineKind.LineFeed,removeComments:!1}).printFile(u);return c.dispose(),l.replace(/^s*;\s*$/gm,"").trim()},u=o.slice(0,-1),l=o.slice(-1),p=u.map(function(e){return c(e,!0)}),f=l.map(function(e){return c(e,!1)}),d=p.concat(f),[4,wait(1e3)];case 2:return _.sent(),m=/["']((?!\.\/|\.\.\/)[^"']+)["']/,a=mergeImports(a=a.filter(function(e){return m.test(e)})),[2,{code:"".concat(a.join("\n"),"\n").concat(d.join("\n"),"\n").trim(),dexport:r&&n}]}})})}var isCjs=function(e){return e.commonjs&&e.commonjsTypes},isEsm=function(e){return e.esm&&e.esmTypes};function getExports(e,t,r){var n,i,s;if(void 0===t&&(t=!0),!t&&(!r||r&&r.split(node_path_1.default.sep).length<2))throw new Error("If it is not main export,required out-dir and sub-dir like `foo/bar`");var o=t?".":"./".concat(null==r?void 0:r.split(node_path_1.default.sep).slice(-1)[0]);return isCjs(e)&&isEsm(e)?((n={})[o]={import:{default:"./".concat(e.esm),types:"./".concat(e.esmTypes)},require:{default:"./".concat(e.commonjs),types:"./".concat(e.commonjsTypes)}},n):isCjs(e)&&!isEsm(e)?((i={})[o]={require:{default:"./".concat(e.commonjs),types:"./".concat(e.commonjsTypes)}},i):!isCjs(e)&&isEsm(e)?((s={})[o]={import:{default:"./".concat(e.esm),types:"./".concat(e.esmTypes)}},s):{}}function writePackage(e){return __awaiter(this,arguments,void 0,function(e,t,r){var n,i,s,o,a,c,u,l,p,f,d,m,_,h,y,v,g,w;return void 0===t&&(t=!0),__generator(this,function(j){switch(j.label){case 0:return console.time("Write package.json"),n=node_path_1.default.resolve(process.cwd(),"package.json"),[4,node_fs_1.default.promises.readFile(n,"utf8")];case 1:return i=j.sent(),s=JSON.parse(i),o=s.name,a=s.version,c=s.description,u=s.main,l=s.module,p=s.type,f=s.types,d=s.exports,m=__rest(s,["name","version","description","main","module","type","types","exports"]),[4,wait(500)];case 2:return j.sent(),t&&(p=isEsm(e)?"module":"commonjs"),_={},h={},y={},v={},t?(_=e.main?{main:e.main}:{},h=e.module?{module:e.module}:{},y=e.types?{types:e.types}:{},v={exports:__assign({},getExports(e,t,r))}):(_=u?{main:u}:{},h=l?{module:l}:{},y=f?{types:f}:{},g=d&&"object"==typeof d&&!Array.isArray(d)?d:d?{".":d}:{},v={exports:__assign(__assign({},g),getExports(e,t,r))}),[4,wait(1e3)];case 3:return j.sent(),w=__assign(__assign(__assign(__assign(__assign({name:o,version:a,description:c,type:p},_),y),h),v),m),[4,node_fs_1.default.promises.writeFile(n,JSON.stringify(w,null,2))];case 4:return j.sent(),console.timeEnd("Write package.json"),[2]}})})}var susee,Compilers=function(){function e(e){this._target=null!=e?e:"both",this.files={commonjs:void 0,commonjsTypes:void 0,esm:void 0,esmTypes:void 0,main:void 0,module:void 0,types:void 0}}return e.prototype.commonjs=function(e,t,r){return __awaiter(this,arguments,void 0,function(e,t,r,n,i,s,o){var a,c,u,l,p,f=this;return void 0===n&&(n=!0),__generator(this,function(d){return console.time("Compiled Commonjs"),a=i||"",c=s||[],u={outDir:r,module:typescript_1.default.ModuleKind.CommonJS,sourceMap:!0,strict:!0,esModuleInterop:!0,noImplicitAny:!0,declaration:!0},l={},p={getSourceFile:function(r,n){if(r===t)return typescript_1.default.createSourceFile(r,e,n)},writeFile:function(e,t){l[e]=t},getDefaultLibFileName:function(e){return typescript_1.default.getDefaultLibFilePath(e)},getCurrentDirectory:function(){return""},getDirectories:function(){return[]},fileExists:function(e){return e===t},readFile:function(r){return r===t?e:void 0},getCanonicalFileName:function(e){return e},useCaseSensitiveFileNames:function(){return!0},getNewLine:function(){return"\n"}},typescript_1.default.createProgram([t],u,p).emit(),Object.entries(l).map(function(e){return __awaiter(f,[e],void 0,function(e){var t,r,i,s,u,l,p,f,d,m=e[0],_=e[1];return __generator(this,function(e){switch(e.label){case 0:if(".js"===(t=node_path_1.default.extname(m))&&(_=_.replace("exports.default = ".concat(a,";"),"module.exports = ".concat(a,";")),c.length>0))for(r=0,i=c;r<i.length;r++)l=i[r],_=_.replace(l,"").trim();if(".ts"===t&&(_=_.replace("export default ".concat(a,";"),"export = ".concat(a,";")),c.length>0))for(s=0,u=c;s<u.length;s++)l=u[s],_=_.replace(l,"").trim();if(!(null==o?void 0:o.length))return[3,5];p=0,f=o,e.label=1;case 1:return p<f.length?(d=f[p]).async?[4,d.func(_,m)]:[3,3]:[3,5];case 2:return _=e.sent(),[3,4];case 3:_=d.func(_,m),e.label=4;case 4:return p++,[3,1];case 5:return m.match(/.js/g)&&(this.files.commonjs=m.replace(/.js/g,".cjs")),m.match(/.d.ts/g)&&(this.files.commonjsTypes=m.replace(/.d.ts/g,".d.cts")),!n||"both"!==this._target&&"commonjs"!==this._target||(this.files.commonjs&&(this.files.main=this.files.commonjs),this.files.commonjsTypes&&(this.files.types=this.files.commonjsTypes)),m=(m=(m=m.replace(/.js/g,".cjs")).replace(/.map.js/g,".map.cjs")).replace(/.d.ts/g,".d.cts"),[4,wait(500)];case 6:return e.sent(),writeOutFile(m,_),[2]}})})}),console.timeEnd("Compiled Commonjs"),[2]})})},e.prototype.esm=function(e,t,r){return __awaiter(this,arguments,void 0,function(e,t,r,n,i){var s,o,a,c=this;return void 0===n&&(n=!0),__generator(this,function(u){return console.time("Compiled ESM"),s={outDir:r,module:typescript_1.default.ModuleKind.ES2020,sourceMap:!0,strict:!0,esModuleInterop:!0,noImplicitAny:!0,declaration:!0},o={},a={getSourceFile:function(r,n){if(r===t)return typescript_1.default.createSourceFile(r,e,n)},writeFile:function(e,t){o[e]=t},getDefaultLibFileName:function(e){return typescript_1.default.getDefaultLibFilePath(e)},getCurrentDirectory:function(){return""},getDirectories:function(){return[]},fileExists:function(e){return e===t},readFile:function(r){return r===t?e:void 0},getCanonicalFileName:function(e){return e},useCaseSensitiveFileNames:function(){return!0},getNewLine:function(){return"\n"}},typescript_1.default.createProgram([t],s,a).emit(),Object.entries(o).map(function(e){return __awaiter(c,[e],void 0,function(e){var t,r,s,o=e[0],a=e[1];return __generator(this,function(e){switch(e.label){case 0:if(!(null==i?void 0:i.length))return[3,5];t=0,r=i,e.label=1;case 1:return t<r.length?(s=r[t]).async?[4,s.func(a,o)]:[3,3]:[3,5];case 2:return a=e.sent(),[3,4];case 3:a=s.func(a,o),e.label=4;case 4:return t++,[3,1];case 5:return o.match(/.js/g)&&(this.files.esm=o.replace(/.js/g,".mjs")),o.match(/.d.ts/g)&&(this.files.esmTypes=o.replace(/.d.ts/g,".d.mts")),n&&"both"===this._target&&this.files.esm&&(this.files.module=this.files.esm),o=(o=(o=o.replace(/.js/g,".mjs")).replace(/.map.js/g,".map.mjs")).replace(/.d.ts/g,".d.mts"),[4,wait(500)];case 6:return e.sent(),writeOutFile(o,a),[2]}})})}),console.timeEnd("Compiled ESM"),[2]})})},e}();!function(e){e.build=function(e){return __awaiter(this,arguments,void 0,function(e){var t,r,n,i,s=e.entry,o=e.target,a=void 0===o?"both":o,c=e.defaultExportName,u=void 0===c?void 0:c,l=e.outDir,p=void 0===l?"dist":l,f=e.isMainExport,d=void 0===f||f,m=e.replaceWithBlank,_=void 0===m?[]:m,h=e.hooks,y=void 0===h?[]:h;return __generator(this,function(e){switch(e.label){case 0:return console.time("Build Time"),node_fs_1.default.existsSync(p)?[4,clearFolder(p)]:[3,2];case 1:e.sent(),e.label=2;case 2:return s=getEntryPath(s),t=!1,r=new Compilers,"commonjs"!==a?[3,5]:[4,bundle(s,!1)];case 3:return n=e.sent(),[4,r.commonjs(n.code,s,p,d,u,_,y)];case 4:return e.sent(),t=n.dexport,[3,14];case 5:return"esm"!==a?[3,8]:[4,bundle(s,!0)];case 6:return i=e.sent(),[4,r.esm(i.code,s,p,d,y)];case 7:return e.sent(),[3,14];case 8:return"both"!==a?[3,14]:[4,bundle(s,!1)];case 9:return n=e.sent(),[4,r.commonjs(n.code,s,p,d,u,_,y)];case 10:return e.sent(),t=n.dexport,[4,wait(1e3)];case 11:return e.sent(),[4,bundle(s,!0)];case 12:return i=e.sent(),[4,r.esm(i.code,s,p,d,y)];case 13:e.sent(),e.label=14;case 14:return[4,wait(1e3)];case 15:return e.sent(),[4,writePackage(r.files,d,p)];case 16:return e.sent(),t&&console.warn("Found both `Name Export` and `Default Export` at entry,that will lose `Name Export` in commonjs output. "),console.timeEnd("Build Time"),[2]}})})}}(susee||(susee={})),module.exports=susee;
|
|
8
|
+
"use strict";
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
+
}
|
|
15
|
+
Object.defineProperty(o, k2, desc);
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
+
}) : function(o, v) {
|
|
23
|
+
o["default"] = v;
|
|
24
|
+
});
|
|
25
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
26
|
+
var ownKeys = function(o) {
|
|
27
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
28
|
+
var ar = [];
|
|
29
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
30
|
+
return ar;
|
|
31
|
+
};
|
|
32
|
+
return ownKeys(o);
|
|
33
|
+
};
|
|
34
|
+
return function (mod) {
|
|
35
|
+
if (mod && mod.__esModule) return mod;
|
|
36
|
+
var result = {};
|
|
37
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
38
|
+
__setModuleDefault(result, mod);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
})();
|
|
42
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
43
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
exports.susee = susee;
|
|
47
|
+
const tsconfig_1 = __importDefault(require("@suseejs/tsconfig"));
|
|
48
|
+
const anonymous_1 = __importDefault(require("@suseejs/anonymous"));
|
|
49
|
+
const dependencies_1 = __importDefault(require("@suseejs/dependencies"));
|
|
50
|
+
const duplicates_1 = __importDefault(require("@suseejs/duplicates"));
|
|
51
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
52
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
53
|
+
const resolves_1 = __importDefault(require("@phothinmaung/resolves"));
|
|
54
|
+
const tcolor_1 = __importDefault(require("@suseejs/tcolor"));
|
|
55
|
+
const transformer_1 = __importDefault(require("@suseejs/transformer"));
|
|
56
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
57
|
+
const utils_1 = __importDefault(require("@suseejs/utils"));
|
|
58
|
+
function removeExportExpressionHandler(compilerOptions) {
|
|
59
|
+
return ({ file, content }) => {
|
|
60
|
+
const sourceFile = typescript_1.default.createSourceFile(file, content, typescript_1.default.ScriptTarget.Latest, true);
|
|
61
|
+
const transformer = (context) => {
|
|
62
|
+
const { factory } = context;
|
|
63
|
+
const visitor = (node) => {
|
|
64
|
+
if (typescript_1.default.isFunctionDeclaration(node) ||
|
|
65
|
+
typescript_1.default.isClassDeclaration(node) ||
|
|
66
|
+
typescript_1.default.isInterfaceDeclaration(node) ||
|
|
67
|
+
typescript_1.default.isTypeAliasDeclaration(node) ||
|
|
68
|
+
typescript_1.default.isEnumDeclaration(node) ||
|
|
69
|
+
typescript_1.default.isVariableStatement(node)) {
|
|
70
|
+
const modifiers = node.modifiers?.filter((m) => m.kind !== typescript_1.default.SyntaxKind.ExportKeyword &&
|
|
71
|
+
m.kind !== typescript_1.default.SyntaxKind.DefaultKeyword);
|
|
72
|
+
if (modifiers?.length !== node.modifiers?.length) {
|
|
73
|
+
if (typescript_1.default.isFunctionDeclaration(node)) {
|
|
74
|
+
return factory.updateFunctionDeclaration(node, modifiers, node.asteriskToken, node.name, node.typeParameters, node.parameters, node.type, node.body);
|
|
75
|
+
}
|
|
76
|
+
if (typescript_1.default.isClassDeclaration(node)) {
|
|
77
|
+
return factory.updateClassDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, node.members);
|
|
78
|
+
}
|
|
79
|
+
if (typescript_1.default.isInterfaceDeclaration(node)) {
|
|
80
|
+
return factory.updateInterfaceDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, node.members);
|
|
81
|
+
}
|
|
82
|
+
if (typescript_1.default.isTypeAliasDeclaration(node)) {
|
|
83
|
+
return factory.updateTypeAliasDeclaration(node, modifiers, node.name, node.typeParameters, node.type);
|
|
84
|
+
}
|
|
85
|
+
if (typescript_1.default.isEnumDeclaration(node)) {
|
|
86
|
+
return factory.updateEnumDeclaration(node, modifiers, node.name, node.members);
|
|
87
|
+
}
|
|
88
|
+
if (typescript_1.default.isVariableStatement(node)) {
|
|
89
|
+
return factory.updateVariableStatement(node, modifiers, node.declarationList);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (typescript_1.default.isExportDeclaration(node)) {
|
|
94
|
+
return factory.createEmptyStatement();
|
|
95
|
+
}
|
|
96
|
+
if (typescript_1.default.isExportAssignment(node)) {
|
|
97
|
+
const expr = node.expression;
|
|
98
|
+
if (typescript_1.default.isIdentifier(expr)) {
|
|
99
|
+
return factory.createEmptyStatement();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
103
|
+
};
|
|
104
|
+
return (rootNode) => typescript_1.default.visitNode(rootNode, visitor);
|
|
105
|
+
};
|
|
106
|
+
let _content = (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
|
|
107
|
+
_content = _content.replace(/^s*;\s*$/gm, "").trim();
|
|
108
|
+
return { file, content: _content };
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function removeImportExpressionHandler(removedStatements, compilerOptions) {
|
|
112
|
+
return ({ file, content }) => {
|
|
113
|
+
const sourceFile = typescript_1.default.createSourceFile(file, content, typescript_1.default.ScriptTarget.Latest, true);
|
|
114
|
+
const transformer = (context) => {
|
|
115
|
+
const { factory } = context;
|
|
116
|
+
const visitor = (node) => {
|
|
117
|
+
if (typescript_1.default.isImportDeclaration(node)) {
|
|
118
|
+
const text = node.getText(sourceFile);
|
|
119
|
+
removedStatements.push(text);
|
|
120
|
+
return factory.createEmptyStatement();
|
|
121
|
+
}
|
|
122
|
+
if (typescript_1.default.isImportEqualsDeclaration(node)) {
|
|
123
|
+
const text = node.getText(sourceFile);
|
|
124
|
+
removedStatements.push(text);
|
|
125
|
+
return factory.createEmptyStatement();
|
|
126
|
+
}
|
|
127
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
128
|
+
};
|
|
129
|
+
return (rootNode) => typescript_1.default.visitNode(rootNode, visitor);
|
|
130
|
+
};
|
|
131
|
+
let _content = (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
|
|
132
|
+
_content = _content.replace(/^s*;\s*$/gm, "").trim();
|
|
133
|
+
return { file, content: _content };
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function mergeImports(imports) {
|
|
137
|
+
const importMap = new Map();
|
|
138
|
+
const typeImportMap = new Map();
|
|
139
|
+
const defaultImports = new Map();
|
|
140
|
+
const typeDefaultImports = new Map();
|
|
141
|
+
const namespaceImports = new Map();
|
|
142
|
+
for (const importStr of imports) {
|
|
143
|
+
const importMatch = importStr.match(/import\s+(?:type\s+)?(?:(.*?)\s+from\s+)?["']([^"']+)["'];?/);
|
|
144
|
+
if (!importMatch)
|
|
145
|
+
continue;
|
|
146
|
+
const [, importClause, _modulePath] = importMatch;
|
|
147
|
+
const isTypeImport = importStr.includes("import type");
|
|
148
|
+
const modulePath = _modulePath;
|
|
149
|
+
if (!importClause) {
|
|
150
|
+
const defaultMatch = importStr.match(/import\s+(?:type\s+)?(\w+)/);
|
|
151
|
+
if (defaultMatch) {
|
|
152
|
+
const importName = defaultMatch[1];
|
|
153
|
+
const targetMap = isTypeImport ? typeDefaultImports : defaultImports;
|
|
154
|
+
if (!targetMap.has(modulePath))
|
|
155
|
+
targetMap.set(modulePath, new Set());
|
|
156
|
+
targetMap.get(modulePath)?.add(importName);
|
|
157
|
+
}
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (importClause.startsWith("{")) {
|
|
161
|
+
const targetMap = isTypeImport ? typeImportMap : importMap;
|
|
162
|
+
if (!targetMap.has(modulePath))
|
|
163
|
+
targetMap.set(modulePath, new Set());
|
|
164
|
+
const names = importClause
|
|
165
|
+
.replace(/[{}]/g, "")
|
|
166
|
+
.split(",")
|
|
167
|
+
.map((s) => s.trim())
|
|
168
|
+
.filter(Boolean);
|
|
169
|
+
names.forEach((name) => targetMap.get(modulePath)?.add(name));
|
|
170
|
+
}
|
|
171
|
+
else if (importClause.startsWith("* as")) {
|
|
172
|
+
const namespaceMatch = importClause.match(/\*\s+as\s+(\w+)/);
|
|
173
|
+
if (namespaceMatch) {
|
|
174
|
+
const namespaceName = namespaceMatch[1];
|
|
175
|
+
if (!namespaceImports.has(modulePath))
|
|
176
|
+
namespaceImports.set(modulePath, new Set());
|
|
177
|
+
namespaceImports.get(modulePath)?.add(namespaceName);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
const targetMap = isTypeImport ? typeDefaultImports : defaultImports;
|
|
182
|
+
if (!targetMap.has(modulePath))
|
|
183
|
+
targetMap.set(modulePath, new Set());
|
|
184
|
+
targetMap.get(modulePath)?.add(importClause.trim());
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const mergedImports = [];
|
|
188
|
+
for (const [modulePath, regularNames] of importMap) {
|
|
189
|
+
const typeNames = typeImportMap.get(modulePath) || new Set();
|
|
190
|
+
const finalNames = new Set([...regularNames]);
|
|
191
|
+
for (const typeName of typeNames) {
|
|
192
|
+
if (!regularNames.has(typeName)) {
|
|
193
|
+
finalNames.add(typeName);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (finalNames.size > 0) {
|
|
197
|
+
const importNames = Array.from(finalNames).sort().join(", ");
|
|
198
|
+
mergedImports.push(`import { ${importNames} } from "${modulePath}";`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
for (const [modulePath, typeNames] of typeImportMap) {
|
|
202
|
+
if (!importMap.has(modulePath) && typeNames.size > 0) {
|
|
203
|
+
const importNames = Array.from(typeNames).sort().join(", ");
|
|
204
|
+
mergedImports.push(`import type { ${importNames} } from "${modulePath}";`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const [modulePath, regularDefaultNames] of defaultImports) {
|
|
208
|
+
const typeDefaultNames = typeDefaultImports.get(modulePath) || new Set();
|
|
209
|
+
const finalNames = new Set([...regularDefaultNames]);
|
|
210
|
+
for (const typeName of typeDefaultNames) {
|
|
211
|
+
if (!regularDefaultNames.has(typeName)) {
|
|
212
|
+
finalNames.add(typeName);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (finalNames.size > 0) {
|
|
216
|
+
const importNames = Array.from(finalNames).join(", ");
|
|
217
|
+
mergedImports.push(`import ${importNames} from "${modulePath}";`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
for (const [modulePath, typeDefaultNames] of typeDefaultImports) {
|
|
221
|
+
if (!defaultImports.has(modulePath) && typeDefaultNames.size > 0) {
|
|
222
|
+
const importNames = Array.from(typeDefaultNames).join(", ");
|
|
223
|
+
mergedImports.push(`import type ${importNames} from "${modulePath}";`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (const [modulePath, names] of namespaceImports) {
|
|
227
|
+
if (names.size > 0) {
|
|
228
|
+
const importNames = Array.from(names).join(", ");
|
|
229
|
+
mergedImports.push(`import * as ${importNames} from "${modulePath}";`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return mergedImports.sort();
|
|
233
|
+
}
|
|
234
|
+
async function bundler({ depsFiles, compilerOptions, renameDuplicates, }) {
|
|
235
|
+
const reName = renameDuplicates ?? true;
|
|
236
|
+
const namesMap = new Map();
|
|
237
|
+
const callNameMap = [];
|
|
238
|
+
const importNameMap = [];
|
|
239
|
+
const exportNameMap = [];
|
|
240
|
+
const exportDefaultExportNameMap = [];
|
|
241
|
+
const exportDefaultImportNameMap = [];
|
|
242
|
+
let removedStatements = [];
|
|
243
|
+
if (reName) {
|
|
244
|
+
depsFiles = await duplicates_1.default.renamed(depsFiles, namesMap, callNameMap, importNameMap, exportNameMap, compilerOptions);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
depsFiles = await duplicates_1.default.notRenamed(depsFiles, namesMap, compilerOptions);
|
|
248
|
+
}
|
|
249
|
+
await utils_1.default.wait(1000);
|
|
250
|
+
depsFiles = await (0, anonymous_1.default)(depsFiles, exportDefaultExportNameMap, exportDefaultImportNameMap, compilerOptions);
|
|
251
|
+
await utils_1.default.wait(1000);
|
|
252
|
+
const removeImports = (0, resolves_1.default)([
|
|
253
|
+
[removeImportExpressionHandler, removedStatements, compilerOptions],
|
|
254
|
+
]);
|
|
255
|
+
const removeImport = await removeImports.concurrent();
|
|
256
|
+
depsFiles = depsFiles.map(removeImport[0]);
|
|
257
|
+
await utils_1.default.wait(1000);
|
|
258
|
+
const removeExports = (0, resolves_1.default)([
|
|
259
|
+
[removeExportExpressionHandler, compilerOptions],
|
|
260
|
+
]);
|
|
261
|
+
const removeExport = await removeExports.concurrent();
|
|
262
|
+
const deps_files = depsFiles.slice(0, -1).map(removeExport[0]);
|
|
263
|
+
const mainFile = depsFiles.slice(-1);
|
|
264
|
+
const regexp = /["']((?!\.\/|\.\.\/)[^"']+)["']/;
|
|
265
|
+
removedStatements = removedStatements.filter((i) => regexp.test(i));
|
|
266
|
+
removedStatements = mergeImports(removedStatements);
|
|
267
|
+
const importStatements = removedStatements.join("\n").trim();
|
|
268
|
+
const depFilesContent = deps_files
|
|
269
|
+
.map((i) => {
|
|
270
|
+
const file = `//${node_path_1.default.relative(process.cwd(), i.file)}`;
|
|
271
|
+
return `${file}\n${i.content}`;
|
|
272
|
+
})
|
|
273
|
+
.join("\n")
|
|
274
|
+
.trim();
|
|
275
|
+
const mainFileContent = mainFile
|
|
276
|
+
.map((i) => {
|
|
277
|
+
const file = `//${node_path_1.default.relative(process.cwd(), i.file)}`;
|
|
278
|
+
return `${file}\n${i.content}`;
|
|
279
|
+
})
|
|
280
|
+
.join("\n")
|
|
281
|
+
.trim();
|
|
282
|
+
await utils_1.default.wait(1000);
|
|
283
|
+
const content = `${importStatements}\n${depFilesContent}\n${mainFileContent}`;
|
|
284
|
+
return content;
|
|
285
|
+
}
|
|
286
|
+
function checkExports(file, str) {
|
|
287
|
+
const sourceFile = typescript_1.default.createSourceFile(file, str, typescript_1.default.ScriptTarget.Latest, true);
|
|
288
|
+
let nameExport = false;
|
|
289
|
+
let defExport = false;
|
|
290
|
+
const transformer = (context) => {
|
|
291
|
+
const visitor = (node) => {
|
|
292
|
+
if (typescript_1.default.isExportAssignment(node) &&
|
|
293
|
+
!node.isExportEquals &&
|
|
294
|
+
node.modifiers === undefined &&
|
|
295
|
+
typescript_1.default.isIdentifier(node.expression)) {
|
|
296
|
+
defExport = true;
|
|
297
|
+
}
|
|
298
|
+
else if (typescript_1.default.isFunctionDeclaration(node) ||
|
|
299
|
+
typescript_1.default.isClassDeclaration(node)) {
|
|
300
|
+
let exp = false;
|
|
301
|
+
let def = false;
|
|
302
|
+
node.modifiers?.forEach((mod) => {
|
|
303
|
+
if (mod.kind === typescript_1.default.SyntaxKind.ExportKeyword) {
|
|
304
|
+
exp = true;
|
|
305
|
+
}
|
|
306
|
+
if (mod.kind === typescript_1.default.SyntaxKind.DefaultKeyword) {
|
|
307
|
+
def = true;
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
if (exp && def)
|
|
311
|
+
defExport = true;
|
|
312
|
+
}
|
|
313
|
+
else if (typescript_1.default.isExportAssignment(node) &&
|
|
314
|
+
typescript_1.default.isObjectLiteralExpression(node.expression)) {
|
|
315
|
+
const pros = node.expression.properties;
|
|
316
|
+
for (const pro of pros) {
|
|
317
|
+
if (pro.name && typescript_1.default.isIdentifier(pro.name)) {
|
|
318
|
+
defExport = true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
else if (typescript_1.default.isNamedExports(node)) {
|
|
323
|
+
nameExport = true;
|
|
324
|
+
}
|
|
325
|
+
else if (typescript_1.default.isVariableStatement(node) ||
|
|
326
|
+
typescript_1.default.isFunctionDeclaration(node) ||
|
|
327
|
+
typescript_1.default.isInterfaceDeclaration(node) ||
|
|
328
|
+
typescript_1.default.isTypeAliasDeclaration(node)) {
|
|
329
|
+
const isInsideNamespace = (n) => {
|
|
330
|
+
let current = n.parent;
|
|
331
|
+
while (current) {
|
|
332
|
+
if (typescript_1.default.isModuleDeclaration(current) &&
|
|
333
|
+
current.flags === typescript_1.default.NodeFlags.Namespace) {
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
current = current.parent;
|
|
337
|
+
}
|
|
338
|
+
return false;
|
|
339
|
+
};
|
|
340
|
+
node?.modifiers?.forEach((mod) => {
|
|
341
|
+
if (mod.kind === typescript_1.default.SyntaxKind.ExportKeyword) {
|
|
342
|
+
if (!isInsideNamespace(node)) {
|
|
343
|
+
nameExport = true;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
349
|
+
};
|
|
350
|
+
return (rootNode) => typescript_1.default.visitNode(rootNode, visitor);
|
|
351
|
+
};
|
|
352
|
+
typescript_1.default.transform(sourceFile, [transformer]);
|
|
353
|
+
return { nameExport, defExport };
|
|
354
|
+
}
|
|
355
|
+
const createHost = (sourceCode, fileName) => {
|
|
356
|
+
const createdFiles = {};
|
|
357
|
+
const host = {
|
|
358
|
+
getSourceFile: (file, languageVersion) => {
|
|
359
|
+
if (file === fileName) {
|
|
360
|
+
return typescript_1.default.createSourceFile(file, sourceCode, languageVersion);
|
|
361
|
+
}
|
|
362
|
+
return undefined;
|
|
363
|
+
},
|
|
364
|
+
writeFile: (fileName, contents) => {
|
|
365
|
+
createdFiles[fileName] = contents;
|
|
366
|
+
},
|
|
367
|
+
getDefaultLibFileName: (options) => typescript_1.default.getDefaultLibFilePath(options),
|
|
368
|
+
getCurrentDirectory: () => "",
|
|
369
|
+
getDirectories: () => [],
|
|
370
|
+
fileExists: (file) => file === fileName,
|
|
371
|
+
readFile: (file) => (file === fileName ? sourceCode : undefined),
|
|
372
|
+
getCanonicalFileName: (file) => file,
|
|
373
|
+
useCaseSensitiveFileNames: () => true,
|
|
374
|
+
getNewLine: () => "\n",
|
|
375
|
+
};
|
|
376
|
+
return { createdFiles, host };
|
|
377
|
+
};
|
|
378
|
+
function replaceInJs(fileName, sourceCode, compilerOptions) {
|
|
379
|
+
const sourceFile = typescript_1.default.createSourceFile(fileName, sourceCode, typescript_1.default.ScriptTarget.Latest, true);
|
|
380
|
+
const transformer = (context) => {
|
|
381
|
+
const { factory } = context;
|
|
382
|
+
const visitor = (node) => {
|
|
383
|
+
if (typescript_1.default.isExpressionStatement(node)) {
|
|
384
|
+
const expr = node.expression;
|
|
385
|
+
if (typescript_1.default.isBinaryExpression(expr) &&
|
|
386
|
+
typescript_1.default.isPropertyAccessExpression(expr.left) &&
|
|
387
|
+
typescript_1.default.isIdentifier(expr.left.expression) &&
|
|
388
|
+
expr.left.expression.escapedText === "exports" &&
|
|
389
|
+
typescript_1.default.isIdentifier(expr.left.name) &&
|
|
390
|
+
expr.left.name.escapedText === "default" &&
|
|
391
|
+
expr.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken &&
|
|
392
|
+
typescript_1.default.isIdentifier(expr.right)) {
|
|
393
|
+
const newLeftExpr = factory.createIdentifier("module");
|
|
394
|
+
const newName = factory.createIdentifier("exports");
|
|
395
|
+
const newLeft = factory.updatePropertyAccessExpression(expr.left, newLeftExpr, newName);
|
|
396
|
+
const newExpr = factory.updateBinaryExpression(expr, newLeft, expr.operatorToken, expr.right);
|
|
397
|
+
return factory.updateExpressionStatement(node, newExpr);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
401
|
+
};
|
|
402
|
+
return (rootNode) => typescript_1.default.visitNode(rootNode, visitor);
|
|
403
|
+
};
|
|
404
|
+
return (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
|
|
405
|
+
}
|
|
406
|
+
function replaceInTs(fileName, sourceCode, compilerOptions) {
|
|
407
|
+
const sourceFile = typescript_1.default.createSourceFile(fileName, sourceCode, typescript_1.default.ScriptTarget.Latest, true);
|
|
408
|
+
const transformer = (context) => {
|
|
409
|
+
const { factory } = context;
|
|
410
|
+
const visitor = (node) => {
|
|
411
|
+
if (typescript_1.default.isExportAssignment(node) &&
|
|
412
|
+
node.modifiers === undefined &&
|
|
413
|
+
!node.isExportEquals) {
|
|
414
|
+
return factory.createExportAssignment(node.modifiers, true, node.expression);
|
|
415
|
+
}
|
|
416
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
417
|
+
};
|
|
418
|
+
return (rootNode) => typescript_1.default.visitNode(rootNode, visitor);
|
|
419
|
+
};
|
|
420
|
+
return (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
|
|
421
|
+
}
|
|
422
|
+
async function clearFolder(folderPath) {
|
|
423
|
+
folderPath = node_path_1.default.resolve(process.cwd(), folderPath);
|
|
424
|
+
try {
|
|
425
|
+
const entries = await node_fs_1.default.promises.readdir(folderPath, {
|
|
426
|
+
withFileTypes: true,
|
|
427
|
+
});
|
|
428
|
+
await Promise.all(entries.map((entry) => node_fs_1.default.promises.rm(node_path_1.default.join(folderPath, entry.name), {
|
|
429
|
+
recursive: true,
|
|
430
|
+
})));
|
|
431
|
+
}
|
|
432
|
+
catch (error) {
|
|
433
|
+
if (error.code !== "ENOENT") {
|
|
434
|
+
throw error;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async function writeCompileFile(file, content) {
|
|
439
|
+
const filePath = typescript_1.default.sys.resolvePath(file);
|
|
440
|
+
const dir = node_path_1.default.dirname(filePath);
|
|
441
|
+
if (!typescript_1.default.sys.directoryExists(dir)) {
|
|
442
|
+
await node_fs_1.default.promises.mkdir(dir, { recursive: true });
|
|
443
|
+
}
|
|
444
|
+
await utils_1.default.wait(500);
|
|
445
|
+
await node_fs_1.default.promises.writeFile(filePath, content);
|
|
446
|
+
}
|
|
447
|
+
class Compilers {
|
|
448
|
+
constructor(options) {
|
|
449
|
+
this._target = options?.target ?? "both";
|
|
450
|
+
this._configPath = options?.configPath;
|
|
451
|
+
this.files = {
|
|
452
|
+
commonjs: undefined,
|
|
453
|
+
commonjsTypes: undefined,
|
|
454
|
+
esm: undefined,
|
|
455
|
+
esmTypes: undefined,
|
|
456
|
+
main: undefined,
|
|
457
|
+
module: undefined,
|
|
458
|
+
types: undefined,
|
|
459
|
+
};
|
|
460
|
+
this.outDir = "";
|
|
461
|
+
}
|
|
462
|
+
async commonjs(sourceCode, fileName, compilerOptions, isMain, hooks, isUpdate = true) {
|
|
463
|
+
console.time(tcolor_1.default.green("Compiled Commonjs"));
|
|
464
|
+
const ck = checkExports(fileName, sourceCode);
|
|
465
|
+
if (ck.defExport && ck.nameExport) {
|
|
466
|
+
console.warn(tcolor_1.default.yellow("Both name export and default export are exported from your project,that will effect on default export for commonjs output"));
|
|
467
|
+
}
|
|
468
|
+
const _host = createHost(sourceCode, fileName);
|
|
469
|
+
const createdFiles = _host.createdFiles;
|
|
470
|
+
const host = _host.host;
|
|
471
|
+
const program = typescript_1.default.createProgram([fileName], compilerOptions, host);
|
|
472
|
+
program.emit();
|
|
473
|
+
Object.entries(createdFiles).map(async ([outName, content]) => {
|
|
474
|
+
if (ck.defExport && !ck.nameExport) {
|
|
475
|
+
const ext = utils_1.default.extname(outName);
|
|
476
|
+
if (ext === ".js") {
|
|
477
|
+
content = replaceInJs(fileName, content, compilerOptions);
|
|
478
|
+
}
|
|
479
|
+
if (ext === ".ts") {
|
|
480
|
+
content = replaceInTs(fileName, content, compilerOptions);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (hooks?.length) {
|
|
484
|
+
for (const hook of hooks) {
|
|
485
|
+
if (hook.async) {
|
|
486
|
+
content = await hook.func(content, outName);
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
content = hook.func(content, outName);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
if (isUpdate) {
|
|
494
|
+
if (outName.match(/.js/g)) {
|
|
495
|
+
this.files.commonjs = outName.replace(/.js/g, ".cjs");
|
|
496
|
+
}
|
|
497
|
+
if (outName.match(/.d.ts/g)) {
|
|
498
|
+
this.files.commonjsTypes = outName.replace(/.d.ts/g, ".d.cts");
|
|
499
|
+
}
|
|
500
|
+
if (isMain &&
|
|
501
|
+
(this._target === "both" || this._target === "commonjs")) {
|
|
502
|
+
if (this.files.commonjs)
|
|
503
|
+
this.files.main = this.files.commonjs;
|
|
504
|
+
if (this.files.commonjsTypes)
|
|
505
|
+
this.files.types = this.files.commonjsTypes;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
outName = outName.replace(/.js/g, ".cjs");
|
|
509
|
+
outName = outName.replace(/.map.js/g, ".map.cjs");
|
|
510
|
+
outName = outName.replace(/.d.ts/g, ".d.cts");
|
|
511
|
+
await utils_1.default.wait(500);
|
|
512
|
+
if (this._target !== "both" && this._target !== "esm") {
|
|
513
|
+
await clearFolder(utils_1.default.dirname(outName));
|
|
514
|
+
}
|
|
515
|
+
await writeCompileFile(outName, content);
|
|
516
|
+
});
|
|
517
|
+
console.timeEnd(tcolor_1.default.green("Compiled Commonjs"));
|
|
518
|
+
}
|
|
519
|
+
async esm(sourceCode, fileName, compilerOptions, isMain, hooks, isUpdate = true) {
|
|
520
|
+
console.time(tcolor_1.default.green("Compiled ESM"));
|
|
521
|
+
const _host = createHost(sourceCode, fileName);
|
|
522
|
+
const createdFiles = _host.createdFiles;
|
|
523
|
+
const host = _host.host;
|
|
524
|
+
const program = typescript_1.default.createProgram([fileName], compilerOptions, host);
|
|
525
|
+
program.emit();
|
|
526
|
+
Object.entries(createdFiles).map(async ([outName, content]) => {
|
|
527
|
+
if (hooks?.length) {
|
|
528
|
+
for (const hook of hooks) {
|
|
529
|
+
if (hook.async) {
|
|
530
|
+
content = await hook.func(content, outName);
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
content = hook.func(content, outName);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (isUpdate) {
|
|
538
|
+
if (outName.match(/.js/g)) {
|
|
539
|
+
this.files.esm = outName.replace(/.js/g, ".mjs");
|
|
540
|
+
}
|
|
541
|
+
if (outName.match(/.d.ts/g)) {
|
|
542
|
+
this.files.esmTypes = outName.replace(/.d.ts/g, ".d.mts");
|
|
543
|
+
}
|
|
544
|
+
if (isMain && this._target === "both" && this.files.esm) {
|
|
545
|
+
this.files.module = this.files.esm;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
outName = outName.replace(/.js/g, ".mjs");
|
|
549
|
+
outName = outName.replace(/.map.js/g, ".map.mjs");
|
|
550
|
+
outName = outName.replace(/.d.ts/g, ".d.mts");
|
|
551
|
+
await utils_1.default.wait(500);
|
|
552
|
+
if (this._target !== "commonjs") {
|
|
553
|
+
await clearFolder(utils_1.default.dirname(outName));
|
|
554
|
+
}
|
|
555
|
+
await writeCompileFile(outName, content);
|
|
556
|
+
});
|
|
557
|
+
console.timeEnd(tcolor_1.default.green("Compiled ESM"));
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
const depsCheck = {
|
|
561
|
+
types(deps, compilerOptions) {
|
|
562
|
+
if (!compilerOptions.noCheck) {
|
|
563
|
+
const filePaths = deps.map((i) => i.file);
|
|
564
|
+
let _err = false;
|
|
565
|
+
const program = typescript_1.default.createProgram(filePaths, compilerOptions);
|
|
566
|
+
for (const filePath of filePaths) {
|
|
567
|
+
const sourceFile = program.getSourceFile(filePath);
|
|
568
|
+
if (!sourceFile) {
|
|
569
|
+
console.error(tcolor_1.default.magenta(`File not found: ${filePath}`));
|
|
570
|
+
typescript_1.default.sys.exit(1);
|
|
571
|
+
}
|
|
572
|
+
const diagnostics = [
|
|
573
|
+
...program.getSyntacticDiagnostics(sourceFile),
|
|
574
|
+
...program.getSemanticDiagnostics(sourceFile),
|
|
575
|
+
...program.getDeclarationDiagnostics(sourceFile),
|
|
576
|
+
];
|
|
577
|
+
if (diagnostics.length > 0) {
|
|
578
|
+
const formatHost = {
|
|
579
|
+
getCurrentDirectory: () => process.cwd(),
|
|
580
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
581
|
+
getNewLine: () => typescript_1.default.sys.newLine,
|
|
582
|
+
};
|
|
583
|
+
console.error(typescript_1.default.formatDiagnosticsWithColorAndContext(diagnostics, formatHost));
|
|
584
|
+
_err = true;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
if (_err) {
|
|
588
|
+
typescript_1.default.sys.exit(1);
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
return true;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
ext(deps) {
|
|
596
|
+
const tsExt = new Set([".ts", ".mts", ".cts", ".tsx"]);
|
|
597
|
+
for (const dep of deps) {
|
|
598
|
+
const ext = utils_1.default.extname(dep.file);
|
|
599
|
+
if (!tsExt.has(ext)) {
|
|
600
|
+
console.error(tcolor_1.default.magenta(`${dep.file} has no valid TypeScript extension`));
|
|
601
|
+
typescript_1.default.sys.exit(1);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return true;
|
|
605
|
+
},
|
|
606
|
+
moduleType(deps) {
|
|
607
|
+
let _esmCount = 0;
|
|
608
|
+
let cjsCount = 0;
|
|
609
|
+
let unknowCount = 0;
|
|
610
|
+
for (const dep of deps) {
|
|
611
|
+
try {
|
|
612
|
+
const sourceFile = typescript_1.default.createSourceFile(dep.file, dep.content, typescript_1.default.ScriptTarget.Latest, true);
|
|
613
|
+
let hasESMImports = false;
|
|
614
|
+
let hasCommonJS = false;
|
|
615
|
+
function walk(node) {
|
|
616
|
+
if (typescript_1.default.isImportDeclaration(node) ||
|
|
617
|
+
typescript_1.default.isImportEqualsDeclaration(node) ||
|
|
618
|
+
typescript_1.default.isExportDeclaration(node) ||
|
|
619
|
+
typescript_1.default.isExportSpecifier(node) ||
|
|
620
|
+
typescript_1.default.isExportAssignment(node)) {
|
|
621
|
+
hasESMImports = true;
|
|
622
|
+
}
|
|
623
|
+
if ((typescript_1.default.isVariableStatement(node) ||
|
|
624
|
+
typescript_1.default.isFunctionDeclaration(node) ||
|
|
625
|
+
typescript_1.default.isInterfaceDeclaration(node) ||
|
|
626
|
+
typescript_1.default.isTypeAliasDeclaration(node) ||
|
|
627
|
+
typescript_1.default.isEnumDeclaration(node) ||
|
|
628
|
+
typescript_1.default.isClassDeclaration(node)) &&
|
|
629
|
+
node.modifiers?.some((mod) => mod.kind === typescript_1.default.SyntaxKind.ExportKeyword)) {
|
|
630
|
+
hasESMImports = true;
|
|
631
|
+
}
|
|
632
|
+
if (typescript_1.default.isCallExpression(node)) {
|
|
633
|
+
if (typescript_1.default.isIdentifier(node.expression) &&
|
|
634
|
+
node.expression.text === "require" &&
|
|
635
|
+
node.arguments.length > 0) {
|
|
636
|
+
hasCommonJS = true;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
if (typescript_1.default.isPropertyAccessExpression(node)) {
|
|
640
|
+
const text = node.getText(sourceFile);
|
|
641
|
+
if (text.startsWith("module.exports") ||
|
|
642
|
+
text.startsWith("exports.")) {
|
|
643
|
+
hasCommonJS = true;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
typescript_1.default.forEachChild(node, walk);
|
|
647
|
+
}
|
|
648
|
+
walk(sourceFile);
|
|
649
|
+
if (hasESMImports && !hasCommonJS) {
|
|
650
|
+
_esmCount++;
|
|
651
|
+
}
|
|
652
|
+
else if (hasCommonJS && !hasESMImports) {
|
|
653
|
+
cjsCount++;
|
|
654
|
+
}
|
|
655
|
+
else if (hasESMImports && hasCommonJS) {
|
|
656
|
+
_esmCount++;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
catch (error) {
|
|
660
|
+
console.error(tcolor_1.default.magenta(`Error checking module format for ${dep.file} : \n ${error}`));
|
|
661
|
+
unknowCount++;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
if (unknowCount) {
|
|
665
|
+
console.error(tcolor_1.default.magenta("Unknown error when checking module types in the dependencies tree."));
|
|
666
|
+
typescript_1.default.sys.exit(1);
|
|
667
|
+
}
|
|
668
|
+
if (cjsCount) {
|
|
669
|
+
console.error(tcolor_1.default.magenta("The package detects CommonJs format in the dependencies tree, that unsupported."));
|
|
670
|
+
typescript_1.default.sys.exit(1);
|
|
671
|
+
}
|
|
672
|
+
return true;
|
|
673
|
+
},
|
|
674
|
+
nodeCheck(nodeEnvOption, nodeModules) {
|
|
675
|
+
if (!nodeEnvOption && nodeModules.length > 0) {
|
|
676
|
+
console.error();
|
|
677
|
+
typescript_1.default.sys.exit(1);
|
|
678
|
+
}
|
|
679
|
+
return true;
|
|
680
|
+
},
|
|
681
|
+
async make(deps, compilerOptions, nodeModules, nodeEnv = true) {
|
|
682
|
+
const res = (0, resolves_1.default)([
|
|
683
|
+
[depsCheck.ext, deps],
|
|
684
|
+
[depsCheck.nodeCheck, nodeEnv, nodeModules],
|
|
685
|
+
[depsCheck.moduleType, deps],
|
|
686
|
+
[depsCheck.types, deps, compilerOptions],
|
|
687
|
+
]);
|
|
688
|
+
const results = await res.concurrent();
|
|
689
|
+
return results.every((r) => r === true);
|
|
690
|
+
},
|
|
691
|
+
};
|
|
692
|
+
async function getDependencies(entry) {
|
|
693
|
+
const deps = await (0, dependencies_1.default)(entry);
|
|
694
|
+
const sorted = deps.sort();
|
|
695
|
+
const circularMessages = [];
|
|
696
|
+
const nodeModules = deps.node();
|
|
697
|
+
const depFiles = [];
|
|
698
|
+
await utils_1.default.wait(100);
|
|
699
|
+
for (const dep of sorted) {
|
|
700
|
+
const file = utils_1.default.resolvePath(dep);
|
|
701
|
+
const content = utils_1.default.readFile(file);
|
|
702
|
+
depFiles.push({ file, content });
|
|
703
|
+
}
|
|
704
|
+
const circular = deps
|
|
705
|
+
.mutual()
|
|
706
|
+
.map((i) => `${i[0]} -> ${i[1]} \n ${i[1]} -> ${i[0]} \n`);
|
|
707
|
+
const unknown = deps.warn().map((i) => `${i}\n`);
|
|
708
|
+
if (circular.length)
|
|
709
|
+
circularMessages.push(circular.join(""));
|
|
710
|
+
if (unknown.length)
|
|
711
|
+
circularMessages.push(unknown.join(""));
|
|
712
|
+
return {
|
|
713
|
+
depFiles,
|
|
714
|
+
sorted,
|
|
715
|
+
circularMessages,
|
|
716
|
+
nodeModules,
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
const getCompilerOptions = (exportPath, configPath) => {
|
|
720
|
+
const config = new tsconfig_1.default(configPath);
|
|
721
|
+
const generalOptions = () => config.getCompilerOptions();
|
|
722
|
+
const commonjs = () => {
|
|
723
|
+
const _config = new tsconfig_1.default(configPath);
|
|
724
|
+
_config.addCompilerOptions({ outDir: "dist" });
|
|
725
|
+
_config.removeCompilerOption("rootDir");
|
|
726
|
+
_config.removeCompilerOption("module");
|
|
727
|
+
const _options = _config.getCompilerOptions();
|
|
728
|
+
let out_dir = _options.outDir;
|
|
729
|
+
let isMain = true;
|
|
730
|
+
if (exportPath !== ".") {
|
|
731
|
+
out_dir = `${out_dir}/${exportPath.slice(2)}`;
|
|
732
|
+
isMain = false;
|
|
733
|
+
}
|
|
734
|
+
const { outDir, module, ...restOptions } = _options;
|
|
735
|
+
const compilerOptions = {
|
|
736
|
+
outDir: out_dir,
|
|
737
|
+
module: typescript_1.default.ModuleKind.CommonJS,
|
|
738
|
+
...restOptions,
|
|
739
|
+
};
|
|
740
|
+
return {
|
|
741
|
+
isMain,
|
|
742
|
+
compilerOptions,
|
|
743
|
+
out_dir,
|
|
744
|
+
};
|
|
745
|
+
};
|
|
746
|
+
const esm = () => {
|
|
747
|
+
const __config = new tsconfig_1.default(configPath);
|
|
748
|
+
__config.addCompilerOptions({ outDir: "dist" });
|
|
749
|
+
__config.removeCompilerOption("rootDir");
|
|
750
|
+
const _options = __config.getCompilerOptions();
|
|
751
|
+
let out_dir = _options.outDir;
|
|
752
|
+
let isMain = true;
|
|
753
|
+
if (exportPath !== ".") {
|
|
754
|
+
out_dir = `${out_dir}/${exportPath.slice(2)}`;
|
|
755
|
+
isMain = false;
|
|
756
|
+
}
|
|
757
|
+
const { outDir, module, ...restOptions } = _options;
|
|
758
|
+
const compilerOptions = {
|
|
759
|
+
outDir: out_dir,
|
|
760
|
+
module: typescript_1.default.ModuleKind.ES2022,
|
|
761
|
+
...restOptions,
|
|
762
|
+
};
|
|
763
|
+
return {
|
|
764
|
+
isMain,
|
|
765
|
+
compilerOptions,
|
|
766
|
+
out_dir,
|
|
767
|
+
};
|
|
768
|
+
};
|
|
769
|
+
return { commonjs, esm, generalOptions };
|
|
770
|
+
};
|
|
771
|
+
async function entry({ entryPath, exportPath, configPath, nodeEnv, }) {
|
|
772
|
+
const deps = await getDependencies(entryPath);
|
|
773
|
+
const depFiles = deps.depFiles;
|
|
774
|
+
const nodeModules = deps.nodeModules;
|
|
775
|
+
await utils_1.default.wait(1000);
|
|
776
|
+
const opts = getCompilerOptions(exportPath, configPath);
|
|
777
|
+
const generalOptions = opts.generalOptions();
|
|
778
|
+
const modOpts = {
|
|
779
|
+
commonjs: () => opts.commonjs(),
|
|
780
|
+
esm: () => opts.esm(),
|
|
781
|
+
};
|
|
782
|
+
await utils_1.default.wait(1000);
|
|
783
|
+
const checked = await depsCheck.make(depFiles, generalOptions, nodeModules, nodeEnv);
|
|
784
|
+
if (!checked) {
|
|
785
|
+
typescript_1.default.sys.exit(1);
|
|
786
|
+
}
|
|
787
|
+
return {
|
|
788
|
+
depFiles,
|
|
789
|
+
modOpts,
|
|
790
|
+
generalOptions,
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
const getConfigPath = () => {
|
|
794
|
+
const fileNames = ["susee.config.ts", "susee.config.js", "susee.config.mjs"];
|
|
795
|
+
let configFile;
|
|
796
|
+
for (const file of fileNames) {
|
|
797
|
+
const _file = typescript_1.default.sys.resolvePath(file);
|
|
798
|
+
if (typescript_1.default.sys.fileExists(_file)) {
|
|
799
|
+
configFile = _file;
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
return configFile;
|
|
804
|
+
};
|
|
805
|
+
function checkEntries(entries) {
|
|
806
|
+
if (entries.length < 1) {
|
|
807
|
+
console.error(tcolor_1.default.magenta(`No entry found in susee.config file, at least one entry required`));
|
|
808
|
+
typescript_1.default.sys.exit(1);
|
|
809
|
+
}
|
|
810
|
+
const objectStore = {};
|
|
811
|
+
const duplicates = [];
|
|
812
|
+
for (const obj of entries) {
|
|
813
|
+
const value = obj.exportPath;
|
|
814
|
+
if (objectStore[value]) {
|
|
815
|
+
duplicates.push(`"${value}"`);
|
|
816
|
+
}
|
|
817
|
+
else {
|
|
818
|
+
objectStore[value] = true;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
if (duplicates.length > 0) {
|
|
822
|
+
console.error(tcolor_1.default.magenta(`Duplicate export paths/path (${duplicates.join(",")}) found in your susee.config file , that will error for bundled output`));
|
|
823
|
+
typescript_1.default.sys.exit(1);
|
|
824
|
+
}
|
|
825
|
+
for (const obj of entries) {
|
|
826
|
+
if (!typescript_1.default.sys.fileExists(typescript_1.default.sys.resolvePath(obj.entry))) {
|
|
827
|
+
console.error(tcolor_1.default.magenta(`Entry file ${obj.entry} dose not exists.`));
|
|
828
|
+
typescript_1.default.sys.exit(1);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
async function getConfig() {
|
|
833
|
+
const configPath = getConfigPath();
|
|
834
|
+
if (configPath === undefined) {
|
|
835
|
+
console.error(tcolor_1.default.magenta(`No susee.config file ("susee.config.ts", "susee.config.js", "susee.config.mjs") found`));
|
|
836
|
+
typescript_1.default.sys.exit(1);
|
|
837
|
+
}
|
|
838
|
+
const _default = await Promise.resolve(`${configPath}`).then(s => __importStar(require(s)));
|
|
839
|
+
const config = _default.default;
|
|
840
|
+
const entryCheck = (0, resolves_1.default)([[checkEntries, config.entryPoints]]);
|
|
841
|
+
await entryCheck.series();
|
|
842
|
+
await utils_1.default.wait(1000);
|
|
843
|
+
return {
|
|
844
|
+
entryPoints: config.entryPoints,
|
|
845
|
+
postProcessHooks: config.postProcessHooks ?? [],
|
|
846
|
+
allowUpdatePackageJson: config.allowUpdatePackageJson ?? true,
|
|
847
|
+
nodeEnv: config.nodeEnv ?? true,
|
|
848
|
+
renameDuplicates: config.renameDuplicates ?? true,
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
const isCjs = (files) => files.commonjs && files.commonjsTypes;
|
|
852
|
+
const isEsm = (files) => files.esm && files.esmTypes;
|
|
853
|
+
function getExports(files, exportPath) {
|
|
854
|
+
return isCjs(files) && isEsm(files)
|
|
855
|
+
? {
|
|
856
|
+
[exportPath]: {
|
|
857
|
+
import: {
|
|
858
|
+
default: `./${node_path_1.default.relative(process.cwd(), files.esm)}`,
|
|
859
|
+
types: `./${node_path_1.default.relative(process.cwd(), files.esmTypes)}`,
|
|
860
|
+
},
|
|
861
|
+
require: {
|
|
862
|
+
default: `./${node_path_1.default.relative(process.cwd(), files.commonjs)}`,
|
|
863
|
+
types: `./${node_path_1.default.relative(process.cwd(), files.commonjsTypes)}`,
|
|
864
|
+
},
|
|
865
|
+
},
|
|
866
|
+
}
|
|
867
|
+
: isCjs(files) && !isEsm(files)
|
|
868
|
+
? {
|
|
869
|
+
[exportPath]: {
|
|
870
|
+
require: {
|
|
871
|
+
default: `./${node_path_1.default.relative(process.cwd(), files.commonjs)}`,
|
|
872
|
+
types: `./${node_path_1.default.relative(process.cwd(), files.commonjsTypes)}`,
|
|
873
|
+
},
|
|
874
|
+
},
|
|
875
|
+
}
|
|
876
|
+
: !isCjs(files) && isEsm(files)
|
|
877
|
+
? {
|
|
878
|
+
[exportPath]: {
|
|
879
|
+
import: {
|
|
880
|
+
default: `./${node_path_1.default.relative(process.cwd(), files.esm)}`,
|
|
881
|
+
types: `./${node_path_1.default.relative(process.cwd(), files.esmTypes)}`,
|
|
882
|
+
},
|
|
883
|
+
},
|
|
884
|
+
}
|
|
885
|
+
: {};
|
|
886
|
+
}
|
|
887
|
+
async function writePackage(files, exportPath) {
|
|
888
|
+
let isMain = true;
|
|
889
|
+
if (exportPath !== ".") {
|
|
890
|
+
isMain = false;
|
|
891
|
+
}
|
|
892
|
+
const pkgFile = utils_1.default.resolvePath("package.json");
|
|
893
|
+
const _pkgtext = utils_1.default.readFile(pkgFile);
|
|
894
|
+
const pkgtext = JSON.parse(_pkgtext);
|
|
895
|
+
let { name, version, description, main, module, type, types, exports, ...rest } = pkgtext;
|
|
896
|
+
await utils_1.default.wait(500);
|
|
897
|
+
type = isEsm(files) ? "module" : "commonjs";
|
|
898
|
+
let _main = {};
|
|
899
|
+
let _module = {};
|
|
900
|
+
let _types = {};
|
|
901
|
+
let _exports = {};
|
|
902
|
+
if (isMain) {
|
|
903
|
+
_main = files.main
|
|
904
|
+
? { main: node_path_1.default.relative(process.cwd(), files.main) }
|
|
905
|
+
: {};
|
|
906
|
+
_module = files.module
|
|
907
|
+
? { module: node_path_1.default.relative(process.cwd(), files.module) }
|
|
908
|
+
: {};
|
|
909
|
+
_types = files.types
|
|
910
|
+
? { types: node_path_1.default.relative(process.cwd(), files.types) }
|
|
911
|
+
: {};
|
|
912
|
+
_exports = { exports: { ...getExports(files, exportPath) } };
|
|
913
|
+
}
|
|
914
|
+
else {
|
|
915
|
+
_main = main ? { main: main } : {};
|
|
916
|
+
_module = module ? { module: module } : {};
|
|
917
|
+
_types = types ? { types: types } : {};
|
|
918
|
+
const normalizedExports = exports && typeof exports === "object" && !Array.isArray(exports)
|
|
919
|
+
? { ...exports }
|
|
920
|
+
: {};
|
|
921
|
+
_exports = {
|
|
922
|
+
exports: { ...normalizedExports, ...getExports(files, exportPath) },
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
await utils_1.default.wait(1000);
|
|
926
|
+
const pkgJson = {
|
|
927
|
+
name,
|
|
928
|
+
version,
|
|
929
|
+
description,
|
|
930
|
+
type,
|
|
931
|
+
..._main,
|
|
932
|
+
..._types,
|
|
933
|
+
..._module,
|
|
934
|
+
..._exports,
|
|
935
|
+
...rest,
|
|
936
|
+
};
|
|
937
|
+
utils_1.default.writeFile(pkgFile, JSON.stringify(pkgJson, null, 2));
|
|
938
|
+
}
|
|
939
|
+
async function susee() {
|
|
940
|
+
const config = await getConfig();
|
|
941
|
+
console.info(tcolor_1.default.cyan("Start Bundle"));
|
|
942
|
+
const compile = async (e) => {
|
|
943
|
+
const target = e.moduleType ? e.moduleType : "esm";
|
|
944
|
+
const configPath = e.tsconfigFilePath;
|
|
945
|
+
const ent = await entry({
|
|
946
|
+
entryPath: e.entry,
|
|
947
|
+
exportPath: e.exportPath,
|
|
948
|
+
configPath: e.tsconfigFilePath,
|
|
949
|
+
nodeEnv: config.nodeEnv,
|
|
950
|
+
});
|
|
951
|
+
const sourceCode = await bundler({
|
|
952
|
+
depsFiles: ent.depFiles,
|
|
953
|
+
compilerOptions: ent.generalOptions,
|
|
954
|
+
renameDuplicates: config.renameDuplicates,
|
|
955
|
+
});
|
|
956
|
+
const mdOpts = ent.modOpts;
|
|
957
|
+
const cjsOpts = mdOpts.commonjs();
|
|
958
|
+
const esmOpts = mdOpts.esm();
|
|
959
|
+
const compiler = new Compilers({ target, configPath });
|
|
960
|
+
if (target === "commonjs") {
|
|
961
|
+
await compiler.commonjs(sourceCode, e.entry, cjsOpts.compilerOptions, cjsOpts.isMain, config.postProcessHooks, config.allowUpdatePackageJson);
|
|
962
|
+
}
|
|
963
|
+
else if (target === "esm") {
|
|
964
|
+
await compiler.esm(sourceCode, e.entry, esmOpts.compilerOptions, esmOpts.isMain, config.postProcessHooks, config.allowUpdatePackageJson);
|
|
965
|
+
}
|
|
966
|
+
else if (target === "both") {
|
|
967
|
+
await compiler.esm(sourceCode, e.entry, esmOpts.compilerOptions, esmOpts.isMain, config.postProcessHooks, config.allowUpdatePackageJson);
|
|
968
|
+
await utils_1.default.wait(1000);
|
|
969
|
+
await compiler.commonjs(sourceCode, e.entry, cjsOpts.compilerOptions, cjsOpts.isMain, config.postProcessHooks, config.allowUpdatePackageJson);
|
|
970
|
+
}
|
|
971
|
+
await utils_1.default.wait(1000);
|
|
972
|
+
if (config.allowUpdatePackageJson) {
|
|
973
|
+
await writePackage(compiler.files, e.exportPath);
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
for (const entry of config.entryPoints) {
|
|
977
|
+
const entName = entry.exportPath === "." ? "main" : entry.exportPath.slice(2);
|
|
978
|
+
await utils_1.default.wait(1000);
|
|
979
|
+
console.info(tcolor_1.default.cyan(`Start ${tcolor_1.default.green("->")} "${entName}" export path`));
|
|
980
|
+
await compile(entry);
|
|
981
|
+
console.info(tcolor_1.default.cyan(`End ${tcolor_1.default.green("->")} "${entName}" export path`));
|
|
982
|
+
if (config.entryPoints.indexOf(entry) + 1 < config.entryPoints.length) {
|
|
983
|
+
console.info("-----------------------------------");
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
console.info(tcolor_1.default.cyan("Finished Bundle"));
|
|
987
|
+
}
|
|
988
|
+
//# sourceMappingURL=index.js.map
|