Skip to content

Commit

Permalink
fix: add namespace imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Jul 4, 2023
1 parent 8f3aafb commit 865f533
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
".npmignore",
".npmrc",
"lib/",
"src/",
"LICENSE",
"README.md"
],
"types": "./lib/index.d.ts",
"types": "./lib/types/index.d.ts",
"exports": {
".": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"types": "./lib/index.d.ts"
"types": "./lib/types/index.d.ts"
}
},
"sideEffects": false,
"scripts": {
"commit": "git-cz",
"prebuild": "shx rm -rf lib/*",
"build": "tsup --dts-only && tsup --tsconfig tsconfig.build.json && ./postbuild",
"build": "tsup --dts-only && tsup --config tsup.browser.ts && tsup --tsconfig tsconfig.build.json && ./postbuild",
"type-check": "tsc --emitDeclarationOnly false --noEmit",
"lint": "npm run lint:ci -- --fix",
"lint:ci": "eslint . --ext .js,.ts,.tsx",
Expand Down
53 changes: 45 additions & 8 deletions postbuild
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
#!/usr/bin/env bash
shopt -s globstar

pushd lib/ || exit 1

mkdir cjs
mv index.js index.js.map cjs

rm index.d.mts

cat >cjs/package.json <<!EOF
function create_packages() {
cat >cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >esm/package.json <<!EOF
cat >esm/package.json <<!EOF
{
"type": "module"
}
!EOF
}

function copy_esm_dts() {
mkdir types
pushd cjs || exit 1
find . -type f -name "*.d.ts" -exec cp --parents {} ../types \;
rm ./**/*.{d.mts,d.ts}
popd || exit1
}

function copy_esm() {
mv ./cjs/esm ./esm
}

function fix_esm_extensions() {
esm="./esm"

find "$esm" -type f -name "*.js" | while read -r file; do
content=$(cat "$file")
updated_content=$(echo "$content" | sed "s/import \(.*\) from '\(.*\)\.ts'/import \1 from '\2\.js'/g")
updated_content=$(echo "$updated_content" | sed "s/export \(.*\) from '\(.*\)\.ts'/export \1 from '\2\.js'/g")
echo "$updated_content" >"$file"
done
}

function fix_cjs_extensions() {
cjs="./cjs"

find "$cjs" -type f -name "*.js" | while read -r file; do
content=$(cat "$file")
updated_content=$(echo "$content" | sed "s/require('\(.*\)\.ts'/require('\1\.js'/g")
echo "$updated_content" >"$file"
done
}

cd lib/ || exit 1
copy_esm_dts
copy_esm
create_packages
fix_cjs_extensions
fix_esm_extensions
3 changes: 1 addition & 2 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"noEmit": true
},
"files": ["./mod.ts", "./tsup.config.ts", "./vite.config.ts"],
"include": ["./src", "./docs/"],
"include": ["./src", "./docs/", "./*.ts"],
"exclude": ["lib/"]
}
13 changes: 13 additions & 0 deletions tsup.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
splitting: true,
sourcemap: true,
treeshake: true,
minify: true,
legacyOutput: true,
bundle: true,
format: 'iife',
outDir: './lib',
});
8 changes: 4 additions & 4 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
entry: ['src/**/!(*.spec).ts'],
splitting: true,
sourcemap: true,
treeshake: true,
minify: true,
legacyOutput: true,
format: ['cjs', 'esm', 'iife'],
outDir: './lib/',
bundle: false,
format: ['esm', 'cjs'],
outDir: './lib/cjs',
});

0 comments on commit 865f533

Please sign in to comment.