Skip to content

Commit

Permalink
feat: separate static asset types to separate global package (#20433)
Browse files Browse the repository at this point in the history
* feat(typings): create separate global declaration for static assets

* feat(tools): use static-assets instead of custom-global global typings

* Update typings/static-assets/README.md

Co-authored-by: Peter Varholak <peter.varholak@gmail.com>
  • Loading branch information
Hotell and varholak-peter authored Nov 8, 2021
1 parent f28a39a commit bdeb8ff
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
4 changes: 2 additions & 2 deletions tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('migrate-converged-pkg generator', () => {
outDir: 'dist',
preserveConstEnums: true,
target: 'ES2019',
types: ['jest', 'custom-global', 'inline-style-expand-shorthand'],
types: ['jest', 'static-assets', 'inline-style-expand-shorthand'],
},
extends: '../../tsconfig.base.json',
include: ['src'],
Expand All @@ -212,7 +212,7 @@ describe('migrate-converged-pkg generator', () => {

expect(tsConfig.compilerOptions.types).toEqual([
'jest',
'custom-global',
'static-assets',
'inline-style-expand-shorthand',
'@testing-library/jest-dom',
'foo-bar',
Expand Down
2 changes: 1 addition & 1 deletion tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const templates = {
importHelpers: true,
noUnusedLocals: true,
preserveConstEnums: true,
types: ['jest', 'custom-global', 'inline-style-expand-shorthand'],
types: ['jest', 'static-assets', 'inline-style-expand-shorthand'],
} as TsConfig['compilerOptions'],
},
babelConfig: (options: { extraPlugins: Array<string> }) => {
Expand Down
16 changes: 3 additions & 13 deletions typings/custom-global/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
// @TODO https://github.com/microsoft/fluentui/issues/20544
/// <reference path="../static-assets/index.d.ts" />

/**
* Generic typings for sass files.
*/
declare module '*.scss' {
const styles: { [className: string]: string };
export default styles;
}
declare module '*.svg' {
const svgPath: string;
export default svgPath;
}
declare module '*.png' {
const value: any;
export default value;
}

/**
* Generic typings for Markdown files.
*/
declare module '*.md';

// These declarations are meant to represent the parts of Map/WeakMap/Set that exist in IE 11.
// Therefore, some functionality (such as constructor parameters) is intentionally missing.
Expand Down
38 changes: 38 additions & 0 deletions typings/static-assets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# static-assets

Enables importing various static assets to JS/TS modules (which is invalid ECMA syntax enabled by various tools like webpack etc).

This definition list is maintained manually and should be extended as needed.

## Usage

```json
{
"compilerOptions": {
"types": ["static-assets"]
}
}
```

Now you can import images and others:

```ts
// @ExpectType string
import myImgSrc from './hello-world.png`

```

## Adding new asset types

Add new asset types extension as needed.

**Example:**

```ts
// Adding .avif image type support
// ↓↓↓
declare module '*.avif' {
const src: string;
export default src;
}
```
33 changes: 33 additions & 0 deletions typings/static-assets/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Global ambient declaration of various non javascript assets being imported into JS/TS
*/

declare module '*.svg' {
const src: string;
export default src;
}

declare module '*.gif' {
const src: string;
export default src;
}

declare module '*.jpg' {
const src: string;
export default src;
}

declare module '*.jpeg' {
const src: string;
export default src;
}

declare module '*.png' {
const src: string;
export default src;
}

declare module '*.md' {
const src: string;
export default src;
}
6 changes: 4 additions & 2 deletions typings/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": false,
"noEmit": true,
"esModuleInterop": true,
"types": ["node"]
"types": ["node"],
"typeRoots": ["node_modules/@types"]
},
"include": ["**/*.d.ts"]
}

0 comments on commit bdeb8ff

Please sign in to comment.