Skip to content

Commit

Permalink
feat(stark-build): tslintLoader - disable typecheck rules to remove w…
Browse files Browse the repository at this point in the history
…arnings while typeCheck is false

- update inline documentation
- update documentation

ISSUES CLOSED: #405
  • Loading branch information
SuperITMan authored and carlo-nomes committed Apr 8, 2019
1 parent 7914fb0 commit 9e0bf65
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 9 deletions.
10 changes: 9 additions & 1 deletion docs/stark-build/NG_CLI_BUILD_CUSTOMIZATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@ Webpack plugins as well as some utils in order to leverage some functionality to

This is the list of utils and plugins used by the Stark-Build package:

### Loaders

#### [tslint-loader](https://github.com/wbuchwalter/tslint-loader)

This loader [lints](https://palantir.github.io/tslint/) your TypeScript files based on the configuration in your `tslint.json`.

**Because at this moment disabling `typeCheck` in this loader drasticly improves build time we opted to overwrite and disable all [tslint rules](https://palantir.github.io/tslint/rules/) that require Type Info.**

### Plugins

#### <a name="define-plugin"></a>[DefinePlugin](https://webpack.js.org/plugins/define-plugin)
#### [DefinePlugin](https://webpack.js.org/plugins/define-plugin)

Allows to define global variables that will be available during the compilation and building of the application bundles.

Expand Down
2 changes: 1 addition & 1 deletion docs/stark-core/ENVIRONMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export const environment: StarkEnvironment = {
Thanks to the customizations done by Stark-Build to the default Angular CLI build configuration you have some global variables available at compilation time,
which means that you can implement some checks in your code and this will be analyzed when your application bundle is being built by Webpack.

See [Stark-Build: Webpack build customizations - DefinePlugin](https://github.com/NationalBankBelgium/stark/blob/master/docs/stark-build/NG_CLI_BUILD_CUSTOMIZATIONS.md#define-plugin)
See [Stark-Build: Webpack build customizations - DefinePlugin](https://github.com/NationalBankBelgium/stark/blob/master/docs/stark-build/NG_CLI_BUILD_CUSTOMIZATIONS.md#defineplugin)

### Why do you need the target environment at compilation time?

Expand Down
62 changes: 62 additions & 0 deletions packages/stark-build/config/build-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const ts = require("typescript");
const path = require("path");
const fs = require("fs");
const os = require("os");
const crypto = require("crypto");

const helpers = require("./helpers");
const ngCliUtils = require("./ng-cli-utils");

Expand Down Expand Up @@ -90,8 +93,67 @@ function getEnvironmentFile(environment) {
}
}

/**
* Gets the value at `path` of `object`. If the resolved value is
* `undefined`, the `defaultValue` is returned in its place.
*
* @param {Object} obj The object to query.
* @param {string|Array} path The path of the property to get.
* @param {*} [defaultValue] The value returned for `undefined` resolved values.
* @returns {*} Returns the resolved value.
*/
function get(obj, path, defaultValue) {
let args;
if (typeof path === "string") {
args = path.split(".");
} else {
args = path;
}

for (let i = 0; i < args.length; i++) {
if (!obj || !obj.hasOwnProperty(args[i])) {
return defaultValue;
}
obj = obj[args[i]];
}
return obj;
}

/**
* Makes a new tslint.json file that extends the default (tslint.json) and disables all the tslint rules that require typechecking
* @see FIXME on webpack-partial.common.js (module.exports().module.rules)
*
* @param {string} tslintConfig The path to the projects tslint.json
* @return {string} Returns the path to the temporary tslint configuration for tslint-loader
*/
function getFixedTSLintConfig(tslintConfig) {
// Windows style separators need to be replaced before injecting it into the new tslint.json
const tslintConfigPath = helpers.root(tslintConfig).replace(/\\/g, "/");

const noTypeCheckTSLintConfig = fs
.readFileSync(helpers.rootStark("config/tslint-disabled-typecheck-rules.json"), "utf8")
.replace(/TSLINT_CONFIG_PLACEHOLDER/, tslintConfigPath);

const contentHash = crypto
.createHash("md5")
.update(noTypeCheckTSLintConfig)
.digest("hex");

const noTypeCheckTSLintConfigPath = path.resolve(os.tmpdir(), `national-bank-belgium_stark-build_tslint-${contentHash}.json`);

// check if file already exists before writing it
if (!fs.existsSync(noTypeCheckTSLintConfigPath)) {
console.log(`Writing TSLint configuration to ${noTypeCheckTSLintConfigPath}`);
fs.writeFileSync(noTypeCheckTSLintConfigPath, noTypeCheckTSLintConfig, "utf8");
}

return noTypeCheckTSLintConfigPath;
}

exports.ANGULAR_APP_CONFIG = ANGULAR_APP_CONFIG;
exports.DEFAULT_METADATA = DEFAULT_METADATA;
exports.getEnvironmentFile = getEnvironmentFile;
exports.readTsConfig = readTsConfig;
exports.getFixedTSLintConfig = getFixedTSLintConfig;
exports.supportES2015 = supportES2015;
exports.get = get;
57 changes: 57 additions & 0 deletions packages/stark-build/config/tslint-disabled-typecheck-rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"extends": ["TSLINT_CONFIG_PLACEHOLDER"],
"rules": {
"no-unnecessary-type-assertion": false,
"promise-function-async": false,
"await-promise": false,
"no-floating-promises": false,
"no-for-in-array": false,
"no-inferred-empty-object-type": false,
"no-null-undefined-union": false,
"no-restricted-globals": false,
"no-unbound-method": false,
"no-unsafe-any": false,
"no-unused-variable": false,
"no-use-before-declare": false,
"no-void-expression": false,
"restrict-plus-operands": false,
"strict-boolean-expressions": false,
"strict-type-predicates": false,
"use-default-type-parameter": false,
"deprecation": false,
"prefer-readonly": false,
"match-default-export-name": false,
"no-boolean-literal-compare": false,
"no-unnecessary-qualifier": false,
"return-undefined": false,
"unnecessary-bind": false,
"arguments-order": false,
"consecutive-overloads": false,
"no-accessor-field-mismatch": false,
"no-alphabetical-sort": false,
"no-array-delete": false,
"no-collection-size-mischeck": false,
"no-dead-store": false,
"no-element-overwrite": false,
"no-empty-array": false,
"no-gratuitous-expressions": false,
"no-ignored-initial-value": false,
"no-ignored-return": false,
"no-in-misuse": false,
"no-invalid-await": false,
"no-misleading-array-reverse": false,
"no-redundant-boolean": false,
"no-return-type-any": false,
"no-self-assignment": false,
"no-try-promise": false,
"no-undefined-argument": false,
"no-unused-array": false,
"no-use-of-empty-return-value": false,
"no-useless-cast": false,
"no-useless-intersection": false,
"no-variable-usage-before-declaration": false,
"parameters-max-number": false,
"prefer-immediate-return": false,
"use-type-alias": false
}
}
29 changes: 22 additions & 7 deletions packages/stark-build/config/webpack-partial.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPl
// const InlineManifestWebpackPlugin = require("inline-manifest-webpack-plugin");
// const ScriptExtHtmlWebpackPlugin = require("script-ext-html-webpack-plugin");

const fixedTSLintConfig = buildUtils.getFixedTSLintConfig(
buildUtils.get(buildUtils.ANGULAR_APP_CONFIG.config, "architect.lint.options.tslintConfig", "tslint.json")
);

/**
* Webpack configuration
*
Expand Down Expand Up @@ -65,22 +69,24 @@ module.exports = metadata => {
/**
* Options affecting the normal modules.
*
* See: http://webpack.github.io/docs/configuration.html#module
* See: https://webpack.js.org/configuration/module
*/
module: {
rules: [
// TsLint loader support for *.ts files
// reference: https://github.com/wbuchwalter/tslint-loader
// FIXME: given the warnings we have with build:prod (see https://github.com/NationalBankBelgium/stark/issues/397)
// this probably doesn't load the tslint configuration we think?
/**
* TSLint loader support for *.ts files
* @see https://github.com/wbuchwalter/tslint-loader
*/
{
enforce: "pre",
test: /\.ts$/,
use: [
{
loader: "tslint-loader",
options: {
typeCheck: false // FIXME remove this line once the type checking issues are gone (cfr FIXME above)
typeCheck: false, // FIXME enable type checking when it is improved in tslint-loader (https://github.com/wbuchwalter/tslint-loader/pull/114)
tsconfig: buildUtils.ANGULAR_APP_CONFIG.buildOptions.tsconfig || "tsconfig.json",
configFile: fixedTSLintConfig //FIXME use the configured tslint.json when type checking is enabled
}
}
],
Expand All @@ -92,7 +98,7 @@ module.exports = metadata => {
/**
* Add additional plugins to the compiler.
*
* See: http://webpack.github.io/docs/configuration.html#plugins
* See: https://webpack.js.org/configuration/plugins
*/
plugins: [
// TODO: cannot enable this WriteFilePlugin due to Error: Content and Map of this Source is no longer available (only size() is supported)
Expand Down Expand Up @@ -190,6 +196,15 @@ module.exports = metadata => {
// TODO evaluate this
// new InlineManifestWebpackPlugin(),

/**
* Plugin: WriteFilePlugin
* Description: This plugin makes sure that bundles and assets are written to disk
* this is necessary so that assets are available in dev mode
* See: https://www.npmjs.com/package/write-file-webpack-plugin
*/
// TODO evaluate this
// new WriteFilePlugin(),

/**
* Generate html tags based on javascript maps.
*
Expand Down

0 comments on commit 9e0bf65

Please sign in to comment.