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

Typescript config support #4947

Closed

Conversation

jesusbotella
Copy link

Which issue, if any, is this issue related to?

Closes #4940.

Is there anything in the PR that needs further explanation?

I think it is self-explanatory, but I have two doubts:

  • Don't know where to add some more documentation about TypeScript config files (e.g., how to be able to export CommonJS for stylelint if you have another export format already, and some other issues that may appear)
  • Are there any tests for configuration files? I would like to add one for TypeScript

@vankop
Copy link
Member

vankop commented Sep 24, 2020

Are there any tests for configuration files? I would like to add one for TypeScript

Maybe system tests will help you
https://github.com/stylelint/stylelint/tree/master/system-tests

@ota-meshi
Copy link
Member

Does this change require users to install "typescript" due to "peerDependencies" of "ts-node"?

@ota-meshi
Copy link
Member

I think we need to add "typescript" to "dependencies" to not require the user to install it.

@hudochenkov
Copy link
Member

Does this change require users to install "typescript" due to "peerDependencies" of "ts-node"?
I think we need to add "typescript" to "dependencies" to not require the user to install it.

Installing TypeScript for 99.999% of users, who don't need it is bad. Also showing peer dependency warning about it. We need different solution, probably.

@ybiquitous
Copy link
Member

I agree with @ota-meshi and @hudochenkov. So, I suggest another solution as follows:

  • Dynamic require @endemolshinegroup/cosmiconfig-typescript-loader. The typescriptLoader() function is called only when a user has own stylelint.config.ts file.
  • Move @endemolshinegroup/cosmiconfig-typescript-loader from dependencies to peerDependencies.
  • Add the peerDependenciesMeta field in order to suppress an installation warning such as:

    npm WARN stylelint@13.7.0 requires a peer of @endemolshinegroup/cosmiconfig-typescript-loader@>=3.0.2 but none is installed. You must install peer dependencies yourself.

diff --git a/lib/createStylelint.js b/lib/createStylelint.js
index 3d0b17aa..39a6c438 100644
--- a/lib/createStylelint.js
+++ b/lib/createStylelint.js
@@ -8,9 +8,13 @@ const getPostcssResult = require('./getPostcssResult');
 const isPathIgnored = require('./isPathIgnored');
 const lintSource = require('./lintSource');
 const path = require('path');
-const typescriptLoader = require('@endemolshinegroup/cosmiconfig-typescript-loader').default;
 const { cosmiconfig } = require('cosmiconfig');
 
+const typescriptLoader = (...args) => {
+	const loader = require('@endemolshinegroup/cosmiconfig-typescript-loader').default;
+	return loader(...args);
+}
+
 const IS_TEST = process.env.NODE_ENV === 'test';
 const STOP_DIR = IS_TEST ? path.resolve(__dirname, '..') : undefined;
 
diff --git a/package.json b/package.json
index 841913d6..7510b222 100644
--- a/package.json
+++ b/package.json
@@ -110,7 +110,6 @@
     ]
   },
   "dependencies": {
-    "@endemolshinegroup/cosmiconfig-typescript-loader": "^3.0.2",
     "@stylelint/postcss-css-in-js": "^0.37.2",
     "@stylelint/postcss-markdown": "^0.36.1",
     "autoprefixer": "^9.8.6",
@@ -161,6 +160,7 @@
     "write-file-atomic": "^3.0.3"
   },
   "devDependencies": {
+    "@endemolshinegroup/cosmiconfig-typescript-loader": "^3.0.2",
     "@stylelint/prettier-config": "^2.0.0",
     "@stylelint/remark-preset": "^1.0.0",
     "@types/balanced-match": "^1.0.1",
@@ -196,6 +196,14 @@
     "remark-cli": "^8.0.1",
     "typescript": "^3.9.6"
   },
+  "peerDependencies": {
+    "@endemolshinegroup/cosmiconfig-typescript-loader": ">=3.0.2"
+  },
+  "peerDependenciesMeta": {
+    "@endemolshinegroup/cosmiconfig-typescript-loader": {
+      "optional": true
+    }
+  },
   "engines": {
     "node": ">=10.13.0"
   }

@jesusbotella
Copy link
Author

Thank you for your comment @ybiquitous! I have just pushed the changes, but it seems like I need to work on this a little more because some checks are failing.

@ybiquitous
Copy link
Member

@jesusbotella I think the following ways can resolve the errors:

Rest parameter 'args' implicitly has an 'any[]' type.

Explicitly set the missing types as follows:

--- a/lib/createStylelint.js
+++ b/lib/createStylelint.js
@@ -10,10 +10,14 @@ const lintSource = require('./lintSource');
 const path = require('path');
 const { cosmiconfig } = require('cosmiconfig');
 
-const typescriptLoader = (...args) => {
+/**
+ * @param {string} filepath
+ * @param {string} content
+ */
+const typescriptLoader = (filepath, content) => {
 	const loader = require('@endemolshinegroup/cosmiconfig-typescript-loader').default;
 
-	return loader(...args);
+	return loader(filepath, content);
 };
 
 const IS_TEST = process.env.NODE_ENV === 'test';

Cannot find module '@endemolshinegroup/cosmiconfig-typescript-loader' or its corresponding type declarations.

Add the package also to devDependencies:

--- a/package.json
+++ b/package.json
@@ -160,6 +160,7 @@
     "write-file-atomic": "^3.0.3"
   },
   "devDependencies": {
+    "@endemolshinegroup/cosmiconfig-typescript-loader": "^3.0.2",
     "@stylelint/prettier-config": "^2.0.0",
     "@stylelint/remark-preset": "^1.0.0",
     "@types/balanced-match": "^1.0.1",

@jeddy3
Copy link
Member

jeddy3 commented Nov 1, 2020

Closing following discussion in #4940 and solution prorposed in #4940 (comment)

@jeddy3 jeddy3 closed this Nov 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add support for TypeScript configuration files
6 participants