Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: refactor build script #4861

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 2 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- checkout
- *restore_cache
- run: npm i
- run: npm run build_all
- run: npm run build
- persist_to_workspace:
root: .
paths:
Expand Down Expand Up @@ -58,13 +58,6 @@ jobs:
- attach_workspace: *attach_options
- run: npm run dtslint

typescript3:
<<: *defaults
steps:
- attach_workspace: *attach_options
- run: npm i --no-save typescript@3.*
- run: npm run build_cjs

workflows:
version: 2
build_and_test:
Expand All @@ -78,9 +71,4 @@ workflows:
- build
- dtslint:
requires:
- build
- typescript3:
requires:
- lint
- test
- dtslint
- build
2 changes: 1 addition & 1 deletion .dependency-cruiser.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"severity": "error",
"comment": "Error in case we have circular dependencies",
"from": {
"path": "^dist/esm5"
"path": "^dist/formats/esm5"
},
"to": {
"circular": true
Expand Down
42 changes: 0 additions & 42 deletions .make-helpers.js

This file was deleted.

188 changes: 57 additions & 131 deletions .make-packages.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,62 @@
"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;
let makePackages = require('./.make-helpers');
let copySources = makePackages.copySources;
let cleanSourceMapRoot = makePackages.cleanSourceMapRoot;
let bo = null;
// Build Optimizer is not available on Node 4.x. Using a try/catch
// here to make sure the build passes on Travis using Node 4, but
// the NPM distribution will run through build-optimizer.
try {
bo = require('@angular-devkit/build-optimizer');
} catch (e) {}


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

// License info for minified files
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);

let rootPackageJson = Object.assign({}, pkg, {
name: 'rxjs',
main: './index.js',
typings: './index.d.ts',
module: './_esm5/index.js',
es2015: './_esm2015/index.js'
const { writeFileSync } = require('fs');
const { cp, exec, ls, mkdir, rm } = require('shelljs');
const { buildOptimizer } = require('@angular-devkit/build-optimizer');
const pkg = require('./package.json');

// TODO:
// - what about sourcemaps?
// Angular itself has inline sourcemaps. I don't see the point, since you don't have sources
// on the package.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to do about sourcemaps. I think the current ones are a bit weird and work only because the TS sourcefiles are bundled. We could inline sourcemaps but I'm not sure if that impacts parsing performance.

// - what about shipping TS for bazel support?
// I think this isn't needed anymore?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does RxJS still need to ship TS for Bazel support? I thought this wasn't needed anymore.

// - maybe add a debug flag?
// Would turn silent off for execs, and print stuff on non-exec things
// - whats src/Rx.global.js for?

// Ensure commands are run from __dirname.
process.chdir(__dirname);

// Clean directories before building.
console.log('# Deleting dist dir.')
rm('-rf', './dist');

// Run TS to produce cjs, esm5, esm2015, and typings.
console.log('# Compiling TypeScript sources.');
exec('npm run tsc -- --build ./build-configuration/tsconfig.project.json', { silent: true });

// Run BO over esm5 and esm2015 to remove side effects.
console.log('# Removing side effects from esm5 and esm2015.');
const buildOptimizerOptions = { emitSourceMap: false, isSideEffectFree: true };
ls('./dist/formats/{esm5,esm2015}/**/*.js').forEach(inputFilePath => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running Build Optimizer over both ESM5 and ESM2015 should enable tree shaking for both (and prevent rollup/rollup#2415).

const { content } = buildOptimizer({ inputFilePath, ...buildOptimizerOptions });
writeFileSync(inputFilePath, content);
});

// Execute build optimizer transforms on ESM5 files
klawSync(ESM5_ROOT, {
nodir: true,
filter: function(item) {
return item.path.endsWith('.js');
// Create UMD bundles, then remove the UMD entry point.
console.log('# Creating UMD bundles.')
exec('npm run rollup -- --config ./build-configuration/rollup.config.js', { silent: true });
rm('-rf', './dist/**/internal/umd.js');

// Add package.json, licenses, and readme.
console.log('# Adding package metadata.');
['', 'ajax/', 'fetch/', 'operators/', 'testing/', 'webSocket/'].forEach(entryPoint => {
let entryPointPkg = {
'name': `rxjs/${entryPoint.replace('/', '')}`,
'main': `../formats/cjs/${entryPoint}index.js`,
'module': `../formats/esm5/${entryPoint}index.js`,
'es2015': `../formats/esm2015/${entryPoint}index.js`,
'typings': `../formats/typings/${entryPoint}index.d.ts`,
'sideEffects': false
};

if (entryPoint === '') {
entryPointPkg = { ...pkg, ...entryPointPkg, name: 'rxjs' };
cp('./README.md', `./dist/README.md`);
} else {
mkdir(`./dist/${entryPoint}`);
}
})
.map(item => item.path.slice((`${__dirname}/${ESM5_ROOT}`).length))
.map(fileName => {
if (!bo) return fileName;
let fullPath = path.resolve(__dirname, ESM5_ROOT, fileName);
// The file won't exist when running build_test as we don't create the ESM5 sources
if (!fs.existsSync(fullPath)) return fileName;
let content = fs.readFileSync(fullPath).toString();
let transformed = bo.transformJavascript({
content: content,
getTransforms: [bo.getPrefixClassesTransformer, bo.getPrefixFunctionsTransformer, bo.getFoldFileTransformer]
});
fs.writeFileSync(fullPath, transformed.content);
return fileName;
writeFileSync(`./dist/${entryPoint}package.json`, JSON.stringify(entryPointPkg, undefined, 2));
cp('./LICENSE.txt', `./dist/${entryPoint}LICENSE.txt`);
});

// Make the distribution folder
mkdirp.sync(PKG_ROOT);

// Copy over the sources
copySources('src/', SRC_ROOT_PKG);

copySources(CJS_ROOT, CJS_PKG);

// Clean up the source maps for CJS sources
cleanSourceMapRoot(PKG_ROOT, SRC_ROOT_PKG);
fs.copySync(TYPE_ROOT, TYPE_PKG);

copySources(ESM5_ROOT, ESM5_PKG, true);
cleanSourceMapRoot(ESM5_PKG, SRC_ROOT_PKG);
copySources(ESM2015_ROOT, ESM2015_PKG, true);
cleanSourceMapRoot(ESM2015_PKG, SRC_ROOT_PKG);

// Copy over tsconfig.json for bazel build support
fs.copySync('./tsconfig.base.json', PKG_ROOT + 'src/tsconfig.json');

fs.writeJsonSync(PKG_ROOT + 'package.json', rootPackageJson, {spaces: 2});
fs.copySync('src/operators/package.json', PKG_ROOT + '/operators/package.json');
fs.copySync('src/ajax/package.json', PKG_ROOT + '/ajax/package.json');
fs.copySync('src/webSocket/package.json', PKG_ROOT + '/webSocket/package.json');
fs.copySync('src/testing/package.json', PKG_ROOT + '/testing/package.json');

if (fs.existsSync(UMD_ROOT)) {
fs.copySync(UMD_ROOT, UMD_PKG);
// Clean up source map paths so they can be re-mapped
klawSync(UMD_PKG, {filter: (item) => item.path.endsWith('.js.map')})
.map(f => f.path)
.forEach(fName => {
const sourceMap = fs.readJsonSync(fName);
sourceMap.sources = sourceMap.sources.map(s => {
const nm = 'node_modules/';
const rr = path.resolve(ESM5_FOR_ROLLUP_ROOT);
if (s.includes(nm)) {
return s.substring(s.indexOf(nm) + nm.length);
} else if (s.includes(rr)) {
return s.substring(s.indexOf(rr) + rr.length);
}
return s;
});
fs.writeJsonSync(fName, sourceMap);
});

// Add licenses to tops of bundles
addLicenseToFile('LICENSE.txt', UMD_PKG + 'rxjs.umd.js');
addLicenseTextToFile(license, UMD_PKG + 'rxjs.umd.min.js');
addLicenseToFile('LICENSE.txt', UMD_PKG + 'rxjs.umd.js');
addLicenseTextToFile(license, UMD_PKG + 'rxjs.umd.min.js');
cleanSourceMapRoot(UMD_PKG, PKG_ROOT);
}

// remove umd.js/umd.d.ts files that are only needed for creation of the umd bundle
fs.removeSync(CJS_PKG + '/internal/umd.js');
fs.removeSync(CJS_PKG + '/internal/umd.js.map');
fs.removeSync(ESM5_PKG + '/internal/umd.js');
fs.removeSync(ESM5_PKG + '/internal/umd.js.map');
fs.removeSync(ESM2015_PKG + '/internal/umd.js');
fs.removeSync(ESM2015_PKG + '/internal/umd.js.map');
fs.removeSync(TYPE_PKG + '/internal/umd.d.ts');
console.log('# Done!');
31 changes: 31 additions & 0 deletions build-configuration/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { join } from 'path';
import { readFileSync } from 'fs';
import resolve from 'rollup-plugin-node-resolve';
import license from 'rollup-plugin-license';
import { terser } from "rollup-plugin-terser";

const licenseText = readFileSync(join(__dirname, '../LICENSE.txt'));
const banner = `/**
@license
${licenseText}
**/`;

const generateConfig = (minify = false) => ({
input: join(__dirname, '../dist/formats/esm5/internal/umd.js'),
output: {
file: join(__dirname, `../dist/bundles/rxjs.umd${minify ? '.min' : ''}.js`),
format: 'umd',
name: 'rxjs',
amd: { id: 'rxjs' },
},
plugins: [
resolve(),
...(minify ? [terser()] : []),
license({ banner }),
]
});

export default [
generateConfig(),
generateConfig(true),
];
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"sourceMap": false,
"noEmit": false,
"baseUrl": "../src",
"sourceRoot": "../src",
"paths": {
"rxjs": ["./"],
"rxjs/*": ["./*"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"extends": "./tsconfig.base.json",
"composite": true,
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "../dist/cjs"
"outDir": "../dist/formats/cjs"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"extends": "./tsconfig.base.json",
"composite": true,
"compilerOptions": {
"module": "es2015",
"importHelpers": true,
"moduleResolution": "node",
"target": "es2015",
"outDir": "../dist/esm2015"
"outDir": "../dist/formats/esm2015"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"extends": "./tsconfig.base.json",
"composite": true,
"compilerOptions": {
"module": "es2015",
"importHelpers": true,
"moduleResolution": "node",
"target": "es5",
"outDir": "../dist/esm5"
"outDir": "../dist/formats/esm5"
}
}
16 changes: 16 additions & 0 deletions build-configuration/tsconfig.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"references": [
{
"path": "./tsconfig.cjs.json"
},
{
"path": "./tsconfig.esm5.json"
},
{
"path": "./tsconfig.esm2015.json"
},
{
"path": "./tsconfig.types.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"extends": "./tsconfig.base.json",
"composite": true,
"compilerOptions": {
"module": "es2015",
"target": "esnext",
"removeComments": false,
"declaration": true,
"declarationDir": "../dist/typings",
"declarationDir": "../dist/formats/typings",
"emitDeclarationOnly": true
}
}