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

How to load user-defined js file in html? #30

Closed sunm0710 closed 4 years ago

sunm0710 commented 4 years ago

Hello, I really like this tool as a beginner of GAS developer. But I have some issue loading a js script in html file. Is there a recommend way to do this? Thank you!

labnol commented 4 years ago

You can use a tool like gulp to bundle all the CSS and JS in a single HTML file that can be pushed to the cloud.

I've written a tutorial for bundling React apps in a single file but the technique should be similar for vanilla HTML / JS.


const gulp = require('gulp')
const inlinesource = require('gulp-inline-source')
const replace = require('gulp-replace')

gulp.task('default', () => {
    return gulp.src('./build/*.html')
        .pipe(replace('.js"></script>', '.js" inline></script>'))
        .pipe(replace('rel="stylesheet">', 'rel="stylesheet" inline>'))
        .pipe(inlinesource({
            compress: false,
            ignore: ['png']
        }))
        .pipe(gulp.dest('./build'))
});