diff --git a/packages/cli/generators/example/index.js b/packages/cli/generators/example/index.js index 4059053121f9..373dfe4fe66b 100644 --- a/packages/cli/generators/example/index.js +++ b/packages/cli/generators/example/index.js @@ -132,10 +132,22 @@ module.exports = class extends BaseGenerator { const absOutDir = await downloadAndExtractExample(this.exampleName, cwd); this.outDir = path.relative(cwd, absOutDir); const tsconfig = path.join(absOutDir, 'tsconfig.json'); + + // Support older versions of examples that are using `tsconfig.build.json` const tsBuildConfig = path.join(absOutDir, 'tsconfig.build.json'); const exists = await fs.exists(tsconfig); - if (exists) return; - return fs.rename(tsBuildConfig, tsconfig); + if (!exists) { + return fs.rename(tsBuildConfig, tsconfig); + } + + // Recent versions of examples are using project references inside monorepo, + // see https://github.com/strongloop/loopback-next/pull/5155 + // we must switch to standalone mode (no project references) when the code + // was checked out outside of our monorepo. + const tsconfigContent = await fs.readJson(tsconfig); + delete tsconfigContent.references; + delete tsconfigContent.compilerOptions.composite; + await fs.writeJson(tsconfig, tsconfigContent); } install() { diff --git a/packages/cli/test/integration/generators/example.integration.js b/packages/cli/test/integration/generators/example.integration.js index de908c331aa9..e3ec413306b3 100644 --- a/packages/cli/test/integration/generators/example.integration.js +++ b/packages/cli/test/integration/generators/example.integration.js @@ -8,6 +8,7 @@ const assert = require('yeoman-assert'); const expect = require('@loopback/testlab').expect; const path = require('path'); +const {readJsonSync} = require('fs-extra'); const generator = path.join(__dirname, '../../../generators/example'); const baseTests = require('../lib/base-generator')(generator); @@ -87,4 +88,28 @@ describe('lb4 example', function () { }, ); }); + + it('removes project references from tsconfig', () => { + return testUtils + .executeGenerator(generator) + .withPrompts({name: VALID_EXAMPLE}) + .then(() => { + const tsconfigFile = 'tsconfig.json'; + const expectedConfig = readJsonSync( + require.resolve( + `../../../../../examples/${VALID_EXAMPLE}/${tsconfigFile}`, + ), + ); + delete expectedConfig.references; + delete expectedConfig.compilerOptions.composite; + + assert.file(tsconfigFile); + + // IMPORTANT! We cannot use `assert.jsonFileContent` here + // because the helper only checks if the file contains all expected + // properties, it does not verify there is no additional data. + const actualConfig = readJsonSync(tsconfigFile); + expect(actualConfig).to.deepEqual(expectedConfig); + }); + }); });