Skip to content

Commit

Permalink
Allow jsxFactory and jsxFragment to be passed in buildOptions for use…
Browse files Browse the repository at this point in the history
… by esbuild (#1838)

* Allow jsxFactory and jsxFragment to be passed in buildOptions for use by esbuild

* Fixes as per FredKSchott's feedback

Co-authored-by: Robert Hurst <[email protected]>
  • Loading branch information
robert-hurst-cmd and RobertWHurst authored Dec 7, 2020
1 parent 844f322 commit 1f42e44
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ const configSchema = {
watch: {type: 'boolean'},
ssr: {type: 'boolean'},
htmlFragments: {type: 'boolean'},
jsxFactory: {type: 'string'},
jsxFragment: {type: 'string'},
},
},
testOptions: {
Expand Down
6 changes: 4 additions & 2 deletions snowpack/src/plugins/plugin-esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export function esbuildPlugin(config: SnowpackConfig, {input}: {input: string[]}
esbuildService = esbuildService || (await startService());
const contents = await fs.readFile(filePath, 'utf-8');
const isPreact = checkIsPreact(filePath, contents);
let jsxFactory = config.buildOptions.jsxFactory ?? (isPreact ? 'h' : undefined);
let jsxFragment = config.buildOptions.jsxFragment ?? (isPreact ? 'Fragment' : undefined);
const {code, map, warnings} = await esbuildService!.transform(contents, {
loader: getLoader(filePath),
jsxFactory: isPreact ? 'h' : undefined,
jsxFragment: isPreact ? 'Fragment' : undefined,
jsxFactory,
jsxFragment,
sourcefile: filePath,
sourcemap: config.buildOptions.sourceMaps,
});
Expand Down
2 changes: 2 additions & 0 deletions snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ export interface SnowpackConfig {
sourceMaps: boolean;
watch: boolean;
htmlFragments: boolean;
jsxFactory: string | undefined;
jsxFragment: string | undefined;
};
testOptions: {
files: string[];
Expand Down
8 changes: 8 additions & 0 deletions www/_template/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ module.exports = {

- Rename your web modules directory.

#### buildOptions.jsxFactory | `string` | Default: `React.createElement` (or `h` if Preact import is detected)

- Set the name of the used function to create JSX elements.

#### buildOptions.jsxFragment | `string` | Default: `React.Fragment` (or `Fragment` if Preact import is detected)

- Set the name of the used function to create JSX fragments.

### config.testOptions

#### testOptions.files | `string[]` | Default: `["__tests__/**/*", "**/*.@(spec|test).*"]`
Expand Down

1 comment on commit 1f42e44

@vercel
Copy link

@vercel vercel bot commented on 1f42e44 Dec 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.