Skip to content

Commit deaa19d

Browse files
authored
fix(cli): node_modules of the main plugin unexpectedly copied to the dist-dynamic folder. (#1482)
fix(cli) : `node_modules` of the main plugin unexpectedly copued to the `dist-dynamic` folder. Signed-off-by: David Festal <[email protected]>
1 parent 6107732 commit deaa19d

File tree

1 file changed

+107
-70
lines changed

1 file changed

+107
-70
lines changed

packages/cli/src/commands/export-dynamic-plugin/backend-embed-as-dependencies.ts

Lines changed: 107 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import {
3939
} from './backend-utils';
4040

4141
export async function backend(opts: OptionValues): Promise<string> {
42-
const target = path.join(paths.targetDir, 'dist-dynamic');
42+
const targetRelativePath = 'dist-dynamic';
43+
const target = path.join(paths.targetDir, targetRelativePath);
4344
const yarn = 'yarn';
4445

4546
const pkgContent = await fs.readFile(
@@ -124,7 +125,48 @@ export async function backend(opts: OptionValues): Promise<string> {
124125
);
125126
}
126127
for (const embedded of embeddedResolvedPackages) {
127-
const customizeManifest = customizeForDynamicUse({
128+
const embeddedDestRelativeDir = embeddedPackageRelativePath(embedded);
129+
const embeddedDestDir = path.join(target, embeddedDestRelativeDir);
130+
if (!embedded.alreadyPacked) {
131+
if (opts.build) {
132+
Task.log(
133+
`Building embedded package ${chalk.cyan(
134+
embedded.packageName,
135+
)} in ${chalk.cyan(embedded.dir)}`,
136+
);
137+
await Task.forCommand(`${yarn} build`, {
138+
cwd: embedded.dir,
139+
optional: false,
140+
});
141+
}
142+
Task.log(
143+
`Packing embedded package ${chalk.cyan(
144+
embedded.packageName,
145+
)} in ${chalk.cyan(embedded.dir)} to ${chalk.cyan(
146+
embeddedDestRelativeDir,
147+
)}`,
148+
);
149+
await productionPack({
150+
packageDir: embedded.dir,
151+
targetDir: embeddedDestDir,
152+
});
153+
} else {
154+
Task.log(
155+
`Copying embedded package ${chalk.cyan(
156+
embedded.packageName,
157+
)} from ${chalk.cyan(embedded.dir)} to ${chalk.cyan(
158+
embeddedDestRelativeDir,
159+
)}`,
160+
);
161+
fs.rmSync(embeddedDestDir, { force: true, recursive: true });
162+
fs.cpSync(embedded.dir, embeddedDestDir, { recursive: true });
163+
}
164+
Task.log(
165+
`Customizing embedded package ${chalk.cyan(
166+
embedded.packageName,
167+
)} for dynamic loading`,
168+
);
169+
await customizeForDynamicUse({
128170
embedded: embeddedResolvedPackages,
129171
monoRepoPackages,
130172
sharedPackages: sharedPackagesRules,
@@ -145,40 +187,7 @@ export async function backend(opts: OptionValues): Promise<string> {
145187
);
146188
}
147189
},
148-
});
149-
const embeddedDestDir = path.join(
150-
target,
151-
embeddedPackageRelativePath(embedded),
152-
);
153-
if (!embedded.alreadyPacked) {
154-
if (opts.build) {
155-
Task.log(`Building embedded package ${chalk.cyan(embedded.dir)}`);
156-
await Task.forCommand(`${yarn} build`, {
157-
cwd: embedded.dir,
158-
optional: false,
159-
});
160-
}
161-
Task.log(`Packing embedded package ${chalk.cyan(embedded.dir)}`);
162-
await productionPack({
163-
packageDir: embedded.dir,
164-
targetDir: embeddedDestDir,
165-
customizeManifest,
166-
});
167-
} else {
168-
Task.log(`Packing embedded package ${chalk.cyan(embedded.dir)}`);
169-
fs.rmSync(embeddedDestDir, { force: true, recursive: true });
170-
fs.cpSync(embedded.dir, embeddedDestDir, { recursive: true });
171-
const embeddedPkgPath = path.join(embeddedDestDir, 'package.json');
172-
const embeddedPkgContent = await fs.readFile(embeddedPkgPath, 'utf8');
173-
const embeddedPkg = JSON.parse(
174-
embeddedPkgContent,
175-
) as BackstagePackageJson;
176-
customizeManifest(embeddedPkg);
177-
await fs.writeJson(embeddedPkgPath, embeddedPkg, {
178-
encoding: 'utf8',
179-
spaces: 2,
180-
});
181-
}
190+
})(path.join(embeddedDestDir, 'package.json'));
182191
}
183192

184193
const embeddedDependenciesResolutions: { [key: string]: any } = {};
@@ -196,41 +205,59 @@ export async function backend(opts: OptionValues): Promise<string> {
196205
});
197206
}
198207

199-
Task.log(`Packing main package`);
208+
Task.log(
209+
`Packing main package to ${chalk.cyan(
210+
path.join(targetRelativePath, 'package.json'),
211+
)}`,
212+
);
200213
await productionPack({
201214
packageDir: '',
202215
targetDir: target,
203-
customizeManifest: customizeForDynamicUse({
204-
embedded: embeddedResolvedPackages,
205-
monoRepoPackages,
206-
sharedPackages: sharedPackagesRules,
207-
overridding: {
208-
name: `${pkg.name}-dynamic`,
209-
bundleDependencies: true,
210-
// We remove scripts, because they do not make sense for this derived package.
211-
// They even bring errors, especially the pre-pack and post-pack ones:
212-
// we want to be able to use npm pack on this derived package to distribute it as a dynamic plugin,
213-
// and obviously this should not trigger the backstage pre-pack or post-pack actions
214-
// which are related to the packaging of the original static package.
215-
scripts: {},
216-
},
217-
additionalResolutions: embeddedDependenciesResolutions,
218-
after(mainPkg) {
219-
if (Object.keys(embeddedPeerDependencies).length === 0) {
220-
return;
221-
}
222-
Task.log(
223-
`Hoisting peer dependencies of embedded packages to the main package`,
224-
);
225-
const mainPeerDependencies = mainPkg.peerDependencies || {};
226-
addToMainDependencies(embeddedPeerDependencies, mainPeerDependencies);
227-
if (Object.keys(mainPeerDependencies).length > 0) {
228-
mainPkg.peerDependencies = mainPeerDependencies;
229-
}
230-
},
231-
}),
232216
});
233217

218+
// Small cleanup in case the `dist-dynamic` entr was still in the `files` of the main plugin package.
219+
if (fs.pathExistsSync(path.join(target, 'dist-dynamic'))) {
220+
fs.rmSync(path.join(target, 'dist-dynamic'), {
221+
force: true,
222+
recursive: true,
223+
});
224+
}
225+
226+
Task.log(
227+
`Customizing main package in ${chalk.cyan(
228+
path.join(targetRelativePath, 'package.json'),
229+
)} for dynamic loading`,
230+
);
231+
await customizeForDynamicUse({
232+
embedded: embeddedResolvedPackages,
233+
monoRepoPackages,
234+
sharedPackages: sharedPackagesRules,
235+
overridding: {
236+
name: `${pkg.name}-dynamic`,
237+
bundleDependencies: true,
238+
// We remove scripts, because they do not make sense for this derived package.
239+
// They even bring errors, especially the pre-pack and post-pack ones:
240+
// we want to be able to use npm pack on this derived package to distribute it as a dynamic plugin,
241+
// and obviously this should not trigger the backstage pre-pack or post-pack actions
242+
// which are related to the packaging of the original static package.
243+
scripts: {},
244+
},
245+
additionalResolutions: embeddedDependenciesResolutions,
246+
after(mainPkg) {
247+
if (Object.keys(embeddedPeerDependencies).length === 0) {
248+
return;
249+
}
250+
Task.log(
251+
`Hoisting peer dependencies of embedded packages to the main package`,
252+
);
253+
const mainPeerDependencies = mainPkg.peerDependencies || {};
254+
addToMainDependencies(embeddedPeerDependencies, mainPeerDependencies);
255+
if (Object.keys(mainPeerDependencies).length > 0) {
256+
mainPkg.peerDependencies = mainPeerDependencies;
257+
}
258+
},
259+
})(path.resolve(target, 'package.json'));
260+
234261
const yarnLock = path.resolve(target, 'yarn.lock');
235262
const yarnLockExists = await fs.pathExists(yarnLock);
236263

@@ -282,9 +309,10 @@ export async function backend(opts: OptionValues): Promise<string> {
282309
: `${yarn} install${yarnLockExists ? ' --immutable' : ''}`;
283310

284311
await Task.forCommand(yarnInstall, { cwd: target, optional: false });
285-
await fs.remove(paths.resolveTarget('dist-dynamic', '.yarn'));
312+
await fs.remove(paths.resolveTarget(targetRelativePath, '.yarn'));
286313

287314
// Checking if some shared dependencies have been included inside the private dependencies
315+
Task.log(`Validating private dependencies`);
288316
const lockFile = await Lockfile.load(yarnLock);
289317
const sharedPackagesInPrivateDeps: string[] = [];
290318
for (const key of lockFile.keys()) {
@@ -364,7 +392,6 @@ export async function backend(opts: OptionValues): Promise<string> {
364392
}
365393

366394
// Check whether private dependencies contain native modules, and fail for now (not supported).
367-
368395
const nativePackages: string[] = [];
369396
for await (const n of gatherNativeModules(target)) {
370397
nativePackages.push(n);
@@ -575,8 +602,13 @@ function customizeForDynamicUse(options: {
575602
additionalOverrides?: { [key: string]: any } | undefined;
576603
additionalResolutions?: { [key: string]: any } | undefined;
577604
after?: ((pkg: BackstagePackageJson) => void) | undefined;
578-
}): (pkg: BackstagePackageJson) => void {
579-
return (pkgToCustomize: BackstagePackageJson) => {
605+
}): (dynamicPkgPath: string) => Promise<void> {
606+
return async (dynamicPkgPath: string): Promise<void> => {
607+
const dynamicPkgContent = await fs.readFile(dynamicPkgPath, 'utf8');
608+
const pkgToCustomize = JSON.parse(
609+
dynamicPkgContent,
610+
) as BackstagePackageJson;
611+
580612
for (const field in options.overridding || {}) {
581613
if (!Object.prototype.hasOwnProperty.call(options.overridding, field)) {
582614
continue;
@@ -700,6 +732,11 @@ function customizeForDynamicUse(options: {
700732
if (options.after) {
701733
options.after(pkgToCustomize);
702734
}
735+
736+
await fs.writeJson(dynamicPkgPath, pkgToCustomize, {
737+
encoding: 'utf8',
738+
spaces: 2,
739+
});
703740
};
704741
}
705742

0 commit comments

Comments
 (0)