Skip to content

Instantly share code, notes, and snippets.

@ronsims2
Created May 1, 2023 02:06
Show Gist options
  • Save ronsims2/78ed52583f84056969577286b881c0bc to your computer and use it in GitHub Desktop.
Save ronsims2/78ed52583f84056969577286b881c0bc to your computer and use it in GitHub Desktop.
Fix wasm-pack package file by adding a main entry to the package.json
#!/bin/zsh
# Run this from the root of the project!!!
wasm-pack build --target web --release
python fix_package_file.py
cd pkg
npm link
#!/usr/bin/python3
# Right now this will use the project folder name to guess the entry file name.
# It has the bones to accept a path to the package file and the name of the main entry you want to add.
# I didn't add this feature yet since as is it works for me... ronsims2@gmail.com
from argparse import ArgumentParser
import json
from pathlib import Path
import os
parser = ArgumentParser('This script will add a main entry in the package.json.')
parser.add_argument('--pack', help='The path to your package.json.')
parser.add_argument('--main', help='Optional name of the main JS file. When omitted, this script will guess the name.')
args = parser.parse_args()
package_file_path = args.pack
proj_dir = Path(__file__).parent
package_file_path = proj_dir / 'pkg/package.json'
print(proj_dir.name)
main_entry = args.main if args.main else '{}.js'.format(proj_dir.name.replace('-', '_'))
package_data = None
with open(package_file_path, 'r') as f:
package_data = json.loads(f.read())
if package_data.get('main') is None:
package_data['main'] = main_entry
with open(package_file_path, 'w') as f:
f.write(json.dumps(package_data, indent=4))
print('package update, main entry:', main_entry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment