From d25b0c15ba1e1e935ad267b092cccfbd2795c86e Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Tue, 23 Apr 2024 09:08:45 -0400 Subject: [PATCH] fix(cli): prevent generate task from crashing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this pr is motivated by a scneario where running `npx stencil generate` would result in either an error or an early return: ``` npm run generate > gen-test-1@0.0.1 generate > stencil generate [49:23.1] @stencil/core [49:23.2] v4.17.0 ♨️ ``` note: the reported error was never reproduced locally, only the early return. technically, this was introduced in v4.17.0, when we switched over to using esbuild for production builds. however, this has been present in any build that is esbuild-based since we migrated the cli submodule to esbuild (where we had a mixed rollup-esbuild build during the migration for dev builds). removing this alias eliminates dynamic imports in the CJS output of the cli submodule: ``` const { prompt: r } = await import('../sys/node/prompts.js') ``` which wouldn't run properly, causing the error/early return. this change does cause an increase in bundle size, as we end up importing more of prompts.js than we do. to mitigate this, we direct the cli module to the export we actually use (leading to less getting bundled in the cli module). fixes: #5692 STENCIL-1294 cannot identified the prompts.js as a function? ? --- .github/workflows/test-component-starter.yml | 31 ++++++++++++++++++++ scripts/esbuild/cli.ts | 2 ++ scripts/esbuild/util.ts | 3 +- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-component-starter.yml b/.github/workflows/test-component-starter.yml index ff6e3f93994..0fbc2f01a32 100644 --- a/.github/workflows/test-component-starter.yml +++ b/.github/workflows/test-component-starter.yml @@ -103,3 +103,34 @@ jobs: run: npm run test -- --no-build # the project was just built, don't build it again working-directory: ./tmp-component-starter shell: bash + + - name: Test npx stencil generate + # `stencil generate` doesn't have a way to skip file generation, so we provide it with a component name and run + # `echo` with a newline to select "all files" to generate (and use -e to interpret that backslash for a newline) + run: echo -e '\n' | npm run generate -- hello-world + working-directory: ./tmp-component-starter + shell: bash + + - name: Verify Files Exist + run: | + file_list=( + src/components/hello-world/hello-world.tsx + src/components/hello-world/hello-world.css + src/components/hello-world/test/hello-world.spec.tsx + src/components/hello-world/test/hello-world.e2e.ts + ) + for file in "${file_list[@]}"; do + if [ -f "$file" ]; then + echo "File '$file' exists." + else + echo "File '$file' does not exist." + exit 1 + fi + done + working-directory: ./tmp-component-starter + shell: bash + + - name: Test Generated Files + run: npm run test + working-directory: ./tmp-component-starter + shell: bash diff --git a/scripts/esbuild/cli.ts b/scripts/esbuild/cli.ts index 9cb3b8b8469..9c39b80b91b 100644 --- a/scripts/esbuild/cli.ts +++ b/scripts/esbuild/cli.ts @@ -27,6 +27,8 @@ export async function buildCli(opts: BuildOptions) { const dtsFilename = 'index.d.ts'; const cliAliases = getEsbuildAliases(); + // this isn't strictly necessary to alias - however, this minimizes cuts down the bundle size by ~70kb. + cliAliases['prompts'] = 'prompts/lib/index.js'; const external = getEsbuildExternalModules(opts, opts.output.cliDir); diff --git a/scripts/esbuild/util.ts b/scripts/esbuild/util.ts index 50926983abe..9a8f1ced419 100644 --- a/scripts/esbuild/util.ts +++ b/scripts/esbuild/util.ts @@ -26,10 +26,9 @@ export function getEsbuildAliases(): Record { // mapping node.js module names to `sys/node` "cache" // - // these allow us to bundle and ship a dependency (like `prompts`) as part + // these allow us to bundle and ship a dependency (like `glob`) as part // of the Stencil distributable but also have our separate bundles // reference the same file - prompts: './sys/node/prompts.js', glob: './sys/node/glob.js', // dev server related aliases