UNPKG

6.78 kBJavaScriptView Raw
1"use strict";
2
3let pkg = require('./package.json');
4let fs = require('fs-extra');
5let mkdirp = require('mkdirp');
6let path = require('path');
7let klawSync = require('klaw-sync');
8let licenseTool = require('./tools/add-license-to-file');
9let addLicenseToFile = licenseTool.addLicenseToFile;
10let addLicenseTextToFile = licenseTool.addLicenseTextToFile;
11let makePackages = require('./.make-helpers');
12let copySources = makePackages.copySources;
13let createImportTargets = makePackages.createImportTargets;
14let cleanSourceMapRoot = makePackages.cleanSourceMapRoot;
15let bo = null;
16// Build Optimizer is not available on Node 4.x. Using a try/catch
17// here to make sure the build passes on Travis using Node 4, but
18// the NPM distribution will run through build-optimizer.
19try {
20 bo = require('@angular-devkit/build-optimizer');
21} catch (e) {}
22
23
24const ROOT = 'dist/';
25const CJS_ROOT = ROOT + 'cjs/';
26const ESM5_ROOT = ROOT + 'esm5/';
27const ESM2015_ROOT = ROOT + 'esm2015/';
28const UMD_ROOT = ROOT + 'global/';
29const ESM5_FOR_ROLLUP_ROOT = ROOT + 'esm5_for_rollup/';
30const LEGACY_REEXPORT_ROOT = ROOT + 'legacy-reexport/';
31const TYPE_ROOT = ROOT + 'typings/';
32const MIGRATION_PKG = ROOT + 'migrations/';
33const PKG_ROOT = ROOT + 'package/';
34const CJS_PKG = PKG_ROOT + '';
35const ESM5_PKG = PKG_ROOT + '_esm5/';
36const ESM2015_PKG = PKG_ROOT + '_esm2015/';
37const UMD_PKG = PKG_ROOT + 'bundles/';
38const SRC_ROOT_PKG = PKG_ROOT + 'src/';
39const TYPE_PKG = PKG_ROOT;
40
41// License info for minified files
42let licenseUrl = 'https://github.com/ReactiveX/RxJS/blob/master/LICENSE.txt';
43let license = 'Apache License 2.0 ' + licenseUrl;
44
45delete pkg.scripts;
46fs.removeSync(PKG_ROOT);
47
48let rootPackageJson = Object.assign({}, pkg, {
49 name: 'rxjs',
50 main: './index.js',
51 typings: './index.d.ts',
52 module: './_esm5/index.js',
53 es2015: './_esm2015/index.js'
54});
55
56// Execute build optimizer transforms on ESM5 files
57klawSync(ESM5_ROOT, {
58 nodir: true,
59 filter: function(item) {
60 return item.path.endsWith('.js');
61 }
62})
63.map(item => item.path.slice((`${__dirname}/${ESM5_ROOT}`).length))
64.map(fileName => {
65 if (!bo) return fileName;
66 let fullPath = path.resolve(__dirname, ESM5_ROOT, fileName);
67 // The file won't exist when running build_test as we don't create the ESM5 sources
68 if (!fs.existsSync(fullPath)) return fileName;
69 let content = fs.readFileSync(fullPath).toString();
70 let transformed = bo.transformJavascript({
71 content: content,
72 getTransforms: [bo.getPrefixClassesTransformer, bo.getPrefixFunctionsTransformer, bo.getFoldFileTransformer]
73 });
74 fs.writeFileSync(fullPath, transformed.content);
75 return fileName;
76});
77
78/**
79 * Get a list of the file names. Sort in reverse order so re-export files
80 * such as "operators.js" are AFTER their more specfic exports, such as
81 * "operators/map.js". This is due to a Webpack bug for node-resolved imports
82 * (rxjs/operators resolves to rxjs/operators.js), Webpack's "alias"
83 * functionality requires that the most broad mapping (rxjs/operators) be at
84 * the end of the alias mapping object. Created Webpack issue:
85 * https://github.com/webpack/webpack/issues/5870
86 *
87 * This is only needed for items in legacy-reexport as others should be resolved
88 * through their package.json.
89 */
90const fileNames = klawSync(LEGACY_REEXPORT_ROOT, {
91 nodir: true,
92 filter: function(item) {
93 return item.path.endsWith('.js');
94 }
95})
96.map(item => item.path)
97.map(path => path.slice((`${__dirname}/${LEGACY_REEXPORT_ROOT}`).length))
98.sort().reverse();
99
100// Create an object hash mapping imports to file names
101const importTargets = fileNames.reduce((acc, fileName) => {
102 // Get the name of the file to be the new directory
103 const directory = fileName.slice(0, fileName.length - 3);
104
105 acc[directory] = fileName;
106 return acc;
107}, {});
108
109createImportTargets(importTargets, "_esm5/", ESM5_PKG);
110createImportTargets(importTargets, "_esm2015/", ESM2015_PKG);
111
112// Make the distribution folder
113mkdirp.sync(PKG_ROOT);
114
115// Copy over the sources
116copySources('src/', SRC_ROOT_PKG);
117// Copy legacy-reexport sources
118copySources('legacy-reexport/', SRC_ROOT_PKG);
119
120copySources(CJS_ROOT, CJS_PKG);
121fs.copySync(LEGACY_REEXPORT_ROOT, CJS_PKG, {overwrite: false, errorOnExist: true});
122
123// Clean up the source maps for CJS sources
124cleanSourceMapRoot(PKG_ROOT, SRC_ROOT_PKG);
125fs.copySync(TYPE_ROOT, TYPE_PKG);
126
127copySources(ESM5_ROOT, ESM5_PKG, true);
128cleanSourceMapRoot(ESM5_PKG, SRC_ROOT_PKG);
129copySources(ESM2015_ROOT, ESM2015_PKG, true);
130cleanSourceMapRoot(ESM2015_PKG, SRC_ROOT_PKG);
131
132// Copy over tsconfig.json for bazel build support
133fs.copySync('./tsconfig.base.json', PKG_ROOT + 'src/tsconfig.json');
134
135fs.writeJsonSync(PKG_ROOT + 'package.json', rootPackageJson, {spaces: 2});
136fs.copySync('src/operators/package.json', PKG_ROOT + '/operators/package.json');
137fs.copySync('src/ajax/package.json', PKG_ROOT + '/ajax/package.json');
138fs.copySync('src/fetch/package.json', PKG_ROOT + '/fetch/package.json');
139fs.copySync('src/webSocket/package.json', PKG_ROOT + '/webSocket/package.json');
140fs.copySync('src/testing/package.json', PKG_ROOT + '/testing/package.json');
141fs.copySync('src/internal-compatibility/package.json', PKG_ROOT + '/internal-compatibility/package.json');
142
143// Copy over migrations
144fs.copySync(MIGRATION_PKG, PKG_ROOT + 'migrations/');
145fs.copySync('./migrations/collection.json', PKG_ROOT + 'migrations/collection.json');
146
147if (fs.existsSync(UMD_ROOT)) {
148 fs.copySync(UMD_ROOT, UMD_PKG);
149 // Clean up source map paths so they can be re-mapped
150 klawSync(UMD_PKG, {filter: (item) => item.path.endsWith('.js.map')})
151 .map(f => f.path)
152 .forEach(fName => {
153 const sourceMap = fs.readJsonSync(fName);
154 sourceMap.sources = sourceMap.sources.map(s => {
155 const nm = 'node_modules/';
156 const rr = path.resolve(ESM5_FOR_ROLLUP_ROOT);
157 if (s.includes(nm)) {
158 return s.substring(s.indexOf(nm) + nm.length);
159 } else if (s.includes(rr)) {
160 return s.substring(s.indexOf(rr) + rr.length);
161 }
162 return s;
163 });
164 fs.writeJsonSync(fName, sourceMap);
165 });
166
167 // Add licenses to tops of bundles
168 addLicenseToFile('LICENSE.txt', UMD_PKG + 'rxjs.umd.js');
169 addLicenseTextToFile(license, UMD_PKG + 'rxjs.umd.min.js');
170 addLicenseToFile('LICENSE.txt', UMD_PKG + 'rxjs.umd.js');
171 addLicenseTextToFile(license, UMD_PKG + 'rxjs.umd.min.js');
172 cleanSourceMapRoot(UMD_PKG, PKG_ROOT);
173}
174
175// remove umd.js/umd.d.ts files that are only needed for creation of the umd bundle
176fs.removeSync(CJS_PKG + '/internal/umd.js');
177fs.removeSync(CJS_PKG + '/internal/umd.js.map');
178fs.removeSync(ESM5_PKG + '/internal/umd.js');
179fs.removeSync(ESM5_PKG + '/internal/umd.js.map');
180fs.removeSync(ESM2015_PKG + '/internal/umd.js');
181fs.removeSync(ESM2015_PKG + '/internal/umd.js.map');
182fs.removeSync(TYPE_PKG + '/internal/umd.d.ts');