Skip to content

Commit

Permalink
fix(javascript): publish script esm
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#1787

Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
  • Loading branch information
algolia-bot and shortcuts committed Jul 20, 2023
1 parent daaac16 commit 1b66715
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -10,7 +10,7 @@
"build:many": "lerna run build --include-dependencies --scope ${0:-'{@algolia/*,algoliasearch}'}",
"clean": "lerna run clean --include-dependencies",
"release:bump": "lerna version ${0:-patch} --no-changelog --no-git-tag-version --no-push --exact --force-publish --yes",
"release:publish": "ts-node --project tsconfig.script.json scripts/publish.ts",
"release:publish": "ts-node --esm --project tsconfig.script.json scripts/publish.ts",
"test:size": "bundlesize"
},
"devDependencies": {
Expand Down
26 changes: 5 additions & 21 deletions scripts/publish.ts
@@ -1,29 +1,13 @@
import fsp from 'fs/promises';
import path from 'path';

import execa from 'execa';
import { execaCommand } from 'execa';
import semver from 'semver';

async function publish(): Promise<void> {
// Read the local version of `algoliasearch/package.json`
const { version } = JSON.parse(
(
await fsp.readFile(
path.resolve(
__dirname,
'..',
'packages',
'algoliasearch',
'package.json'
)
)
).toString()
);
import packageJSON from "../packages/algoliasearch/package.json" assert { type: "json" };

async function publish(): Promise<void> {
// Get tag like `alpha`, `beta`, ...
const tag = semver.prerelease(version)?.[0];
const tag = semver.prerelease(packageJSON.version)?.[0];

await execa.command(
await execaCommand(
`lerna exec --no-bail -- npm_config_registry=https://registry.npmjs.org/ npm publish --access public ${
tag ? `--tag ${tag}` : ''
}`,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.script.json
@@ -1,6 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs"
"resolveJsonModule": true
}
}

1 comment on commit 1b66715

@Albertbol
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this commit broke import for me in Firebase cloud functions, they do not support ESM, the following error:

Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_REQUIRE_ESM]: require() of ES /functions/node_modules/algoliasearch/dist/algoliasearch.cjs.js from /functions/dist/functions/index.js not supported.
algoliasearch.cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename algoliasearch.cjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /functions/node_modules/algoliasearch/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Is it necessary?
Thanks

Please sign in to comment.