Skip to content

Commit

Permalink
Merge pull request #61 from commercelayer/move-to-esm
Browse files Browse the repository at this point in the history
Move to Pure ESM.  It still bundle to `cjs` and `esm`.
  • Loading branch information
acasazza authored Mar 14, 2024
2 parents 1d480e7 + 17386e5 commit db22f34
Show file tree
Hide file tree
Showing 29 changed files with 587 additions and 1,331 deletions.
5 changes: 0 additions & 5 deletions .eslintrc.json

This file was deleted.

3 changes: 2 additions & 1 deletion .github/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ changelog:
labels:
- ignore-for-release
authors:
- octocat
- dependabot
categories:
- title: 💥 Breaking Change
labels:
- breaking-change
- title: 🚀 Enhancement
labels:
- enhancement
- feature
- title: 🐛 Bug Fix
labels:
- bug
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: pnpm 🧰
uses: pnpm/action-setup@v2.2.2
uses: pnpm/action-setup@v3
with:
version: 8.x

- name: Node 🧰
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: 20.x
cache: 'pnpm'

- name: Install 📦
Expand All @@ -46,7 +46,7 @@ jobs:

- name: Post to a Slack channel
id: slack
uses: slackapi/slack-github-action@v1.22.0
uses: slackapi/slack-github-action@v1.25.0
with:
# Slack channel id, channel name, or user id to post message.
# See also: https://api.slack.com/methods/chat.postMessage#channels
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Create a draft GitHub release 🎁
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
with:
token: ${{ secrets.COMMERCELAYER_CI_TOKEN }}
draft: true
Expand Down
13 changes: 13 additions & 0 deletions examples/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { core } = require('@commercelayer/js-auth')

async function run() {
const auth = await core.authentication('client_credentials', {
clientId: 'BISG8bb3GWpC8_D7Nt1SuWWdieS5bJq831A50LgB_Ig',
slug: 'demo-store',
scope: 'market:11279'
})

console.log(auth)
}

run()
12 changes: 12 additions & 0 deletions examples/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"private": true,
"name": "cjs",
"type": "commonjs",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@commercelayer/js-auth": "workspace:^5.0.0"
}
}
16 changes: 16 additions & 0 deletions examples/esm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="root"></div>
<script type="importmap">
{
"imports": {
"@commercelayer/js-auth": "./node_modules/@commercelayer/js-auth/dist/index.js"
}
}
</script>
<script type="module" src="./index.js"></script>
Open the developer console with <code>Cmd + Option + J</code> (on a Mac) or <code>Ctrl + Shift + J</code> (on Windows) and check the log. You may need to refresh the page.
</body>
</html>
13 changes: 13 additions & 0 deletions examples/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { core } from '@commercelayer/js-auth'

async function run() {
const auth = await core.authentication('client_credentials', {
clientId: 'BISG8bb3GWpC8_D7Nt1SuWWdieS5bJq831A50LgB_Ig',
slug: 'demo-store',
scope: 'market:11279'
})

console.log(auth)
}

run()
13 changes: 13 additions & 0 deletions examples/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"name": "esm",
"type": "module",
"main": "index.js",
"scripts": {
"start": "node index.js",
"serve": "npx serve"
},
"dependencies": {
"@commercelayer/js-auth": "workspace:^5.0.0"
}
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
},
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "pnpm -r build",
"build": "pnpm --filter js-auth build",
"prepare": "husky install",
"test": "pnpm -r test",
"make:version": "lerna version --no-private"
"test": "pnpm --filter js-auth test",
"make:version": "lerna version --no-private",
"example:cjs": "pnpm --filter cjs start",
"example:esm": "pnpm --filter esm start",
"example:esm:serve": "pnpm --filter esm serve"
},
"devDependencies": {
"@commercelayer/eslint-config-ts": "^1.3.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/js-auth/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: ['@commercelayer/eslint-config-ts'],
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module'
}
}
41 changes: 15 additions & 26 deletions packages/js-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
"name": "Alessandro Casazza",
"email": "[email protected]"
},
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
"types": "lib/esm/index.d.ts",
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.js",
"files": [
"lib",
"package.json",
"README.md"
"dist"
],
"keywords": [
"jamstack",
Expand All @@ -32,32 +35,18 @@
"lint": "eslint src --ext .ts,.tsx",
"lint:fix": "eslint src --ext .ts,.tsx --fix",
"test": "pnpm run lint && vitest run --silent",
"coverage": "vitest run --coverage",
"test:e2e": "NODE_ENV=test playwright test",
"test:e2e:coverage": "nyc pnpm test:e2e && pnpm coverage:report",
"coverage:report": "nyc report --reporter=html",
"build": "tsc -b tsconfig.prod.json tsconfig.prod.esm.json --verbose && pnpm postbuild",
"build:dev": "tsc -b tsconfig.prod.json tsconfig.prod.esm.json --verbose && tsc-alias -p tsconfig.prod.json && tsc-alias -p tsconfig.prod.esm.json",
"postbuild": "tsc-alias -p tsconfig.prod.json && tsc-alias -p tsconfig.prod.esm.json && minimize-js lib -w -s"
"build": "tsup"
},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"dependencies": {
"tslib": "^2.6.2"
},
"devDependencies": {
"@types/node": "^20.11.16",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-c8": "^0.33.0",
"jsdom": "^24.0.0",
"minimize-js": "^1.4.0",
"tsc-alias": "^1.8.8",
"typescript": "^5.3.3",
"vite": "^5.1.0",
"vite-tsconfig-paths": "^4.3.1",
"vitest": "^1.2.2"
"@types/node": "^20.11.27",
"tsup": "^8.0.2",
"typescript": "^5.4.2",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.3.1"
},
"engines": {
"node": ">=18.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/js-auth/specs/authentication.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { core } from 'src'
import { core } from '../src/index.js'

const slug = process.env.VITE_TEST_SLUG
const clientId = process.env.VITE_TEST_CLIENT_ID
Expand Down
2 changes: 1 addition & 1 deletion packages/js-auth/specs/provisioning.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { provisioning } from 'src'
import { provisioning } from '../src/index.js'

const clientId = process.env.VITE_TEST_PROVISIONING_CLIENT_ID
const clientSecret = process.env.VITE_TEST_PROVISIONING_CLIENT_SECRET
Expand Down
8 changes: 4 additions & 4 deletions packages/js-auth/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type TReturn, type GrantType, type TOptions } from '#types'
import { camelCaseToSnake } from '#utils/camelCaseToSnake'
import { snakeToCamelCase } from '#utils/snakeToCamelCase'
import { type TokenJson } from './provisioning'
import type { TReturn, GrantType, TOptions } from '#types/index.js'
import { camelCaseToSnake } from '#utils/camelCaseToSnake.js'
import { snakeToCamelCase } from '#utils/snakeToCamelCase.js'
import type { TokenJson } from './provisioning.js'

async function authentication<G extends GrantType>(
grantType: G,
Expand Down
10 changes: 8 additions & 2 deletions packages/js-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export { core } from './core'
export { provisioning } from './provisioning'
export { core } from './core.js'
export { provisioning } from './provisioning.js'

export type * from './types/index.js'
export type * from './types/authorizationCode.js'
export type * from './types/clientCredentials.js'
export type * from './types/password.js'
export type * from './types/refreshToken.js'
8 changes: 4 additions & 4 deletions packages/js-auth/src/provisioning.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TClientCredentials } from '#types/clientCredentials'
import { camelCaseToSnake } from '#utils/camelCaseToSnake'
import { snakeToCamelCase } from '#utils/snakeToCamelCase'
import type { TBaseReturn } from './types'
import type { TClientCredentials } from '#types/clientCredentials.js'
import { camelCaseToSnake } from '#utils/camelCaseToSnake.js'
import { snakeToCamelCase } from '#utils/snakeToCamelCase.js'
import type { TBaseReturn } from '#types/index.js'

export type TProvisioningOptions = Omit<TClientCredentials, 'slug' | 'scope'>
export type TProvisioningReturn = TBaseReturn
Expand Down
4 changes: 2 additions & 2 deletions packages/js-auth/src/types/authorizationCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type TBaseOptions } from '#types'
import { type TPasswordReturn } from './password'
import type { TBaseOptions } from '#types/index.js'
import type { TPasswordReturn } from './password.js'

/**
* The authorization code grant type is used by clients to exchange an authorization code for an access token.
Expand Down
2 changes: 1 addition & 1 deletion packages/js-auth/src/types/clientCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TBaseOptions } from '#types'
import type { TBaseOptions } from '#types/index.js'

/**
* The client credentials grant type is used by clients to obtain an access token outside of the context of a user.
Expand Down
14 changes: 7 additions & 7 deletions packages/js-auth/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type TPasswordReturn, type TPassword } from '#types/password'
import { type TRefreshToken } from '#types/refreshToken'
import {
type TAuthorizationCodeReturn,
type TAuthorizationCode
} from '#types/authorizationCode'
import { type TClientCredentials } from '#types/clientCredentials'
import type { TPasswordReturn, TPassword } from '#types/password.js'
import type { TRefreshToken } from '#types/refreshToken.js'
import type {
TAuthorizationCodeReturn,
TAuthorizationCode
} from '#types/authorizationCode.js'
import type { TClientCredentials } from '#types/clientCredentials.js'

/**
* The grant type. Possible values are: password, refresh_token, client_credentials, authorization_code.
Expand Down
2 changes: 1 addition & 1 deletion packages/js-auth/src/types/password.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TBaseOptions, TBaseReturn } from '#types'
import type { TBaseOptions, TBaseReturn } from '#types/index.js'

/**
* The password grant type is used by first-party clients to exchange a user's credentials for an access token.
Expand Down
2 changes: 1 addition & 1 deletion packages/js-auth/src/types/refreshToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type TBaseOptions } from '#types'
import type { TBaseOptions } from '#types/index.js'

/**
* The refresh token grant type is used by clients to exchange a refresh token for an access token when the access token has expired.
Expand Down
Loading

0 comments on commit db22f34

Please sign in to comment.