Skip to content

Commit ef529cd

Browse files
committed
[FIX] gmail: use rollup to avoid ES6 imports
Google App Scripts do not support ES6 imports which causes issues when trying to push the typescript files directly. They recommend using rollup to package every file into a single file to get rid of imports. We introduce roll-up as a new compilation step and update the README accordingly. The setting for sourcemap is moved from tsconfig to rollup.config.js as using tsc sourcemap with rollup does not work. task-3329768
1 parent 07d8ae0 commit ef529cd

File tree

6 files changed

+116
-25
lines changed

6 files changed

+116
-25
lines changed

gmail/.claspignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ node_modules
44
node_modules/**
55
node_modules/**/.*/**
66
node_modules/**/.*
7+
8+
# ignore all files…
9+
**/**
10+
11+
# include appscript and build result
12+
!appsscript.json
13+
!build/*.js

gmail/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ bower_components
3434
.DS_Store
3535
Thumbs.db
3636

37+
# Ignore final built js file
38+
build/
39+
3740
# Ignore built ts files
3841
dist/**/*
3942

gmail/README.md

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,78 @@ and also to link your Gmail contacts to your Odoo partners, to create leads from
55
![Odoo Gmail Extension](./assets/img/readme.png)
66

77
# Development
8-
## Installation
8+
## Requirements
99
First you need npm,
1010
> apt-get install -y npm
1111
12-
We use [clasp](https://github.com/google/clasp) to push the project on the Google platform
13-
> npm install -g @google/clasp
12+
Install the dependencies
13+
> npm install
14+
15+
## Prettier
16+
You should auto-format the code using the prettier configuration,
17+
> `npx prettier --config .prettierrc 'src/**/*.ts' --write`
18+
19+
## Compiling
20+
21+
We use [rollup.js](https://github.com/rollup/rollup) to package all of the source files into a single one.
22+
This is necessary as App Scripts do not support ES6 import/export statements yet.
23+
24+
Once you have applied the necessary changes, run the following command:
25+
> npx rollup -c
26+
27+
This will simultaneously compile and package the typescript sourcecode inside `build/main.js`
28+
29+
Now all you need to do is upload the script to your account and deploy it!
30+
31+
## Uploading method 1: Manually copying the file
32+
If you do not plan on updating this script regularly, perhaps you will prefer using Google's GUI.
33+
34+
- Head to [the App Scripts manager](https://script.google.com/) and create a project
35+
- Go to the project settings and enable appscript.json editing: `Show "appsscript.json" manifest file in editor`
36+
- Copy the contents of your local `appscript.json` to the remote one in the project editor
37+
- Create a file `main.gs` and remove the existing `Code.gs` if any.
38+
- Copy the contents of your local `build/main.js` to the `main.gs` file in the project editor
1439

15-
Project dependencies
16-
> npm install --dev @types/google-apps-script
40+
## Uploading method 2: Using Clasp
41+
You may want to use the Google's CLI tool [clasp](https://github.com/google/clasp) to manage, compile and update your app script.
42+
43+
First install
44+
> npm install -g @google/clasp
1745
18-
## Clasp
1946
Login to your account to be able to push on your Gmail project,
20-
> clasp login --no-localhost
47+
> clasp login
2148
22-
Open your Gmail project
23-
> clasp open
49+
Note: the `--no-localhost` option we previously recommended was [deprecated by google](https://developers.google.com/identity/protocols/oauth2/resources/oob-migration)
2450

25-
Enable the Gmail API
26-
> clasp apis enable gmail
51+
### If you already have a project
52+
Update `.clasp.json` to use your own script id and project.
53+
If you do not have a specific project, use `Default`.
2754

28-
Automatically push the project when a changed is detected
29-
> clasp push --watch
55+
### If you do not have a project yet
56+
Remove `.clasp.json`
3057

31-
## Prettier
32-
You should auto-format the code using the prettier configuration,
33-
> `npx prettier --config .prettierrc 'src/**/*.ts' --write`
58+
Create a project
59+
> clasp create
60+
61+
For the project type, select "api".
62+
63+
### Push your project
64+
Push the project
65+
> clasp push
66+
67+
68+
# Deployment
69+
Finally, you can enable the add-on for your account.
70+
71+
Head to [the App Scripts manager](https://script.google.com/).
72+
- Select your project and click "Deploy".
73+
- For testing on your account just select "Test deployments". "Google Workspace Add-on" should be automatically selected as the type.
74+
- Click "Install" and the add-on should appear in the addons tab of Gmail.
75+
76+
You're done!
3477

35-
## Install the add-on on your Gmail account
36-
1. Share the google project to your Gmail account
37-
2. Then go to "Publish" -> "Deploy from manifest" and click on "Install the add-on"
78+
For final deployments you will need to create a Google Cloud Project with the GMail API and link it to this script.
79+
Refer to Google's documentation for more information.
3880

3981
# Documentation
4082
`GmailApp` object,

gmail/package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
2-
"dependencies": {
3-
"@types/google-apps-script": "^1.0.31"
4-
},
52
"devDependencies": {
6-
"prettier": "^2.2.1"
7-
}
3+
"@rollup/plugin-node-resolve": "^15.0.2",
4+
"@rollup/plugin-typescript": "^11.1.1",
5+
"@types/google-apps-script": "^1.0.64",
6+
"prettier": "^2.2.1",
7+
"rollup": "^3.22.0",
8+
"tslib": "^2.5.3"
9+
},
10+
"type": "module"
811
}

gmail/rollup.config.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import typescript from "@rollup/plugin-typescript";
2+
import { nodeResolve } from "@rollup/plugin-node-resolve";
3+
4+
const extensions = [".ts"];
5+
6+
/**
7+
* Prevent tree-shaking the entry-point
8+
* by not shaking any module that isn't imported by anyone.
9+
* @returns side-effects or nothing
10+
*/
11+
function preventEntrypointShakingPlugin() {
12+
return {
13+
name: "no-treeshaking",
14+
resolveId(id, importer) {
15+
if (!importer) {
16+
return { id, moduleSideEffects: "no-treeshake" };
17+
}
18+
return null;
19+
},
20+
};
21+
}
22+
23+
export default {
24+
input: "./src/main.ts",
25+
output: {
26+
dir: "./build",
27+
format: "esm",
28+
sourcemap: true,
29+
},
30+
plugins: [
31+
preventEntrypointShakingPlugin(),
32+
nodeResolve({
33+
extensions,
34+
}),
35+
typescript(),
36+
],
37+
};

gmail/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"compilerOptions": {
33
"outDir": "./build",
44
"baseUrl": ".",
5-
"sourceMap": true,
65
"strictNullChecks": false,
76
"noImplicitThis": true,
87
"noEmitOnError": true,

0 commit comments

Comments
 (0)