@@ -6,84 +6,101 @@ import external from 'rollup-plugin-peer-deps-external';
66import { dts } from 'rollup-plugin-dts' ;
77import tsPaths from 'rollup-plugin-tsconfig-paths' ;
88import preserveDirectives from 'rollup-plugin-preserve-directives' ;
9+ import postcss from 'rollup-plugin-postcss' ;
910
1011import tsBuildConfig from './bundle-base.tsconfig.json' assert { type : 'json ' } ;
1112import packageJson from './package.json' assert { type : 'json ' } ;
1213
13- // suppresses warnings printed to console as part of bundling components with directives present.
1414const onWarnSuppression = {
1515 onwarn ( warning , warn ) {
1616 if ( warning . code === 'MODULE_LEVEL_DIRECTIVE' && warning . message . includes ( `"use client"` ) ) return ;
1717 warn ( warning ) ;
1818 } ,
1919} ;
2020
21- // Keep react 19 out of the bundle
22- const externals = [ 'react' , 'react-dom' , 'react/jsx-runtime' ] ;
23-
2421const commonPlugins = [
25- external ( ) , // auto-externalize peer deps
26- tsPaths ( ) , // respect tsconfig paths in source
22+ external ( ) ,
23+ tsPaths ( ) ,
2724 resolve ( { extensions : [ '.mjs' , '.js' , '.ts' , '.tsx' ] } ) ,
2825 commonjs ( ) ,
26+ // ⬇️ compile SCSS → CSS for any `import './file.scss'`
27+ postcss ( {
28+ extract : true , // or a filename like 'styles.css'
29+ modules : false ,
30+ minimize : false ,
31+ use : { sass : { quietDeps : true } } , // uses Dart Sass
32+ includePaths : [ 'node_modules' ] , // lets you @use "nhsuk-frontend/..." cleanly
33+ } ) ,
2934] ;
3035
36+ // JS-only version of preserveDirectives
37+ const jsOnlyPreserveDirectives = preserveDirectives ( {
38+ include : [ '**/*.{js,jsx,ts,tsx,mjs,cjs}' ] ,
39+ exclude : [ '**/*.{css,scss,sass,less,styl}' ] ,
40+ } ) ;
41+
3142export default [
32- // CJS (single file)
43+ // cjs export
3344 {
3445 input : 'src/index.ts' ,
35- external : externals ,
36- output : [
37- {
38- file : packageJson . main , // e.g. dist/cjs/index.js
39- format : 'cjs' ,
40- sourcemap : true ,
41- } ,
42- ] ,
46+ output : [ { file : packageJson . main , format : 'cjs' , sourcemap : true } ] ,
4347 plugins : [
4448 ...commonPlugins ,
4549 typescript ( {
46- // IMPORTANT: dedicated build tsconfig (noEmit, no jest/node types)
47- tsconfig : './tsconfig.build.json' ,
50+ tsconfig : 'bundle-base. tsconfig.json' ,
51+ compilerOptions : { declaration : false } ,
4852 } ) ,
49- preserveDirectives ( ) , // keep "use client" etc.
50- terser ( { compress : { directives : false } } ) ,
53+ terser ( ) ,
5154 ] ,
5255 ...onWarnSuppression ,
5356 } ,
5457
55- // ESM (single file)
58+ // esm export
5659 {
5760 input : 'src/index.ts' ,
58- external : externals ,
5961 output : [
6062 {
61- file : packageJson . module , // e.g. dist/esm/index.js
63+ dir : packageJson . module ,
6264 format : 'esm' ,
6365 sourcemap : true ,
66+ preserveModules : true ,
67+ preserveModulesRoot : 'src' ,
6468 } ,
6569 ] ,
6670 plugins : [
6771 ...commonPlugins ,
6872 typescript ( {
69- tsconfig : './tsconfig.build.json' ,
73+ tsconfig : 'bundle-base.tsconfig.json' ,
74+ compilerOptions : {
75+ declaration : false , // don’t emit .d.ts here
76+ emitDeclarationOnly : false ,
77+ outDir : undefined , // let Rollup handle files
78+ } ,
7079 } ) ,
71- preserveDirectives ( ) ,
80+ jsOnlyPreserveDirectives , // ⬅️ don't touch .scss
7281 terser ( { compress : { directives : false } } ) ,
7382 ] ,
7483 ...onWarnSuppression ,
7584 } ,
7685
77- // Type definitions (bundled .d.ts)
86+ // type bundling
87+ // type bundling
7888 {
7989 input : 'src/index.ts' ,
80- external : externals ,
8190 output : [ { file : 'dist/index.d.ts' , format : 'esm' } ] ,
91+
92+ // ⬅️ Ignore any style imports during .d.ts bundling
93+ external : [ / \. s ? c s s $ / i, / \. l e s s $ / i, / \. s t y l ( u s ) ? $ / i] ,
94+
8295 plugins : [
8396 dts ( {
84- // carry over path mapping if you have any
85- compilerOptions : { paths : tsBuildConfig ?. compilerOptions ?. paths ?? { } } ,
97+ respectExternal : true , // ⬅️ key: don't inline externals (like the styles we exclude)
98+ compilerOptions : {
99+ // carry over paths if you use them
100+ paths : tsBuildConfig . compilerOptions . paths ,
101+ } ,
86102 } ) ,
87103 ] ,
88- } ,
104+ }
105+
89106] ;
0 commit comments