-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(icons-react): add TypeScript types to icons-react package
- Loading branch information
Showing
20 changed files
with
503 additions
and
95 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
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,155 @@ | ||
/** | ||
* Copyright IBM Corp. 2018, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { babel } = require('@rollup/plugin-babel'); | ||
const commonjs = require('@rollup/plugin-commonjs'); | ||
const { nodeResolve } = require('@rollup/plugin-node-resolve'); | ||
const typescript = require('@rollup/plugin-typescript'); | ||
const { pascalCase } = require('change-case'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const { rollup } = require('rollup'); | ||
|
||
async function bundle(entrypoint, options) { | ||
const globals = options.globals ? formatGlobals(options.globals) : {}; | ||
const { name } = options; | ||
const packageFolder = await findPackageFolder(entrypoint); | ||
|
||
const outputFolders = [ | ||
{ | ||
format: 'esm', | ||
directory: path.join(packageFolder, 'es'), | ||
}, | ||
{ | ||
format: 'cjs', | ||
directory: path.join(packageFolder, 'lib'), | ||
}, | ||
{ | ||
format: 'umd', | ||
directory: path.join(packageFolder, 'umd'), | ||
}, | ||
]; | ||
|
||
await Promise.all(outputFolders.map(({ directory }) => fs.remove(directory))); | ||
|
||
const jsEntryPoints = outputFolders.map(({ directory, format }) => ({ | ||
outputDir: directory, | ||
file: path.join(directory, 'index.js'), | ||
format, | ||
})); | ||
|
||
const packageJsonPath = path.join(packageFolder, 'package.json'); | ||
const packageJson = await fs.readJson(packageJsonPath); | ||
const { dependencies = {} } = packageJson; | ||
|
||
await Promise.all( | ||
jsEntryPoints.map(async ({ outputDir, file, format }) => { | ||
const bundle = await rollup({ | ||
input: entrypoint, | ||
external: Object.keys(dependencies), | ||
plugins: [ | ||
typescript({ | ||
noEmitOnError: true, | ||
noForceEmit: true, | ||
outputToFilesystem: false, | ||
compilerOptions: { | ||
rootDir: 'src', | ||
emitDeclarationOnly: true, | ||
outDir: outputDir, | ||
}, | ||
}), | ||
babel({ | ||
exclude: 'node_modules/**', | ||
babelrc: false, | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
modules: false, | ||
targets: { | ||
browsers: ['last 1 version', 'ie >= 11', 'Firefox ESR'], | ||
}, | ||
}, | ||
], | ||
'@babel/preset-typescript', | ||
], | ||
babelHelpers: 'bundled', | ||
extensions: ['.ts', '.tsx', '.js', '.jsx'], | ||
}), | ||
nodeResolve(), | ||
commonjs({ | ||
include: [/node_modules/], | ||
extensions: ['.js'], | ||
}), | ||
], | ||
}); | ||
const outputOptions = { | ||
exports: 'auto', | ||
file, | ||
format, | ||
}; | ||
|
||
if (format === 'umd') { | ||
outputOptions.name = name; | ||
outputOptions.globals = { | ||
...formatDependenciesIntoGlobals(dependencies), | ||
...globals, | ||
}; | ||
} | ||
|
||
return bundle.write(outputOptions); | ||
}) | ||
); | ||
} | ||
|
||
function formatGlobals(string) { | ||
const mappings = string.split(',').map((mapping) => { | ||
return mapping.split('='); | ||
}); | ||
return mappings.reduce( | ||
(acc, [pkg, global]) => ({ | ||
...acc, | ||
[pkg]: global, | ||
}), | ||
{} | ||
); | ||
} | ||
|
||
function formatDependenciesIntoGlobals(dependencies) { | ||
return Object.keys(dependencies).reduce((acc, key) => { | ||
const parts = key.split('/').map((identifier, i) => { | ||
if (i === 0) { | ||
return identifier.replace(/@/, ''); | ||
} | ||
return identifier; | ||
}); | ||
|
||
return { | ||
...acc, | ||
[key]: pascalCase(parts.join(' ')), | ||
}; | ||
}, {}); | ||
} | ||
|
||
async function findPackageFolder(entrypoint) { | ||
let packageFolder = entrypoint; | ||
|
||
while (packageFolder !== '/' && path.dirname(packageFolder) !== '/') { | ||
packageFolder = path.dirname(packageFolder); | ||
const packageJsonPath = path.join(packageFolder, 'package.json'); | ||
|
||
if (await fs.pathExists(packageJsonPath)) { | ||
break; | ||
} | ||
} | ||
|
||
return packageFolder; | ||
} | ||
|
||
module.exports = bundle; |
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
15 changes: 15 additions & 0 deletions
15
packages/icon-build-helpers/src/builders/react/components/CarbonIcon.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Copyright IBM Corp. 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import { IconProps } from './Icon'; | ||
|
||
export interface CarbonIconProps extends IconProps { | ||
size?: number | string; | ||
} | ||
|
||
export type CarbonIconType = React.ForwardRefExoticComponent< | ||
CarbonIconProps & React.RefAttributes<React.ReactSVGElement> | ||
>; |
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
Oops, something went wrong.