V3: Migrate to Vite (#221)

This is version 3 of the boilerplate, which makes significant changes to the build script and local development tooling. It replaces Webpack with Vite, which required some significant changes to how the local development setup works with Google Apps Script.

A new dev-server-wrapper.html file is used as the wrapper app in development, which is a standalone html + js file instead of a React app. This reduces the need for a complex build step for the wrapper app. Instead the file is just copied over for each entrypoing and the file location is changed with a "replace" step.

Due to the way Vite works, the html templates are changed a bit to support local development.

Vite doesn't easily allow completely separate multiple builds, so some Vite plugins and custom plugins are written to support multiple entrypoints (sidebars/dialogs). The externalization for large packages is now handled in the Vite config instead of a plugin, and by manually adding in the script tags into the index.html templates.

Additional changes here:
- yarn is now used instead of npm as package manager
- eslint configs are updated to support vite
- GitHub workflows for tests are updated to use yarn and other minor changes such as node versions and OS.
- A VS Code launch.json configuration is provided
- Dev wrapper setup has been updated (see above description)
- ES Modules (import/export) updated throughout
- Unneeded packages supporting webpack configurations are removed. Many packages have been upgraded. NPM scripts have been updated to use yarn.
- Removed `import React` due to Vite config.
- Added script tags to each template since webpack externalization plugin is no longer used
- Added <script type="module" src="./index.jsx"></script> to index.html templates to support Vite local development
- Changed src/server/sheets.js to .ts typescript as exemplar
- Updated tests to support yarn commands, Vite changes, and listening for Vite stdout triggers.
- Add Vite-style tsconfig.json
- README is updated
This commit is contained in:
Elisha Nuchi
2024-05-20 00:25:31 -04:00
committed by GitHub
parent 5f2012ce56
commit 31befe1d68
54 changed files with 8328 additions and 35322 deletions
+17 -5
View File
@@ -1,19 +1,31 @@
{
"root": true,
"parser": "@babel/eslint-parser",
"extends": ["airbnb-base", "plugin:prettier/recommended"],
"plugins": ["prettier"],
"extends": [
"airbnb-base",
"plugin:prettier/recommended",
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended"
],
"plugins": ["react-refresh", "prettier"],
"rules": {
"prettier/prettier": "error",
"camelcase": "warn",
"import/prefer-default-export": "warn",
"import/no-extraneous-dependencies": "warn",
"prefer-object-spread": "warn",
"spaced-comment":"off"
"spaced-comment": "off",
"react-refresh/only-export-components": [
"warn",
{ "allowConstantExport": true }
]
},
"parserOptions": {
"babelOptions": {
"configFile": "./dev/.babelrc"
}
}
},
"env": { "browser": true, "es2020": true },
"ignorePatterns": ["dist", ".eslintrc.json"],
"parser": "@typescript-eslint/parser"
}
+10 -10
View File
@@ -9,18 +9,18 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-11, macos-latest, windows-latest]
os: [macos-12, macos-13, windows-2022]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: [14, 16, 18]
node-version: [18, 20]
timeout-minutes: 8
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install packages [npm ci]
run: npm ci
- name: Install packages
run: yarn install
- name: Allow running mkcert on Mac
run: sudo security authorizationdb write com.apple.trust-settings.admin allow
if: runner.os == 'MacOS'
@@ -30,14 +30,14 @@ jobs:
- name: Run mkcert setup [mkcert -install]
run: mkcert -install
if: runner.os == 'MacOS'
- name: Install https cert [npm setup:https]
run: npm run setup:https
- name: Install https cert [yarn setup:https]
run: yarn setup:https
if: runner.os == 'MacOS'
- run: |
mkdir certs
.\test\generate-cert.ps1
.\scripts\generate-cert.ps1
shell: pwsh
if: runner.os == 'Windows'
- name: Run integration tests
run: npm run test:integration
run: yarn test:integration
shell: bash
@@ -1,25 +1,26 @@
name: Local integration tests - Extended Version
on:
push:
pull_request:
branches: [main]
jobs:
extended-integration-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-11, macos-latest, windows-latest]
os: [macos-12, macos-13, windows-2022]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: [14, 16, 18]
node-version: [18, 20]
timeout-minutes: 11
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install packages [npm ci]
run: npm ci
- name: Install packages
run: yarn install
- name: Allow running mkcert on Mac
run: sudo security authorizationdb write com.apple.trust-settings.admin allow
if: runner.os == 'MacOS'
@@ -29,12 +30,12 @@ jobs:
- name: Run mkcert setup [mkcert -install]
run: mkcert -install
if: runner.os == 'MacOS'
- name: Install https cert [npm setup:https]
run: npm run setup:https
- name: Install https cert [yarn setup:https]
run: yarn setup:https
if: runner.os == 'MacOS'
- run: |
mkdir certs
.\test\generate-cert.ps1
.\scripts\generate-cert.ps1
shell: pwsh
if: runner.os == 'Windows'
- name: Add .clasprc.json to home folder
@@ -65,10 +66,12 @@ jobs:
S3_BUCKET_NAME: ${{ secrets.S3_BUCKET_NAME }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
- name: Build and deploy dev setup [npm run deploy:dev]
run: npm run deploy:dev
- name: Build and deploy dev setup [yarn deploy:dev]
run: yarn deploy:dev
env:
NODE_OPTIONS: '--max_old_space_size=4096'
- name: Run integration tests
run: npm run test:integration:extended
# use ci-reporter to publish failing diff images to s3 bucket
# run: yarn test:integration:extended:ci-reporter
run: yarn test:integration:extended
shell: bash
-1
View File
@@ -1 +0,0 @@
engine-strict=true
+21
View File
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch spreadsheet with debugger",
"trace": true,
"sourceMaps": true,
"pauseForSourceMap": false,
"skipFiles": ["**/node_modules/**", "!${workspaceFolder}/**"],
"webRoot": "${workspaceFolder}/src/client",
// Need random open port for logging into spreadsheets:
// https://github.com/microsoft/vscode-js-debug/issues/918
"port": 12345,
}
]
}
+40 -52
View File
@@ -3,7 +3,7 @@
<img width="400" src="https://i.imgur.com/83Y7bWN.png" alt="React & Google Apps Script logos"></a>
</p>
<p align="center"><i>
With support for React v18 and React Fast Refresh
Built with React v18 and Vite for best-in-class frontend development.
</i></p>
<div align="center">
@@ -46,62 +46,57 @@
[Google Apps Script](https://developers.google.com/apps-script/overview) is Google's Javascript-based development platform for building applications and add-ons for Google Sheets, Docs, Forms and other Google Apps.
You can add custom [user interfaces inside dialog windows](https://developers.google.com/apps-script/guides/html), but the platform is designed for simple HTML pages built with [templates](https://developers.google.com/apps-script/guides/html/templates) and [jQuery](https://developers.google.com/apps-script/guides/html/best-practices#take_advantage_of_jquery).
However, using this repo, it's easy to run [React](https://reactjs.org/) apps inside these dialogs, and build everything from small projects to advanced add-ons that can be published on the G Suite Marketplace.
Google Apps Scripts lets you add custom [user interfaces inside dialog windows](https://developers.google.com/apps-script/guides/html). Using this template, it's easy to run [React](https://reactjs.org/) apps inside these dialogs, and build everything from small projects to advanced add-ons that can be published in the Google Workspace Marketplace.
<p align="center">
<img width="75%" src="https://i.imgur.com/BZvQ5ua.png" alt="React & Google Apps Script">
</p>
This repo is a boilerplate project that uses React and the same development tools that you use for building traditional websites, all inside Google Apps Script projects.
See below how to get started!
This repo is a boilerplate project for developing React apps with Google Apps Script projects. You can use this starter template to build your own React apps and deploy them inside Google Sheets, Docs, Forms and Slides for use in dialogs and sidebars. Sample code is provided showing how your React app can interact with the underlying Google Apps Script server-side code.
Read on to get started with your own project!
<br/>
## 🚜 Install <a name = "install"></a>
These instructions will get you set up with a copy of the React project code on your local machine. It will also get you logged in to `clasp` so you can manage script projects from the command line.
These instructions will get you set up with a copy of the React project code on your local machine. It will also get you logged in to `clasp`, which lets you manage script projects from the command line.
See [deploy](#deploy) for notes on how to deploy the project and see it live in a Google Spreadsheet.
### Prerequisites <a name = "prerequisites"></a>
- Make sure you're running at least [Node.js](https://nodejs.org/en/download/) v14 and `npm` v6.
- Make sure you're running at least [Node.js](https://nodejs.org/en/download/) v18 and [yarn (classic)](https://classic.yarnpkg.com/lang/en/docs/install/).
- You'll need to enable the Google Apps Script API. You can do that by visiting [script.google.com/home/usersettings](https://script.google.com/home/usersettings).
- To use live reload while developing, you'll need to serve your files locally using HTTPS. See [local development](#local-development) below for how to set up your local environment.
- To use live reload while developing, you'll need to serve your files locally using HTTPS. See [local development](#local-development) below for instructions on setting up your local environment.
### 🏁 Getting started <a name = "getting-started"></a>
**1.** First, let's clone the repo and install the dependencies.
Full steps to getting your local environment set up, deploying your app, and also running your app locally for local development are shown in the video below:
<!-- Add video here -->
**1.** First, let's clone the repo and install the dependencies. This project is published as a public template, so you can also fork the repo or select "Use this template" in GitHub.
```bash
git clone https://github.com/enuchi/React-Google-Apps-Script.git
cd React-Google-Apps-Script
npm install
yarn install
```
<img width="100%" src="https://i.imgur.com/EGSsCqO.gif">
**2.** Next, we'll need to log in to [clasp](https://github.com/google/clasp), which lets us manage our Google Apps Script projects locally.
```bash
npm run login
yarn run login
```
<img width="100%" src="https://i.imgur.com/zKCgkMl.gif">
**3.** Now let's run the setup script to create a New spreadsheet and script project from the command line.
```bash
npm run setup
yarn run setup
```
<img width="100%" src="https://imgur.com/Zk2eHFV.gif">
Alternatively, you can use an existing Google Spreadsheet and Script file instead of creating a new one.
<details>
@@ -134,14 +129,12 @@ Next, let's deploy the app so we can see it live in Google Spreadsheets.
Run the deploy command. You may be prompted to update your manifest file. Type 'yes'.
```bash
npm run deploy
yarn run deploy
```
The deploy command will build all necessary files using production settings, including all server code (Google Apps Script code), client code (React bundle), and config files. All bundled files will be outputted to the `dist/` folder, then pushed to the Google Apps Script project.
Now open Google Sheets and navigate to your new spreadsheet (e.g. the file "My React Project"). You can also run `npm run open`. Make sure to refresh the page if you already had it open. You will now see a new menu item appear containing your app!
<img width="100%" src="https://i.imgur.com/W7UkEpv.gif">
Now open Google Sheets and navigate to your new spreadsheet (e.g. the file "My React Project"). You can also run `yarn run open`. Make sure to refresh the page if you already had it open. You will now see a new menu item appear containing your app!
<br/>
@@ -149,8 +142,6 @@ Now open Google Sheets and navigate to your new spreadsheet (e.g. the file "My R
We can develop our client-side React apps locally, and see our changes directly inside our Google Spreadsheet dialog window.
<img width="100%" src="https://i.imgur.com/EsnOEHP.gif">
There are two steps to getting started: installing a certificate (first time only), and running the start command.
1. Generating a certificate for local development <a name = "generatingcerts"></a>
@@ -176,24 +167,18 @@ There are two steps to getting started: installing a certificate (first time onl
Create the certs in your repo:
```
npm run setup:https
yarn run setup:https
```
2. Now you're ready to start:
```bash
npm run start
yarn run start
```
The start command will create and deploy a development build, and serve your local files.
<img width="100%" src="https://imgur.com/uD4uZZK.gif">
After running the start command, navigate to your spreadsheet and open one of the menu items. It should now be serving your local files. When you make and save changes to your React app, your app will reload instantly within the Google Spreadsheet, and have access to any server-side functions!
<img width="100%" src="https://i.imgur.com/EsnOEHP.gif">
Support for [Fast Refresh](https://github.com/pmmmwh/react-refresh-webpack-plugin) now means that only modified components are refreshed when files are changed, and state is not lost.
<br/>
### 🔍 Using React DevTools <a name="dev-tools"></a>
@@ -210,14 +195,14 @@ You will need to use the "standalone" version of React DevTools since our React
1. In your repo install the React DevTools package as a dev dependency:
```bash
npm install -D react-devtools
yarn add -D react-devtools
```
2. In a new terminal window run `npx react-devtools` to launch the DevTools standalone app.
3. Add `<script src="http://localhost:8097"></script>` to the top of your `<head>` in your React app, e.g. in the [index.html](https://github.com/enuchi/React-Google-Apps-Script/blob/e73e51e56e99903885ef8dd5525986f99038d8bf/src/client/dialog-demo-bootstrap/index.html) file in the sample Bootstrap app.
4. Deploy your app (`npm run deploy:dev`) and you should see DevTools tool running and displaying your app hierarchy.
4. Deploy your app (`yarn run deploy:dev`) and you should see DevTools tool running and displaying your app hierarchy.
<img width="100%" src="https://user-images.githubusercontent.com/31550519/110273600-ee9eae80-7f9a-11eb-9796-31353b47dfa8.gif">
@@ -238,7 +223,7 @@ The included sample app has five menu items that demonstrate how to load pages i
- `Sheet Editor` - This is a basic app that opens in a dialog window that demonstrates how to select, create and delete sheets in a Google Sheets documents through server calls. It uses vanilla React with no component library.
- `Sheet Editor (Boostrap)` - The same basic app is included but styled with the Bootstrap library using [`react-bootstrap`](https://react-bootstrap.github.io/). The bootstrap example also contains an example of a page built with typescript (see below).
- `Sheet Editor (MUI)` - A similar example using [`Material UI`](https://mui.com/).
- `Sheet Editor (Tailwind CSS)` - Another example, using [`Tailwind`](https://tailwindcss.com/)
- `Sheet Editor (Tailwind CSS)` - Another example, using [`Tailwind CSS`](https://tailwindcss.com/)
- `About me` - This is just a simple page that demonstrates the use of a sidebar dialog.
Access the dialogs through the new menu item that appears. You may need to refresh the spreadsheet and approve the app's permissions the first time you use it.
@@ -248,13 +233,13 @@ Note that if you are choosing to use one framework, for example `Tailwind`, for
<details>
<summary>Here are some steps to take to clean up the repo if you are only using a single library</summary>
1. Uninstall unneeded dependencies (`npm uninstall react-bootstrap ...` etc.)
1. Uninstall unneeded dependencies (`yarn remove react-bootstrap ...` etc.)
2. Remove the unneeded menu bar items from the server code.
3. Remove the unneeded client code.
4. Update the `clientEntrypoints` in the [webpack config file](./webpack.config.js) to only target the relevant apps.
4. Update the `clientEntrypoints` in the [vite config file](./vite.config.ts) to only target the relevant apps.
<br/>
@@ -262,29 +247,34 @@ Note that if you are choosing to use one framework, for example `Tailwind`, for
</br>
### [New!] Typescript
### Typescript
This project now supports typescript!
This project is built mainly with typescript but also supports Javascript, and examples of both are included here, both in server-side and client-side (React) code. The included sample app has a typescript example using the Bootstrap component library.
To use, simply use a typescript extension in either the client code (.ts/.tsx) or the server code (.ts), and your typescript file will compile to the proper format.
To use typescript, simply use a typescript extension in either the client code (.ts/.tsx) or the server code (.ts), and your typescript file will compile to the proper format.
For client-side code, see [FormInput.tsx in the Bootstrap demo](./src/client/dialog-demo-bootstrap/components/FormInput.tsx) for an example file. Note that it is okay to have a mix of javascript and typescript, as seen in the Bootstrap demo.
To use typescript in server code, just change the file extension to .ts. The server-side code already utilizes type definitions for Google Apps Script APIs.
A basic typescript configuration is used here, because after code is transpiled from typescript to javascript it is once again transpiled to code that is compatible with Google Apps Script. However, if you want more control over your setup you can modify the included [tsconfig.json file](./tsconfig.json).
A basic typescript configuration is used here that correctly transpiles to code that is compatible with Google Apps Script. However, if you want more control over your setup you can modify the included [tsconfig.json file](./tsconfig.json).
### Adding packages
You can add packages to your client-side React app.
For instance, install `react-transition-group` from npm:
For instance, install `react-transition-group`:
```bash
npm install react-transition-group
yarn add react-transition-group
```
Important: Since Google Apps Scripts projects don't let you easily reference external files, this project will bundle an entire app into one HTML file. This can result in large files if you are importing large packages. To help reduce the size of these large HTML files, you can try to externalize packages by using a CDN to load packages. For packages that can be loaded through a CDN (usually they will have a UMD build), you can configure the CDN details here in the [webpack config file](./webpack.config.js#L187). If set up properly, this will add a script tag that will load packages from a CDN, reducing your overall bundle size.
Important: Since Google Apps Scripts projects don't let you easily reference external files, this project will bundle an entire app into one HTML file. If you are importing large libraries this can result in a large file. To help reduce the size of these large HTML files, you can try to externalize packages by using a CDN to load packages. For packages that can be loaded through a CDN (usually they will have a UMD build), you can configure the externals and globals details in the [vite config file](./vite.config.ts). You will also need to include a script element in the head of the `index.html` file, loading the library from a CDN, and making sure it supports a UMD build, e.g.
`<script crossorigin src="https://unpkg.com/react-transition-group@4.4.2/dist/react-transition-group.min.js"></script>`.
If set up properly, this will load packages from the CDN in production and will reduce your overall bundle size.
Make sure that you update the script tag with the same version of the package you are installing with yarn, so that you are using the same version in development and production.
### Styles
@@ -296,8 +286,6 @@ import './styles.css';
Many external component libraries require a css stylesheet in order to work properly. You can import stylesheets in the HTML template, [as shown here with the Bootstrap stylesheet](./src/client/dialog-demo-bootstrap/index.html).
The webpack.config.js file can also be modified to support scss and other style libraries.
### Modifying scopes
The included app only requires access to Google Spreadsheets and to loading dialog windows. If you make changes to the app's requirements, for instance, if you modify this project to work with Google Forms or Docs, make sure to edit the oauthScopes in the [appscript.json file](./appsscript.json).
@@ -309,13 +297,13 @@ See https://developers.google.com/apps-script/manifest for information on the `a
This project uses the [gas-client](https://github.com/enuchi/gas-client) package to more easily call server-side functions using promises.
```js
// Google's documentation wants you to do this. Boo.
// Google's client-side google.script.run utility requires calling server-side functions like this:
google.script.run
.withSuccessHandler((response) => doSomething(response))
.withFailureHandler((err) => handleError(err))
.addSheet(sheetTitle);
// Poof! With a little magic we can now do this:
// Using gas-client we can use more familiar promises style like this:
import Server from 'gas-client';
const { serverFunctions } = new Server();
@@ -325,7 +313,7 @@ serverFunctions
.then((response) => doSomething(response))
.catch((err) => handleError(err));
// Or we can equally use async/await style:
// Or with async/await:
async () => {
try {
const response = await serverFunctions.addSheet(sheetTitle);
@@ -336,7 +324,7 @@ async () => {
};
```
In development, `gas-client` will interact with [the custom Webpack Dev Server package](https://github.com/enuchi/Google-Apps-Script-Webpack-Dev-Server) which allows us to run our app within the dialog window and still interact with Google Apps Script functions.
In development, `gas-client` will allow you to call server-side functions from your local environment. In production, it will use Google's underlying `google.script.run` utility.
### Autocomplete
-9
View File
@@ -1,9 +0,0 @@
{
"presets": [
"@babel/react"
],
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties"
]
}
-44
View File
@@ -1,44 +0,0 @@
{
"root": true,
"parser": "@babel/eslint-parser",
"extends": [
"airbnb-base",
"plugin:prettier/recommended",
"plugin:react/recommended"
],
"plugins": ["babel", "react", "prettier"],
"env": {
"browser": true,
"es6": true
},
"globals": {
"google": false,
"alert": false,
"css": true
},
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"prettier/prettier": "error",
"react/prop-types": "warn",
"camelcase": "warn",
"import/prefer-default-export": "warn",
"import/no-extraneous-dependencies": "warn",
"prefer-object-spread": "warn"
},
"settings": {
"react": {
"version": "detect"
},
"import/resolver": {
"node": {
"extensions": [".js", ".jsx"]
}
}
}
}
-15
View File
@@ -1,15 +0,0 @@
# Development App Wrapper
This directory contains the app needed to run development mode.
It utilizes special Webpack configurations as well as two packages, [gas-client](https://github.com/enuchi/gas-client) and [Webpack Dev Server for Google Apps Script](https://github.com/enuchi/Google-Apps-Script-Webpack-Dev-Server), in order to achieve hot reloading inside of a dialog window.
## How it works
Running `npm run start` will build and deploy the development app, and then serve files locally.
The simple React app in this directory, found at [index.js](./index.js), is designed to only be used with development builds. It loads an iframe with the source pointing to `https://localhost:${PORT}/gas/${FILENAME}-impl.html`. During the build step, we will replace `FILENAME` with the appropriate name of the HTML file to load.
As an example, during the development build, the file `dialog-demo-bootstrap.html` will be generated from the app in this directory, using `dialog-demo-bootstrap` as the `FILENAME` for the iframe source of this page. Opening the menu items in the Google Spreadsheet will load this app, and webpack's devServer settings will serve `https://localhost:3000/gas/dialog-demo-bootstrap-impl.html` within the iframe, using the customized Webpack Dev Server build.
The customized Google Apps Script Webpack Dev Server acts very similarly to Webpack Dev Server's iframe mode, but is able to pass requests to Google Apps Script server functions back and forth between all the iframes being used in development.
+82
View File
@@ -0,0 +1,82 @@
<!--
This is a development server page that serves as a wrapper for Google Apps Script (GAS) client-side development.
It is meant to be run inside a Google Sheets/Docs/Forms dialog window during local development.
It loads the gas-client library (as an external), sets up an iframe that points to a local development
server (such as running with vite), and establishes a communication bridge between the GAS server functions and the local development server.
This allows for local development and testing of client-side code while still being able to interact with
the GAS server-side functions.
Two placeholders are used in this file that will need to be replaced in a build step:
- _ _PORT_ _: The port number of the local development server. (e.g. 3000)
- _ _FILE_NAME_ _: The name of the file being loaded. (e.g. dialog-demo-bootstrap/index.html)
-->
<!DOCTYPE html>
<html>
<head>
<base target="_top" />
<title>Dev Server</title>
<!-- Load gas-client as external. Exposed global variable is GASClient. -->
<script src="https://unpkg.com/gas-client@1.1.1/dist/index.js"></script>
<style>
body,
html {
margin: 0;
width: 100%;
height: 100%;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
// These values need to be replaced during the build process
const PORT = '__PORT__';
const FILE_NAME = '__FILE_NAME__';
const iframe = document.getElementById('iframe');
iframe.src = 'https://localhost:' + PORT + '/' + FILE_NAME;
const { serverFunctions } = new window.GASClient.GASClient({
allowedDevelopmentDomains: (origin) =>
/https:\/\/.*\.googleusercontent\.com$/.test(origin),
});
const handleRequest = (event) => {
const request = event.data;
const { type, functionName, id, args } = request;
if (type !== 'REQUEST') return;
serverFunctions[functionName](...args)
.then((response) => {
iframe.contentWindow.postMessage(
{ type: 'RESPONSE', id, status: 'SUCCESS', response },
'https://localhost:' + PORT
);
})
.catch((err) => {
iframe.contentWindow.postMessage(
{
type: 'RESPONSE',
id,
status: 'ERROR',
response: err,
},
'https://localhost:' + PORT
);
});
};
window.addEventListener('message', handleRequest, false);
});
</script>
</head>
<body>
<div style="width: 100%; height: 100%">
<iframe
id="iframe"
style="width: 100%; height: 100%; border: 0; position: absolute"
></iframe>
</div>
</body>
</html>
-19
View File
@@ -1,19 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<base target="_top" />
<!-- Add any external scripts and stylesheets here -->
<style>
body,
html {
margin: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<section id="index" />
<!-- bundled js and css will get inlined here during build -->
</body>
</html>
-63
View File
@@ -1,63 +0,0 @@
import React, { useEffect } from 'react';
import { createRoot } from 'react-dom/client';
import { serverFunctions } from '../src/client/utils/serverFunctions.ts';
const { FILENAME, PORT } = process.env;
const DevServer = () => {
const iframe = React.useRef(null);
useEffect(() => {
const handleRequest = (event) => {
const request = event.data;
const { type, functionName, id, args } = request;
if (type !== 'REQUEST') return;
serverFunctions[functionName](...args)
.then((response) => {
iframe.current.contentWindow.postMessage(
{ type: 'RESPONSE', id, status: 'SUCCESS', response },
`https://localhost:${PORT}`
);
})
.catch((err) => {
iframe.current.contentWindow.postMessage(
{
type: 'RESPONSE',
id,
status: 'ERROR',
response: err,
},
`https://localhost:${PORT}`
);
});
};
window.addEventListener('message', handleRequest, false);
}, []);
return (
<div
// we want our dev environment to fill the dialog window
style={{
width: '100%',
height: '100%',
}}
>
<iframe
style={{
width: '100%',
height: '100%',
border: '0',
position: 'absolute',
}}
ref={iframe}
src={`https://localhost:${PORT}/${FILENAME}-impl.html`}
/>
</div>
);
};
const container = document.getElementById('index');
const root = createRoot(container);
root.render(<DevServer />);
+1 -1
View File
@@ -1,4 +1,4 @@
module.exports = {
export default {
globalSetup: './test/global-setup.js',
globalTeardown: './test/global-teardown.js',
testEnvironment: './test/puppeteer-environment.js',
-34512
View File
File diff suppressed because it is too large Load Diff
+34 -57
View File
@@ -1,24 +1,27 @@
{
"name": "react-google-apps-script",
"version": "2.1.1",
"version": "3.0.0",
"type": "module",
"description": "Starter project for using React with Google Apps Script",
"repository": {
"type": "git",
"url": "https://github.com/enuchi/React-Google-Apps-Script.git"
},
"scripts": {
"dev": "vite",
"test:integration": "jest --forceExit test/local-development.test",
"test:integration:extended": "cross-env IS_EXTENDED=true jest --forceExit test/local-development.test",
"test:integration:extended:ci-reporter": "cross-env IS_EXTENDED=true jest --forceExit || node test/utils/image-reporter-standalone.js",
"login": "clasp login",
"setup": "rimraf .clasp.json && mkdirp dist && clasp create --type sheets --title \"My React Project\" --rootDir ./dist && mv ./dist/.clasp.json ./.clasp.json && rimraf dist",
"open": "clasp open --addon",
"push": "clasp push",
"setup:https": "mkdirp certs && mkcert -key-file ./certs/key.pem -cert-file ./certs/cert.pem localhost 127.0.0.1",
"build:dev": "cross-env NODE_ENV=development webpack",
"build": "cross-env NODE_ENV=production webpack",
"deploy:dev": "rimraf dist && npm run build:dev && npx clasp push",
"deploy": "rimraf dist && npm run build && npx clasp push",
"serve": "cross-env NODE_ENV=development webpack serve",
"start": "npm run deploy:dev && npm run serve"
"build:dev": "tsc && vite build --mode development",
"build": "tsc && vite build --mode production",
"deploy:dev": "yarn build:dev && yarn push",
"deploy": "yarn build && yarn push",
"start": "yarn deploy:dev && yarn dev"
},
"keywords": [
"react",
@@ -37,79 +40,53 @@
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@mui/material": "^5.11.11",
"gas-client": "^1.1.1",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.2.0",
"react-transition-group": "^4.4.2",
"tailwindcss": "^3.1.6"
"react-transition-group": "^4.4.2"
},
"devDependencies": {
"@babel/cli": "^7.17.6",
"@babel/core": "^7.17.8",
"@babel/eslint-parser": "^7.17.0",
"@babel/plugin-proposal-class-properties": "^7.17.12",
"@babel/plugin-proposal-object-rest-spread": "^7.18.0",
"@babel/plugin-proposal-optional-chaining": "^7.17.12",
"@babel/plugin-transform-object-assign": "^7.16.7",
"@babel/polyfill": "^7.12.1",
"@babel/preset-env": "^7.18.2",
"@babel/preset-react": "^7.17.12",
"@effortlessmotion/dynamic-cdn-webpack-plugin": "^5.0.1",
"@effortlessmotion/html-webpack-inline-source-plugin": "^1.0.3",
"@google/clasp": "^2.4.1",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
"@babel/core": "^7.24.4",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/preset-env": "^7.24.4",
"@google/clasp": "^2.4.2",
"@types/expect-puppeteer": "^5.0.0",
"@types/jest-environment-puppeteer": "^5.0.2",
"@types/node": "^20.11.30",
"@types/puppeteer": "^5.4.6",
"@types/react": "^18.0.14",
"autoprefixer": "10.4.5",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"autoprefixer": "^10.4.19",
"aws-sdk": "^2.1106.0",
"babel-loader": "^8.2.5",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-transform-es3-member-expression-literals": "^6.22.0",
"babel-plugin-transform-es3-property-literals": "^6.22.0",
"copy-webpack-plugin": "^11.0.0",
"cross-env": "^7.0.3",
"css-loader": "^6.7.1",
"dotenv": "^16.0.1",
"eslint": "^8.17.0",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-googleappsscript": "^1.0.4",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.30.0",
"eslint-plugin-standard": "^5.0.0",
"gas-client": "^1.1.1",
"gas-lib": "^2.0.4",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"gas-types-detailed": "^1.1.2",
"gas-webpack-plugin": "^2.2.2",
"html-webpack-plugin": "^5.5.0",
"jest": "^28.1.1",
"jest-environment-node": "^28.1.1",
"jest-image-snapshot": "^5.1.0",
"mkdirp": "^1.0.4",
"module-to-cdn": "^3.1.5",
"postcss-loader": "^7.0.1",
"postcss-preset-env": "^7.7.2",
"postcss": "^8.4.38",
"postcss-preset-env": "^9.5.4",
"prettier": "^2.7.0",
"puppeteer": "^14.3.0",
"puppeteer-extra": "^3.2.3",
"puppeteer-extra-plugin-stealth": "^2.9.0",
"react-refresh": "^0.14.0",
"rimraf": "^3.0.2",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.3.3",
"ts-loader": "^9.3.0",
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.9.2"
"tailwindcss": "^3.4.3",
"typescript": "^5.2.2",
"vite": "^5.2.0",
"vite-plugin-singlefile": "^2.0.1",
"vite-plugin-static-copy": "^1.0.1"
}
}
+6
View File
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
-6
View File
@@ -1,6 +0,0 @@
const tailwindcss = require('tailwindcss');
const autoprefixer = require('autoprefixer');
module.exports = {
plugins: ['postcss-preset-env', tailwindcss, autoprefixer],
};
@@ -1,4 +1,4 @@
import React, { useState, ChangeEvent, FormEvent } from 'react';
import { useState, ChangeEvent, FormEvent } from 'react';
import { Form, Button, Col, Row } from 'react-bootstrap';
interface FormInputProps {
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import { useState, useEffect } from 'react';
import { TransitionGroup, CSSTransition } from 'react-transition-group';
import { Button, ListGroup } from 'react-bootstrap';
import FormInput from './FormInput';
@@ -3,6 +3,30 @@
<head>
<base target="_top" />
<!-- Add any external scripts and stylesheets here -->
<script
crossorigin
src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-transition-group@4.4.2/dist/react-transition-group.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-bootstrap@2.4.0/dist/react-bootstrap.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/gas-client@1.1.1/dist/index.js"
></script>
<script
crossorigin
src="https://unpkg.com/@types/react@18.2.66/index.d.ts"
></script>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
@@ -18,6 +42,7 @@
</head>
<body>
<section id="index">
<script type="module" src="./index.jsx"></script>
<!-- bundled js and css will get inlined here during build-->
</section>
</body>
@@ -1,4 +1,3 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import SheetEditor from './components/SheetEditor';
@@ -1,4 +1,4 @@
import React, { useState, ChangeEvent, FormEvent } from 'react';
import { useState, ChangeEvent, FormEvent } from 'react';
import { Button, Grid, TextField, Typography } from '@mui/material';
interface FormInputProps {
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import { useState, useEffect } from 'react';
import { Button, Typography } from '@mui/material';
import FormInput from './FormInput';
+21
View File
@@ -3,9 +3,30 @@
<head>
<base target="_top" />
<!-- Add any external scripts and stylesheets here -->
<script
crossorigin
src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/@mui/material@5.11.11/umd/material-ui.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/gas-client@1.1.1/dist/index.js"
></script>
<script
crossorigin
src="https://unpkg.com/@types/react@18.2.66/index.d.ts"
></script>
</head>
<body>
<section id="index">
<script type="module" src="./index.jsx"></script>
<!-- bundled js and css will get inlined here during build-->
</section>
</body>
@@ -1,4 +1,3 @@
import React from 'react';
import ReactDOM from 'react-dom';
import SheetEditor from './components/SheetEditor';
@@ -1,12 +1,13 @@
import React, { useState } from 'react';
import { useState, ChangeEvent, FormEvent } from 'react';
import PropTypes from 'prop-types';
const FormInput = ({ submitNewSheet }) => {
const FormInput = ({ submitNewSheet }: { submitNewSheet: Function }) => {
const [inputValue, setInputValue] = useState('');
const handleChange = (event) => setInputValue(event.target.value);
const handleChange = (event: ChangeEvent<HTMLInputElement>) =>
setInputValue(event.target.value);
const handleSubmit = (event) => {
const handleSubmit = (event: FormEvent) => {
event.preventDefault();
if (inputValue.length === 0) return;
@@ -18,16 +19,18 @@ const FormInput = ({ submitNewSheet }) => {
<form className="flex w-full mx-auto items-center" onSubmit={handleSubmit}>
<div className="grow pr-2 py-1">
<input
className="w-full bg-gray-100 bg-opacity-50 rounded border border-gray-300 focus:border-indigo-500 focus:bg-transparent focus:ring-2 focus:ring-indigo-200 text-base outline-none text-gray-700 py-1 transition-colors duration-200 ease-in-out"
onChange={handleChange}
value={inputValue}
placeholder="New sheet name"
className="w-full bg-gray-100 bg-opacity-50 rounded border border-gray-300 focus:border-indigo-500 focus:bg-transparent focus:ring-2 focus:ring-indigo-200 text-base outline-none text-gray-700 py-1 transition-colors duration-200 ease-in-out"
onChange={handleChange}
value={inputValue}
placeholder="New sheet name"
/>
</div>
<button
className="text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded"
type="submit"
>Add Sheet</button>
>
Add Sheet
</button>
</form>
);
};
@@ -1,4 +1,3 @@
import React from 'react';
import PropTypes from 'prop-types';
const SheetButton = ({ sheetDetails, deleteSheet, setActiveSheet }) => {
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import { useState, useEffect } from 'react';
import { TransitionGroup, CSSTransition } from 'react-transition-group';
import FormInput from './FormInput';
import SheetButton from './SheetButton';
@@ -3,6 +3,26 @@
<head>
<base target="_top" />
<!-- Add any external scripts and stylesheets here -->
<script
crossorigin
src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-transition-group@4.4.2/dist/react-transition-group.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/gas-client@1.1.1/dist/index.js"
></script>
<script
crossorigin
src="https://unpkg.com/@types/react@18.2.66/index.d.ts"
></script>
<link
href="https://fonts.googleapis.com/css2?family=Nunito"
rel="stylesheet"
@@ -10,6 +30,7 @@
</head>
<body>
<section id="index">
<script type="module" src="./index.jsx"></script>
<!-- bundled js and css will get inlined here during build -->
</section>
</body>
@@ -1,4 +1,3 @@
import React from 'react';
import ReactDOM from 'react-dom';
import SheetEditor from './components/SheetEditor';
import './styles.css';
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import { useState } from 'react';
import PropTypes from 'prop-types';
const FormInput = ({ submitNewSheet }) => {
@@ -1,4 +1,3 @@
import React from 'react';
import PropTypes from 'prop-types';
const SheetButton = ({ sheetDetails, deleteSheet, setActiveSheet }) => {
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import { useState, useEffect } from 'react';
import { TransitionGroup, CSSTransition } from 'react-transition-group';
import FormInput from './FormInput';
import SheetButton from './SheetButton';
+21
View File
@@ -3,6 +3,26 @@
<head>
<base target="_top" />
<!-- Add any external scripts and stylesheets here -->
<script
crossorigin
src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-transition-group@4.4.2/dist/react-transition-group.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/gas-client@1.1.1/dist/index.js"
></script>
<script
crossorigin
src="https://unpkg.com/@types/react@18.2.66/index.d.ts"
></script>
<link
href="https://fonts.googleapis.com/css2?family=Nunito"
rel="stylesheet"
@@ -10,6 +30,7 @@
</head>
<body>
<section id="index">
<script type="module" src="./index.jsx"></script>
<!-- bundled js and css will get inlined here during build -->
</section>
</body>
@@ -1,4 +1,3 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import SheetEditor from './components/SheetEditor';
@@ -1,5 +1,3 @@
import React from 'react';
const About = () => (
<div>
<p>
+9 -1
View File
@@ -3,10 +3,18 @@
<head>
<base target="_top" />
<!-- Add any external scripts and stylesheets here -->
<script
crossorigin
src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"
></script>
</head>
<body>
<section id="index">
<!-- bundled js and css will get inlined here during build -->
<script type="module" src="./index.jsx"></script>
</section>
</body>
</html>
@@ -1,4 +1,3 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import About from './components/About';
@@ -14,18 +14,18 @@ export const getSheetsData = () => {
});
};
export const addSheet = (sheetTitle) => {
export const addSheet = (sheetTitle: string) => {
SpreadsheetApp.getActive().insertSheet(sheetTitle);
return getSheetsData();
};
export const deleteSheet = (sheetIndex) => {
export const deleteSheet = (sheetIndex: number) => {
const sheets = getSheets();
SpreadsheetApp.getActive().deleteSheet(sheets[sheetIndex]);
return getSheetsData();
};
export const setActiveSheet = (sheetName) => {
export const setActiveSheet = (sheetName: string) => {
SpreadsheetApp.getActive().getSheetByName(sheetName).activate();
return getSheetsData();
};
+1 -4
View File
@@ -1,6 +1,3 @@
{
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties"
]
"presets": ["@babel/preset-env"]
}
+10 -9
View File
@@ -2,18 +2,19 @@
// jestjs.io/docs/puppeteer#custom-example-without-jest-puppeteer-preset
// This allows using stealth mode.
const { mkdir, writeFile } = require('fs').promises;
const os = require('os');
const path = require('path');
const puppeteer = require('puppeteer-extra');
import fs from 'fs';
const fsPromises = fs.promises;
import os from 'os';
import path from 'path';
import puppeteer from 'puppeteer-extra';
// add stealth plugin and use defaults (all evasion techniques)
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
const jestPuppeteerConfig = require('./jest-puppeteer.config');
import jestPuppeteerConfig from './jest-puppeteer.config.js';
module.exports = async function globalSetup() {
export default async function globalSetup() {
puppeteer.use(StealthPlugin());
const browser = await puppeteer.launch(jestPuppeteerConfig.launch);
// store the browser instance so we can teardown it later
@@ -21,6 +22,6 @@ module.exports = async function globalSetup() {
global.__BROWSER_GLOBAL__ = browser;
// use the file system to expose the wsEndpoint for TestEnvironments
await mkdir(DIR, { recursive: true });
await writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
await fsPromises.mkdir(DIR, { recursive: true });
await fsPromises.writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
};
+6 -5
View File
@@ -2,15 +2,16 @@
// jestjs.io/docs/puppeteer#custom-example-without-jest-puppeteer-preset
// This allows using stealth mode.
const fs = require('fs').promises;
const os = require('os');
const path = require('path');
import fs from 'fs';
const fsPromises = fs.promises;
import os from 'os';
import path from 'path';
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
module.exports = async function globalTeardown() {
export default async function globalTeardown() {
// close the browser instance
await global.__BROWSER_GLOBAL__.close();
// clean-up the wsEndpoint file
await fs.rmdir(DIR, { recursive: true, force: true });
await fsPromises.rmdir(DIR, { recursive: true, force: true });
};
+1 -1
View File
@@ -1,4 +1,4 @@
module.exports = {
export default {
launch: {
headless: false,
product: 'chrome',
+11 -10
View File
@@ -1,10 +1,11 @@
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const { configureToMatchImageSnapshot } = require('jest-image-snapshot');
const { openAddon } = require('./utils/open-addon');
import fs from 'fs';
import path from 'path';
import { exec } from 'child_process';
import { configureToMatchImageSnapshot } from 'jest-image-snapshot';
import { openAddon } from './utils/open-addon';
require('dotenv').config();
import dotenv from 'dotenv';
dotenv.config();
const isExtended = `${process.env.IS_EXTENDED}` === 'true';
@@ -26,10 +27,10 @@ const srcTestFile = path.join(
);
const webpackDevServerReady = async (process) => {
console.log('Waiting for Webpack Dev Server to finish loading...');
console.log('Waiting for vite to serve...');
return new Promise((resolve) => {
process.stdout.on('data', (data) => {
if (data.includes('CLIENT - Dialog Demo Bootstrap')) {
if (data.includes('ready in')) {
resolve();
}
});
@@ -42,7 +43,7 @@ describe(`Local setup ${isExtended ? '*extended*' : ''}`, () => {
const containerSelector = isExtended ? '.script-app-dialog' : 'body';
beforeAll(async () => {
process = exec('npm run serve');
process = exec('yarn dev');
page = await global.__BROWSER_GLOBAL__.newPage();
await page.setViewport({
@@ -56,7 +57,7 @@ describe(`Local setup ${isExtended ? '*extended*' : ''}`, () => {
if (isExtended) {
await openAddon(page);
} else {
await page.goto('https://localhost:3000/dialog-demo-bootstrap-impl.html');
await page.goto('https://localhost:3000/dialog-demo-bootstrap/index.html');
await page.waitForTimeout(3000);
}
});
+6 -8
View File
@@ -2,15 +2,15 @@
// jestjs.io/docs/puppeteer#custom-example-without-jest-puppeteer-preset
// This allows using stealth mode.
const { readFile } = require('fs').promises;
const os = require('os');
const path = require('path');
const puppeteer = require('puppeteer');
const NodeEnvironment = require('jest-environment-node').default;
import { readFile } from 'fs/promises';
import os from 'os';
import path from 'path';
import puppeteer from 'puppeteer';
import NodeEnvironment from 'jest-environment-node';
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
class PuppeteerEnvironment extends NodeEnvironment {
export default class PuppeteerEnvironment extends NodeEnvironment.default {
constructor(config) {
super(config);
}
@@ -37,5 +37,3 @@ class PuppeteerEnvironment extends NodeEnvironment {
return super.getVmContext();
}
}
module.exports = PuppeteerEnvironment;
+7 -5
View File
@@ -10,13 +10,15 @@
*
* Note: If image reporter doesn't work in pipeline can try running as standalone script
* by creating a separate script file and running like this:
* "test:integration": "jest test/local-development.test || node test/utils/image-reporter-standalone.js"
* "test:integration:extended:report": "cross-env IS_EXTENDED=true jest --forceExit test/local-development.test || node test/utils/image-reporter-standalone.js",
*/
const fs = require('fs');
const AWS = require('aws-sdk/global');
const S3 = require('aws-sdk/clients/s3'); // this is needed
require('dotenv').config();
import fs from 'fs';
import AWS from 'aws-sdk/global.js';
import S3 from 'aws-sdk/clients/s3.js'; // this is needed
import dotenv from 'dotenv';
dotenv.config();
const UPLOAD_BUCKET = process.env.S3_BUCKET_NAME;
+7 -7
View File
@@ -13,10 +13,12 @@
* "test:integration": "jest test/local-development.test || node test/utils/image-reporter-standalone.js"
*/
const fs = require('fs');
const AWS = require('aws-sdk/global');
const S3 = require('aws-sdk/clients/s3'); // this is needed
require('dotenv').config();
import fs from 'fs';
import AWS from 'aws-sdk/global.js';
import S3 from 'aws-sdk/clients/s3.js'; // this is needed
import dotenv from "dotenv";
dotenv.config();
const UPLOAD_BUCKET = process.env.S3_BUCKET_NAME;
@@ -27,7 +29,7 @@ AWS.config.update({
const s3 = new AWS.S3({ apiVersion: '2006-03-01' });
class ImageReporter {
export default class ImageReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
@@ -79,5 +81,3 @@ class ImageReporter {
}
}
}
module.exports = ImageReporter;
+1 -3
View File
@@ -1,4 +1,4 @@
const openAddon = async (page) => {
export const openAddon = async (page) => {
await page.goto(process.env.SHEET_URL);
await page.click('a:nth-child(2)'); // click on signin button
@@ -104,5 +104,3 @@ const openAddon = async (page) => {
await page.waitForTimeout(3000);
};
module.exports = { openAddon };
+23 -3
View File
@@ -1,7 +1,27 @@
{
"compilerOptions": {
"target": "ES2019",
"types": ["gas-types-detailed"],
"jsx": "react",
"esModuleInterop": true
}
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"allowJs": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.vite.json" }]
}
+11
View File
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["vite.config.ts"]
}
+210
View File
@@ -0,0 +1,210 @@
import { resolve } from 'path';
import { BuildOptions, ServerOptions, build, defineConfig } from 'vite';
import { existsSync, readFileSync } from 'fs';
import react from '@vitejs/plugin-react-swc';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { viteSingleFile } from 'vite-plugin-singlefile';
import { writeFile } from 'fs/promises';
const PORT = 3000;
const clientRoot = './src/client';
const outDir = './dist';
const serverEntry = 'src/server/index.ts';
const copyAppscriptEntry = './appsscript.json';
const devServerWrapper = './dev/dev-server-wrapper.html';
const clientEntrypoints = [
{
name: 'CLIENT - Dialog Demo',
filename: 'dialog-demo', // we'll add the .html suffix to these
template: 'dialog-demo/index.html',
},
{
name: 'CLIENT - Dialog Demo Bootstrap',
filename: 'dialog-demo-bootstrap',
template: 'dialog-demo-bootstrap/index.html',
},
{
name: 'CLIENT - Dialog Demo MUI',
filename: 'dialog-demo-mui',
template: 'dialog-demo-mui/index.html',
},
{
name: 'CLIENT - Dialog Demo Tailwind CSS',
filename: 'dialog-demo-tailwindcss',
template: 'dialog-demo-tailwindcss/index.html',
},
{
name: 'CLIENT - Sidebar About Page',
filename: 'sidebar-about-page',
template: 'sidebar-about-page/index.html',
},
];
const keyPath = resolve(__dirname, './certs/key.pem');
const certPath = resolve(__dirname, './certs/cert.pem');
const pfxPath = resolve(__dirname, './certs/cert.pfx'); // if needed for Windows
const devServerOptions: ServerOptions = {
port: PORT,
};
// use key and cert settings only if they are found
if (existsSync(keyPath) && existsSync(certPath)) {
devServerOptions.https = {
key: readFileSync(resolve(__dirname, './certs/key.pem')),
cert: readFileSync(resolve(__dirname, './certs/cert.pem')),
};
}
// If mkcert -install cannot be used on Windows machines (in pipeline, for example), the
// script at scripts/generate-cert.ps1 can be used to create a .pfx cert
if (existsSync(pfxPath)) {
// use pfx file if it's found
devServerOptions.https = {
pfx: readFileSync(pfxPath),
passphrase: 'abc123',
};
}
const clientServeConfig = () =>
defineConfig({
plugins: [react()],
server: devServerOptions,
root: clientRoot,
});
const clientBuildConfig = ({
clientEntrypointRoot,
template,
}: {
clientEntrypointRoot: string;
template: string;
}) =>
defineConfig({
plugins: [react(), viteSingleFile({ useRecommendedBuildConfig: true })],
root: resolve(__dirname, clientRoot, clientEntrypointRoot),
build: {
sourcemap: false,
write: false, // don't write to disk
outDir,
emptyOutDir: true,
minify: true,
rollupOptions: {
external: [
'react',
'react-dom',
'react-transition-group',
'react-bootstrap',
'@mui/material',
'@emotion/react',
'@emotion/styled',
'gas-client',
'@types/react',
],
output: {
format: 'iife', // needed to use globals from UMD builds
dir: outDir,
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'react-transition-group': 'ReactTransitionGroup',
'react-bootstrap': 'ReactBootstrap',
'@mui/material': 'MaterialUI',
'@emotion/react': 'emotionReact',
'@emotion/styled': 'emotionStyled',
'gas-client': 'GASClient',
'@types/react': '@types/react',
},
},
input: resolve(__dirname, clientRoot, template),
},
},
});
const serverBuildConfig: BuildOptions = {
emptyOutDir: true,
minify: false, // needed to work with footer
lib: {
entry: resolve(__dirname, serverEntry),
fileName: 'code',
name: 'globalThis',
formats: ['iife'],
},
rollupOptions: {
output: {
entryFileNames: 'code.js',
extend: true,
footer: (chunk) =>
chunk.exports
.map((exportedFunction) => `function ${exportedFunction}() {};`)
.join('\n'),
},
},
};
const buildConfig = ({ mode }: { mode: string }) => {
const targets = [{ src: copyAppscriptEntry, dest: './' }];
if (mode === 'development') {
targets.push(
...clientEntrypoints.map((entrypoint) => ({
src: devServerWrapper,
dest: './',
rename: entrypoint.filename + '.html',
transform: (contents: string) =>
contents
.toString()
.replace(/__PORT__/g, String(PORT))
.replace(/__FILE_NAME__/g, entrypoint.template),
}))
);
}
return defineConfig({
plugins: [
viteStaticCopy({
targets,
}),
/**
* This builds the client react app bundles for production, and writes them to disk.
* Because multiple client entrypoints (dialogs) are built, we need to loop through
* each entrypoint and build the client bundle for each. Vite doesn't have great tooling for
* building multiple single-page apps in one project, so we have to do this manually with a
* post-build closeBundle hook (https://rollupjs.org/guide/en/#closebundle).
*/
mode === 'production' && {
name: 'build-client-production-bundles',
closeBundle: async () => {
console.log('Building client production bundles...');
for (const clientEntrypoint of clientEntrypoints) {
console.log('Building client bundle for', clientEntrypoint.name);
const buildOutput = await build(
clientBuildConfig({
clientEntrypointRoot: clientEntrypoint.filename,
template: clientEntrypoint.template,
})
);
await writeFile(
resolve(__dirname, outDir, clientEntrypoint.filename + '.html'),
// @ts-expect-error - output is an array of RollupOutput
buildOutput.output[0].source
);
}
console.log('Finished building client bundles!');
},
},
].filter(Boolean),
build: serverBuildConfig,
});
};
// https://vitejs.dev/config/
export default async ({ command, mode }: { command: string; mode: string }) => {
if (command === 'serve') {
// for 'serve' mode, we only want to serve the client bundle locally
return clientServeConfig();
}
if (command === 'build') {
// for 'build' mode, we have two paths: build assets for local development, and build for production
return buildConfig({ mode });
}
};
-428
View File
@@ -1,428 +0,0 @@
/*********************************
* import webpack plugins
********************************/
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const GasPlugin = require('gas-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('@effortlessmotion/html-webpack-inline-source-plugin');
const DynamicCdnWebpackPlugin = require('@effortlessmotion/dynamic-cdn-webpack-plugin');
const moduleToCdn = require('module-to-cdn');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
/*********************************
* set up environment variables
********************************/
const dotenv = require('dotenv').config();
const parsed = dotenv.error ? {} : dotenv.parsed;
const envVars = parsed || {};
const PORT = envVars.PORT || 3000;
envVars.NODE_ENV = process.env.NODE_ENV;
envVars.PORT = PORT;
const isProd = process.env.NODE_ENV === 'production';
const isWebpackServe = process.env.WEBPACK_SERVE === 'true';
const publicPath = process.env.ASSET_PATH || '/';
/*********************************
* define entrypoints
********************************/
// our destination directory
const destination = path.resolve(__dirname, 'dist');
// define server paths
const serverEntry = './src/server/index.ts';
// define appsscript.json file path
const copyAppscriptEntry = './appsscript.json';
// define live development dialog paths
const devDialogEntry = './dev/index.js';
// define client entry points and output names
const clientEntrypoints = [
{
name: 'CLIENT - Dialog Demo',
entry: './src/client/dialog-demo/index.js',
filename: 'dialog-demo', // we'll add the .html suffix to these
template: './src/client/dialog-demo/index.html',
},
{
name: 'CLIENT - Dialog Demo Bootstrap',
entry: './src/client/dialog-demo-bootstrap/index.js',
filename: 'dialog-demo-bootstrap',
template: './src/client/dialog-demo-bootstrap/index.html',
},
{
name: 'CLIENT - Dialog Demo MUI',
entry: './src/client/dialog-demo-mui/index.js',
filename: 'dialog-demo-mui',
template: './src/client/dialog-demo-mui/index.html',
},
{
name: 'CLIENT - Dialog Demo Tailwind CSS',
entry: './src/client/dialog-demo-tailwindcss/index.js',
filename: 'dialog-demo-tailwindcss',
template: './src/client/dialog-demo-tailwindcss/index.html',
},
{
name: 'CLIENT - Sidebar About Page',
entry: './src/client/sidebar-about-page/index.js',
filename: 'sidebar-about-page',
template: './src/client/sidebar-about-page/index.html',
},
];
// define certificate locations
// see "npm run setup:https" script in package.json
const keyPath = path.resolve(__dirname, './certs/key.pem');
const certPath = path.resolve(__dirname, './certs/cert.pem');
const pfxPath = path.resolve(__dirname, './certs/cert.pfx'); // if needed for Windows
/*********************************
* Declare settings
********************************/
// webpack settings for copying files to the destination folder
const copyFilesConfig = {
name: 'COPY FILES - appsscript.json',
mode: 'production', // unnecessary for this config, but removes console warning
entry: copyAppscriptEntry,
output: {
path: destination,
publicPath,
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: copyAppscriptEntry,
to: destination,
},
],
}),
],
};
// webpack settings used by both client and server
const sharedClientAndServerConfig = {
context: __dirname,
};
// webpack settings used by all client entrypoints
const clientConfig = ({ isDevClientWrapper }) => ({
...sharedClientAndServerConfig,
mode: isProd ? 'production' : 'development',
output: {
path: destination,
// this file will get added to the html template inline
// and should be put in .claspignore so it is not pushed
filename: 'main.js',
publicPath,
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
},
module: {
rules: [
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
// typescript config
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
// only enable react-refresh for dev builds, and not when building the dev client "wrapper"
options: {
plugins: [
!isProd &&
!isDevClientWrapper &&
require.resolve('react-refresh/babel'),
].filter(Boolean),
},
},
{
loader: 'ts-loader',
},
],
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
// only enable react-refresh for dev builds, and not when building the dev client "wrapper"
options: {
plugins: [
!isProd &&
!isDevClientWrapper &&
require.resolve('react-refresh/babel'),
].filter(Boolean),
},
},
},
// we could add support for scss here
{
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader'],
},
],
},
});
// DynamicCdnWebpackPlugin settings
// these settings help us load 'react', 'react-dom' and the packages defined below from a CDN
// see https://github.com/enuchi/React-Google-Apps-Script#adding-new-libraries-and-packages
const DynamicCdnWebpackPluginConfig = {
// set "verbose" to true to print console logs on CDN usage while webpack builds
verbose: false,
resolver: (packageName, packageVersion, options) => {
const packageSuffix = isProd ? '.min.js' : '.js';
const moduleDetails = moduleToCdn(packageName, packageVersion, options);
// don't externalize react during development due to issue with react-refresh
// https://github.com/pmmmwh/react-refresh-webpack-plugin/issues/334
if (!isProd && packageName === 'react') {
return null;
}
// define custom CDN configuration for new packages
// "name" should match the package being imported
// "var" is important to get right -- this should be the exposed global. Look up "webpack externals" for info.
switch (packageName) {
case 'react-transition-group':
return {
name: packageName,
var: 'ReactTransitionGroup',
version: packageVersion,
url: `https://unpkg.com/react-transition-group@${packageVersion}/dist/react-transition-group${packageSuffix}`,
};
case 'react-bootstrap':
return {
name: packageName,
var: 'ReactBootstrap',
version: packageVersion,
url: `https://unpkg.com/react-bootstrap@${packageVersion}/dist/react-bootstrap${packageSuffix}`,
};
case '@mui/material':
return {
name: packageName,
var: 'MaterialUI',
version: packageVersion,
url: `https://unpkg.com/@mui/material@${packageVersion}/umd/material-ui.${
isProd ? 'production.min.js' : 'development.js'
}`,
};
case '@emotion/react':
return {
name: packageName,
var: 'emotionReact',
version: packageVersion,
url: `https://unpkg.com/@emotion/react@${packageVersion}/dist/emotion-react.umd.min.js`,
};
case '@emotion/styled':
return {
name: packageName,
var: 'emotionStyled',
version: packageVersion,
url: `https://unpkg.com/@emotion/styled@${packageVersion}/dist/emotion-styled.umd.min.js`,
};
// externalize gas-client to keep bundle size even smaller
case 'gas-client':
return {
name: packageName,
var: 'GASClient',
version: packageVersion,
url: `https://unpkg.com/gas-client@${packageVersion}/dist/index.js`,
};
// must include peer dependencies for any custom imports
case '@types/react':
return {
name: packageName,
var: '@types/react',
version: packageVersion,
url: `https://unpkg.com/@types/react@${packageVersion}/index.d.ts`,
};
// return defaults/null depending if Dynamic CDN plugin finds package
default:
return moduleDetails;
}
},
};
// webpack settings used by each client entrypoint defined at top
const clientConfigs = clientEntrypoints.map((clientEntrypoint) => {
const isDevClientWrapper = false;
return {
...clientConfig({ isDevClientWrapper }),
name: clientEntrypoint.name,
entry: clientEntrypoint.entry,
plugins: [
!isProd && new ReactRefreshWebpackPlugin(),
new webpack.DefinePlugin({
'process.env': JSON.stringify(envVars),
}),
new HtmlWebpackPlugin({
template: clientEntrypoint.template,
filename: `${clientEntrypoint.filename}${isProd ? '' : '-impl'}.html`,
inlineSource: '^/.*(js|css)$', // embed all js and css inline, exclude packages from dynamic cdn insertion
scriptLoading: 'blocking',
inject: 'body',
}),
// add the generated js code to the html file inline
new HtmlWebpackInlineSourcePlugin(),
// this plugin allows us to add dynamically load packages from a CDN
new DynamicCdnWebpackPlugin(DynamicCdnWebpackPluginConfig),
].filter(Boolean),
};
});
// webpack settings for devServer https://webpack.js.org/configuration/dev-server/
const devServer = {
hot: true,
port: PORT,
server: 'https',
};
if (fs.existsSync(keyPath) && fs.existsSync(certPath)) {
// use key and cert settings only if they are found
devServer.server = {
type: 'https',
options: { key: fs.readFileSync(keyPath), cert: fs.readFileSync(certPath) },
};
}
// If mkcert -install cannot be used on Windows machines (in pipeline, for example), the
// script at test/generate-cert.ps1 can be used to create a .pfx cert
if (fs.existsSync(pfxPath)) {
// use pfx file if it's found
devServer.server = {
type: 'https',
options: { pfx: fs.readFileSync(pfxPath), passphrase: 'abc123' },
};
}
// webpack settings for the development client wrapper
const devClientConfigs = clientEntrypoints.map((clientEntrypoint) => {
envVars.FILENAME = clientEntrypoint.filename;
const isDevClientWrapper = true;
return {
...clientConfig({ isDevClientWrapper }),
name: `DEVELOPMENT: ${clientEntrypoint.name}`,
entry: devDialogEntry,
plugins: [
new webpack.DefinePlugin({
'process.env': JSON.stringify(envVars),
}),
new HtmlWebpackPlugin({
template: './dev/index.html',
// this should match the html files we load in src/server/ui.js
filename: `${clientEntrypoint.filename}.html`,
inlineSource: '^/.*(js|css)$', // embed all js and css inline, exclude packages from dynamic cdn insertion
scriptLoading: 'blocking',
inject: 'body',
}),
new HtmlWebpackInlineSourcePlugin(),
new DynamicCdnWebpackPlugin({}),
],
};
});
// webpack settings used by the server-side code
const serverConfig = {
...sharedClientAndServerConfig,
name: 'SERVER',
// server config can't use 'development' mode
// https://github.com/fossamagna/gas-webpack-plugin/issues/135
mode: isProd ? 'production' : 'none',
entry: serverEntry,
output: {
filename: 'code.js',
path: destination,
libraryTarget: 'this',
publicPath,
},
resolve: {
extensions: ['.ts', '.js', '.json'],
},
module: {
rules: [
// typescript config
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
},
{
loader: 'ts-loader',
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
// ecma 5 is needed to support Rhino "DEPRECATED_ES5" runtime
// can use ecma 6 if V8 runtime is used
ecma: 5,
warnings: false,
parse: {},
compress: {
properties: false,
},
mangle: false,
module: false,
output: {
beautify: true,
// support custom function autocompletion
// https://developers.google.com/apps-script/guides/sheets/functions
comments: /@customfunction/,
},
},
}),
],
},
plugins: [
new GasPlugin({
// removes need for assigning public server functions to "global"
autoGlobalExportsFiles: [serverEntry],
}),
],
};
module.exports = [
// 1. Copy appsscript.json to destination,
// 2. Set up webpack dev server during development
// Note: devServer settings are only read in the first element when module.exports is an array
{ ...copyFilesConfig, ...(isProd ? {} : { devServer }) },
// 3. Create the server bundle. Don't serve server bundle when running webpack serve.
!isWebpackServe && serverConfig,
// 4. Create one client bundle for each client entrypoint.
...clientConfigs,
// 5. Create a development dialog wrapper bundle for each client entrypoint during development.
// Don't actually serve it though when running webpack serve.
...(isProd || isWebpackServe ? [] : devClientConfigs),
].filter(Boolean);
+7685
View File
File diff suppressed because it is too large Load Diff