Skip to content

Commit

Permalink
feat: defer internal cli imports to ng-compat component
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Oct 9, 2023
1 parent 0b0fa55 commit 6ebf5cf
Show file tree
Hide file tree
Showing 33 changed files with 377 additions and 136 deletions.
108 changes: 108 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module.exports = {
parser: '@typescript-eslint/parser',
ignorePatterns: ["node_modules/"],
parserOptions: {
createDefaultProgram: true
},
env: {
browser: true,
node: true,
es6: true,
jest: true
},
extends: [
'plugin:@typescript-eslint/recommended',
"plugin:markdown/recommended"
],
plugins: [
'@typescript-eslint',
'import'
],
overrides: [
{
files: ['*.ts', '*.js'],
parserOptions: {
project: require.resolve('./tsconfig.json'),
createDefaultProgram: true,
}
}
],
rules: {
complexity: ['error', { max: 25 }],
'no-console': ['error'],
'@typescript-eslint/no-unused-vars': ['error', { args: 'after-used' }],
'@typescript-eslint/no-use-before-define': [
'error',
{ functions: false, classes: true, variables: true, typedefs: true },
],
'@typescript-eslint/return-await': 'error',
'no-return-await': 'off',
'@typescript-eslint/no-misused-promises': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'no-shadow': 'off',
'@typescript-eslint/no-shadow': ['error'],
// ERRORS OF plugin:@typescript-eslint/recommended
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/no-explicit-any': 'off',
// END ERRORS OF plugin:@typescript-eslint/recommended

// ERRORS OF 'plugin:promise/recommended'
'promise/always-return': 'off',
'promise/no-nesting': 'off',
// END ERRORS OF 'plugin:promise/recommended'
'import/prefer-default-export': 'off', // typescript works better without default export
'import/export': 'off', // typescript does allow multiple export default when overloading. not sure why it's enabled here. rule source: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/export.md
'prefer-object-spread': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'import/no-cycle': 'off',
'import/no-useless-path-segments': 'off',
'no-restricted-imports': [
'error',
{
name: '@teambit/ui-foundation.ui.react-router.link',
message: 'use @teambit/base-ui.routing.link',
},
{
name: '@teambit/ui-foundation.ui.react-router.nav-link',
message: 'use @teambit/base-ui.routing.nav-link',
},
],
'lines-between-class-members': 'off',
radix: 'off',
'no-underscore-dangle': 'off',
'no-param-reassign': 'off',
'no-return-assign': [0, 'except-parens'],
'class-methods-use-this': 'off',
// 'simple-import-sort/sort': 'error',
// 'sort-imports': 'off',
// 'import/first': 'error',
// 'import/newline-after-import': 'error',
'import/no-duplicates': 'error',
'prefer-destructuring': 'off',
'import/no-extraneous-dependencies': 'off',
'no-restricted-syntax': [2, 'ForInStatement', 'LabeledStatement', 'WithStatement'],
'no-unused-expressions': 'off',
'max-len': [
2,
200,
2,
{
ignoreUrls: true,
ignoreComments: true,
ignoreRegExpLiterals: true,
ignoreStrings: true,
ignoreTemplateLiterals: true,
},
],
'max-lines': [2, 1800],
'func-names': [0],

// ERRORS OF plugin:react/recommended
'react/no-unescaped-entities': 'off',
}
};
7 changes: 7 additions & 0 deletions angular/devkit/ng-compat/build-angular/browser-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { VERSION } from '@angular/cli';

export let BrowserBuilderSchema: any;

if (Number(VERSION.major) > 12) {
BrowserBuilderSchema = require('@angular-devkit/build-angular/src/builders/browser/schema').Schema;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { VERSION } from '@angular/cli';

export let IndexHtmlWebpackPlugin: any;
if (VERSION.major === '16' && Number(VERSION.minor) >= 2) {
// 16.2+
IndexHtmlWebpackPlugin = require('@angular-devkit/build-angular/src/tools/webpack/plugins/index-html-webpack-plugin').IndexHtmlWebpackPlugin;
} else {
IndexHtmlWebpackPlugin = require('@angular-devkit/build-angular/src/webpack/plugins/index-html-webpack-plugin').IndexHtmlWebpackPlugin;
}
7 changes: 7 additions & 0 deletions angular/devkit/ng-compat/build-angular/server-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { VERSION } from '@angular/cli';

export let OutputHashing: any;

if (VERSION.major === '12') {
OutputHashing = require('@angular-devkit/build-angular/src/server/schema').OutputHashing;
}
18 changes: 18 additions & 0 deletions angular/devkit/ng-compat/build-angular/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { json, logging } from '@angular-devkit/core';
import { VERSION } from '@angular/cli';
import { BrowserBuilderSchema } from '../browser-schema';

export * from './normalize-cache';
export * from './package-chunk-sort';
export * from './webpack-browser-config';

export let normalizeBrowserSchema: (workspaceRoot: string, projectRoot: string, projectSourceRoot: string | undefined, options: typeof BrowserBuilderSchema, metadata?: json.JsonObject, logger?: logging.LoggerApi) => any;
export let normalizeOptimization: (optimization?: any) => any;
export let BuildBrowserFeatures: any;

if (VERSION.major) {
const utils = require('@angular-devkit/build-angular/src/utils');
normalizeBrowserSchema = utils.normalizeBrowserSchema;
normalizeOptimization = utils.normalizeOptimization;
BuildBrowserFeatures = utils.BuildBrowserFeatures;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { VERSION } from '@angular/cli';
import { json } from '@angular-devkit/core';


export let normalizeCacheOptions: (metadata: json.JsonObject, worspaceRoot: string) => any;
if (Number(VERSION.major) > 12) {
normalizeCacheOptions = require('@angular-devkit/build-angular/src/utils/normalize-cache').normalizeCacheOptions;
}
11 changes: 11 additions & 0 deletions angular/devkit/ng-compat/build-angular/utils/package-chunk-sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { VERSION } from '@angular/cli';

export let generateEntryPoints: (options: {
styles: any[];
scripts: any[];
isHMREnabled?: boolean;
}) => any[];

if (VERSION.major) {
generateEntryPoints = require('@angular-devkit/build-angular/src/utils/package-chunk-sort').generateEntryPoints;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { logging } from '@angular-devkit/core';
import { VERSION } from '@angular/cli';
import { Configuration } from '@teambit/webpack';
import { BrowserBuilderSchema } from '../browser-schema';

type wbConfigFn = (
workspaceRoot: string,
projectRoot: string,
sourceRoot: string | undefined,
projectName: string,
options: any,
webpackPartialGenerator: any,
logger: logging.LoggerApi,
extraBuildOptions: Partial<any>
) => Promise<Configuration>;

type wbConfigFnV12 = (
workspaceRoot: string,
projectRoot: string,
sourceRoot: string | undefined,
options: any,
webpackPartialGenerator: any,
logger: logging.LoggerApi,
extraBuildOptions: Partial<any>
) => Promise<Configuration>;

export let generateWebpackConfig: wbConfigFn & wbConfigFnV12;
export let getIndexOutputFile: (index: typeof BrowserBuilderSchema['index']) => string;

if (VERSION.major) {
const webpackBrowserConfig = require('@angular-devkit/build-angular/src/utils/webpack-browser-config');
generateWebpackConfig = webpackBrowserConfig.generateWebpackConfig;
getIndexOutputFile = webpackBrowserConfig.getIndexOutputFile;
}
20 changes: 20 additions & 0 deletions angular/devkit/ng-compat/build-angular/webpack/configs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Configuration } from 'webpack';
import { VERSION } from '@angular/cli';

type configFn = (wco: any) => Promise<Configuration> | Configuration;
let configs: any;
if (VERSION.major === '16' && Number(VERSION.minor) >= 2) {
// 16.2+
configs = require('@angular-devkit/build-angular/src/tools/webpack/configs');
} else {
configs = require('@angular-devkit/build-angular/src/webpack/configs');
}

export const getCommonConfig: configFn = configs.getCommonConfig;
export const getDevServerConfig: configFn = configs.getDevServerConfig;
export const getStylesConfig: configFn = configs.getStylesConfig;

// Angular v12, undefined for other versions
export const getBrowserConfig: configFn = configs.getBrowserConfig;
export const getStatsConfig: (wco: any) => { stats: any } = configs.getStatsConfig;
export const getTypeScriptConfig: configFn = configs.getTypeScriptConfig;
2 changes: 2 additions & 0 deletions angular/devkit/ng-compat/build-angular/webpack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './configs';
export * from './stats';
12 changes: 12 additions & 0 deletions angular/devkit/ng-compat/build-angular/webpack/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { WebpackLoggingCallback } from '@angular-devkit/build-webpack';
import { logging } from '@angular-devkit/core';
import { VERSION } from '@angular/cli';
import { BrowserBuilderSchema } from '../browser-schema';

export let createWebpackLoggingCallback: (options: typeof BrowserBuilderSchema, logger: logging.LoggerApi) => WebpackLoggingCallback;

if (VERSION.major === '16' && Number(VERSION.minor) >= 2) {
createWebpackLoggingCallback = require('@angular-devkit/build-angular/src/tools/webpack/utils/stats').createWebpackLoggingCallback;
} else {
createWebpackLoggingCallback = require('@angular-devkit/build-angular/src/webpack/utils/stats').createWebpackLoggingCallback;
}
21 changes: 21 additions & 0 deletions angular/devkit/ng-compat/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"componentId": {
"scope": "bitdev.angular",
"name": "dev-services/ng-compat"
},
"propagate": true,
"extensions": {
"teambit.dependencies/dependency-resolver": {
"policy": {
"dependencies": {
},
"peerDependencies": {
"@angular-devkit/build-angular": ">= 0.0.1",
"@angular/cli": ">= 8.0.0",
"webpack": ">= 4.44.2"
}
}
},
"teambit.node/[email protected]": {}
}
}
4 changes: 4 additions & 0 deletions angular/devkit/ng-compat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './build-angular/index-html-webpack-plugin';
export * from './build-angular/webpack';
export * from './build-angular/utils';
export * from './build-angular/browser-schema';
2 changes: 1 addition & 1 deletion angular/devkit/preview/preview/angular-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AsyncEnvHandler, EnvHandler } from '@teambit/envs';
import { EnvPreviewConfig, Preview } from '@teambit/preview';
import { WebpackConfigTransformer, WebpackConfigWithDevServer } from '@teambit/webpack';
import objectHash from 'object-hash';
import { Configuration } from 'webpack';
import type { Configuration } from 'webpack';
// Make sure bit recognizes the dependencies
import 'webpack-dev-server';

Expand Down
2 changes: 2 additions & 0 deletions angular/devkit/preview/preview/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"@angular/platform-browser-dynamic": ">= 8.0.0",
"rxjs": "^6.5.5 || ^7.4.0",
"typescript": ">= 3.5.3",
"webpack": ">= 4.44.2",
"webpack-dev-server": ">= 3.11.2",
"zone.js": ">= 0.9.1"
}
}
Expand Down
3 changes: 2 additions & 1 deletion angular/devkit/webpack/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"peerDependencies": {
"@angular-devkit/build-angular": ">= 0.0.1",
"typescript": ">= 3.5.3"
"typescript": ">= 3.5.3",
"webpack": ">= 4.44.2"
},
"devDependencies": {
"@babel/runtime": "7.22.15"
Expand Down
2 changes: 1 addition & 1 deletion angular/devkit/webpack/ng-webpack-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from '@teambit/webpack';
import { generateTransformers, runTransformers } from '@teambit/webpack.webpack-bundler';
import { join } from 'path';
import { Configuration, WebpackPluginInstance } from 'webpack';
import type { Configuration, WebpackPluginInstance } from 'webpack';
import { WebpackConfigFactoryOpts } from './utils';
import { StatsLoggerPlugin } from './webpack-plugins/stats-logger';

Expand Down
2 changes: 1 addition & 1 deletion angular/devkit/webpack/ng-webpack-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '@teambit/webpack';
import { generateTransformers, runTransformers } from '@teambit/webpack.webpack-bundler';
import { join, posix } from 'path';
import { Configuration } from 'webpack';
import type { Configuration } from 'webpack';
import { WebpackConfigFactoryOpts } from './utils';

export type WebpackDevServerOptions = {
Expand Down
2 changes: 1 addition & 1 deletion angular/devkit/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BundlerSetup } from '@bitdev/angular.dev-services.common';
import { BundlerContext, DevServerContext } from '@teambit/bundler';
import { Logger } from '@teambit/logger';
import { WebpackConfigWithDevServer } from '@teambit/webpack';
import { Configuration, WebpackPluginInstance } from 'webpack';
import type { Configuration, WebpackPluginInstance } from 'webpack';


export type WebpackConfig = Configuration;
Expand Down
2 changes: 1 addition & 1 deletion angular/devkit/webpack/webpack-plugins/angular-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// eslint-disable-next-line max-classes-per-file
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { NgccProcessor } from '@bitdev/angular.dev-services.ngcc';
import { Compiler } from 'webpack';
import type { Compiler } from 'webpack';
import { NodeJSFileSystem } from './nodejs-file-system';

interface ResourceData {
Expand Down
23 changes: 6 additions & 17 deletions angular/devkit/webpack/webpack-plugins/stats-logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Compiler, Stats } from 'webpack';
import { loadEsmModule } from '@bitdev/angular.dev-services.common';

const PLUGIN_NAME = 'angular-stats-logger-plugin';

Expand All @@ -11,24 +10,14 @@ export class StatsLoggerPlugin {
};
try {
// "Executed when the compilation has completed."
try {
const { createWebpackLoggingCallback } = await loadEsmModule('@angular-devkit/build-angular/src/webpack/utils/stats') as any;
const loggingCallback = createWebpackLoggingCallback({} as any, logger as any) as any;
compiler.hooks.done.tap(PLUGIN_NAME, (stats: Stats) => {
loggingCallback(stats, { stats: { logging: 'info', colors: true } });
});
} catch (e) {
// angular v16+
try {
const { createWebpackLoggingCallback } = await loadEsmModule('@angular-devkit/build-angular/src/tools/webpack/utils/stats') as any;
const loggingCallback = createWebpackLoggingCallback({} as any, logger as any) as any;
compiler.hooks.done.tap(PLUGIN_NAME, (stats: Stats) => {
loggingCallback(stats, { stats: { logging: 'info', colors: true } });
});
} catch (e) {}
}
const { createWebpackLoggingCallback } = await import('@bitdev/angular.dev-services.ng-compat');
const loggingCallback = createWebpackLoggingCallback({} as any, logger as any) as any;
compiler.hooks.done.tap(PLUGIN_NAME, (stats: Stats) => {
loggingCallback(stats, { stats: { logging: 'info', colors: true } });
});
} catch (e) {
// if it fails, just continue (we don't need logging to break the build)
console.log('Failed to load @bitdev/angular.dev-services.ng-compat', e);
}
}
}
2 changes: 2 additions & 0 deletions angular/envs/angular-v12-env/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@angular-eslint/eslint-plugin-template": "^12.1.0",
"@angular-eslint/template-parser": "^12.1.0",
"@angular/animations": "^12.2.16",
"@angular/cli": "~12.2.16",
"@angular/common": "^12.2.16",
"@angular/compiler": "^12.2.16",
"@angular/compiler-cli": "^12.2.16",
Expand All @@ -31,6 +32,7 @@
"@types/react-dev-utils": "~9.0.8",
"@types/remark-prism": "~1.3.0",
"@types/webpack-dev-server": "4.3.1",
"@types/webpack-dev-middleware": "5.0.2",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"eslint": "7.26.0",
Expand Down
Loading

0 comments on commit 6ebf5cf

Please sign in to comment.