-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script to generate d.ts files
- Loading branch information
1 parent
2bfe315
commit ca3d877
Showing
11 changed files
with
223 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
/packages/doc/.cache/** | ||
/packages/yoga/node_modules/** | ||
dist | ||
*.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
const fs = require('fs-extra'); | ||
const { Command } = require('commander'); | ||
const { globSync } = require('glob'); | ||
|
||
const CONTENT_WEB = `import type { JSX, SVGProps } from 'react'; | ||
type SVGWebComponent = (props: SVGProps<SVGSVGElement>) => JSX.Element;\n | ||
`; | ||
const CONTENT_NATIVE = `import type { JSX } from 'react'; | ||
import type { SvgProps } from 'react-native-svg'; | ||
type SVGNativeComponent = (props: SvgProps) => JSX.Element;\n | ||
`; | ||
|
||
const program = new Command(); | ||
|
||
program | ||
.argument('<entries...>', 'specify the entries', value => value.split(',')) | ||
.option('-n, --native', 'generate types for react-native', false) | ||
.action((entries, options) => { | ||
let content = options.native ? CONTENT_NATIVE : CONTENT_WEB; | ||
const files = globSync(entries); | ||
|
||
for (let i = 0; i < files.length; i++) { | ||
const file = fs.readFileSync(`./${files[i]}`, { | ||
encoding: 'utf8', | ||
}); | ||
const matches = file.matchAll(/import\s+([A-Za-z]+?[0-9]*?)\s+from/g); | ||
|
||
for (const match of matches) { | ||
content += options.native | ||
? `export declare const ${match[1]}: SVGNativeComponent; \n` | ||
: `export declare const ${match[1]}: SVGWebComponent; \n`; | ||
} | ||
} | ||
const dir = options.native ? 'native' : 'web'; | ||
|
||
fs.outputFileSync(`./dist/typings/${dir}/index.d.ts`, content); | ||
}); | ||
|
||
program.parse(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"files": ["svg.d.ts"], | ||
"include": ["src"], | ||
"exclude": ["node_modules", "dist", "**/*.test.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
import { defineConfig } from 'tsup' | ||
import svgr from './svgr-plugin' | ||
import { defineConfig } from 'tsup'; | ||
import { replace } from 'esbuild-plugin-replace'; | ||
import svgr from './svgr-plugin'; | ||
|
||
export default defineConfig( opts => { | ||
const { target } = opts.define || {}; | ||
const ext = target === 'native' ? '.native.js' : '.js'; | ||
const entry = target === 'native' ? ["src/**/*.svg"] : [ | ||
"src/index.ts", | ||
"src/**/*.ts", | ||
"src/**/*.svg" | ||
]; | ||
export default defineConfig(options => { | ||
const native = options['--'].includes('native'); | ||
|
||
return { | ||
name: "tsup", | ||
entry, | ||
entry: ['src/index.ts', 'src/**/*.ts', 'src/**/*.svg'], | ||
splitting: false, | ||
bundle: false, | ||
treeshake: true, | ||
minify: true, | ||
esbuildPlugins: [ | ||
svgr({ target, typescript: true }), | ||
svgr({ native }), | ||
replace({ | ||
include: /\.ts$/, | ||
values: { | ||
'.svg': '' | ||
'.svg': '', | ||
}, | ||
}), | ||
}), | ||
], | ||
outExtension: () => ({ | ||
js: ext, | ||
js: native ? '.native.js' : '.js', | ||
}), | ||
} | ||
}; | ||
}); |
Oops, something went wrong.