Skip to content

Commit 7cc1f40

Browse files
committed
Migrate all packages to use rollup
1 parent 291e3ae commit 7cc1f40

24 files changed

Lines changed: 351 additions & 11094 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ node_modules/
44
sites/*/node_modules
55
sites/*/dist
66
sites/*/package-lock.json
7+
sites/*/yarn.lock
78
packages/*/node_modules
89
packages/*/dist
910
packages/*/package-lock.json
11+
packages/*/yarn.lock
12+
package-lock.json
13+
yarn.lock

package-lock.json

Lines changed: 0 additions & 8554 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true
5+
},
6+
"extends": [
7+
"plugin:react/recommended",
8+
"standard-with-typescript",
9+
"prettier"
10+
],
11+
"overrides": [
12+
],
13+
"parserOptions": {
14+
"ecmaVersion": "latest",
15+
"sourceType": "module",
16+
"project": "./tsconfig.json"
17+
},
18+
"plugins": [
19+
"react"
20+
],
21+
"rules": {
22+
}
23+
}

packages/google-analytics-embed-react/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ pnpm-debug.log*
88
lerna-debug.log*
99

1010
node_modules
11-
dist
12-
dist-ssr
13-
*.local
11+
build
1412

1513
# Editor directories and files
1614
.vscode/*
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 100,
3+
"singleQuote": true,
4+
"trailingComma": "none"
5+
}

packages/google-analytics-embed-react/package.json

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@
22
"name": "google-analytics-embed-react",
33
"version": "0.0.1",
44
"type": "module",
5+
"main": "dist/index.js",
6+
"module": "dist/index.esm.js",
7+
"types": "dist/index.d.ts",
58
"scripts": {
6-
"dev": "vite",
7-
"build": "mkdirp dist && tsc && vite build",
8-
"pre-bundle": "cp package.json dist"
9+
"build": "rollup -c --bundleConfigAsCjs",
10+
"prettier:check": "prettier --check src/*",
11+
"prettier:fix": "prettier --write src/*",
12+
"lint:check": "eslint src/*",
13+
"lint:fix": "eslint --fix src/*"
914
},
1015
"devDependencies": {
16+
"@rollup/plugin-commonjs": "^24.0.0",
17+
"@rollup/plugin-node-resolve": "^15.0.1",
1118
"@types/google.visualization": "^0.0.68",
12-
"@types/react": "^18.0.26",
13-
"@types/react-dom": "^18.0.9",
14-
"@vitejs/plugin-react": "^3.0.0",
15-
"copy": "^0.3.2",
16-
"mkdirp": "^1.0.4",
17-
"typescript": "^4.9.3",
18-
"vite": "^4.0.0",
19-
"vite-plugin-dts": "^1.7.1"
19+
"@types/react": "*",
20+
"@types/react-dom": "*",
21+
"@typescript-eslint/eslint-plugin": "^5.48.2",
22+
"eslint": "^8.32.0",
23+
"eslint-config-prettier": "^8.6.0",
24+
"eslint-config-standard-with-typescript": "^27.0.1",
25+
"eslint-plugin-import": "^2.27.5",
26+
"eslint-plugin-n": "^15.6.1",
27+
"eslint-plugin-promise": "^6.1.1",
28+
"eslint-plugin-react": "^7.32.0",
29+
"google-analytics-embed-types": "file:../google-analytics-embed-types",
30+
"prettier": "2.8",
31+
"rollup-plugin-peer-deps-external": "^2.2.4",
32+
"rollup-plugin-typescript2": "^0.34.1",
33+
"typescript": "^4.9.4"
2034
},
2135
"peerDependencies": {
2236
"react": "*",

packages/google-analytics-embed-react/prettier.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
2+
import resolve from '@rollup/plugin-node-resolve';
3+
import commonjs from '@rollup/plugin-commonjs';
4+
import typescript from 'rollup-plugin-typescript2';
5+
6+
const packageJson = require('./package.json');
7+
8+
export default {
9+
input: 'src/index.ts',
10+
output: [
11+
{
12+
file: packageJson.main,
13+
format: 'cjs',
14+
sourcemap: true
15+
},
16+
{
17+
file: packageJson.module,
18+
format: 'esm',
19+
sourcemap: true
20+
}
21+
],
22+
plugins: [
23+
peerDepsExternal(),
24+
resolve({
25+
browser: true
26+
}),
27+
commonjs(),
28+
typescript()
29+
]
30+
};
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
1+
import * as React from 'react';
2+
import GoogleAnalyticsContext, { GoogleAnalyticsContextContent } from './GoogleAnalyticsContext';
13

2-
export default class DataChart {
4+
export type DataChartProps<T> = {
5+
query: any;
6+
} & T;
37

8+
export default class DataChart<O> extends React.Component<DataChartProps<O>> {
9+
public static contextType = GoogleAnalyticsContext;
10+
private readonly elementRef: React.RefObject<HTMLDivElement>;
11+
private googleDataChart: gapi.analytics.googleCharts.DataChart | null;
12+
private readonly chartType: string;
13+
14+
constructor(type: string, props: DataChartProps<O>) {
15+
super(props);
16+
this.elementRef = React.createRef();
17+
this.googleDataChart = null;
18+
this.chartType = type;
19+
}
20+
21+
componentDidUpdate() {
22+
const [gaState, _] = this.context as GoogleAnalyticsContextContent;
23+
const { query, ...chartOptions } = this.props;
24+
if (gaState == 'AUTH_SUCCESS') {
25+
if (this.googleDataChart) {
26+
this.googleDataChart.set({
27+
query,
28+
chart: {
29+
container: this.elementRef.current as HTMLElement,
30+
type: this.chartType as any,
31+
options: chartOptions as any
32+
}
33+
});
34+
} else {
35+
this.googleDataChart = new gapi.analytics.googleCharts.DataChart({
36+
query,
37+
chart: {
38+
container: this.elementRef.current as HTMLElement,
39+
type: this.chartType as any,
40+
options: chartOptions as any
41+
}
42+
});
43+
this.googleDataChart.execute();
44+
}
45+
} else if (this.googleDataChart) {
46+
this.googleDataChart = null;
47+
}
48+
}
49+
50+
render(): React.ReactNode {
51+
return <div ref={this.elementRef}></div>;
52+
}
453
}
Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,17 @@
1-
import { createContext } from "react";
2-
3-
interface AuthenticateUsingButton {
4-
type: "button";
5-
clientId: string;
6-
userInfoLabel?: string;
7-
scopes?: string[];
8-
overwriteDefaultScopes?: boolean;
9-
}
10-
11-
interface AuthenticateUsingServer {
12-
type: "server";
13-
access_token: string;
14-
scopes?: string[];
15-
overwriteDefaultScopes?: boolean;
16-
}
17-
18-
type AuthenticateMethod = AuthenticateUsingButton | AuthenticateUsingServer;
19-
20-
export type AuthenticationState =
21-
| "AUTHENTICATING"
22-
| "AUTH_ERROR"
23-
| "AUTH_SUCCESS"
24-
| "NOT_AUTHENTICATED";
25-
26-
export class GoogleContext {
27-
protected authButton?: string | HTMLElement = undefined;
28-
29-
setAuthButton(button: HTMLElement) {
30-
this.authButton = button;
31-
}
32-
33-
authenticate(method: AuthenticateMethod): Promise<null> {
34-
const authContainer = this.authButton;
35-
return new Promise((res, rej) => {
36-
window.gapi.analytics.auth.once("error", (res) => {
37-
rej(res);
38-
});
39-
window.gapi.analytics.auth.once("signIn", () => {
40-
res(null);
41-
});
42-
if (method.type === "server") {
43-
const rest = window.gapi.analytics.auth.authorize({
44-
scopes: method.scopes,
45-
overwriteDefaultScopes: method.overwriteDefaultScopes,
46-
serverAuth: {
47-
access_token: method.access_token,
48-
},
49-
});
50-
console.log(rest);
51-
res(null);
52-
} else {
53-
console.log(method.clientId);
54-
window.gapi.analytics.auth.authorize({
55-
container: authContainer,
56-
clientid: method.clientId,
57-
scopes: method.scopes,
58-
overwriteDefaultScopes: method.overwriteDefaultScopes,
59-
userInfoLabel: method.userInfoLabel,
60-
});
61-
}
62-
});
63-
}
64-
}
65-
66-
export const googleContext = new GoogleContext();
67-
68-
export type GoogleAnalyticsContextContent = [AuthenticationState];
69-
const GoogleAnalyticsContext =
70-
createContext<GoogleAnalyticsContextContent | null>(null);
1+
import { createContext } from 'react';
2+
3+
// INITIALIZED -> LOADING -> PROCESSING -> READY
4+
// -> AUTHENTICATING -> AUTH_ERROR/AUTH_SUCCESS
5+
export type GoogleAnalyticsState =
6+
| 'INITIALIZED'
7+
| 'LOADING'
8+
| 'PROCESSING'
9+
| 'READY'
10+
| 'AUTHENTICATING'
11+
| 'SIGNOUT'
12+
| 'AUTH_ERROR'
13+
| 'AUTH_SUCCESS';
14+
15+
export type GoogleAnalyticsContextContent = [GoogleAnalyticsState, (el: HTMLElement | null) => void];
16+
const GoogleAnalyticsContext = createContext<GoogleAnalyticsContextContent | null>(null);
7117
export default GoogleAnalyticsContext;

0 commit comments

Comments
 (0)