从Webpack模块使功能可用于Google App脚本

时间:2019-05-17 11:24:33

标签: webpack google-apps-script

我正在将webpack与gas-webpack-plugin一起使用,以将我的打字稿代码转换为JS,以便可以在code.gs(GAS主文件)中使用。但是,当webpack将条目文件中的功能模块化时,它们将不再对Google App脚本可用。

我尝试使用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...');
}

1 个答案:

答案 0 :(得分:0)

要允许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;