Skip to content

Commit 1ef5298

Browse files
shuowueng-prod-CI-bot-okta
authored andcommitted
fix: remove require syntax
add cjs bundle validation test use vite for react-oie test app adjust rollup config for development generate esm node bundle add exports field as guiduice of different envs OKTA-405564 <<<Jenkins Check-In of Tested SHA: 0dcad6d for [email protected]>>> Artifact: okta-auth-js Files changed count: 38 PR Link: "#1114"
1 parent dc55146 commit 1ef5298

35 files changed

+772
-284
lines changed

.bacon.yml

+7-8
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ test_suites:
2020
script_name: integration
2121
criteria: MERGE
2222
queue_name: small
23-
# Re-enable once ESM bundle issue is fixed
24-
# - name: validate-esm-bundle
25-
# script_path: ../okta-auth-js/scripts
26-
# sort_order: '2'
27-
# timeout: '10'
28-
# script_name: validate-esm-bundle
29-
# criteria: MERGE
30-
# queue_name: small
23+
- name: validate-bundles
24+
script_path: ../okta-auth-js/scripts
25+
sort_order: '2'
26+
timeout: '10'
27+
script_name: validate-bundles
28+
criteria: MERGE
29+
queue_name: small
3130
- name: e2e
3231
script_path: ../okta-auth-js/scripts
3332
sort_order: '3'

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
- [#1113](https://github.com/okta/okta-auth-js/pull/1113) Updates types for `SigninWithCredentialsOptions` and `SignInOptions` to support `SP Initiated Auth`
88
- [#1125](https://github.com/okta/okta-auth-js/pull/1125) IDX - Supports auto select methodType (when only one selection is available) for `authenticator-verification-data` remediation
9+
- [#1114](https://github.com/okta/okta-auth-js/pull/1114) Exposes ESM node bundle
10+
11+
### Fixes
12+
13+
- [#1114](https://github.com/okta/okta-auth-js/pull/1114) Fixes ESM browser bundle issue by only using ESM `import` syntax
914

1015
## 6.1.0
1116

babel.cjs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ module.exports = {
66
[
77
'@babel/preset-env', {
88
'targets': {
9-
'node': true
9+
'node': 11
1010
},
1111
'modules': false
1212
}
1313
]],
1414
'plugins': [
1515
'@babel/plugin-transform-typescript',
1616
'@babel/plugin-proposal-class-properties',
17+
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#corejs
1718
['@babel/plugin-transform-runtime', {
1819
corejs: 3
1920
}],

jest.cjs.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*!
2+
* Copyright (c) 2015-present, Okta, Inc. and/or its affiliates. All rights reserved.
3+
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
4+
*
5+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
6+
* Unless required by applicable law or agreed to in writing, software
7+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
8+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
*
10+
* See the License for the specific language governing permissions and limitations under the License.
11+
*/
12+
13+
//Jest doc: https://jestjs.io/docs/ecmascript-modules
14+
15+
var OktaAuth = '<rootDir>/build/cjs/index.js';
16+
17+
module.exports = {
18+
'roots': [
19+
'test/validate-bundles'
20+
],
21+
'testMatch': [
22+
'**/test/validate-bundles/**/*.{js,ts}'
23+
],
24+
'transform': {
25+
'^.+\\.(ts)$': 'babel-jest'
26+
},
27+
'transformIgnorePatterns': [
28+
OktaAuth
29+
],
30+
'restoreMocks': true,
31+
'moduleNameMapper': {
32+
'^@okta/okta-auth-js$': OktaAuth
33+
},
34+
'testPathIgnorePatterns': [],
35+
'reporters': [
36+
'default',
37+
'jest-junit'
38+
]
39+
};

jest.esm.mjs

+16-8
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,28 @@
1212

1313
//Jest doc: https://jestjs.io/docs/ecmascript-modules
1414

15-
var OktaAuth = '<rootDir>/build/bundles-for-validation/esm/index.mjs';
15+
const OktaAuth = process.env.BUNDLE_ENV === 'browser' ?
16+
`<rootDir>/build/bundles-for-validation/esm/esm.browser.mjs` :
17+
`<rootDir>/build/esm/esm.node.mjs`;
18+
const testEnvironment = process.env.BUNDLE_ENV === 'browser' ? 'jsdom' : 'node';
1619

1720
export default {
18-
'roots': [
21+
roots: [
1922
'test/validate-bundles'
2023
],
21-
'testMatch': [
24+
testEnvironment,
25+
testMatch: [
2226
'**/test/validate-bundles/**/*.{js,ts}'
2327
],
24-
'transform': {},
25-
'restoreMocks': true,
26-
'moduleNameMapper': {
28+
transform: {},
29+
restoreMocks: true,
30+
moduleNameMapper: {
2731
'^@okta/okta-auth-js$': OktaAuth
2832
},
29-
'extensionsToTreatAsEsm': ['.ts'],
30-
'testPathIgnorePatterns': []
33+
extensionsToTreatAsEsm: ['.ts'],
34+
testPathIgnorePatterns: [],
35+
reporters: [
36+
'default',
37+
'jest-junit'
38+
]
3139
};

lib/AuthStateManager.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
*
1010
* See the License for the specific language governing permissions and limitations under the License.
1111
*/
12-
13-
12+
13+
// @ts-ignore
14+
// Do not use this type in code, so it won't be emitted in the declaration output
15+
import PCancelable from 'p-cancelable';
1416
import { AuthSdkError } from './errors';
1517
import { AuthState, AuthStateLogOptions } from './types';
1618
import { OktaAuth } from '.';
1719
import { getConsole } from './util';
1820
import { EVENT_ADDED, EVENT_REMOVED } from './TokenManager';
19-
// eslint-disable-next-line import/no-commonjs
20-
const PCancelable = require('p-cancelable');
2121

2222
export const INITIAL_AUTH_STATE = null;
2323
const DEFAULT_PENDING = {
@@ -43,7 +43,7 @@ const isSameAuthState = (prevState: AuthState | null, state: AuthState) => {
4343
export class AuthStateManager {
4444
_sdk: OktaAuth;
4545
_pending: {
46-
updateAuthStatePromise: typeof PCancelable;
46+
updateAuthStatePromise: any;
4747
canceledTimes: number;
4848
};
4949
_authState: AuthState | null;

lib/OktaAuth.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ import {
133133
clearTransactionMeta,
134134
isTransactionMetaValid
135135
} from './idx/transactionMeta';
136-
137-
// eslint-disable-next-line import/no-commonjs
138-
const Emitter = require('tiny-emitter');
136+
// @ts-ignore
137+
// Do not use this type in code, so it won't be emitted in the declaration output
138+
import Emitter from 'tiny-emitter';
139139

140140
class OktaAuth implements OktaAuthInterface, SigninAPI, SignoutAPI {
141141
options: OktaAuthOptions;
@@ -151,7 +151,7 @@ class OktaAuth implements OktaAuthInterface, SigninAPI, SignoutAPI {
151151
features!: FeaturesAPI;
152152
token: TokenAPI;
153153
_tokenQueue: PromiseQueue;
154-
emitter: typeof Emitter;
154+
emitter: any;
155155
tokenManager: TokenManager;
156156
authStateManager: AuthStateManager;
157157
http: HttpAPI;

lib/browser/browserStorage.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*
1313
*/
1414

15+
import Cookies from 'js-cookie';
1516
import AuthSdkError from '../errors/AuthSdkError';
1617
import {
1718
StorageProvider,
@@ -25,9 +26,6 @@ import {
2526
} from '../types';
2627
import { warn } from '../util';
2728

28-
// eslint-disable-next-line import/no-commonjs
29-
const Cookies = require('js-cookie');
30-
3129
// Building this as an object allows us to mock the functions in our tests
3230
var storageUtil: BrowserStorageUtil = {
3331

@@ -246,7 +244,11 @@ var storageUtil: BrowserStorageUtil = {
246244
return storageUtil.storage.get(name);
247245
},
248246

249-
get: function(name: string): string {
247+
get: function(name?: string): string {
248+
// return all cookies when no args is provided
249+
if (!arguments.length) {
250+
return Cookies.get();
251+
}
250252
return Cookies.get(name);
251253
},
252254

lib/crypto/node.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
// Ponyfill for NodeJS
1717
// Webpack config excludes this file
1818

19+
import atobModule from 'atob';
20+
import btoaModule from 'btoa';
21+
import { Crypto } from '@peculiar/webcrypto';
22+
1923
let a;
2024
if (typeof atob !== 'undefined') {
2125
a = atob;
2226
} else {
23-
a = require('atob');
27+
a = atobModule;
2428
}
2529
export { a as atob };
2630

@@ -29,22 +33,23 @@ let b;
2933
if (typeof btoa !== 'undefined') {
3034
b = btoa;
3135
} else {
32-
b = require('btoa');
36+
b = btoaModule;
3337
}
3438
export { b as btoa };
3539

36-
let crypto;
37-
try {
38-
crypto = require('crypto');
39-
} catch (err) {
40-
// this environment has no crypto module!
41-
}
40+
const crypto = (async () => {
41+
try {
42+
return await import('crypto');
43+
} catch (err) {
44+
// this environment has no crypto module!
45+
return undefined;
46+
}
47+
})();
4248

4349
let webcrypto;
4450
if (typeof crypto !== 'undefined' && crypto['webcrypto']) {
4551
webcrypto = crypto['webcrypto'];
4652
} else {
47-
const { Crypto } = require('@peculiar/webcrypto');
4853
webcrypto = new Crypto();
4954
}
5055

lib/server/serverStorage.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
*
1212
*/
1313

14+
// @ts-ignore
15+
// Do not use this type in code, so it won't be emitted in the declaration output
16+
import NodeCache from 'node-cache';
1417
import { SimpleStorage, StorageType, StorageUtil, Cookies } from '../types';
1518
import { AuthSdkError } from '../errors';
1619
// eslint-disable-next-line import/no-commonjs
17-
const NodeCache = require('node-cache'); // commonJS module cannot be imported without esModuleInterop
1820

1921
// this is a SHARED memory storage to support a stateless http server
2022
const sharedStorage = typeof NodeCache === 'function' ? new NodeCache() : null;

package.json

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@
66
"homepage": "https://github.com/okta/okta-auth-js",
77
"license": "Apache-2.0",
88
"main": "build/cjs/index.js",
9-
"module": "build/esm/index.js",
9+
"module": "build/esm/esm.node.mjs",
1010
"browser": "build/dist/okta-auth-js.umd.js",
1111
"types": "build/lib/index.d.ts",
12+
"exports": {
13+
"node": {
14+
"import": "./build/esm/esm.node.mjs",
15+
"require": "./build/cjs/index.js"
16+
},
17+
"browser": {
18+
"import": "./build/esm/esm.browser.js"
19+
},
20+
"default": "./build/dist/okta-auth-js.umd.js"
21+
},
1222
"repository": {
1323
"type": "git",
1424
"url": "https://github.com/okta/okta-auth-js.git"
@@ -20,7 +30,7 @@
2030
"banners": "node ./scripts/maintain-banners.js",
2131
"clean": "yarn clean:build",
2232
"clean:build": "rimraf build && rimraf build2",
23-
"dev": "webpack --config webpack.dev.config.js",
33+
"dev": "NODE_ENV=development yarn build:esm --watch",
2434
"lint": "eslint .",
2535
"tsd": "tsd",
2636
"lint:report": "eslint -f checkstyle -o ./build2/reports/lint/eslint-checkstyle-result.xml .",
@@ -36,7 +46,9 @@
3646
"test:report": "yarn test --ci --silent || true",
3747
"test:samples": "yarn workspace @okta/test.e2e.samples start",
3848
"test:integration": "jest --config ./jest.integration.js",
39-
"test:bundle:esm": "NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.esm.mjs",
49+
"test:bundle:esm:browser": "cross-env BUNDLE_ENV=browser NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.esm.mjs",
50+
"test:bundle:esm:node": "cross-env BUNDLE_ENV=node NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.esm.mjs",
51+
"test:bundle:cjs": "cross-env BUNDLE_ENV=node jest --config ./jest.cjs.js",
4052
"build": "node scripts/build.js",
4153
"build:cdn": "cross-env NODE_ENV=production webpack --config webpack.cdn.config.js",
4254
"build:web": "cross-env NODE_ENV=production webpack --config webpack.config.js",
@@ -78,7 +90,7 @@
7890
"btoa": "^1.2.1",
7991
"core-js": "^3.6.5",
8092
"cross-fetch": "^3.1.5",
81-
"js-cookie": "2.2.1",
93+
"js-cookie": "^3.0.1",
8294
"jsonpath-plus": "^6.0.1",
8395
"node-cache": "^5.1.2",
8496
"p-cancelable": "^2.0.0",

0 commit comments

Comments
 (0)