Skip to content

Commit 9efbf4b

Browse files
authored
fix(cli): prevent generate task from crashing (#5693)
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 > [email protected] 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? ?
1 parent 9a28fa5 commit 9efbf4b

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.github/workflows/test-component-starter.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,34 @@ jobs:
103103
run: npm run test -- --no-build # the project was just built, don't build it again
104104
working-directory: ./tmp-component-starter
105105
shell: bash
106+
107+
- name: Test npx stencil generate
108+
# `stencil generate` doesn't have a way to skip file generation, so we provide it with a component name and run
109+
# `echo` with a newline to select "all files" to generate (and use -e to interpret that backslash for a newline)
110+
run: echo -e '\n' | npm run generate -- hello-world
111+
working-directory: ./tmp-component-starter
112+
shell: bash
113+
114+
- name: Verify Files Exist
115+
run: |
116+
file_list=(
117+
src/components/hello-world/hello-world.tsx
118+
src/components/hello-world/hello-world.css
119+
src/components/hello-world/test/hello-world.spec.tsx
120+
src/components/hello-world/test/hello-world.e2e.ts
121+
)
122+
for file in "${file_list[@]}"; do
123+
if [ -f "$file" ]; then
124+
echo "File '$file' exists."
125+
else
126+
echo "File '$file' does not exist."
127+
exit 1
128+
fi
129+
done
130+
working-directory: ./tmp-component-starter
131+
shell: bash
132+
133+
- name: Test Generated Files
134+
run: npm run test
135+
working-directory: ./tmp-component-starter
136+
shell: bash

scripts/esbuild/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export async function buildCli(opts: BuildOptions) {
2727
const dtsFilename = 'index.d.ts';
2828

2929
const cliAliases = getEsbuildAliases();
30+
// this isn't strictly necessary to alias - however, this minimizes cuts down the bundle size by ~70kb.
31+
cliAliases['prompts'] = 'prompts/lib/index.js';
3032

3133
const external = getEsbuildExternalModules(opts, opts.output.cliDir);
3234

scripts/esbuild/util.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ export function getEsbuildAliases(): Record<string, string> {
2626

2727
// mapping node.js module names to `sys/node` "cache"
2828
//
29-
// these allow us to bundle and ship a dependency (like `prompts`) as part
29+
// these allow us to bundle and ship a dependency (like `glob`) as part
3030
// of the Stencil distributable but also have our separate bundles
3131
// reference the same file
32-
prompts: './sys/node/prompts.js',
3332
glob: './sys/node/glob.js',
3433

3534
// dev server related aliases

0 commit comments

Comments
 (0)