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
  • Loading branch information
Chau Tran authored and Chau Tran committed Oct 10, 2022
1 parent 65a079c commit 42ce197
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 12 additions & 1 deletion e2e/js/src/js.test.ts
Expand Up @@ -307,7 +307,7 @@ export function ${lib}Wildcard() {
);
}, 120000);

describe('inlining', () => {
describe.only('inlining', () => {
it.each(['tsc', 'swc'])(
'should inline libraries with --compiler=%s',
async (compiler) => {
Expand All @@ -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
14 changes: 11 additions & 3 deletions packages/js/src/utils/inline.ts
Expand Up @@ -73,7 +73,13 @@ export function postProcessInlinedDependencies(
inlineDependency.buildOutputPath ||
join(outputPath, inlineDependency.root);
const destDepOutputPath = join(outputPath, inlineDependency.name);
movePackage(depOutputPath, destDepOutputPath);
const isBuildable = !!inlineDependency.buildOutputPath;

movePackage(
depOutputPath,
destDepOutputPath,
!isBuildable // buildable libraries should be copied instead of moved.
);

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

export function movePackage(from: string, to: string) {
export function movePackage(from: string, to: string, shouldRemove = true) {
copySync(from, to, { overwrite: true, recursive: true });
removeSync(from);
if (shouldRemove) {
removeSync(from);
}
}

function updateImports(
Expand Down

0 comments on commit 42ce197

Please sign in to comment.