Skip to content

Commit

Permalink
feat(package): rxjs distribution now supports main, module and es2015…
Browse files Browse the repository at this point in the history
… keys in package.json

* refactor(package): change distribution to support main, module, es2015 keys in package.json

* refactor(package): re-target from dist/cjs to dist/packages as the new npm published dir
  • Loading branch information
jasonaden authored and benlesh committed Sep 17, 2017
1 parent 7bb8280 commit 988e1af
Show file tree
Hide file tree
Showing 174 changed files with 363 additions and 279 deletions.
127 changes: 102 additions & 25 deletions .make-packages.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,113 @@
var pkg = require('./package.json');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var licenseTool = require('./tools/add-license-to-file');
var addLicenseToFile = licenseTool.addLicenseToFile;
var addLicenseTextToFile = licenseTool.addLicenseTextToFile;
"use strict";

let pkg = require('./package.json');
let fs = require('fs-extra');
let mkdirp = require('mkdirp');
let path = require('path');
let klawSync = require('klaw-sync');
let licenseTool = require('./tools/add-license-to-file');
let addLicenseToFile = licenseTool.addLicenseToFile;
let addLicenseTextToFile = licenseTool.addLicenseTextToFile;

const ROOT = 'dist/';
const CJS_ROOT = ROOT + 'cjs/';
const ESM5_ROOT = ROOT + 'esm5/';
const ESM2015_ROOT = ROOT + 'esm2015/';
const UMD_ROOT = ROOT + 'global/';
const TYPE_ROOT = ROOT + 'typings/';
const PKG_ROOT = ROOT + 'package/';
const CJS_PKG = PKG_ROOT + '_cjs/';
const ESM5_PKG = PKG_ROOT + '_esm5/';
const ESM2015_PKG = PKG_ROOT + '_esm2015/';
const UMD_PKG = PKG_ROOT + 'bundles/';
const TYPE_PKG = PKG_ROOT + '_typings/';


// License info for minified files
var licenseUrl = 'https://github.com/ReactiveX/RxJS/blob/master/LICENSE.txt';
var license = 'Apache License 2.0 ' + licenseUrl;
let licenseUrl = 'https://github.com/ReactiveX/RxJS/blob/master/LICENSE.txt';
let license = 'Apache License 2.0 ' + licenseUrl;

delete pkg.scripts;
fs.removeSync(PKG_ROOT);

var cjsPkg = Object.assign({}, pkg, {
let rootPackageJson = Object.assign({}, pkg, {
name: 'rxjs',
main: 'Rx.js',
typings: 'Rx.d.ts'
main: './_cjs/Rx.js',
module: './_esm5/Rx.js',
es2015: './_esm2015/Rx.js',
typings: './_typings/Rx.d.ts'
});

// Read the files and create package.json files for each. This allows Node,
// Webpack, and any other tool to resolve using the "main", "module", or
// other keys we add to package.json.
klawSync(CJS_ROOT, {
nodir: true,
filter: function(item) {
return item.path.endsWith('.js');
}
})
.map(item => item.path)
.map(path => path.slice((`${__dirname}/${CJS_ROOT}`).length))
.forEach(fileName => {
// Get the name of the directory to create
let parentDirectory = path.dirname(fileName);
// Get the name of the file to be the new directory
let directory = fileName.slice(0, fileName.length - 3);
let targetFileName = path.basename(directory);

fs.ensureDirSync(PKG_ROOT + parentDirectory);

// For "index.js" files, these are re-exports and need a package.json
// in-place rather than in a directory
if (targetFileName !== "index") {
fs.ensureDirSync(PKG_ROOT + directory);
fs.writeJsonSync(PKG_ROOT + directory + '/package.json', {
main: path.relative(PKG_ROOT + directory, CJS_PKG + directory) + '.js',
module: path.relative(PKG_ROOT + directory, ESM5_PKG + directory) + '.js',
es2015: path.relative(PKG_ROOT + directory, ESM2015_PKG + directory) + '.js',
typings: path.relative(PKG_ROOT + directory, TYPE_PKG + directory) + '.d.ts'
});
} else {
// If targeting an "index", there is no directory
directory = directory.split('/').slice(0, -1).join('/');
fs.writeJsonSync(PKG_ROOT + directory + '/package.json', {
main: path.relative(PKG_ROOT + directory, CJS_PKG + directory + '/index.js'),
module: path.relative(PKG_ROOT + directory, ESM5_PKG + directory + '/index.js'),
es2015: path.relative(PKG_ROOT + directory, ESM2015_PKG + directory + '/index.js'),
typings: path.relative(PKG_ROOT + directory, TYPE_PKG + directory + '/index.d.ts')
});
}
});

fs.writeFileSync('dist/cjs/package.json', JSON.stringify(cjsPkg, null, 2));
fs.writeFileSync('dist/cjs/LICENSE.txt', fs.readFileSync('./LICENSE.txt').toString());
fs.writeFileSync('dist/cjs/README.md', fs.readFileSync('./README.md').toString());
// Make the distribution folder
mkdirp.sync(PKG_ROOT);

// Copy over the sources
copySources('src/', PKG_ROOT + 'src/');
copySources(CJS_ROOT, CJS_PKG);
copySources(ESM5_ROOT, ESM5_PKG);
copySources(ESM2015_ROOT, ESM2015_PKG);
fs.copySync(TYPE_ROOT, TYPE_PKG);

// Bundles for CJS only
mkdirp.sync('dist/cjs/bundles');
// UMD bundles
fs.writeFileSync('dist/cjs/bundles/Rx.js', fs.readFileSync('dist/global/Rx.js').toString());
fs.writeFileSync('dist/cjs/bundles/Rx.min.js', fs.readFileSync('dist/global/Rx.min.js').toString());
fs.writeFileSync('dist/cjs/bundles/Rx.min.js.map', fs.readFileSync('dist/global/Rx.min.js.map').toString());
fs.writeJsonSync(PKG_ROOT + 'package.json', rootPackageJson);

fs.copySync(UMD_ROOT, UMD_PKG);
// Add licenses to tops of bundles
addLicenseToFile('LICENSE.txt', 'dist/cjs/bundles/Rx.js');
addLicenseTextToFile(license, 'dist/cjs/bundles/Rx.min.js');
addLicenseToFile('LICENSE.txt', 'dist/global/Rx.js');
addLicenseTextToFile(license, 'dist/global/Rx.min.js');
addLicenseToFile('LICENSE.txt', UMD_PKG + 'Rx.js');
addLicenseTextToFile(license, UMD_PKG + 'Rx.min.js');
addLicenseToFile('LICENSE.txt', UMD_PKG + 'Rx.js');
addLicenseTextToFile(license, UMD_PKG + 'Rx.min.js');

// Copy over the ESM5 files
fs.copySync(ESM5_ROOT, ESM5_PKG);

function copySources(rootDir, packageDir, packageJson) {
// Copy over the CommonJS files
fs.copySync(rootDir, packageDir);
fs.copySync('./LICENSE.txt', packageDir + 'LICENSE.txt');
fs.copySync('./README.md', packageDir + 'README.md');
if (packageJson) {
fs.writeJsonSync(packageDir + 'package.json', packageJson);
}
}
2 changes: 1 addition & 1 deletion .markdown-doctest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {

regexRequire: {
'rxjs/(.*)': function (_, moduleName) {
return require(__dirname + '/dist/cjs/' + moduleName);
return require(__dirname + '/dist/package/' + moduleName);
}
},

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./dist/cjs/Rx');
module.exports = require('./dist/package/Rx');
47 changes: 27 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@
"scripts-info": {
"info": "List available script",
"build_all": "Build all packages (ES6, CJS, UMD) and generate packages",
"build_cjs": "Build CJS package with clean up existing build, copy source into dist",
"build_es6": "Build ES6 package with clean up existing build, copy source into dist",
"build_cjs": "Build CJS package with clean up existing build",
"build_esm5": "Build ESM/ES5 package with clean up existing build",
"build_esm2015": "Build ESM/ES2015 package with clean up existing build",
"build_closure_core": "Minify Global core build using closure compiler",
"build_global": "Build Global package, then minify build",
"build_perf": "Build CJS & Global build, run macro performance test",
"build_test": "Build CJS package & test spec, execute mocha test runner",
"build_cover": "Run lint to current code, build CJS & test spec, execute test coverage",
"build_docs": "Build ES6 & global package, create documentation using it",
"build_docs": "Build ESM2015 & global package, create documentation using it",
"build_spec": "Build test specs",
"check_circular_dependencies": "Check codebase has circular dependencies",
"clean_spec": "Clean up existing test spec build output",
"clean_dist_cjs": "Clean up existing CJS package output",
"clean_dist_es6": "Clean up existing ES6 package output",
"clean_dist_esm5": "Clean up existing ESM/ES5 package output",
"clean_dist_esm2015": "Clean up existing ESM/ES2015 package output",
"clean_dist_global": "Clean up existing Global package output",
"commit": "Run git commit wizard",
"compile_dist_cjs": "Compile codebase into CJS module",
"compile_module_es6": "Compile codebase into ES6",
"compile_module_esm5": "Compile codebase into ESM/ES5",
"compile_module_esm2015": "Compile codebase into ESM/ES2015",
"cover": "Execute test coverage",
"lint_perf": "Run lint against performance test suite",
"lint_spec": "Run lint against test spec",
Expand All @@ -55,29 +58,32 @@
"precommit": "lint-staged",
"commitmsg": "validate-commit-msg",
"info": "npm-scripts-info",
"build_all": "npm-run-all build_cjs build_global generate_packages",
"build_cjs": "npm-run-all clean_dist_cjs copy_src_cjs compile_dist_cjs",
"build_es6": "npm-run-all clean_dist_es6 copy_src_es6 compile_module_es6",
"build_es6_for_docs": "npm-run-all clean_dist_es6 copy_src_es6 compile_dist_es6_for_docs",
"build_all": "npm-run-all clean_dist build_cjs build_esm5 build_esm2015 build_umd generate_packages",
"build_cjs": "npm-run-all clean_dist_cjs compile_dist_cjs",
"build_esm5": "npm-run-all clean_dist_esm5 compile_dist_esm5",
"build_esm2015": "npm-run-all clean_dist_esm2015 compile_module_esm2015",
"build_esm2015_for_docs": "npm-run-all clean_dist_esm2015 compile_dist_esm2015_for_docs",
"build_closure_core": "node ./tools/make-closure-core.js",
"build_global": "npm-run-all clean_dist_global build_es6 && mkdirp ./dist/global && node ./tools/make-umd-bundle.js && npm-run-all build_closure_core clean_dist_es6",
"build_global": "npm-run-all clean_dist_global build_esm5 && mkdirp ./dist/global && node ./tools/make-umd-bundle.js && npm-run-all build_closure_core clean_dist_esm5",
"build_umd": "npm-run-all clean_dist_global && mkdirp ./dist/global && node ./tools/make-umd-bundle.js && npm-run-all build_closure_core",
"build_perf": "webdriver-manager update && npm-run-all build_cjs build_global perf",
"build_test": "shx rm -rf ./dist/ && npm-run-all build_cjs clean_spec build_spec test_mocha",
"build_cover": "shx rm -rf ./dist/ && npm-run-all build_cjs build_spec cover",
"build_docs": "npm-run-all build_global build_es6_for_docs build_cjs clean_spec build_spec tests2png decision_tree_widget && esdoc -c esdoc.json && npm-run-all clean_dist_es6",
"build_test": "shx rm -rf ./dist/ && npm-run-all build_all clean_spec build_spec test_mocha",
"build_cover": "shx rm -rf ./dist/ && npm-run-all build_all build_spec cover",
"build_docs": "npm-run-all build_global build_esm2015_for_docs build_cjs clean_spec build_spec tests2png decision_tree_widget && esdoc -c esdoc.json && npm-run-all clean_dist_esm2015",
"build_spec": "tsc --project ./spec --pretty",
"build_spec_browser": "webpack --config spec/support/webpack.mocha.config.js",
"check_circular_dependencies": "madge ./dist/cjs --circular",
"clean_spec": "shx rm -rf spec-js",
"clean_dist": "shx rm -rf ./dist",
"clean_dist_cjs": "shx rm -rf ./dist/cjs",
"clean_dist_es6": "shx rm -rf ./dist/es6",
"clean_dist_esm5": "shx rm -rf ./dist/esm5",
"clean_dist_esm2015": "shx rm -rf ./dist/esm2015",
"clean_dist_global": "shx rm -rf ./dist/global",
"copy_src_cjs": "mkdirp ./dist/cjs/src && shx cp -r ./src/* ./dist/cjs/src",
"copy_src_es6": "mkdirp ./dist/es6/src && shx cp -r ./src/* ./dist/es6/src",
"commit": "git-cz",
"compile_dist_cjs": "tsc ./dist/cjs/src/Rx.ts ./dist/cjs/src/add/observable/of.ts -m commonjs --lib es5,es2015.iterable,es2015.collection,es2015.promise,dom --sourceMap --outDir ./dist/cjs --target ES5 -d --diagnostics --pretty --noImplicitAny --noImplicitReturns --noImplicitThis --suppressImplicitAnyIndexErrors --moduleResolution node",
"compile_module_es6": "tsc ./dist/es6/src/Rx.ts ./dist/es6/src/add/observable/of.ts -m es2015 --sourceMap --outDir ./dist/es6 --target ES5 -d --diagnostics --pretty --noImplicitAny --noImplicitReturns --noImplicitThis --suppressImplicitAnyIndexErrors --moduleResolution node --noEmitHelpers --lib es5,es2015.iterable,es2015.collection,es2015.promise,dom ",
"compile_dist_es6_for_docs": "tsc ./dist/es6/src/Rx.ts ./dist/es6/src/add/observable/of.ts ./dist/es6/src/MiscJSDoc.ts -m es2015 --sourceMap --outDir ./dist/es6 --target ES6 -d --diagnostics --pretty --noImplicitAny --noImplicitReturns --noImplicitThis --suppressImplicitAnyIndexErrors --moduleResolution node",
"compile_dist_cjs": "tsc ./src/Rx.ts ./src/add/observable/of.ts -m commonjs --lib es5,es2015.iterable,es2015.collection,es2015.promise,dom --sourceMap --outDir ./dist/cjs --target ES5 --diagnostics --pretty --noImplicitAny --noImplicitReturns --noImplicitThis --suppressImplicitAnyIndexErrors --moduleResolution node -d --declarationDir ./dist/typings",
"compile_dist_esm5": "tsc ./src/Rx.ts ./src/add/observable/of.ts -m es2015 --lib es5,es2015.iterable,es2015.collection,es2015.promise,dom --sourceMap --outDir ./dist/esm5 --target ES5 --diagnostics --pretty --noImplicitAny --noImplicitReturns --noImplicitThis --suppressImplicitAnyIndexErrors --moduleResolution node",
"compile_module_esm2015": "tsc ./src/Rx.ts ./src/add/observable/of.ts -m es2015 --lib es5,es2015.iterable,es2015.collection,es2015.promise,dom --sourceMap --outDir ./dist/esm2015 --target es2015 --diagnostics --pretty --noImplicitAny --noImplicitReturns --noImplicitThis --suppressImplicitAnyIndexErrors --moduleResolution node",
"compile_dist_esm2015_for_docs": "tsc ./src/Rx.ts ./src/add/observable/of.ts ./src/MiscJSDoc.ts -m es2015 --sourceMap --outDir ./dist/es6 --target es2015 -d --diagnostics --pretty --noImplicitAny --noImplicitReturns --noImplicitThis --suppressImplicitAnyIndexErrors --moduleResolution node",
"cover": "shx rm -rf dist/cjs && tsc src/Rx.ts src/add/observable/of.ts -m commonjs --lib es5,es2015.iterable,es2015.collection,es2015.promise,dom --outDir dist/cjs --sourceMap --target ES5 -d && nyc --reporter=lcov --reporter=html --exclude=spec/support/**/* --exclude=spec-js/**/* --exclude=node_modules mocha --opts spec/support/default.opts spec-js",
"decision_tree_widget": "cd doc/decision-tree-widget && npm run build && cd ../..",
"doctoc": "doctoc CONTRIBUTING.md",
Expand Down Expand Up @@ -168,6 +174,7 @@
"gzip-size": "^3.0.0",
"http-server": "^0.9.0",
"husky": "^0.13.3",
"klaw-sync": "^3.0.0",
"lint-staged": "3.2.5",
"lodash": "^4.15.0",
"madge": "^1.4.3",
Expand Down Expand Up @@ -204,7 +211,7 @@
"engines": {
"npm": ">=2.0.0"
},
"typings": "./dist/cjs/Rx.d.ts",
"typings": "./dist/package/typings/Rx.d.ts",
"dependencies": {
"symbol-observable": "^1.0.1"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var RxOld = require('rx');
var RxNew = require('../../../../dist/cjs/Rx');
var RxNew = require('../../../../dist/package/Rx');

module.exports = function (suite) {
var source = Array.from({ length: 25 }, function (_, i) { return { value: i % 3 }; });
Expand Down
2 changes: 1 addition & 1 deletion perf/micro/immediate-scheduler/operators/distinct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var RxOld = require('rx');
var RxNew = require('../../../../dist/cjs/Rx');
var RxNew = require('../../../../dist/package/Rx');

module.exports = function (suite) {
var source = Array.from({ length: 25 }, function (_, i) { return i % 3; });
Expand Down
2 changes: 1 addition & 1 deletion spec/Notification-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import * as Rx from '../dist/cjs/Rx';
import * as Rx from '../dist/package/Rx';

declare const expectObservable;
const Notification = Rx.Notification;
Expand Down
6 changes: 3 additions & 3 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {expect} from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../dist/cjs/Rx';
import {TeardownLogic} from '../dist/cjs/Subscription';
import * as Rx from '../dist/package/Rx';
import {TeardownLogic} from '../dist/package/Subscription';
import marbleTestingSignature = require('./helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { map } from '../dist/cjs/operators';
import { map } from '../dist/package/operators';

declare const { asDiagram, rxTestScheduler };
declare const cold: typeof marbleTestingSignature.cold;
Expand Down
2 changes: 1 addition & 1 deletion spec/Scheduler-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import * as Rx from '../dist/cjs/Rx';
import * as Rx from '../dist/package/Rx';

const Scheduler = Rx.Scheduler;

Expand Down
2 changes: 1 addition & 1 deletion spec/Subject-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import * as Rx from '../dist/cjs/Rx';
import * as Rx from '../dist/package/Rx';
import marbleTestingSignature = require('./helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const { time };
Expand Down
2 changes: 1 addition & 1 deletion spec/Subscriber-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../dist/cjs/Rx';
import * as Rx from '../dist/package/Rx';

const Subscriber = Rx.Subscriber;

Expand Down
2 changes: 1 addition & 1 deletion spec/Subscription-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import * as Rx from '../dist/cjs/Rx';
import * as Rx from '../dist/package/Rx';

const Observable = Rx.Observable;
const Subscription = Rx.Subscription;
Expand Down
50 changes: 25 additions & 25 deletions spec/exports-spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { expect } from 'chai';
import { bindCallback } from '../dist/cjs/observable/bindCallback';
import { bindNodeCallback } from '../dist/cjs/observable/bindNodeCallback';
import { combineLatest } from '../dist/cjs/observable/combineLatest';
import { concat } from '../dist/cjs/observable/concat';
import { defer } from '../dist/cjs/observable/defer';
import { empty } from '../dist/cjs/observable/empty';
import { forkJoin } from '../dist/cjs/observable/forkJoin';
import { from } from '../dist/cjs/observable/from';
import { fromEvent } from '../dist/cjs/observable/fromEvent';
import { fromEventPattern } from '../dist/cjs/observable/fromEventPattern';
import { fromPromise } from '../dist/cjs/observable/fromPromise';
import { _if } from '../dist/cjs/observable/if';
import { interval } from '../dist/cjs/observable/interval';
import { merge } from '../dist/cjs/observable/merge';
import { never } from '../dist/cjs/observable/never';
import { of } from '../dist/cjs/observable/of';
import { onErrorResumeNext } from '../dist/cjs/observable/onErrorResumeNext';
import { pairs } from '../dist/cjs/observable/pairs';
import { race } from '../dist/cjs/observable/race';
import { range } from '../dist/cjs/observable/range';
import { _throw } from '../dist/cjs/observable/throw';
import { timer } from '../dist/cjs/observable/timer';
import { using } from '../dist/cjs/observable/using';
import { zip } from '../dist/cjs/observable/zip';
import * as Rx from '../dist/cjs/Rx';
import { bindCallback } from '../dist/package/observable/bindCallback';
import { bindNodeCallback } from '../dist/package/observable/bindNodeCallback';
import { combineLatest } from '../dist/package/observable/combineLatest';
import { concat } from '../dist/package/observable/concat';
import { defer } from '../dist/package/observable/defer';
import { empty } from '../dist/package/observable/empty';
import { forkJoin } from '../dist/package/observable/forkJoin';
import { from } from '../dist/package/observable/from';
import { fromEvent } from '../dist/package/observable/fromEvent';
import { fromEventPattern } from '../dist/package/observable/fromEventPattern';
import { fromPromise } from '../dist/package/observable/fromPromise';
import { _if } from '../dist/package/observable/if';
import { interval } from '../dist/package/observable/interval';
import { merge } from '../dist/package/observable/merge';
import { never } from '../dist/package/observable/never';
import { of } from '../dist/package/observable/of';
import { onErrorResumeNext } from '../dist/package/observable/onErrorResumeNext';
import { pairs } from '../dist/package/observable/pairs';
import { race } from '../dist/package/observable/race';
import { range } from '../dist/package/observable/range';
import { _throw } from '../dist/package/observable/throw';
import { timer } from '../dist/package/observable/timer';
import { using } from '../dist/package/observable/using';
import { zip } from '../dist/package/observable/zip';
import * as Rx from '../dist/package/Rx';

describe('exports', () => {
it('should have rxjs/observable/bindCallback', () => {
Expand Down
10 changes: 5 additions & 5 deletions spec/helpers/marble-testing.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
///<reference path='../../typings/index.d.ts'/>
import {Observable} from '../../dist/cjs/Observable';
import {SubscriptionLog} from '../../dist/cjs/testing/SubscriptionLog';
import {ColdObservable} from '../../dist/cjs/testing/ColdObservable';
import {HotObservable} from '../../dist/cjs/testing/HotObservable';
import {TestScheduler, observableToBeFn, subscriptionLogsToBeFn} from '../../dist/cjs/testing/TestScheduler';
import {Observable} from '../../dist/package/Observable';
import {SubscriptionLog} from '../../dist/package/testing/SubscriptionLog';
import {ColdObservable} from '../../dist/package/testing/ColdObservable';
import {HotObservable} from '../../dist/package/testing/HotObservable';
import {TestScheduler, observableToBeFn, subscriptionLogsToBeFn} from '../../dist/package/testing/TestScheduler';

declare const global: any;

Expand Down
Loading

0 comments on commit 988e1af

Please sign in to comment.