Skip to content

Commit

Permalink
Merge pull request #1238 from SuperITMan/feature/disable-typecheck-ru…
Browse files Browse the repository at this point in the history
…les-tslint-loader

feat(stark-build): tslintLoader - Disable typecheck rules to remove warnings since typeCheck is false
  • Loading branch information
christophercr authored Apr 8, 2019
2 parents f20ecf9 + ab61379 commit 8bae536
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 34 deletions.
12 changes: 10 additions & 2 deletions 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 Expand Up @@ -335,4 +343,4 @@ the environment variable temporarily only for this command:
```

The interactive treemap visualization showing the contents of all the app bundles will be served automatically on port 3030.
Also all the stats generated by WebpackBundleAnalyzer will be available in a `stats.json` file under `reports/bundle-analyzer`.
Also all the stats generated by WebpackBundleAnalyzer will be available in a `stats.json` file under `reports/bundle-analyzer`.
4 changes: 2 additions & 2 deletions 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 All @@ -211,4 +211,4 @@ This is why knowing the target environment at compilation time is useful. You ca
if (ENV === "development") {
/* the code inside this block will only be included in development */
}
```
```
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": {
"arguments-order": false,
"await-promise": false,
"consecutive-overloads": false,
"deprecation": false,
"match-default-export-name": false,
"no-accessor-field-mismatch": false,
"no-alphabetical-sort": false,
"no-array-delete": false,
"no-boolean-literal-compare": false,
"no-collection-size-mischeck": false,
"no-dead-store": false,
"no-element-overwrite": false,
"no-empty-array": false,
"no-floating-promises": false,
"no-for-in-array": false,
"no-gratuitous-expressions": false,
"no-inferred-empty-object-type": false,
"no-ignored-initial-value": false,
"no-ignored-return": false,
"no-in-misuse": false,
"no-invalid-await": false,
"no-null-undefined-union": false,
"no-restricted-globals": 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-unbound-method": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-unsafe-any": false,
"no-unused-array": false,
"no-unused-variable": false,
"no-use-before-declare": false,
"no-use-of-empty-return-value": false,
"no-useless-cast": false,
"no-useless-intersection": false,
"no-variable-usage-before-declaration": false,
"no-void-expression": false,
"parameters-max-number": false,
"prefer-immediate-return": false,
"prefer-readonly": false,
"promise-function-async": false,
"restrict-plus-operands": false,
"return-undefined": false,
"strict-boolean-expressions": false,
"strict-type-predicates": false,
"unnecessary-bind": false,
"use-default-type-parameter": false,
"use-type-alias": false
}
}
75 changes: 45 additions & 30 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 @@ -150,10 +156,10 @@ module.exports = metadata => {
// xhtml: true, // TODO: why XHTML?
minify: isProd
? {
caseSensitive: true,
collapseWhitespace: true,
keepClosingSlash: true
}
caseSensitive: true,
collapseWhitespace: true,
keepClosingSlash: true
}
: false
}),

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 All @@ -214,10 +229,10 @@ module.exports = metadata => {
*/
...(fs.existsSync(helpers.root("config/index-head-config.js"))
? [
new HtmlElementsWebpackPlugin({
headTags: require(helpers.root("config/index-head-config"))
})
]
new HtmlElementsWebpackPlugin({
headTags: require(helpers.root("config/index-head-config"))
})
]
: []),

/**
Expand All @@ -237,14 +252,14 @@ module.exports = metadata => {
*/
...(MONITOR
? [
new WebpackMonitor({
capture: true, // -> default 'true'
target: helpers.root("reports/webpack-monitor/stats.json"), // default -> '../monitor/stats.json'
launch: true, // -> default 'false'
port: 3030, // default 8081
excludeSourceMaps: true // default 'true'
})
]
new WebpackMonitor({
capture: true, // -> default 'true'
target: helpers.root("reports/webpack-monitor/stats.json"), // default -> '../monitor/stats.json'
launch: true, // -> default 'false'
port: 3030, // default 8081
excludeSourceMaps: true // default 'true'
})
]
: []),

/**
Expand All @@ -254,13 +269,13 @@ module.exports = metadata => {
*/
...(BUNDLE_ANALYZER
? [
new BundleAnalyzerPlugin({
generateStatsFile: true, // default 'false'
statsFilename: helpers.root("reports/bundle-analyzer/stats.json"), // default -> 'stats.json'
openAnalyzer: true, //default 'true'
analyzerPort: 3030 // default 8888
})
]
new BundleAnalyzerPlugin({
generateStatsFile: true, // default 'false'
statsFilename: helpers.root("reports/bundle-analyzer/stats.json"), // default -> 'stats.json'
openAnalyzer: true, //default 'true'
analyzerPort: 3030 // default 8888
})
]
: []),

/**
Expand Down

0 comments on commit 8bae536

Please sign in to comment.