【问题标题】:Make function available to google app script, from webpack module使功能可用于谷歌应用程序脚本,来自 webpack 模块
【发布时间】:2019-10-04 17:28:26
【问题描述】:

我正在使用带有 gas-webpack-plugin 的 webpack 将我的 typescript 代码转换为 JS,以便它可以在 code.gs(GAS 主文件)中使用。但是当 webpack 将入口文件中的函数模块化后,Google App Script 就不再可以使用它们了。

我尝试使用 gas-webpack-plugin 使 GAS 可以全局使用该功能,但没有成功。 https://github.com/fossamagna/gas-webpack-plugin

这是我的 webpack.config.js

var path = require('path');
var GasPlugin = require('gas-webpack-plugin');

module.exports = env => {

return {
    context: __dirname,
    mode: 'production',
    entry: './main/Code.ts',
    optimization: {
        minimize: false
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: 'ts-loader',
                exclude: /node_modules/
            },
        ]
    },
    resolve: {
        extensions: ['.ts', '.js']
    },
    output: {
        filename: 'Code.js',
        path: path.resolve(__dirname, 'build/gs'),
    },
    plugins: [
        new GasPlugin()
    ]
};

};

我正在像这样在我的入口文件中导出函数

export function onOpen(){
    console.log('not working...');
}

export function testFunc(){
    console.log('not working...');
}

【问题讨论】:

  • 你试过扣吗?它还允许您将 TypeScript 与 Google Apps Script 一起使用。
  • 对链接源的快速检查表明,自实际作者上次提交以来,多个依赖项已自动更新。这些更新可能改变了某些行为。否则,我鼓励您查看实际的 README,因为它似乎涵盖了您应该如何实现顶级功能公开......
  • 感谢您的帮助@tehhowch,我没有将函数分配给全局对象,这是我的问题。

标签: webpack google-apps-script


【解决方案1】:

为了让 GAS-webpack-plugin 添加顶级函数声明,我们必须像这样将函数分配给 Node.js 全局对象。这是丢失的。所以我的Entry文件变成了

来自

export function onOpen(){
    console.log('not working...');
}

export function testFunc(){
    console.log('not working...');
}

function onOpen(){
    console.log('not working...');
}

export function testFunc(){
    console.log('not working...');
}

global.onOpen = onOpen;
global.testFunc= testFunc;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多