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

fix(react): prevent esm modules from appearing in cjs dist #1207

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
7 changes: 6 additions & 1 deletion packages/react/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export default {
input: 'src/index.ts',
external: [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies)
...Object.keys(pkg.peerDependencies),
// Note: We directly import only the specific language syntax needed
// directly in the Code component. This ensures it is still treated as
// an external dependency since it won't match the dependencies or
// peerDependencies when pulled from package.json.
/^react-syntax-highlighter/
],
output: {
dir: 'lib',
Expand Down
15 changes: 7 additions & 8 deletions packages/react/src/components/Code/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import React, { useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { SyntaxHighlighterProps } from 'react-syntax-highlighter';
import SyntaxHighlighter from 'react-syntax-highlighter/dist/esm/light';
import SyntaxHighlighter from 'react-syntax-highlighter/dist/cjs/light';
import classNames from 'classnames';
import js from 'react-syntax-highlighter/dist/esm/languages/hljs/javascript';
import css from 'react-syntax-highlighter/dist/esm/languages/hljs/css';
import xml from 'react-syntax-highlighter/dist/esm/languages/hljs/xml';
import yaml from 'react-syntax-highlighter/dist/esm/languages/hljs/yaml';
import js from 'react-syntax-highlighter/dist/cjs/languages/hljs/javascript';
import css from 'react-syntax-highlighter/dist/cjs/languages/hljs/css';
import xml from 'react-syntax-highlighter/dist/cjs/languages/hljs/xml';
import yaml from 'react-syntax-highlighter/dist/cjs/languages/hljs/yaml';

SyntaxHighlighter.registerLanguage('javascript', js);
SyntaxHighlighter.registerLanguage('css', css);
SyntaxHighlighter.registerLanguage('html', xml);
SyntaxHighlighter.registerLanguage('yaml', yaml);

// HACK: This is a workaround for a bug in react-syntax-highlighter's types.
const Highlighter = SyntaxHighlighter as React.ComponentType<
SyntaxHighlighterProps
>;
const Highlighter =
SyntaxHighlighter as React.ComponentType<SyntaxHighlighterProps>;

type Props = {
children: string;
Expand Down