Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom pragma #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ module.exports = {
};
```

### Options

The loader accepts the following options:

- `options.pragma` (default: `React.createElement`)
Pragma for JSX. e.g. A `pragma` of `h` will compile `<Elem />` to `h(Elem, {})`.
- `options.pragmaImportSource` (default: `react`)
Where to import the pragma from.

## Contributing

We welcome contributions to Storybook!
Expand Down
8 changes: 5 additions & 3 deletions loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { getOptions } = require('loader-utils');
const { compile } = require('./compiler');

const DEFAULT_RENDERER = `
import React from 'react';
const renderer = (importStr, importSrc) => `
import ${importStr} from '${importSrc}';
`;

// Lifted from MDXv1 loader
Expand All @@ -18,6 +18,8 @@ const loader = async function (content) {
const options = Object.assign({}, queryOptions, {
filepath: this.resourcePath,
});
const importSrc = options.pragmaImportSource || 'react';
const importStr = options.pragma ? `{ createElement as ${options.pragma} }` : 'React';

let result;
try {
Expand All @@ -26,7 +28,7 @@ const loader = async function (content) {
return callback(err);
}

const code = `${DEFAULT_RENDERER}\n${result}`;
const code = `${renderer(importStr, importSrc)}\n${result}`;
return callback(null, code);
};

Expand Down