15
15
*/
16
16
17
17
import { BackstagePackageJson , PackageRoleInfo } from '@backstage/cli-node' ;
18
- import { ConfigReader } from '@backstage/config' ;
19
- import { loadConfig } from '@backstage/config-loader' ;
20
18
21
19
import { getPackages } from '@manypkg/get-packages' ;
22
20
import { OptionValues } from 'commander' ;
23
21
import fs from 'fs-extra' ;
24
22
import { InteropType , rollup } from 'rollup' ;
25
- import * as semver from 'semver' ;
26
23
27
24
import { execSync } from 'child_process' ;
28
- import path , { basename } from 'path' ;
25
+ import path from 'path' ;
29
26
30
27
import { Output } from '../../lib/builder' ;
31
28
import { makeRollupConfigs } from '../../lib/builder/config' ;
@@ -35,11 +32,15 @@ import { readEntryPoints } from '../../lib/entryPoints';
35
32
import { productionPack } from '../../lib/packager/productionPack' ;
36
33
import { paths } from '../../lib/paths' ;
37
34
import { Task } from '../../lib/tasks' ;
35
+ import {
36
+ addToDependenciesForModule ,
37
+ addToMainDependencies ,
38
+ } from './backend-utils' ;
38
39
39
40
export async function backend (
40
41
roleInfo : PackageRoleInfo ,
41
42
opts : OptionValues ,
42
- ) : Promise < void > {
43
+ ) : Promise < string > {
43
44
if ( ! fs . existsSync ( paths . resolveTarget ( 'src' , 'dynamic' ) ) ) {
44
45
console . warn (
45
46
`Package doesn't seem to provide dynamic loading entrypoints. You might want to add dynamic loading entrypoints in a src/dynamic folder.` ,
@@ -166,46 +167,15 @@ export async function backend(
166
167
rollupConfig . plugins ?. push (
167
168
embedModules ( {
168
169
filter : filter ,
169
- addDependency ( embeddedModule , dependencyName , newDependencyVersion ) {
170
- const existingDependencyVersion = dependenciesToAdd [ dependencyName ] ;
171
- if ( existingDependencyVersion === undefined ) {
172
- dependenciesToAdd [ dependencyName ] = newDependencyVersion ;
173
- return ;
174
- }
175
-
176
- if ( existingDependencyVersion === newDependencyVersion ) {
177
- return ;
178
- }
179
-
180
- const existingDependencyMinVersion = semver . minVersion (
181
- existingDependencyVersion ,
182
- ) ;
183
- if (
184
- existingDependencyMinVersion &&
185
- semver . satisfies ( existingDependencyMinVersion , newDependencyVersion )
186
- ) {
187
- console . log (
188
- `Several compatible versions ('${ existingDependencyVersion } ', '${ newDependencyVersion } ') of the same transitive dependency ('${ dependencyName } ') for embedded module ('${ embeddedModule } '): keeping '${ existingDependencyVersion } '` ,
189
- ) ;
190
- return ;
191
- }
192
-
193
- const newDependencyMinVersion = semver . minVersion ( newDependencyVersion ) ;
194
- if (
195
- newDependencyMinVersion &&
196
- semver . satisfies ( newDependencyMinVersion , existingDependencyVersion )
197
- ) {
198
- dependenciesToAdd [ dependencyName ] = newDependencyVersion ;
199
- console . log (
200
- `Several compatible versions ('${ existingDependencyVersion } ', '${ newDependencyVersion } ') of the same transitive dependency ('${ dependencyName } ') for embedded module ('${ embeddedModule } '): keeping '${ newDependencyVersion } '` ,
201
- ) ;
202
- return ;
203
- }
204
-
205
- throw new Error (
206
- `Several incompatible versions ('${ existingDependencyVersion } ', '${ newDependencyVersion } ') of the same transitive dependency ('${ dependencyName } ') for embedded module ('${ embeddedModule } ')` ,
207
- ) ;
208
- } ,
170
+ addDependency : ( embeddedModule , dependencyName , newDependencyVersion ) =>
171
+ addToDependenciesForModule (
172
+ {
173
+ name : dependencyName ,
174
+ version : newDependencyVersion ,
175
+ } ,
176
+ dependenciesToAdd ,
177
+ embeddedModule ,
178
+ ) ,
209
179
} ) ,
210
180
) ;
211
181
@@ -267,33 +237,19 @@ export async function backend(
267
237
f => ! f . startsWith ( 'dist-dynamic/' ) ,
268
238
) ;
269
239
270
- for ( const dep in dependenciesToAdd ) {
271
- if ( ! Object . prototype . hasOwnProperty . call ( dependenciesToAdd , dep ) ) {
272
- continue ;
273
- }
274
- pkgToCustomize . dependencies ||= { } ;
275
- const existingVersion = pkgToCustomize . dependencies [ dep ] ;
276
- if ( existingVersion === undefined ) {
277
- pkgToCustomize . dependencies [ dep ] = dependenciesToAdd [ dep ] ;
278
- continue ;
279
- }
280
- if ( existingVersion !== dependenciesToAdd [ dep ] ) {
281
- const existingMinVersion = semver . minVersion ( existingVersion ) ;
282
-
283
- if (
284
- existingMinVersion &&
285
- semver . satisfies ( existingMinVersion , dependenciesToAdd [ dep ] )
286
- ) {
287
- console . log (
288
- `The version of a dependency ('${ dep } ') of an embedded module differs from the main module's dependencies: '${ dependenciesToAdd [ dep ] } ', '${ existingVersion } ': keeping it as it is compatible` ,
289
- ) ;
290
- } else {
291
- throw new Error (
292
- `The version of a dependency ('${ dep } ') of an embedded module conflicts with main module dependencies: '${ dependenciesToAdd [ dep ] } ', '${ existingVersion } ': cannot proceed!` ,
293
- ) ;
294
- }
295
- }
240
+ // We remove scripts, because they do not make sense for this derived package.
241
+ // They even bring errors, especially the pre-pack and post-pack ones:
242
+ // we want to be able to use npm pack on this derived package to distribute it as a dynamic plugin,
243
+ // and obviously this should not trigger the backstage pre-pack or post-pack actions
244
+ // which are related to the packaging of the original static package.
245
+ pkgToCustomize . scripts = { } ;
246
+
247
+ const pkgDependencies = pkgToCustomize . dependencies || { } ;
248
+ addToMainDependencies ( dependenciesToAdd , pkgDependencies ) ;
249
+ if ( Object . keys ( pkgDependencies ) . length > 0 ) {
250
+ pkgToCustomize . dependencies = pkgDependencies ;
296
251
}
252
+
297
253
if ( pkgToCustomize . dependencies ) {
298
254
for ( const monoRepoPackage of monoRepoPackages . packages ) {
299
255
if ( pkgToCustomize . dependencies [ monoRepoPackage . packageJson . name ] ) {
@@ -376,10 +332,13 @@ export async function backend(
376
332
}
377
333
378
334
if ( opts . install ) {
379
- const version = execSync ( 'yarn --version' ) . toString ( ) . trim ( ) ;
335
+ const yarn = 'yarn' ;
336
+ const version = execSync ( `${ yarn } --version` ) . toString ( ) . trim ( ) ;
380
337
const yarnInstall = version . startsWith ( '1.' )
381
- ? `yarn install --production${ yarnLockExists ? ' --frozen-lockfile' : '' } `
382
- : `yarn install${ yarnLockExists ? ' --immutable' : '' } ` ;
338
+ ? `${ yarn } install --production${
339
+ yarnLockExists ? ' --frozen-lockfile' : ''
340
+ } `
341
+ : `${ yarn } install${ yarnLockExists ? ' --immutable' : '' } ` ;
383
342
384
343
await Task . forCommand ( yarnInstall , { cwd : target , optional : false } ) ;
385
344
await fs . remove ( paths . resolveTarget ( 'dist-dynamic' , '.yarn' ) ) ;
@@ -393,39 +352,5 @@ export async function backend(
393
352
minify : Boolean ( opts . minify ) ,
394
353
} ) ;
395
354
396
- if ( opts . dev ) {
397
- const appConfigs = await loadConfig ( {
398
- configRoot : paths . targetRoot ,
399
- configTargets : [ ] ,
400
- } ) ;
401
- const fullConfig = ConfigReader . fromConfigs ( appConfigs . appConfigs ) ;
402
-
403
- const dynamicPlugins = fullConfig . getOptional ( 'dynamicPlugins' ) ;
404
- if (
405
- typeof dynamicPlugins === 'object' &&
406
- dynamicPlugins !== null &&
407
- 'rootDirectory' in dynamicPlugins &&
408
- typeof dynamicPlugins . rootDirectory === 'string'
409
- ) {
410
- await fs . ensureSymlink (
411
- paths . resolveTarget ( 'src' ) ,
412
- path . resolve ( target , 'src' ) ,
413
- 'dir' ,
414
- ) ;
415
- const dynamicPluginsRootPath = path . isAbsolute (
416
- dynamicPlugins . rootDirectory ,
417
- )
418
- ? dynamicPlugins . rootDirectory
419
- : paths . resolveTargetRoot ( dynamicPlugins . rootDirectory ) ;
420
- await fs . ensureSymlink (
421
- target ,
422
- path . resolve ( dynamicPluginsRootPath , basename ( paths . targetDir ) ) ,
423
- 'dir' ,
424
- ) ;
425
- } else {
426
- throw new Error (
427
- `'dynamicPlugins.rootDirectory' should be configured in the app config in order to use the --dev option.` ,
428
- ) ;
429
- }
430
- }
355
+ return target ;
431
356
}
0 commit comments