Giter Club home page Giter Club logo

babel-preset-extendscript's Introduction

babel-preset-extendscript

What

Babel preset for transpiling ES2015 and modern JS conventions to ES3. Intended for use with Extendscript.

Install

With NPM:

$ npm install --save-dev babel-preset-extendscript

With Yarn:

$ yarn add --dev babel-preset-extendscript

Usage

Via .babelrc (Recommended)

.babelrc

{
  "presets": ["extendscript"]
}

Via CLI

$ babel script.js --presets extendscript

Via Node API

require('babel-core').transform('code', {
  presets: ['extendscript'],
});

Options

  • loose - Enable "loose" transformations for any plugins in this preset that allow them (Disabled by default).
  • modules - Enable transformation of ES6 module syntax to another module type (Enabled by default to "commonjs").
    • Can be false to not transform modules, or one of ["amd", "umd", "systemjs", "commonjs"]
{
  presets: [
    ["extendscript", {"loose": true, "modules": "amd"}]
  ]
}
{
  presets: [
    ["extendscript", {"loose": true, "modules": false}]
  ]
}

Features

ES2015

  • babel-plugin-transform-es2015

Convenience

  • console.log
  • JSON.stringify
  • JSON.parse
  • setTimeout
  • setInterval
  • clearTimeout
  • clearInterval

Shims

  • Object.keys
  • Object.assign
  • Object.create
  • Array.isArray
  • Number.isFinite
  • Array.prototype.forEach
  • Array.prototype.find
  • Array.prototype.filter
  • Array.prototype.map
  • Array.prototype.reduce

Shams

  • Object.getPrototypeOf
  • Object.defineProperty
  • Object.defineProperties
  • Object.getOwnPropertyNames
  • Object.getOwnPropertyDescriptor
  • Object.seal
  • Object.freeze
  • Object.isSealed
  • Object.isFrozen
  • Object.isExtensible

Fixes

  • Wraps all conditional expressions in parentheses to prevent Expected: : error.

Known Issues

โ€ข CallExpression's that uses other transforms from this preset only insert them 1 level deep due to path.unshiftContainer adding a new CallExpression in Program:exit(). Need to figure out a way to do another Program:exit() after the first if new CallExpressions have been added so state[name].matches can be += 1'd.

babel-preset-extendscript's People

Contributors

fusepilot avatar tracker1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

babel-preset-extendscript's Issues

namedFunction is not defined

Module build failed: ReferenceError: /Users/mryan/src/github.com/tracker1/ids-test/src/index.js: namedFunction is not defined

Is there a missing library for this?

Array.reduce only accepts two arguements

The transpiled Array.reduce only accepts two arguments (source array and callback). This is how it's transpiled:

var _reduce = function _reduce(array, callback)

The following code will not run correctly as it discards the initial value:

const foo = [1, 2].reduce((prev, curr) => {
  prev.push(curr + 1)
  return prev
}, [])

Transpiled code:

(function () {
  // prev.push is not a function
  var foo = _reduce([1, 2], function (prev, curr) {
    prev.push(curr + 1)
    return prev
  }) // no initial value
})()

indexOf on string not working

Hey hey,
Thanks for making this preset, it's a really neat idea. I'm running into an issue with one of the transformations, though, and I'm not sure exactly what to do about it.

I have a couple of instances of string.indexOf('testString') in my code, but after transformation it seems to use a function that's not working with strings, as I'm getting the following error while debugging in Extendscript Toolkit: testString is not an object.

The function that is used instead of the default indexOf:
image

Do you know of a way of preventing this error, maybe to ignore this specific transformation or overwriting it?

TypeError: Cannot read property 'start' of undefined

Hi,
I'm currently unable to run this babel preset (macOS 10.13.3, node 8.10.0). I'm using a npm script like:

  "scripts": {
    "babel": "babel src/photoshop.js --presets extendscript"
  },

but the result (no matter what the content of photoshop.js is, even var a = 0; turns out as TypeError: src/photoshop.js: Cannot read property 'start' of undefined. The log file is as follows:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'babel' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prebabel', 'babel', 'postbabel' ]
5 info lifecycle [email protected]~prebabel: [email protected]
6 info lifecycle [email protected]~babel: [email protected]
7 verbose lifecycle [email protected]~babel: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~babel: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/davidebarranca/Desktop/WEBPACK/node_modules/.bin:/Users/davidebarranca/.rbenv/shims:/usr/local/opt/openssl/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/TeX/texbin:/Users/davidebarranca/bin:/Applications/calibre.app/Contents/console.app/Contents/MacOS:/usr/local/Cellar/ruby/2.4.2_1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
9 verbose lifecycle [email protected]~babel: CWD: /Users/davidebarranca/Desktop/WEBPACK
10 silly lifecycle [email protected]~babel: Args: [ '-c', 'babel src/photoshop.js --presets extendscript' ]
11 silly lifecycle [email protected]~babel: Returned: code: 1  signal: null
12 info lifecycle [email protected]~babel: Failed to exec babel script
13 verbose stack Error: [email protected] babel: `babel src/photoshop.js --presets extendscript`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:925:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid [email protected]
15 verbose cwd /Users/davidebarranca/Desktop/WEBPACK
16 verbose Darwin 17.4.0
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "babel"
18 verbose node v8.10.0
19 verbose npm  v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] babel: `babel src/photoshop.js --presets extendscript`
22 error Exit status 1
23 error Failed at the [email protected] babel script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

Do you have any idea why it fails? Thank you!

Davide

Doesn't escape forward slashes in regex

Like mentioned in this issue, when a regex has unescaped forward slashes it throws Expected: ;. This is significant because some polyfills from core.js contains unescaped forward slashes (/function\b(?:\s|\/\*[\S\s]*?\*\/|\/\/[^\n\r]*[\n\r]+)*([^\s(/]*)/), making it incompatible with extendscript. Can this be solved?

Current ES support

It would be nice to include the presets/fills necessary to bring this up to an equivalent to babel-preset-env in terms of coverage, so that this can be used with, for example babel-preset-stage-3 or similar.

Perhaps a separate preset that can be used in conjunction with this one. (babel-preset-extendscript-stage-4 or babel-preset-extendscript-current) Not sure what else is needed in terms of regenerator runtime, etc. Or for that matter the performance impact as such. Would this work with the env preset configured for IE8, for example?

I also want to say, thank you for providing this, it's much appreciated.

Transform nested `forEach`

Nested forEach don't seem to get transformed.

Input

tracks.forEach((track) => {
  track?.clips.forEach((clip) => clip.setSelected(1, 1))
})

Output

_forEach(tracks, function(track) {
  track == null ? void 0 : track.clips.forEach(function(clip) {
    return clip.setSelected(1, 1);
  });
});

default/named export support

When I split code for Illustrator into several files and export something in one file like export default myVar and then import the file in index.jsx.ts, an error happens:

Uncaught (in promise) Error: ExtendScript ReferenceError: Object.defineProperty is not a function at utils.js:36

this happens because the following code gets injected at the start of the file where export is used:

Object.defineProperty(exports, "__esModule", {
  value: true
});

And defineProperty is not present on Object.
Is there a way to fix this?

Not working in babel 7

this preset is not compatible with babel 7 anymore. It produces the following error msg:

 Error: Plugin/Preset files are not allowed to export objects, only functions. In /Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-preset-extendscript/src/index.js
        at createDescriptor (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
        at items.map (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
        at Array.map (<anonymous>)
        at createDescriptors (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
        at createPresetDescriptors (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
        at presets (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
        at mergeChainOpts (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-chain.js:315:26)
        at /Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-chain.js:278:7
        at buildRootChain (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/config-chain.js:118:22)
        at loadPrivatePartialConfig (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/partial.js:57:55)
        at Object.loadPartialConfig (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/@babel/core/lib/config/partial.js:82:18)
        at Object.<anonymous> (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-loader/lib/index.js:82:26)
        at Generator.next (<anonymous>)
        at step (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-loader/lib/index.js:3:221)
        at _next (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-loader/lib/index.js:3:409)
        at /Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-loader/lib/index.js:3:477
        at new Promise (<anonymous>)
        at Object.<anonymous> (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-loader/lib/index.js:3:97)
        at Object.loader (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-loader/lib/index.js:36:18)
        at Object.<anonymous> (/Users/florian/Github Repositories/cm-indesign-scripts/node_modules/babel-loader/lib/index.js:31:12)

Any help how to fix this and make it work for babel 7 is appreciated.

Conditional expressions not wrapped

Contrary to what's described in the README, conditional expressions (i.e. chained ternary operations) are not wrapped in brackets, leading to Expected: : error.

Compatibility with Extendscript UI markup

It seems this babel preset chokes on some Extendscript markup for UI elements:

SyntaxError: /Users/XXXX/lib/main-panel.jsx:  Unterminated string constant. (1:24)
> 1 | var events_resource = """Group {  orientation: 'column', alignment: ['fill', 'fill'],

Should this be supported? I can skip transpiling of course but some of these files have logic in them too.

Arbitrarily renaming functions while transpiling breaks some scripting reference compatibility

I ran into this funny "gotcha" that is very subtle and could easily catch people off guard.

I'm working on a PS script that makes many changes to the document history, and I only want it to have a single history state change for the entire script. Photoshop has a function called suspendHistory which is defined in the JS Reference Guide as:

suspendHistory (historyString, javaScriptString)
Provides a single entry in history states for the entire script provided by javaScriptString. Allows a single undo for all actions taken in the script. The historyString parameter provides the string to use for the history state. The javaScriptString parameter provides a string of JavaScript code to execute while history is suspended.

Here is a simple example:

function main() {
    alert('Hello world')
}

app.activeDocument.suspendHistory("My Script", "main()")

However, this doesn't work, and main() never runs. After scratching my head for a few minutes debugging my script and double checking my implementation I realized that it is caused by the transpiler prepending underscores to function names which breaks references:

    var _main = function _main() {
        try {
            alert('Hello world');
        } catch (_e) {
            if (!_e.stack) _e.stack = "";
            _e.stack += "at " + "main" + "\\n";
            throw _e;
        }
    };

    app.activeDocument.suspendHistory("My Script", "main()"); // ERROR

I'm not sure if renaming the functions during transpilation is necessary, but if it is, it would be nice to warn people of this behavior in the README so they don't get frustrated trying to figure out why basic code isn't working. This is a problem in any area where string references to functions are used.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.