Skip to content

Commit 5b198a6

Browse files
Preserve build output (non-minified) for all modules
1 parent 144c5a7 commit 5b198a6

File tree

9 files changed

+331
-423
lines changed

9 files changed

+331
-423
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import React, { PureComponent } from 'react';
2727
import { Button } from 'nhsuk-react-components';
2828

2929
// Or you can import components directly
30-
import Button from 'nhsuk-react-components/lib/components/button';
30+
import Button from 'nhsuk-react-components/dist/esm/components/button';
3131

3232
class GetStartedButton extends PureComponent {
3333
render() {

bundle-base.tsconfig.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { pathsToModuleNameMapper } = require('ts-jest');
2-
const { compilerOptions } = require('./tsconfig.json');
2+
const { compilerOptions } = require('./tsconfig.base.json');
33

44
const jestConfig = {
55
testEnvironment: 'jsdom',

package.json

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@
55
"name": "NHS England"
66
},
77
"files": [
8-
"dist",
9-
"lib"
8+
"dist"
109
],
10+
"sideEffects": false,
11+
"exports": {
12+
".": {
13+
"import": {
14+
"types": "./dist/esm/index.d.ts",
15+
"default": "./dist/esm/index.js"
16+
},
17+
"require": {
18+
"types": "./dist/cjs/index.d.ts",
19+
"default": "./dist/cjs/index.js"
20+
}
21+
},
22+
"./*": "./*",
23+
"./package.json": "./package.json"
24+
},
1125
"main": "dist/cjs/index.js",
12-
"module": "dist/esm",
13-
"types": "dist/index.d.ts",
26+
"module": "dist/esm/index.js",
27+
"types": "dist/esm/index.d.ts",
1428
"scripts": {
1529
"cleanup": "rm -rf dist/ > /dev/null && rm -rf lib/ > /dev/null",
1630
"storybook": "storybook dev -p 6006",
@@ -28,10 +42,9 @@
2842
"devDependencies": {
2943
"@babel/core": "^7.24.1",
3044
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
31-
"@rollup/plugin-commonjs": "^25.0.7",
32-
"@rollup/plugin-node-resolve": "^15.2.3",
33-
"@rollup/plugin-terser": "^0.4.4",
34-
"@rollup/plugin-typescript": "^11.1.6",
45+
"@rollup/plugin-commonjs": "^28.0.6",
46+
"@rollup/plugin-node-resolve": "^16.0.1",
47+
"@rollup/plugin-typescript": "^12.1.4",
3548
"@storybook/addon-actions": "^8.0.5",
3649
"@storybook/addon-essentials": "^8.0.5",
3750
"@storybook/addon-links": "^8.0.5",
@@ -49,7 +62,6 @@
4962
"@types/node": "^15.0.2",
5063
"@types/react": "^18.2.60",
5164
"@types/react-dom": "^18.2.19",
52-
"@types/rollup-plugin-peer-deps-external": "^2.2.1",
5365
"@typescript-eslint/eslint-plugin": "^7.1.0",
5466
"@typescript-eslint/parser": "^7.1.0",
5567
"babel-jest": "^29.7.0",
@@ -72,11 +84,8 @@
7284
"react": "^18.2.0",
7385
"react-dom": "^18.2.0",
7486
"regenerator-runtime": "^0.13.7",
75-
"rollup": "^4.13.0",
76-
"rollup-plugin-dts": "^6.1.0",
77-
"rollup-plugin-peer-deps-external": "^2.2.4",
87+
"rollup": "^4.52.4",
7888
"rollup-plugin-preserve-directives": "^0.4.0",
79-
"rollup-plugin-tsconfig-paths": "^1.5.2",
8089
"sass": "^1.53.0",
8190
"storybook": "^8.0.5",
8291
"ts-jest": "^29.1.2",

rollup.config.mjs

Lines changed: 47 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,55 @@
1-
import resolve from '@rollup/plugin-node-resolve';
1+
import { join } from 'node:path';
22
import commonjs from '@rollup/plugin-commonjs';
3+
import { nodeResolve } from '@rollup/plugin-node-resolve';
34
import typescript from '@rollup/plugin-typescript';
4-
import terser from '@rollup/plugin-terser';
5-
import external from 'rollup-plugin-peer-deps-external';
6-
import { dts } from 'rollup-plugin-dts';
7-
import tsPaths from 'rollup-plugin-tsconfig-paths';
85
import preserveDirectives from 'rollup-plugin-preserve-directives';
9-
10-
import tsBuildConfig from './bundle-base.tsconfig.json' with { type: 'json' };
6+
import { defineConfig } from 'rollup';
117
import packageJson from './package.json' with { type: 'json' };
8+
import tsBuildConfig from './tsconfig.build.json' with { type: 'json' };
129

13-
// suppresses warnings printed to console as part of bundling components with directives present.
14-
const onWarnSuppression = {
15-
onwarn(warning, warn) {
16-
if (warning.code === 'MODULE_LEVEL_DIRECTIVE' && warning.message.includes(`"use client"`)) {
17-
return;
18-
}
19-
warn(warning);
20-
},
21-
};
22-
23-
const commonPlugins = [external(), tsPaths(), resolve(), commonjs()];
10+
const { outDir } = tsBuildConfig.compilerOptions;
11+
const external = Object.keys(packageJson.peerDependencies);
2412

25-
export default [
26-
// cjs export
27-
{
28-
input: 'src/index.ts',
29-
output: [
30-
{
31-
file: packageJson.main,
32-
format: 'cjs',
33-
sourcemap: true,
34-
},
35-
],
36-
plugins: [
37-
...commonPlugins,
38-
typescript({
39-
tsconfig: 'bundle-base.tsconfig.json',
40-
compilerOptions: {
41-
declaration: false,
13+
export default defineConfig(
14+
/** @type {const} */ (['cjs', 'esm']).map(
15+
/**
16+
* Rollup options for each module format
17+
*/
18+
(format) => ({
19+
input: 'src/index.ts',
20+
output: [
21+
{
22+
dir: join(outDir, format),
23+
format,
24+
preserveModules: true,
25+
preserveModulesRoot: 'src',
26+
sourcemap: true,
4227
},
43-
}),
44-
terser(),
45-
],
46-
...onWarnSuppression,
47-
},
48-
// esm export
49-
{
50-
input: 'src/index.ts',
51-
output: [
52-
{
53-
dir: packageJson.module,
54-
format: 'esm',
55-
sourcemap: true,
56-
preserveModules: true,
57-
preserveModulesRoot: 'src',
28+
],
29+
external,
30+
plugins: [
31+
nodeResolve(),
32+
commonjs(),
33+
typescript({
34+
tsconfig: 'tsconfig.build.json',
35+
compilerOptions: {
36+
outDir: join(outDir, format),
37+
},
38+
}),
39+
preserveDirectives(),
40+
],
41+
42+
// Handle warnings as errors
43+
onwarn(warning) {
44+
const { code, message } = warning;
45+
46+
// Skip warnings about "use client" directives
47+
if (code === 'MODULE_LEVEL_DIRECTIVE' && message.includes(`"use client"`)) {
48+
return;
49+
}
50+
51+
throw new Error(warning.message, { cause: warning });
5852
},
59-
],
60-
plugins: [
61-
...commonPlugins,
62-
typescript({
63-
tsconfig: 'bundle-base.tsconfig.json',
64-
compilerOptions: {
65-
declaration: true,
66-
declarationDir: 'dist/esm',
67-
emitDeclarationOnly: true,
68-
outDir: 'dist/esm',
69-
},
70-
}),
71-
preserveDirectives(),
72-
terser({ compress: { directives: false } }),
73-
],
74-
...onWarnSuppression,
75-
},
76-
// type bundling
77-
{
78-
input: 'src/index.ts',
79-
output: [{ file: 'dist/index.d.ts', format: 'esm' }],
80-
external: [],
81-
plugins: [
82-
dts({
83-
compilerOptions: {
84-
paths: tsBuildConfig.compilerOptions.paths,
85-
},
86-
}),
87-
],
88-
},
89-
];
53+
}),
54+
),
55+
);

tsconfig.base.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"esModuleInterop": true,
6+
"module": "ESNext",
7+
"moduleResolution": "Bundler",
8+
"noEmit": true,
9+
"resolveJsonModule": true,
10+
"skipLibCheck": true,
11+
"strict": false,
12+
"strictBindCallApply": true,
13+
"strictFunctionTypes": true,
14+
"strictNullChecks": true,
15+
"target": "ESNext",
16+
"types": [],
17+
"paths": {
18+
"@components/*": ["./src/components/*"],
19+
"@content-presentation/*": ["./src/components/content-presentation/*"],
20+
"@form-elements/*": ["./src/components/form-elements/*"],
21+
"@navigation/*": ["./src/components/navigation/*"],
22+
"@typography/*": ["./src/components/typography/*"],
23+
"@util/*": ["./src/util/*"],
24+
"@patterns/*": ["./src/patterns/*"]
25+
}
26+
},
27+
"exclude": ["./dist", "./node_modules"]
28+
}

tsconfig.build.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"declarationMap": true,
6+
"emitDeclarationOnly": true,
7+
"jsx": "react",
8+
"lib": ["ESNext", "DOM"],
9+
"outDir": "./dist",
10+
"rootDir": "./src",
11+
"strict": true,
12+
"sourceMap": true,
13+
"target": "ES2015",
14+
"types": ["node"]
15+
},
16+
"include": ["./src"],
17+
"exclude": ["**/__mocks__", "**/__tests__", "./src/setupTests.ts"]
18+
}

tsconfig.json

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
{
2+
"extends": "./tsconfig.base.json",
23
"compilerOptions": {
34
"jsx": "react",
4-
"target": "es6",
5-
"module": "esnext",
6-
"moduleResolution": "node",
7-
"declaration": true,
8-
"baseUrl": "./",
9-
"types": ["jest", "node"],
10-
"sourceMap": true,
11-
"forceConsistentCasingInFileNames": true,
12-
"allowSyntheticDefaultImports": true,
13-
"esModuleInterop": true,
14-
"skipLibCheck": true,
15-
"strict": true,
16-
"strictNullChecks": true,
17-
"resolveJsonModule": true,
18-
"allowJs": true,
19-
"outDir": "./dist",
20-
"paths": {
21-
"@components/*": ["src/components/*"],
22-
"@content-presentation/*": ["src/components/content-presentation/*"],
23-
"@form-elements/*": ["src/components/form-elements/*"],
24-
"@navigation/*": ["src/components/navigation/*"],
25-
"@typography/*": ["src/components/typography/*"],
26-
"@util/*": ["src/util/*"],
27-
"@patterns/*": ["src/patterns/*"]
28-
}
5+
"lib": ["ESNext", "DOM"],
6+
"target": "ES2015",
7+
"types": ["jest", "node"]
298
},
30-
"include": ["src", "stories"],
31-
"exclude": ["node_modules", "build"]
9+
"include": [
10+
"./.storybook/*.ts",
11+
"./src/**/*.ts",
12+
"./src/**/*.tsx",
13+
"./stories/**/*.tsx",
14+
"./*.config.js",
15+
"./*.config.mjs"
16+
]
3217
}

0 commit comments

Comments
 (0)