fossamagna/babel-preset-gas

Syntax Error

bvillaroman opened this issue · 5 comments

Hi. Thank you for great preset! With your preset and gas-webpack-plugin, I tried to make a library but I got this error when I push the script produced by clasp:

errors: [
    {
      message: 'Syntax error: Missing name after . operator. line: 1 file: lib.bundle',
      domain: 'global',
      reason: 'badRequest'
    }
]

And in the first line i see in lib.bundle' uses of t.default.

Perhaps i have the wrong webpack config setup?

my config is:

var path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const GasPlugin = require("gas-webpack-plugin");

module.exports = {
  context: __dirname,
  entry:{
    lib:'./server/lib.js'
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    libraryTarget: 'var',
    library: 'AppLib'
  },
  module:
  {
	  rules: [
        {
          test: /\.js$/,
          exclude: /(node_modules|bower_components)/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  {
                    useBuiltIns: 'usage'
                  }
                ]
              ]
            }
          }
        }
      ] 
  },
  plugins: [
    new GasPlugin(),
    new CopyPlugin([
      'server/api.js',
      'appsscript.json',
      '.clasp.json'
    ])
  ]
}

and my .babelrc file:

{ "presets": ["gas"] }

@bvillaroman Edited the webpack.config.js and removed the .babelrc.

var path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const GasPlugin = require("gas-webpack-plugin");

module.exports = {
  context: __dirname,
  entry:{
    lib:'./server/lib.js'
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    libraryTarget: 'var',
    library: 'AppLib'
  },
  module:
  {
	  rules: [
        {
          test: /\.js$/,
          exclude: /(node_modules|bower_components)/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  {
                    useBuiltIns: 'usage'
                  }
                ],
                // Add babel-preset-gas here.
                [
                  'gas'
                ]
              ]
            }
          }
        }
      ] 
  },
  plugins: [
    new GasPlugin(),
    new CopyPlugin([
      'server/api.js',
      'appsscript.json',
      '.clasp.json'
    ])
  ]
}

https://webpack.js.org/loaders/babel-loader/

this new configuration give s me the same error :(.

A little more context to what im trying to do:

When I execute this config in development mode, theres a specific library i want to use (tsscmp) whose bundle code contains the keyword "global" with the dot operation is being used.

When i run the same config in production mode, the bundle file returns the same error:

[
    {
      message: 'Syntax error: Missing name after . operator. line: 1 file: lib.bundle',
      domain: 'global',
      reason: 'badRequest'
    }
  ]

when trying to upload to appscript using clasp

Is there any way I could achieve this type of config running developer mode while changing global. to [global] like how your gas-webpack-plugin does?

@bvillaroman
Can I see your reproduced code.

In development mode, bundled source code is passed to argument of eval function as string. Now, gas-webpack-plugin is not support webpack development mode, because gas-webpack-plugin do not parse argument of eval function. Please use production or none mode.

the file that produces the error is this specific block in my lib.bundle.js:

var AppLib=function(t){var e={};function i(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(r,n,function(e){return t[e]}.bind(null,n));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=100)}([function(t,e){"function"==typeof Object.create?t.exports=function(t,e){e&&(t.super_=e,t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}))}:t.exports=function(t,e){if(e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}}},function(t,e,i){var r=i(2),n=r.Buffer;function o(t,e){for(var i in t)e[i]=t[i]}function h(t,e,i){return n(t,e,i)}n.from&&n.alloc&&n.allocUnsafe&&n.allocUnsafeSlow?t.exports=r:(o(r,e),e.Buffer=h),o(n,h),h.from=function(t,e,i){if("number"==typeof t)throw new TypeError("Argument must not be a number");return n(t,e,i)},h.alloc=function(t,e,i){if("number"!=typeof t)throw new TypeError("Argument must be a number");var r=n(t);return void 0!==e?"string"==typeof i?r.fill(e,i):r.fill(e):r.fill(0),r},h.allocUnsafe=function(t){if("number"!=typeof t)throw new TypeError("Argument must be a number");return n(t)},h.allocUnsafeSlow=function(t){if("number"!=typeof t)throw new TypeError("Argument must be a number");return r.SlowBuffer(t)}},function(t,e,i){"use strict";(function(t){

@bvillaroman
It is possible that the module that tsscmp depends on (probably buffer) contains the default keyword, which is not converted to es3, causing the error. Under node_modules is not subject to conversion by babel. I think that the module including the default keyword needs to be included in the conversion target by babel.

Using are-you-es5, you can find modules that need conversion and you can get babel-loader exclude regex to ignore all node_modules except non-ES5 ones.

What you use runtime on apps script? If you don't use v8 runtime, Would you to try apps script v8 runtime? In this case babel-gas-preset is not necessary.