typescript-plugin-inner-jsx
Advanced tools
Weekly downloads
Readme
TypeScript transformer to extract compile-time render information for runtime use. Available on npm as typescript-plugin-inner-jsx
.
This is a TypeScript transformer that searches for JSX tags inside of React components and attaches them to the components declarations in which way exposes inner components structure to the consumer.
Example:
// Before transform:
const SomeComponent: React.SFC<SomeComponentProps> = () => (
<InnerComponent1>
<div><InnerComponent2 /></div>
</InnerComponent1>
)
// After transform:
const SomeComponent = (() => (
<InnerComponent1>
<div><InnerComponent2 /></div>
</InnerComponent1>
)) as React.SFC<SomeComponentProps> & {
inner: {
readonly InnerComponent1: typeof InnerComponent1;
readonly InnerComponent2 typeof InnerComponent2;
}
}
SomeComponent.inner = {
get InnerComponent1() {
return InnerComponent1;
},
get InnerComponent2() {
return InnerComponent1;
},
}
The following command adds the packages to the project as a development-time dependency:
npm i typescript-plugin-inner-jsx --dev
Right now, one of the rough edges of TypeScript custom transforms is in how they are used (hopefully to be resolved soon). At least at the time of writing (December 2018), there are no compiler options (for command line or tsconfig.json) for specifying transforms, so typically the compiler API must be used to run transforms.
To build with transformer please use provided custom compiler build-with-transform-jsx
.
Example:
build-with-transform-jsx -t es5 --outDir dist/es5 --declaration --inlineSourceMap --inlineSources --config ./tsconfig.json
Webpack
This section describes how to integrate the plugin into the build/bundling process driven by Webpack and its TypeScript loaders.
There are two popular TypeScript loaders that support specifying custom transformers:
Both loaders use the same setting getCustomTransformers
which is an optional function that returns { before?: Transformer[], after?: Transformer[] }
.
In order to inject the transformer into compilation, add it to before
transformers array, like: { before: [innerJsxTransformer] }
.
awesome-typescript-loader
In the webpack.config.js
file in the section where awesome-typescript-loader is configured as a loader:
// 1. import default from the plugin module
const createInnerJsxTransformer = require('typescript-plugin-inner-jsx').default;
// 2. create a transformer;
// the factory additionally accepts an options object which described below
const innerJsxTransformer = createInnerJsxTransformer();
// 3. add getCustomTransformer method to the loader config
var config = {
...
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
options: {
... // other loader's options
getCustomTransformers: () => ({ before: [innerJsxTransformer] })
}
}
]
}
...
};
Please note, that in the development mode, awesome-typescript-loader
uses multiple separate processes to speed up compilation. In that mode the above configuration cannot work because functions, which getCustomTransformers
is, are not transferrable between processes.
To solve this please refer to Forked process configuration section.
ts-loader
In the webpack.config.js
file in the section where ts-loader is configured as a loader:
// 1. import default from the plugin module
const createInnerJsxTransformer = require('typescript-plugin-inner-jsx').default;
// 2. create a transformer;
// the factory additionally accepts an options object which described below
const innerJsxTransformer = createInnerJsxTransformer();
// 3. add getCustomTransformer method to the loader config
var config = {
...
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
... // other loader's options
getCustomTransformers: () => ({ before: [innerJsxTransformer] })
}
}
]
}
...
};
Please note, when awesome-typescript-loader
is used with HappyPack
or thread-loader
, they use multiple separate processes to speed up compilation. In that mode the above configuration cannot work because functions, which getCustomTransformers
is, are not transferrable between processes.
To solve this please refer to Forked process configuration section.
createTransformer
function createTransformer(): TransformerFactory<SourceFile>;
Package also imports a High order component which adds inner structure to the wrapped Component.
Example:
import { withInner } from 'typescript-plugin-inner-jsx/withInner';
const Parent = () => (
<div>
<Child1 />
<Child2 />
</div>
);
const ParentWithInner = withInner(Parent, { Child1, Child2 });
ParentWithInner.inner.Child1 === Child1; // true
A factory that creates an instance of a TypeScript transformer (which is a factory itself).
Precise UI is released using the MIT license. For more information see the license file.
FAQs
Typescript transformer which adds references to inner JSX components to parent React components.
The npm package typescript-plugin-inner-jsx receives a total of 373 weekly downloads. As such, typescript-plugin-inner-jsx popularity was classified as not popular.
We found that typescript-plugin-inner-jsx demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.