Skip to content

Commit 569698f

Browse files
authored
refactor: Tidy up some code (#28)
1 parent 013a42d commit 569698f

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

integration/cms.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { CmsField, CmsConfig } from 'netlify-cms-core';
22
import CMS from 'netlify-cms-app';
33
import { initIdentity } from './identity-widget';
4+
import type { NormalizedPreviewStyle } from './types';
45

56
export default function initCMS({
67
adminPath,
@@ -22,7 +23,7 @@ export default function initCMS({
2223
fields: (CmsField & { name: keyof any })[];
2324
}
2425
>;
25-
previewStyles: Array<[string] | [string, { raw: boolean }]>;
26+
previewStyles: NormalizedPreviewStyle[];
2627
}) {
2728
initIdentity(adminPath);
2829

@@ -58,9 +59,7 @@ export default function initCMS({
5859
/**
5960
* Register each of our collections’ preview styles and components.
6061
*/
61-
// collections.forEach(({ component, css, collection }) => {
62-
// CMS.registerPreviewStyle(css, { raw: true });
63-
62+
// collections.forEach(({ component, collection }) => {
6463
// CMS.registerPreviewTemplate(
6564
// collection.name,
6665
// ({ entry, widgetFor, getAsset }) => {

integration/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { CmsConfig } from 'netlify-cms-core';
33
import { spawn } from 'node:child_process';
44
import react from '@astrojs/react';
55
import AdminDashboard from './vite-plugin-admin-dashboard.js';
6+
import type { PreviewStyle } from './types.js';
67

78
const widgetPath = 'astro-netlify-cms/identity-widget';
89

@@ -13,7 +14,7 @@ interface NetlifyCMSOptions {
1314
*/
1415
adminPath?: string;
1516
config: Omit<CmsConfig, 'load_config_file' | 'local_backend'>;
16-
previewStyles?: Array<string | [string] | [string, { raw: boolean }]>;
17+
previewStyles?: PreviewStyle[];
1718
}
1819

1920
export default function NetlifyCMS({

integration/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type NormalizedPreviewStyle =
2+
| [pathOrUrl: string]
3+
| [rawCSS: string, meta: { raw: boolean }];
4+
5+
export type PreviewStyle = string | NormalizedPreviewStyle;

integration/vite-plugin-admin-dashboard.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import type { CmsConfig } from 'netlify-cms-core';
22
import type { OutputBundle } from 'rollup';
33
import type { Plugin } from 'vite';
4+
import type { PreviewStyle } from './types';
45

56
const dashboardPath = 'astro-netlify-cms/cms';
67

7-
const AdminPage = ({
8+
function AdminPage({
89
adminPath,
910
assets,
1011
config,
11-
dashboardPath,
1212
previewStyles = [],
1313
}: {
1414
adminPath: string;
1515
config: CmsConfig;
1616
previewStyles: Array<string | [string] | [string, { raw: boolean }]>;
17-
} & (
18-
| { assets: [id: string, filename: string][]; dashboardPath?: undefined }
19-
| { assets?: undefined; dashboardPath: string }
20-
)) => {
17+
/** Array of module ID and filename tuples present only at build time. */
18+
assets?: [id: string, filename: string][];
19+
}) {
2120
const imports: string[] = [];
2221
const styles: string[] = [];
2322

@@ -65,7 +64,7 @@ const AdminPage = ({
6564
<body></body>
6665
</html>
6766
`;
68-
};
67+
}
6968

7069
export default function AdminDashboardPlugin({
7170
adminPath,
@@ -74,7 +73,7 @@ export default function AdminDashboardPlugin({
7473
}: {
7574
adminPath: string;
7675
config: Omit<CmsConfig, 'load_config_file' | 'local_backend'>;
77-
previewStyles: Array<string | [string] | [string, { raw: boolean }]>;
76+
previewStyles: PreviewStyle[];
7877
}): Plugin {
7978
if (!adminPath.startsWith('/')) {
8079
throw new Error(
@@ -92,6 +91,7 @@ export default function AdminDashboardPlugin({
9291
return {
9392
name: 'vite-plugin-netlify-cms-admin-dashboard',
9493

94+
/** Build-only Rollup hook. */
9595
options(options) {
9696
let { input } = options;
9797
if (
@@ -101,14 +101,14 @@ export default function AdminDashboardPlugin({
101101
) {
102102
if (!Array.isArray(input) && typeof input !== 'object') input = [input];
103103
importMap = generateImportMap({
104-
dashboardPath,
105104
previewStyles,
106105
});
107106
input = { ...input, ...importMap };
108107
}
109108
return { ...options, input };
110109
},
111110

111+
/** Dev-only Vite hook. */
112112
configureServer({ transformIndexHtml, middlewares }) {
113113
middlewares.use(async (req, res, next) => {
114114
if (req.url === adminPath || req.url === adminPath + '/') {
@@ -117,7 +117,6 @@ export default function AdminDashboardPlugin({
117117
AdminPage({
118118
adminPath,
119119
config,
120-
dashboardPath,
121120
previewStyles,
122121
})
123122
);
@@ -128,6 +127,7 @@ export default function AdminDashboardPlugin({
128127
});
129128
},
130129

130+
/** Build-only Rollup hook. */
131131
generateBundle(options, bundle) {
132132
const dashboardChunk = Object.values(bundle).find(
133133
({ name }) => name === 'cms'
@@ -163,10 +163,8 @@ interface ImportMap {
163163
* ```
164164
*/
165165
function generateImportMap({
166-
dashboardPath,
167166
previewStyles,
168167
}: {
169-
dashboardPath: string;
170168
previewStyles: Array<string | [string] | [string, { raw: boolean }]>;
171169
}): ImportMap {
172170
const imports: ImportMap = { cms: dashboardPath };

0 commit comments

Comments
 (0)