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

Allow support for module directives in build process #199

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
6 changes: 1 addition & 5 deletions build.tsconfig.json → bundle-base.tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"target": "es6",
"module": "esnext",
"moduleResolution": "node",
"declaration": true,
"declarationDir": "types",
"emitDeclarationOnly": true,
"baseUrl": "./",
"types": ["jest", "node"],
"sourceMap": true,
Expand All @@ -17,7 +14,6 @@
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"allowJs": true,
"paths": {
"@components/*": ["src/components/*"],
"@content-presentation/*": ["src/components/content-presentation/*"],
Expand All @@ -29,5 +25,5 @@
}
},
"include": ["src"],
"exclude": ["node_modules", "build"]
"exclude": ["node_modules", "**/__tests__", "src/setupTests.ts"]
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"lib"
],
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/cjs/index.d.ts",
"module": "dist/esm",
"types": "dist/index.d.ts",
"scripts": {
"cleanup": "rm -rf dist/ > /dev/null && rm -rf lib/ > /dev/null",
"storybook": "storybook dev -p 6006",
Expand All @@ -26,10 +26,8 @@
},
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.13.16",
"@babel/core": "^7.19.6",
"@babel/core": "^7.24.1",
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
"@babel/preset-react": "^7.18.6",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
Expand All @@ -45,6 +43,7 @@
"@storybook/theming": "^7.0.2",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^14.2.1",
"@types/babel__core": "^7",
"@types/jest": "^29.5.12",
"@types/jest-axe": "^3.5.9",
"@types/node": "^15.0.2",
Expand All @@ -53,6 +52,7 @@
"@types/rollup-plugin-peer-deps-external": "^2.2.1",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"babel-jest": "^29.7.0",
"chromatic": "^6.17.3",
"eslint": "^8.57.0",
"eslint-config-airbnb": "^19.0.4",
Expand All @@ -73,6 +73,7 @@
"rollup": "^4.13.0",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-preserve-directives": "^0.4.0",
"sass": "^1.53.0",
"storybook": "^7.6.17",
"ts-jest": "^29.1.2",
Expand Down
55 changes: 46 additions & 9 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,75 @@ import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import external from 'rollup-plugin-peer-deps-external';
import dts from 'rollup-plugin-dts';
import preserveDirectives from 'rollup-plugin-preserve-directives';

import packageJson from './package.json' assert { type: 'json' };

// suppresses warnings printed to console as part of bundling components with directives present.
const onWarnSuppression = {
onwarn(warning, warn) {
if (warning.code === 'MODULE_LEVEL_DIRECTIVE' && warning.message.includes(`"use client"`)) {
return;
}
warn(warning);
},
};

const commonPlugins = [external(), resolve(), commonjs()];

export default [
// cjs export
{
input: 'src/index.ts',
output: [
{
file: packageJson.main,
format: 'cjs',
sourcemap: true,
name: 'react-ts-lib',
},
],
plugins: [
...commonPlugins,
typescript({
tsconfig: 'bundle-base.tsconfig.json',
compilerOptions: {
declaration: false,
},
}),
terser(),
],
...onWarnSuppression,
},
// esm export
{
input: 'src/index.ts',
output: [
{
file: packageJson.module,
dir: packageJson.module,
format: 'esm',
sourcemap: true,
preserveModules: true,
preserveModulesRoot: 'src',
},
],
plugins: [
external(),
resolve(),
commonjs(),
...commonPlugins,
typescript({
tsconfig: './build.tsconfig.json',
exclude: ['**/__tests__', '**/*.test.tsx', 'src/setupTests.ts'],
tsconfig: 'bundle-base.tsconfig.json',
compilerOptions: {
declaration: true,
declarationDir: 'dist/esm',
emitDeclarationOnly: true,
},
}),
terser(),
preserveDirectives(),
terser({ compress: { directives: false } }),
],
...onWarnSuppression,
},
// type bundling
{
input: 'dist/esm/types/index.d.ts',
input: 'dist/esm/index.d.ts',
output: [{ file: 'dist/index.d.ts', format: 'esm' }],
external: [],
plugins: [dts()],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { HTMLProps, createContext, useContext, ReactNode } from 'react';
import classNames from 'classnames';
import { Tick, Cross } from '../../icons';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import classNames from 'classnames';
import React, { HTMLProps, useContext } from 'react';
import useDevWarning from '../../../../util/hooks/UseDevWarning';
Expand Down Expand Up @@ -38,11 +39,7 @@ const TableCell: React.FC<TableCellProps> = ({
case TableSection.NONE:
default:
return (
<td
className={classes}
role={_responsive ? 'cell' : undefined}
{...rest}
>
<td className={classes} role={_responsive ? 'cell' : undefined} {...rest}>
{_responsive && (
<span className="nhsuk-table-responsive__heading">{_responsiveHeading}</span>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import classNames from 'classnames';
import React, { HTMLProps, useContext, useEffect } from 'react';
import TableContext from '../TableContext';
Expand Down
6 changes: 5 additions & 1 deletion src/components/form-elements/checkboxes/components/Box.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, {
HTMLProps,
useContext,
Expand Down Expand Up @@ -90,7 +91,10 @@ const Box: React.FC<BoxProps> = ({
</Label>
) : null}
{hint ? (
<HintText className={classNames('nhsuk-checkboxes__hint', hintClassName)} {...restHintProps}>
<HintText
className={classNames('nhsuk-checkboxes__hint', hintClassName)}
{...restHintProps}
>
{hint}
</HintText>
) : null}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { HTMLProps, useContext, ChangeEvent } from 'react';
import classNames from 'classnames';
import Label, { LabelProps } from '../../label/Label';
Expand Down Expand Up @@ -48,7 +49,8 @@ const IndividualDateInput: React.FC<IndividualDateInputProps> = ({
const inputID = id || `${ctxId}-${inputType}`;
const inputName = name || `${ctxName}-${inputType}`;
const inputValue = value !== undefined ? value : ctxValue?.[inputType];
const inputDefaultValue = defaultValue !== undefined ? defaultValue : ctxDefaultValue?.[inputType];
const inputDefaultValue =
defaultValue !== undefined ? defaultValue : ctxDefaultValue?.[inputType];

const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
e.persist();
Expand Down
1 change: 1 addition & 0 deletions src/components/form-elements/form/FormContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { createContext, useContext } from 'react';

export interface IFormContext {
Expand Down
5 changes: 2 additions & 3 deletions src/components/form-elements/radios/components/Radio.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, {
HTMLProps, useContext, ReactNode, useEffect, useState,
} from 'react';
'use client';
import React, { HTMLProps, useContext, ReactNode, useEffect, useState } from 'react';
import classNames from 'classnames';
import { RadiosContext, IRadiosContext } from '../RadioContext';
import HintText, { HintTextProps } from '../../hint-text/HintText';
Expand Down
1 change: 1 addition & 0 deletions src/components/navigation/card/components/CardContent.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { HTMLProps, useContext } from 'react';
import classNames from 'classnames';
import CardContext from '../CardContext';
Expand Down
1 change: 1 addition & 0 deletions src/components/navigation/card/components/CardHeading.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { HTMLProps, useContext } from 'react';
import classNames from 'classnames';
import HeadingLevel, { HeadingLevelType } from '../../../../util/HeadingLevel';
Expand Down
1 change: 1 addition & 0 deletions src/components/navigation/header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { PureComponent, HTMLProps, useContext } from 'react';
import classNames from 'classnames';
import NHSLogo, { NHSLogoNavProps } from './components/NHSLogo';
Expand Down
1 change: 1 addition & 0 deletions src/components/navigation/header/components/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { HTMLProps, useContext } from 'react';
import classNames from 'classnames';
import HeaderContext, { IHeaderContext } from '../HeaderContext';
Expand Down
1 change: 1 addition & 0 deletions src/components/navigation/header/components/NHSLogo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { useContext, SVGProps } from 'react';
import classNames from 'classnames';
import HeaderContext, { IHeaderContext } from '../HeaderContext';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { HTMLProps, useContext, useEffect, MouseEvent } from 'react';
import HeaderContext, { IHeaderContext } from '../HeaderContext';
import { ChevronDown as ChevronDownIcon } from '../../../icons';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { useContext } from 'react';
import HeaderContext, { IHeaderContext } from '../HeaderContext';
import type { AsElementLink } from '../../../../util/types/LinkTypes';
Expand All @@ -7,7 +8,10 @@ export interface OrganisationalLogoProps extends AsElementLink<HTMLAnchorElement
}

const OrganisationalLogo: React.FC<OrganisationalLogoProps> = ({
logoUrl, alt, asElement: Component = 'a', ...rest
logoUrl,
alt,
asElement: Component = 'a',
...rest
}) => {
const { orgName, orgSplit, orgDescriptor } = useContext<IHeaderContext>(HeaderContext);
return (
Expand Down
5 changes: 2 additions & 3 deletions src/patterns/care-card/CareCard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { HTMLProps, createContext, useContext } from 'react';
import classNames from 'classnames';
import { CareCardType } from '../../util/types/NHSUKTypes';
Expand Down Expand Up @@ -66,9 +67,7 @@ interface CareCard extends React.FC<CareCardProps> {
Heading: React.FC<CareCardHeadingProps>;
}

const CareCard: CareCard = ({
className, type, children, ...rest
}) => (
const CareCard: CareCard = ({ className, type, children, ...rest }) => (
<div className={classNames('nhsuk-care-card', `nhsuk-care-card--${type}`, className)} {...rest}>
<CareCardContext.Provider value={type}>{children}</CareCardContext.Provider>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/util/FormGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import React, { ReactNode, useState, useEffect, HTMLProps, useContext } from 'react';
import classNames from 'classnames';
import HintText from '../components/form-elements/hint-text/HintText';
Expand Down
1 change: 1 addition & 0 deletions src/util/hooks/UseDevWarning.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { useEffect } from 'react';
import isDev from '../IsDev';

Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"allowJs": true,
"paths": {
"@components/*": ["src/components/*"],
"@content-presentation/*": ["src/components/content-presentation/*"],
Expand Down
Loading
Loading