Skip to content

Commit 168c73f

Browse files
committed
# WIP fix for issues with typescript default import
1 parent 1fa74ac commit 168c73f

File tree

14 files changed

+4957
-68
lines changed

14 files changed

+4957
-68
lines changed

examples/ts-loader-example/package-lock.json

Lines changed: 4683 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@teamsupercell/typings-for-css-modules-loader-ts-loader-example",
3+
"scripts": {
4+
"build": "jest"
5+
},
6+
"author": "dannsam",
7+
"license": "MIT",
8+
"devDependencies": {
9+
"@teamsupercell/typings-for-css-modules-loader": "^2.0.0",
10+
"css-loader": "^3.1.0",
11+
"ts-loader": "^6.2.1",
12+
"typescript": "^3.5.3",
13+
"webpack": "^4.39.1",
14+
"webpack-cli": "^3.3.10"
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.shared-style {
2+
color: black;
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.foo {
2+
color: white;
3+
}
4+
5+
.bar-baz {
6+
color: green;
7+
}
8+
9+
.composed {
10+
composes: shared-style from "./example-shared.css";
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import styles from "./example.css";
2+
import { IExampleCss } from "./example.css";
3+
4+
const reassignedStyles: IExampleCss = styles;
5+
6+
console.log(styles.composed);
7+
console.log(reassignedStyles.composed);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as styles from "./example.css";
2+
import { IExampleCss } from "./example.css";
3+
4+
const reassignedStyles: IExampleCss = styles;
5+
6+
console.log(styles.composed);
7+
console.log(reassignedStyles.composed);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './exports-default';
2+
import './exports-module';
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
// "incremental": true, /* Enable incremental compilation */
5+
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
6+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
7+
// "lib": [], /* Specify library files to be included in the compilation. */
8+
// "allowJs": true, /* Allow javascript files to be compiled. */
9+
// "checkJs": true, /* Report errors in .js files. */
10+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
11+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
12+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13+
// "sourceMap": true, /* Generates corresponding '.map' file. */
14+
// "outFile": "./", /* Concatenate and emit output to single file. */
15+
// "outDir": "./", /* Redirect output structure to the directory. */
16+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
17+
// "composite": true, /* Enable project compilation */
18+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
19+
// "removeComments": true, /* Do not emit comments to output. */
20+
// "noEmit": true, /* Do not emit outputs. */
21+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
22+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24+
25+
/* Strict Type-Checking Options */
26+
"strict": true, /* Enable all strict type-checking options. */
27+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28+
// "strictNullChecks": true, /* Enable strict null checks. */
29+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
30+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34+
35+
/* Additional Checks */
36+
// "noUnusedLocals": true, /* Report errors on unused locals. */
37+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
38+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40+
41+
/* Module Resolution Options */
42+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
44+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
46+
// "typeRoots": [], /* List of folders to include type definitions from. */
47+
// "types": [], /* Type declaration files to be included in compilation. */
48+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
52+
53+
/* Source Map Options */
54+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
55+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
56+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
57+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
58+
59+
/* Experimental Options */
60+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
61+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
62+
63+
/* Advanced Options */
64+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
65+
},
66+
"include": ["./src/**/*"]
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = {
2+
entry: "./src/index.ts",
3+
target: "node",
4+
resolve: {
5+
// Add `.ts` and `.tsx` as a resolvable extension.
6+
extensions: [".ts", ".tsx", ".js", ".css"]
7+
},
8+
module: {
9+
rules: [
10+
{
11+
test: /\.css$/,
12+
use: [
13+
{
14+
loader: "@teamsupercell/typings-for-css-modules-loader",
15+
options: {
16+
formatter: "prettier"
17+
}
18+
},
19+
{
20+
loader: "css-loader",
21+
options: { modules: true }
22+
}
23+
]
24+
},
25+
{
26+
test: /\.tsx?$/,
27+
use: "ts-loader",
28+
exclude: /node_modules/
29+
}
30+
]
31+
},
32+
mode: "none"
33+
};

lerna.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"packages": [
3+
".",
4+
"examples/*"
5+
],
6+
"version": "0.0.0"
7+
}

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
const {
3-
filenameToInterfaceName,
3+
filenameToPascalCase,
44
filenameToTypingsFilename,
55
getCssModuleKeys,
66
generateGenericExportInterface
@@ -67,10 +67,9 @@ module.exports = function(content, ...args) {
6767
const filename = this.resourcePath;
6868

6969
const cssModuleInterfaceFilename = filenameToTypingsFilename(filename);
70-
const interfaceName = filenameToInterfaceName(filename);
7170
const cssModuleDefinition = generateGenericExportInterface(
7271
cssModuleKeys,
73-
interfaceName
72+
filenameToPascalCase(filename)
7473
);
7574

7675
applyFormattingAndOptions(cssModuleDefinition, options)

src/utils.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ const getCssModuleKeys = content => {
2222
/**
2323
* @param {string} filename
2424
*/
25-
const filenameToInterfaceName = filename => {
26-
return "I" + camelCase(path.basename(filename), { pascalCase: true });
25+
const filenameToPascalCase = filename => {
26+
return camelCase(path.basename(filename), { pascalCase: true });
2727
};
2828

2929
/**
3030
* @param {string[]} cssModuleKeys
31+
* @param {string=} indent
3132
*/
32-
const cssModuleToTypescriptInterfaceProperties = cssModuleKeys => {
33+
const cssModuleToTypescriptInterfaceProperties = (cssModuleKeys, indent) => {
3334
return [...cssModuleKeys]
3435
.sort()
35-
.map(key => ` '${key}': string;`)
36+
.map(key => `${indent || ""}'${key}': string;`)
3637
.join("\n");
3738
};
3839

@@ -44,23 +45,33 @@ const filenameToTypingsFilename = filename => {
4445

4546
/**
4647
* @param {string[]} cssModuleKeys
47-
* @param {string} interfaceName
48+
* @param {string} pascalCaseFileName
4849
*/
49-
const generateGenericExportInterface = (cssModuleKeys, interfaceName) => {
50+
const generateGenericExportInterface = (cssModuleKeys, pascalCaseFileName) => {
51+
const interfaceName = `I${pascalCaseFileName}`;
52+
const moduleName = `${pascalCaseFileName}Module`;
53+
5054
const interfaceProperties = cssModuleToTypescriptInterfaceProperties(
51-
cssModuleKeys
55+
cssModuleKeys,
56+
" "
5257
);
53-
return `export interface ${interfaceName} {
58+
return `declare namespace ${moduleName} {
59+
export interface I${pascalCaseFileName} {
5460
${interfaceProperties}
61+
}
5562
}
5663
57-
export const locals: ${interfaceName};
58-
export default locals;`;
64+
declare const ${moduleName}: ${moduleName}.${interfaceName} & {
65+
/** WARNING: Only available when \`css-loader\` is used without \`style-loader\` or \`mini-css-extract-plugin\` */
66+
locals: ${moduleName}.${interfaceName};
67+
};
68+
69+
export = ${moduleName};`;
5970
};
6071

6172
module.exports = {
6273
getCssModuleKeys,
63-
filenameToInterfaceName,
74+
filenameToPascalCase,
6475
filenameToTypingsFilename,
6576
generateGenericExportInterface
6677
};

0 commit comments

Comments
 (0)