Skip to content

Commit

Permalink
fix(js): inlined buildable libraries original output should not be re…
Browse files Browse the repository at this point in the history
…moved (#12483)

Co-authored-by: Chau Tran <chautran@Chaus-MacBook-Pro.local>
  • Loading branch information
nartc and Chau Tran committed Oct 11, 2022
1 parent 49a67a5 commit b17893c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
11 changes: 11 additions & 0 deletions e2e/js/src/js.test.ts
Expand Up @@ -317,16 +317,21 @@ export function ${lib}Wildcard() {
const buildable = uniq('buildable');
runCLI(`generate @nrwl/js:lib ${buildable}`);

const buildableTwo = uniq('buildabletwo');
runCLI(`generate @nrwl/js:lib ${buildableTwo}`);

const nonBuildable = uniq('nonbuildable');
runCLI(`generate @nrwl/js:lib ${nonBuildable} --buildable=false`);

updateFile(`libs/${parent}/src/lib/${parent}.ts`, () => {
return `
import { ${buildable} } from '@${scope}/${buildable}';
import { ${buildableTwo} } from '@${scope}/${buildableTwo}';
import { ${nonBuildable} } from '@${scope}/${nonBuildable}';
export function ${parent}() {
${buildable}();
${buildableTwo}();
${nonBuildable}();
}
`;
Expand All @@ -337,6 +342,7 @@ export function ${parent}() {
runCLI(`build ${parent} --external=all`);
checkFilesExist(
`dist/libs/${buildable}/src/index.js`, // buildable
`dist/libs/${buildableTwo}/src/index.js`, // buildable two
`dist/libs/${parent}/src/index.js`, // parent
`dist/libs/${parent}/${nonBuildable}/src/index.js` // inlined non buildable
);
Expand All @@ -350,22 +356,27 @@ export function ${parent}() {
checkFilesExist(
`dist/libs/${parent}/src/index.js`, // parent
`dist/libs/${parent}/${buildable}/src/index.js`, // inlined buildable
`dist/libs/${parent}/${buildableTwo}/src/index.js`, // inlined buildable two
`dist/libs/${parent}/${nonBuildable}/src/index.js` // inlined non buildable
);
fileContent = readFile(`dist/libs/${parent}/src/lib/${parent}.js`);
expect(fileContent).toContain(`${nonBuildable}/src`);
expect(fileContent).toContain(`${buildable}/src`);
expect(fileContent).toContain(`${buildableTwo}/src`);

// 3. external is set to an array of libs
execSync(`rm -rf dist`);
runCLI(`build ${parent} --external=${buildable}`);
checkFilesExist(
`dist/libs/${buildable}/src/index.js`, // buildable
`dist/libs/${buildableTwo}/src/index.js`, // buildable two original output should be persisted
`dist/libs/${parent}/src/index.js`, // parent
`dist/libs/${parent}/${buildableTwo}/src/index.js`, // inlined buildable two
`dist/libs/${parent}/${nonBuildable}/src/index.js` // inlined non buildable
);
fileContent = readFile(`dist/libs/${parent}/src/lib/${parent}.js`);
expect(fileContent).toContain(`${nonBuildable}/src`);
expect(fileContent).toContain(`${buildableTwo}/src`);
expect(fileContent).not.toContain(`${buildable}/src`);
},
120000
Expand Down
24 changes: 16 additions & 8 deletions packages/js/src/utils/inline.ts
Expand Up @@ -73,7 +73,16 @@ export function postProcessInlinedDependencies(
inlineDependency.buildOutputPath ||
join(outputPath, inlineDependency.root);
const destDepOutputPath = join(outputPath, inlineDependency.name);
movePackage(depOutputPath, destDepOutputPath);
const isBuildable = !!inlineDependency.buildOutputPath;

if (isBuildable) {
copySync(depOutputPath, destDepOutputPath, {
overwrite: true,
recursive: true,
});
} else {
movePackage(depOutputPath, destDepOutputPath);
}

// TODO: hard-coded "src"
inlinedDepsDestOutputRecord[inlineDependency.pathAlias] =
Expand Down Expand Up @@ -238,7 +247,7 @@ function buildInlineGraphExternals(
}
}

export function movePackage(from: string, to: string) {
function movePackage(from: string, to: string) {
copySync(from, to, { overwrite: true, recursive: true });
removeSync(from);
}
Expand Down Expand Up @@ -309,13 +318,12 @@ function getBuildOutputPath(
context: ExecutorContext,
options: NormalizedExecutorOptions
): string {
const projectTargets = context.projectGraph.nodes[projectName]?.data?.targets;
if (!projectTargets) return '';

const buildTarget = options.externalBuildTargets.find(
(buildTarget) =>
context.projectGraph.nodes[projectName]?.data?.targets?.[buildTarget]
(buildTarget) => projectTargets[buildTarget]
);

if (buildTarget)
return context.projectGraph.nodes[projectName].data.targets?.[buildTarget]
.options['outputPath'];
return '';
return buildTarget ? projectTargets[buildTarget].options['outputPath'] : '';
}

0 comments on commit b17893c

Please sign in to comment.