Skip to content

Add typescript-plugin-css-modules #5877

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"sass-loader": "7.1.0",
"style-loader": "0.23.0",
"terser-webpack-plugin": "1.1.0",
"typescript-plugin-css-modules": "1.0.4",
"url-loader": "1.1.1",
"webpack": "4.19.1",
"webpack-dev-server": "3.1.9",
Expand Down
22 changes: 21 additions & 1 deletion packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const fs = require('fs');
const resolve = require('resolve');
const path = require('path');
const paths = require('../../config/paths');
const pkgJson = require('../../package.json');
const os = require('os');
const immer = require('react-dev-utils/immer').produce;
const globby = require('react-dev-utils/globby').sync;
Expand All @@ -37,6 +38,16 @@ function verifyNoTypeScript() {
return true;
}

function hasCSSModules() {
const cssModuleFiles = globby('**/*.module.(c|sa|sc)ss', {
cwd: paths.appSrc,
});
if (cssModuleFiles.length > 0) {
return true;
}
return false;
}

function verifyTypeScriptSetup() {
let firstTimeSetup = false;

Expand Down Expand Up @@ -130,6 +141,15 @@ function verifyTypeScriptSetup() {
paths: { value: undefined, reason: 'aliased imports are not supported' },
};

if (hasCSSModules()) {
// We don't install this to the user's project. Instead it's a dependency
// of `react-scripts`. TypeScript won't fail or throw errors if the plugin
// isn't found.
compilerOptions.plugins = {
suggested: [{ name: 'typescript-plugin-css-modules' }],
};
}

const formatDiagnosticHost = {
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: ts.sys.getCurrentDirectory,
Expand Down Expand Up @@ -252,7 +272,7 @@ function verifyTypeScriptSetup() {
if (!fs.existsSync(paths.appTypeDeclarations)) {
fs.writeFileSync(
paths.appTypeDeclarations,
`/// <reference types="react-scripts" />${os.EOL}`
`/// <reference types="${pkgJson.name}" />${os.EOL}`
);
}
}
Expand Down