labnol / apps-script-starter

Setup a local development environment inside Visual Studio Code and build Google Workspace add-ons with Google Apps Script
https://www.youtube.com/watch?v=KxdCIbeO4Uk
MIT License
1.1k stars 176 forks source link

Unusable build output with fresh project #53

Closed riaanduplessis closed 2 years ago

riaanduplessis commented 2 years ago

Steps to reproduce

Run the following commands:

  1. git clone https://github.com/labnol/apps-script-starter test-apps-script
  2. cd test-apps-script
  3. npm install
  4. npm run build

Expected Result

The built code.js file to contain the functions from the three imported files in src/index.js which can be run in Google Apps Script after pushing, which includes:

sendmail()

showHelp() showCredits() onOpen()

doGet()

Actual Result

Empty functions are written to the built file with all the actual code contained in an anonymous function.

function sendmail() {}
function showHelp() {}
function showCredits() {}
function onOpen() {}
function doGet() {}
(() => {
  var e = {
      173: (e, t, r) => {
        r.g.sendmail = () => {
          const e = Session.getActiveUser().getEmail(),
            t =
              '\n    <p>This email was sent using the <a href="https://www.labnol.org/internet/google-apps-script-developers/32305/">Google Apps Script Starter kit</a>.</p>\n    <p> The starter kit is used by <a href="https://digitalinspiration.com/">Digital Inspiration</a> for building popular Google Workspace add-on including <a href="https://workspace.google.com/marketplace/app/mail_merge_with_attachments/223404411203">Gmail Mail Merge</a> and <a href="https://workspace.google.com/marketplace/app/document_studio/429444628321">Document Studio</a>. </p>\n    <p>For assistance, please contact <a href="https://twitter.com/labnol">@labnol</a></p>',
            r = t.replace(/<[^>]+>/g, " ");
          GmailApp.sendEmail(e, "Hello from Google Apps Script", r, {
            htmlBody: t,
          }),
            Logger.log(`Email message sent to${e}`);
        };
      },
      933: (e, t, r) => {
        (r.g.showHelp = () => {
          Browser.msgBox(
            "Develop Google Apps Script project locally inside VS Code"
          );
        }),
          (r.g.showCredits = () => {
            SpreadsheetApp.getActiveSpreadsheet().toast(
              "Developed by Amit Agarwal @labnol"
            );
          }),
          (r.g.onOpen = () => {
            try {
              SpreadsheetApp.getUi()
                .createMenu("Apps Script Starter")
                .addItem("Help", "showHelp")
                .addSeparator()
                .addItem("Credits", "showCredits")
                .addToUi();
            } catch (e) {
              Logger.log(e.message);
            }
          });
      },
      540: (e, t, r) => {
        r.g.doGet = () =>
          HtmlService.createHtmlOutputFromFile("index.html")
            .setTitle("Google Apps Script")
            .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.DEFAULT);
      },
    },
    t = {};
  function __webpack_require__(r) {
    var o = t[r];
    if (void 0 !== o) return o.exports;
    var a = (t[r] = { exports: {} });
    return e[r](a, a.exports, __webpack_require__), a.exports;
  }
  (__webpack_require__.n = (e) => {
    var t = e && e.__esModule ? () => e.default : () => e;
    return __webpack_require__.d(t, { a: t }), t;
  }),
    (__webpack_require__.d = (e, t) => {
      for (var r in t)
        __webpack_require__.o(t, r) &&
          !__webpack_require__.o(e, r) &&
          Object.defineProperty(e, r, { enumerable: !0, get: t[r] });
    }),
    (__webpack_require__.g = (function () {
      if ("object" == typeof globalThis) return globalThis;
      try {
        return this || new Function("return this")();
      } catch (e) {
        if ("object" == typeof window) return window;
      }
    })()),
    (__webpack_require__.o = (e, t) =>
      Object.prototype.hasOwnProperty.call(e, t)),
    (() => {
      "use strict";
      __webpack_require__(173),
        __webpack_require__(933),
        __webpack_require__(540);
    })();
})();

Environment

> node -v

v16.13.0

> npm -v

8.1.0

> npm ls

apps-script-starter@10.2.0 /path/to/test-apps-script
├── @babel/core@7.16.0
├── @babel/eslint-parser@7.16.3
├── @babel/plugin-proposal-object-rest-spread@7.16.0
├── @babel/preset-env@7.16.4
├── @google/clasp@2.4.1
├── @types/google-apps-script-oauth2@38.0.0
├── @types/google-apps-script@1.0.41
├── @types/google.picker@0.0.39
├── @types/jest@27.0.3
├── apps-script-oauth2@1.41.0 (git+ssh://git@github.com/googleworkspace/apps-script-oauth2.git#eebdf7884b019dac5160f6554059bf9537fda1cd)
├── babel-loader@8.2.3
├── copy-webpack-plugin@10.0.0
├── cross-env@7.0.3
├── eslint-config-airbnb-base@15.0.0
├── eslint-config-prettier@8.3.0
├── eslint-plugin-googleappsscript@1.0.4
├── eslint-plugin-html@6.2.0
├── eslint-plugin-import@2.25.3
├── eslint-plugin-jest@25.3.0
├── eslint-plugin-prettier@4.0.0
├── eslint-webpack-plugin@3.1.1
├── eslint@8.3.0
├── gas-webpack-plugin@2.1.0
├── jest@27.3.1
├── npm-watch@0.11.0
├── prettier@2.5.0
├── terser-webpack-plugin@5.2.5
├── webpack-cli@4.9.1
└── webpack@5.64.4
labnol commented 2 years ago

Open the webpack.config.js file and set minimize to false.

optimization: {
    minimize: false,
    minimizer: [
      new TerserPlugin({
        test: /\.js$/i,