Skip to content

Commit d1b0881

Browse files
authored
browser: use ESM modules (#268)
* browser: change output to esm, bundle with rollup * browser: update tests to use esm --------- Co-authored-by: Sebastian Alex <[email protected]>
1 parent 4953027 commit d1b0881

11 files changed

+176
-71
lines changed

package-lock.json

+72-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/browser/jest.config.js

-6
This file was deleted.

packages/browser/jest.config.mjs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
export default {
3+
preset: 'ts-jest/presets/default-esm',
4+
testEnvironment: 'jsdom',
5+
setupFiles: ['./jest.setup.mjs'],
6+
moduleNameMapper: {
7+
'^(\\.{1,2}/.*)\\.js$': '$1',
8+
},
9+
transform: {
10+
'^.+\\.tsx?$': [
11+
'ts-jest',
12+
{
13+
useESM: true,
14+
},
15+
],
16+
},
17+
};
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
import { jest } from '@jest/globals';
2+
global.jest = jest;
3+
14
global.BACKTRACE_AGENT_NAME = 'test';
25
global.BACKTRACE_AGENT_VERSION = 'test';

packages/browser/package.json

+15-11
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
"name": "@backtrace/browser",
33
"version": "0.3.1",
44
"description": "Backtrace-JavaScript web browser integration",
5-
"main": "lib/index.js",
5+
"main": "lib/bundle.cjs",
6+
"module": "lib/bundle.mjs",
7+
"browser": "lib/bundle.mjs",
68
"types": "lib/index.d.ts",
7-
"browser": "lib/index.js",
9+
"type": "module",
810
"scripts": {
9-
"build": "webpack",
11+
"build": "rollup -c rollup.config.mjs",
1012
"clean": "rimraf \"lib\"",
1113
"format": "prettier --write '**/*.ts'",
1214
"lint": "eslint . --ext .ts",
1315
"prepublishOnly": "cross-env NODE_ENV=production npm run build",
14-
"watch": "webpack -w",
15-
"test": "cross-env NODE_ENV=test jest"
16+
"watch": "npm run build -- --watch",
17+
"test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" NODE_NO_WARNINGS=1 NODE_ENV=test jest"
1618
},
1719
"repository": {
1820
"type": "git",
@@ -37,19 +39,21 @@
3739
"/lib"
3840
],
3941
"devDependencies": {
42+
"@backtrace/sdk-core": "^0.3.2",
4043
"@reduxjs/toolkit": "^1.9.5",
44+
"@rollup/plugin-commonjs": "^26.0.1",
45+
"@rollup/plugin-json": "^6.1.0",
46+
"@rollup/plugin-node-resolve": "^15.2.3",
47+
"@rollup/plugin-replace": "^5.0.7",
48+
"@rollup/plugin-terser": "^0.4.4",
4149
"@types/jest": "^29.5.1",
4250
"@types/ua-parser-js": "^0.7.36",
4351
"jest": "^29.5.0",
4452
"jest-environment-jsdom": "^29.5.0",
53+
"rollup": "^4.21.0",
54+
"rollup-plugin-typescript2": "^0.36.0",
4555
"ts-jest": "^29.1.0",
46-
"ts-loader": "^9.4.3",
4756
"typescript": "^5.0.4",
48-
"webpack": "^5.87.0",
49-
"webpack-cli": "^5.1.4"
50-
},
51-
"dependencies": {
52-
"@backtrace/sdk-core": "^0.4.0",
5357
"ua-parser-js": "^1.0.35"
5458
}
5559
}

packages/browser/rollup.config.mjs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import commonjs from '@rollup/plugin-commonjs';
2+
import json from '@rollup/plugin-json';
3+
import nodeResolve from '@rollup/plugin-node-resolve';
4+
import replace from '@rollup/plugin-replace';
5+
import terser from '@rollup/plugin-terser';
6+
import typescript from 'rollup-plugin-typescript2';
7+
import packageJson from './package.json' with { type: 'json' };
8+
9+
const extensions = ['.js', '.ts'];
10+
11+
/** @type {import('rollup').RollupOptions} */
12+
export default {
13+
input: './src/index.ts',
14+
output: [
15+
{
16+
file: 'lib/bundle.mjs',
17+
format: 'esm',
18+
sourcemap: true,
19+
},
20+
{
21+
file: 'lib/bundle.min.mjs',
22+
format: 'esm',
23+
sourcemap: true,
24+
plugins: [terser()],
25+
},
26+
{
27+
file: 'lib/bundle.cjs',
28+
format: 'cjs',
29+
sourcemap: true,
30+
},
31+
{
32+
file: 'lib/bundle.min.cjs',
33+
format: 'cjs',
34+
sourcemap: true,
35+
plugins: [terser()],
36+
},
37+
],
38+
plugins: [
39+
typescript({ tsconfig: './tsconfig.build.json' }),
40+
nodeResolve({ extensions, preferBuiltins: true }),
41+
commonjs({ defaultIsModuleExports: true }),
42+
replace({
43+
BACKTRACE_AGENT_NAME: packageJson.name,
44+
BACKTRACE_AGENT_VERSION: packageJson.version,
45+
preventAssignment: true,
46+
}),
47+
json(),
48+
],
49+
};
+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import type { SdkOptions } from '@backtrace/sdk-core/lib/builder/SdkOptions';
22

3-
// These variables will be set on compilation stage
4-
declare const BACKTRACE_AGENT_NAME: string;
5-
declare const BACKTRACE_AGENT_VERSION: string;
6-
73
export const AGENT: SdkOptions = {
84
langName: 'js',
95
langVersion: navigator.userAgent,
10-
agent: BACKTRACE_AGENT_NAME,
11-
agentVersion: BACKTRACE_AGENT_VERSION,
6+
agent: 'BACKTRACE_AGENT_NAME',
7+
agentVersion: 'BACKTRACE_AGENT_VERSION',
128
};

packages/browser/tests/redux/backtraceReduxMiddlewareTests.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BacktraceBreadcrumbs } from '@backtrace/sdk-core/src';
2-
import { Action, configureStore, createSlice, Middleware, PayloadAction } from '@reduxjs/toolkit';
1+
import { BacktraceBreadcrumbs } from '@backtrace/sdk-core';
2+
import reduxToolkit, { Action, Middleware, PayloadAction } from '@reduxjs/toolkit';
33
import { BacktraceClient } from '../../src/BacktraceClient';
44
import {
55
BacktraceReduxMiddlewareOptions,
@@ -27,7 +27,7 @@ const initialState: TestState = {
2727

2828
const internalError = new Error('Test internal error');
2929

30-
const testSlice = createSlice({
30+
const testSlice = reduxToolkit.createSlice({
3131
name: 'test',
3232
initialState,
3333
reducers: {
@@ -78,7 +78,7 @@ const getBreadcrumbsSpy = (method: 'info' | 'warn') => {
7878
};
7979

8080
const getStore = (options?: BacktraceReduxMiddlewareOptions | ((action: Action) => Action | undefined)) =>
81-
configureStore({
81+
reduxToolkit.configureStore({
8282
reducer: {
8383
test: testSlice.reducer,
8484
},
@@ -237,7 +237,7 @@ describe('createBacktraceReduxMiddleware', () => {
237237

238238
describe('Multiple middleware interaction', () => {
239239
const backtraceMiddleware = createBacktraceReduxMiddleware(clientBreadcrumbsEnabled);
240-
const store = configureStore({
240+
const store = reduxToolkit.configureStore({
241241
reducer: {
242242
test: testSlice.reducer,
243243
},

packages/browser/tsconfig.build.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": ["node_modules", "tests", "lib"],
4+
"compilerOptions": {
5+
"rootDir": "./src",
6+
"outDir": "./lib"
7+
}
8+
}

packages/browser/tsconfig.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4-
"rootDir": "./src",
5-
"outDir": "./lib",
4+
"target": "ES2015",
5+
"module": "ESNext",
6+
"moduleResolution": "Bundler",
67
"composite": true
78
},
8-
"exclude": ["node_modules", "tests", "lib"],
9-
"references": [
10-
{
11-
"path": "../sdk-core/tsconfig.json"
12-
}
13-
]
9+
"include": ["src", "tests"],
10+
"exclude": ["node_modules", "lib"]
1411
}

packages/browser/webpack.config.js

-28
This file was deleted.

0 commit comments

Comments
 (0)