From de7837c37f1ae34fe6cca74e91c6ce89f5e876fb Mon Sep 17 00:00:00 2001 From: Vojtech Szocs Date: Wed, 10 Aug 2022 20:47:24 +0200 Subject: [PATCH] Include referenced .scss files when building core plugin SDK package --- .../scripts/package-definitions.ts | 34 ++++++++++++++++++- .../tsconfig-base.json | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/frontend/packages/console-dynamic-plugin-sdk/scripts/package-definitions.ts b/frontend/packages/console-dynamic-plugin-sdk/scripts/package-definitions.ts index 75b10338e2a..388ae2fbd66 100644 --- a/frontend/packages/console-dynamic-plugin-sdk/scripts/package-definitions.ts +++ b/frontend/packages/console-dynamic-plugin-sdk/scripts/package-definitions.ts @@ -1,6 +1,10 @@ +import * as path from 'path'; +import * as fs from 'fs-extra'; +import * as glob from 'glob'; import * as _ from 'lodash'; import * as readPkg from 'read-pkg'; import { sharedPluginModules } from '../src/shared-modules'; +import { resolvePath } from './utils/path'; type GeneratedPackage = { /** Package output directory. */ @@ -26,11 +30,38 @@ const commonManifestFields: Partial = { keywords: ['openshift', 'console', 'plugin'], }; -const commonFiles: GeneratedPackage['filesToCopy'] = { +const commonFiles: Record = { '../../../LICENSE': 'LICENSE', 'README.md': 'README.md', }; +const getReferencedAssets = (outDir: string) => { + const baseDir = resolvePath(`${outDir}/lib`); + const jsFiles = glob.sync('**/*.js', { cwd: baseDir, absolute: true }); + const importPattern = /^(?:import|import .* from) '(.*)';$/gm; + + const assetExtensions = ['.scss']; + const filesToCopy: Record = {}; + + jsFiles.forEach((filePath) => { + for (const match of fs.readFileSync(filePath, 'utf-8').matchAll(importPattern)) { + const moduleSpecifier = match[1]; + + if ( + moduleSpecifier.startsWith('.') && + assetExtensions.some((ext) => moduleSpecifier.endsWith(ext)) + ) { + const assetPath = path.resolve(path.dirname(filePath), moduleSpecifier); + const assetRelativePath = path.relative(resolvePath(baseDir), assetPath); + + filesToCopy[`src/${assetRelativePath}`] = `lib/${assetRelativePath}`; + } + } + }); + + return filesToCopy; +}; + const parseDeps = ( pkg: readPkg.PackageJson, depNames: string[], @@ -81,6 +112,7 @@ export const getCorePackage: GetPackageDefinition = ( filesToCopy: { ...commonFiles, docs: 'docs', + ...getReferencedAssets('dist/core'), }, }); diff --git a/frontend/packages/console-dynamic-plugin-sdk/tsconfig-base.json b/frontend/packages/console-dynamic-plugin-sdk/tsconfig-base.json index f82f5eb5bfd..acd18c49774 100644 --- a/frontend/packages/console-dynamic-plugin-sdk/tsconfig-base.json +++ b/frontend/packages/console-dynamic-plugin-sdk/tsconfig-base.json @@ -11,7 +11,7 @@ "experimentalDecorators": true, "noUnusedLocals": true, "skipLibCheck": true, - "lib": ["es2016", "es2017.object", "es2020.promise", "dom"], + "lib": ["es2016", "es2017.object", "es2020.promise", "es2020.string", "dom"], "typeRoots": ["node_modules/@types", "@types"], "types": ["node", "sdk"] }