Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

angular

v9.0.15arrow_drop_down
v9.0.15
v9.0.14
v9.0.13
v9.0.12
v9.0.11
v9.0.9
v9.0.8
v9.0.7
v9.0.6
v9.0.5
v9.0.4
v9.0.3
v9.0.2
v9.0.1
v9.0.0
v0.2.2
v0.2.1
v0.2.0
v0.1.9
v0.1.8
v0.1.7
v0.1.6
v0.1.5
v0.1.4
v0.1.3
v0.1.2
v0.1.1
v0.1.0
STATUS
Passing
DOWNLOADS
7,293
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
A day ago
SIZE
N/A
Bit environment for transpiling Angular components according to the Angular Package Format, by using ng-packagr.
3 contributors
Install angular as a package?
Copied
npm i @bit/bit.envs.compilers.angular
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
index.ts
106 Lines(82 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//implicit dependencies made explicit
import '@angular/compiler';
import '@angular/compiler-cli';
import '@angular/core';
import '@bazel/typescript';
import 'typescript';
import 'tslib';
import 'tsickle';
import 'rxjs';

import * as ngPackagr from 'ng-packagr';
import path from 'path';
import debug from 'debug';
import Vinyl from 'vinyl';
import * as fs from 'fs';

import { TSConfig } from './tsconfig';

import {
  CompilerContext,
  BuildResults,
  createCapsule,
  destroyCapsule,
  getTestFiles,
  readFiles,
} from '@bit/bit.envs.compilers.utils';

if (process.env.DEBUG) {
  debug('build');
}

const DEV_DEPS = {
  tslib: '>=1.0.0',
  'webpack-env': '>=0.8.0',
  '@angular/core': '>= 8.0.0',
};
const PKG_JSON_KEYS = ['es2015', 'esm5', 'esm2015', 'fesm5', 'fesm2015', 'module', 'typings'];

export function getDynamicPackageDependencies(): Record<string, any> {
  return {
    devDependencies: DEV_DEPS,
  };
}

export async function action(ctx: CompilerContext): Promise<BuildResults> {
  // build capsule

  const { res, directory } = await createCapsule(ctx.context.isolate, { shouldBuildDependencies: true });
  const distDir = path.join(directory, 'dist');

  const componentObject = res.componentWithDependencies.component.toObject();
  console.log(res.componentWithDependencies);
  const { files, mainFile } = componentObject;

  debug(`Building capsule in ${directory}`);

  // create tsconfig.json
  let tests: Vinyl[] = getTestFiles(files, []);
  let TS = Object.assign(TSConfig, {
    exclude: [...TSConfig.exclude, ...tests.map((s: Vinyl): string => s.path)],
  });

  const TSFile = path.resolve(directory, 'tsconfig.json');
  await fs.writeFileSync(TSFile, JSON.stringify(TS, null, 4));

  //create ng-package.json
  const ngPackageFile = path.resolve(directory, 'ng-package.json');
  const ngPackage = {
    dest: 'dist',
    lib: {
      entryFile: mainFile,
    },
    whitelistedNonPeerDependencies: [...Object.keys(DEV_DEPS), ...Object.keys(componentObject.packageDependencies)],
  };
  if (!fs.existsSync(ngPackageFile)) {
    fs.writeFileSync(ngPackageFile, JSON.stringify(ngPackage, null, 4));
  }

  await ngPackagr
    .ngPackagr()
    .forProject(ngPackageFile)
    .withTsConfig(TSFile)
    .build()
    .catch((e: Error): void => {
      console.error(e);
      throw e;
    });

  //get dists
  const dists = (await readFiles(distDir)) || [];

  //update keys from package.json
  // eslint-disable-next-line @typescript-eslint/no-var-requires
  const pkgJsonContent = require(path.resolve(distDir, 'package.json'));
  const packageJson: Record<string, any> = {};
  PKG_JSON_KEYS.forEach((k: string): void => {
    packageJson[k] = pkgJsonContent[k];
  });
  destroyCapsule(res.capsule);

  return {
    mainFile: pkgJsonContent.main,
    dists,
    packageJson,
  };
}