UNPKG

11.4 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license Typescript-Library-Bundler v0.0.14
4 * (c) 2017 Amin Paks <amin.pakseresht@hotmail.com>
5 * License: MIT
6 */
7var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8 return new (P || (P = Promise))(function (resolve, reject) {
9 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
10 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
11 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
12 step((generator = generator.apply(thisArg, _arguments || [])).next());
13 });
14};
15var __generator = (this && this.__generator) || function (thisArg, body) {
16 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
17 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
18 function verb(n) { return function (v) { return step([n, v]); }; }
19 function step(op) {
20 if (f) throw new TypeError("Generator is already executing.");
21 while (_) try {
22 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
23 if (y = 0, t) op = [0, t.value];
24 switch (op[0]) {
25 case 0: case 1: t = op; break;
26 case 4: _.label++; return { value: op[1], done: false };
27 case 5: _.label++; y = op[1]; op = [0]; continue;
28 case 7: op = _.ops.pop(); _.trys.pop(); continue;
29 default:
30 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
31 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
32 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
33 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
34 if (t[2]) _.ops.pop();
35 _.trys.pop(); continue;
36 }
37 op = body.call(thisArg, _);
38 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
39 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
40 }
41};
42Object.defineProperty(exports, "__esModule", { value: true });
43var path = require("path");
44var uglify = require("rollup-plugin-uglify");
45var tsc_wrapped_1 = require("@angular/tsc-wrapped");
46var preprocess_files_1 = require("./preprocess-files");
47var external_modules_1 = require("./external-modules");
48var tsc_1 = require("./tsc");
49var rollup_1 = require("./rollup");
50var config_helpers_1 = require("./config-helpers");
51var utils_1 = require("./utils");
52function main(projectPath, configFilePath) {
53 return __awaiter(this, void 0, void 0, function () {
54 var configFileDir, _a, configs, error, entryFile, _b, bundlerOptions, possibleEntry, packageFilePath, buildDir, defaultOutDir, outDir, packageConfigs, packageName, moduleId, moduleFilename, projectFileList, externals, _c, externalModules, libraryExternalModules, ngcBuildDir, ngcConfigPath, commonJsSettings, outputES5Module, outputES6Module, rollupEntryFile, rollupES2015Config, transpileConfigES5, bundlesDir, outputUMDModule, rollupUMDConfig, outputMinifiedUMDModule, rollupMinifiedUMDConfig, isAngular, copyDeclarationPattern, keys, outputTypings;
55 return __generator(this, function (_d) {
56 switch (_d.label) {
57 case 0:
58 if (!path.isAbsolute(projectPath)) {
59 projectPath = path.resolve(process.cwd(), projectPath);
60 }
61 if (utils_1.isNil(configFilePath)) {
62 configFilePath = path.resolve(projectPath, 'tsconfig.json');
63 }
64 configFileDir = path.dirname(configFilePath);
65 _a = config_helpers_1.parseConfigFile(configFilePath), configs = _a.configs, error = _a.error;
66 if (!utils_1.isNil(error)) {
67 throw new Error(error.messageText.toString());
68 }
69 _b = configs.bundlerOptions, bundlerOptions = _b === void 0 ? {} : _b;
70 possibleEntry = bundlerOptions.entry;
71 if (utils_1.isString(possibleEntry) && !utils_1.isEmpty(possibleEntry)) {
72 entryFile = path.resolve(configFileDir, possibleEntry);
73 }
74 else {
75 throw new Error('Project tsconfig must have an "entry" that points to the a file to export the library implementations!');
76 }
77 if (!utils_1.isFile(entryFile)) {
78 throw new Error('Cannot find the entry file ');
79 }
80 packageFilePath = path.resolve(projectPath, 'package.json');
81 buildDir = path.resolve(projectPath, '.compile');
82 defaultOutDir = bundlerOptions.outDir || './dist';
83 outDir = path.resolve(projectPath, defaultOutDir);
84 packageConfigs = config_helpers_1.readPackage(packageFilePath);
85 packageName = packageConfigs.name;
86 moduleId = config_helpers_1.getSafePackageName(packageName);
87 moduleFilename = moduleId + '.js';
88 if (utils_1.isNil(moduleId)) {
89 throw new Error("Package name includes invalid characters \"" + packageName + "\"!"); // `
90 }
91 if (utils_1.isNil(packageName)) {
92 throw new Error('Project package.json has no name entry!');
93 }
94 if (path.basename(entryFile, '.ts') === 'index') {
95 throw new Error('Project entry file cannot be named "index.ts"! try "public_api.ts" instead.');
96 }
97 // Clean up working directory
98 utils_1.ensureRemoveDir(buildDir);
99 utils_1.ensureRemoveDir(outDir);
100 utils_1.ensureMakeDir(buildDir);
101 return [4 /*yield*/, preprocess_files_1.preprocessTSFiles(entryFile, buildDir, configFileDir)];
102 case 1:
103 projectFileList = _d.sent();
104 externals = bundlerOptions.externals, _c = bundlerOptions.externalModules, externalModules = _c === void 0 ? {} : _c;
105 libraryExternalModules = {};
106 if (!(externalModules !== false)) return [3 /*break*/, 3];
107 return [4 /*yield*/, external_modules_1.getExternalModuleNames(projectFileList, projectPath, externals || externalModules)];
108 case 2:
109 libraryExternalModules = _d.sent();
110 _d.label = 3;
111 case 3:
112 ngcBuildDir = path.resolve(buildDir, 'ngc-compiled');
113 ngcConfigPath = path.resolve(buildDir, 'tsconfig-ngc.json');
114 config_helpers_1.createNGCConfig(ngcConfigPath, moduleId, configs);
115 // Compile with NGC
116 return [4 /*yield*/, tsc_wrapped_1.main(ngcConfigPath, { basePath: buildDir })];
117 case 4:
118 // Compile with NGC
119 _d.sent();
120 commonJsSettings = bundlerOptions.commonJsSettings;
121 outputES5Module = path.resolve(outDir, moduleId + '.es5.js');
122 outputES6Module = path.resolve(outDir, moduleFilename);
123 rollupEntryFile = path.resolve(ngcBuildDir, moduleFilename);
124 rollupES2015Config = rollup_1.defaultConfigs({
125 moduleEntry: rollupEntryFile,
126 moduleName: moduleId,
127 outputPath: outputES6Module,
128 externalModules: libraryExternalModules,
129 commonJsSettings: commonJsSettings,
130 });
131 return [4 /*yield*/, rollup_1.rollupBy(rollupES2015Config)];
132 case 5:
133 _d.sent();
134 transpileConfigES5 = tsc_1.getTranspileOptions({
135 compilerOptions: { allowJs: true },
136 moduleName: moduleId,
137 fileName: moduleId,
138 });
139 return [4 /*yield*/, tsc_1.transpileModule(outputES6Module, transpileConfigES5, outputES5Module)];
140 case 6:
141 _d.sent();
142 bundlesDir = path.resolve(outDir, 'bundles');
143 outputUMDModule = path.resolve(bundlesDir, moduleId + '.umd.js');
144 rollupUMDConfig = rollup_1.defaultConfigs({
145 format: 'umd',
146 moduleEntry: outputES5Module,
147 moduleName: moduleId,
148 outputPath: outputUMDModule,
149 externalModules: libraryExternalModules,
150 commonJsSettings: commonJsSettings,
151 });
152 return [4 /*yield*/, rollup_1.rollupBy(rollupUMDConfig)];
153 case 7:
154 _d.sent();
155 outputMinifiedUMDModule = path.resolve(bundlesDir, moduleId + '.umd.min.js');
156 rollupMinifiedUMDConfig = rollup_1.defaultConfigs({
157 format: 'umd',
158 moduleEntry: outputES5Module,
159 moduleName: moduleId,
160 outputPath: outputMinifiedUMDModule,
161 externalModules: libraryExternalModules,
162 commonJsSettings: commonJsSettings,
163 plugins: [uglify()],
164 });
165 return [4 /*yield*/, rollup_1.rollupBy(rollupMinifiedUMDConfig)];
166 case 8:
167 _d.sent();
168 isAngular = utils_1.isAngularLib(libraryExternalModules);
169 copyDeclarationPattern = "**/*(*.d.ts" + (isAngular ? '|*.metadata.json' : '') + ")";
170 return [4 /*yield*/, utils_1.copyFromTo({
171 pattern: copyDeclarationPattern,
172 rootDir: ngcBuildDir,
173 toDir: outDir,
174 })];
175 case 9:
176 _d.sent();
177 keys = Object.keys;
178 config_helpers_1.validatePkgDependencies(packageConfigs, keys(libraryExternalModules));
179 outputTypings = path.resolve(outDir, moduleId + '.d.ts');
180 config_helpers_1.validatePkgModuleEntries({
181 pkgMain: outputUMDModule,
182 pkgModule: outputES5Module,
183 pkgES2015: outputES6Module,
184 pkgTypings: outputTypings,
185 }, packageConfigs, projectPath);
186 return [2 /*return*/];
187 }
188 });
189 });
190}
191exports.main = main;