Skip to content

Commit

Permalink
build: properly resolve types for dist files
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Aug 12, 2023
1 parent 8bbf2c5 commit abd2eef
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions lib/node_modules/@stdlib/_tools/scripts/publish_packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,9 +1084,11 @@ function publish( pkg, clbk ) {
function publishToNPM() {
var fmtProdMsgVersion;
var jscodeshift;
var contents;
var command;
var escaped;
var cliPkg;
var alias;
var found;
var opts;
var dep;
Expand Down Expand Up @@ -1162,21 +1164,43 @@ function publish( pkg, clbk ) {
}
}
}
// Add `@stdlib/error-tools-fmtprodmsg` in package.json if the package depends on `@stdlib/string-format`:
if ( pkgJSON.dependencies[ '@stdlib/string-format' ] ) {
fmtProdMsgVersion = npmVersion( '@stdlib/error-tools-fmtprodmsg' );
pkgJSON.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = fmtProdMsgVersion;
deps.push( '@stdlib/error-tools-fmtprodmsg' );
}

// Transform error messages and save files to `dist` subdirectory:
command = 'cp -r lib dist';
command = 'cp -r lib tmp && mkdir dist';
shell( command, opts );
jscodeshift = join( rootDir(), 'node_modules', '.bin', 'jscodeshift' );
command = 'STDLIB_PKG=\'@stdlib/'+distPkg+'\' '+jscodeshift+' ./dist/**/*.js ./dist/*.js -t '+join( __dirname, 'transform.js' );
command = 'STDLIB_PKG=\'@stdlib/'+distPkg+'\' '+jscodeshift+' ./tmp/**/*.js ./tmp/*.js -t '+join( __dirname, 'transform.js' );
debug( 'Executing command: %s', command );
shell( command, opts );
command = 'find ./dist -name "*.js" -exec sed -E -i "1i\\/\\*\\* \\@license '+pkgJSON.license+' \\*\\/\n" {} \\;';
command = 'find ./tmp -name "*.js" -exec sed -E -i "1i\\/\\*\\* \\@license '+pkgJSON.license+' \\*\\/\n" {} \\;';
shell( command, opts );
command = [
'npx esbuild tmp/index.js --bundle --format=cjs',
'--sourcemap --minify --platform=node --outfile=dist/index.js',
'--external:'+deps.join( ' --external:' )
].join( ' ' );
debug( 'Executing command: %s', command );
shell( command, opts );
command = 'rm -rf tmp';
shell( command, opts );

// Add `@stdlib/error-tools-fmtprodmsg` in package.json if the package depends on `@stdlib/string-format`:
if ( pkgJSON.dependencies[ '@stdlib/string-format' ] ) {
fmtProdMsgVersion = npmVersion( '@stdlib/error-tools-fmtprodmsg' );
pkgJSON.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = fmtProdMsgVersion;
}
// Extract the alias from the Typescript definition file (after `export =` but before the semicolon):
command = 'grep -oP \'(?<=export = ).*?(?=;)\' docs/types/index.d.ts';
alias = trim( shell( command, opts ).toString() );

// Create the contents of a `index.d.ts` file in `dist` subdirectory that reexports `docs/types/index.d.ts` (we don't want to duplicate type definitions, so we just reexport them):
contents = [
'/// <reference path="../docs/types/index.d.ts" />',
'import '+alias+' from \'../docs/types/index\';',
'export = '+alias+';'
].join( '\n' );
writeFileSync( join( dist, 'dist', 'index.d.ts' ), contents );

// Replace GitHub MathJax equations with SVGs:
command = 'find . -type f -name \'*.md\' -print0 | xargs -0 perl -0777 -i -pe \'s/```math\\n([\\s\\S]+?)\\n```\\n\\n//g\'';
Expand Down

0 comments on commit abd2eef

Please sign in to comment.