设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 74|回复: 0

[制作教程] How to Transpile ES6 Javascript to ES5 for older versions of MV

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    慵懒
    3 天前
  • 签到天数: 207 天

    连续签到: 1 天

    [LV.7]常住居民III

    3646

    主题

    862

    回帖

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    21262
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    25800

    灌水之王

    发表于 前天 18:49 | 显示全部楼层 |阅读模式
    Topic: Transpile Javascript code written with ES6 features into code compatible for older browsers and versions of MV prior to 1.6.0 using Babel.js

    Intended Audience: Plugin Developers

    Description: RPG Maker MV v1.6.0 and later now come with an updated version of NW.js. What does this mean, practically?

    It means that we can now enjoy a modern browser experience, and can use Javascript code with new features from the ECMAScript2015 specification (commonly known as ES6). I highly recommend using the ES6 features, as they will make your code easier to write and easier to read.

    However, some RPG Maker users may not have upgraded to v1.6.0+ or some games might be deployed to the web and accessed by people with non-modern browsers (looking at you, Internet Explorer).

    With Babel.js, we can get the best of both world - develop plugins with the most up-to-date features, but still retain backwards compatibility with older versions.

    Requirements: A text editor and minimal knowledge of the command line (don’t worry, I will walk you through it).


    Tutorial:

    1.) Verify or install command line / bash

    Let’s take a moment to verify that we have a proper command line.

    Windows:
    I recommend Git Bash, download here.

    Mac:
    The default shell of the Terminal is Bash, so no installation required.
    How to open the Terminal on a Mac.

    Linux:
    I believe that there is a Terminal that runs Bash by default similar to Mac.
    I've never owned a Linux, so I have no idea, but I think that if you use a Linux you'll be savvy enough to figure out how to open it.

    2.) Install npm

    Install Node Package Manager (npm) onto your computer. It comes bundled with Node.js, the download link can be found here. Open Git Bash or the Terminal (depending on your OS), and execute the following command to ensure that npm is installed.

    (Note that the $ symbol just means the start of the line, your symbol may be different depending on your terminal.)
                    Code:        
    $ npm -v


    This will print the version of npm, if there are no errors then it is installed correctly.

    3.) Create your development directory

    I recommend creating a new folder for plugin development

    Why would you avoid developing directly in your game’s js/plugins folder?

    • If you are in-process of working on new features of your plugin, you can still work/test your game with a stable version of your plugin
    • Single location to accumulate useful tools across all of your projects
    • The only downside is having to copy the plugin from your development folder to your game folder… which can be automated! I will show you how at the bottom.
    In my case, I created a folder called:
    C:\Users\me\Documents\RPGMakerMV\PluginDevelopment

    For the rest of the tutorial, this folder is known as your “root” directory.
    Create two folders under your root directory called “src” and “lib”.
    "src" is your source code, and is where you will be working on your plugins. "lib" will hold the transpiled output that can be shared with others or used in your game deployment (if needed).

    4.) Install Babel.js

    4a.) In the terminal, navigate to your plugin development folder. In Windows, you can actually right-click on the folder and select “Git Bash Here” which will open bash already navigated to that directory. This might be possible on Mac/Linux also.

    If not, use the command “cd” to navigate to your directory. In my case:
                    Code:        
    $ cd Documents\RPGMakerMV\PluginDevelopment


    Note: This entire tutorial takes place in your plugin development directory. All commands listed assume that the terminal is opened in this directory. After step 4a, we don't change directories.

    4b.) Initialize npm in your root directory

                    Code:        
    $ npm init


    4c.) Install the Babel Command Line Interface (CLI) as a development dependency

                    Code:        
    $ npm install babel-cli -D


    This will print the version of Babel.js, if there are no errors then it is installed correctly.
                    Code:        
    $ babel -V


    4d.) Install the Babel presets. This is the library that allows code to be transpiled from one specification to another.

                    Code:        
    $ npm install babel-preset-env -D


    5.) Configuring Babel

    Babel needs to know which specifications you are using to transpile it backwards. It’s easier just to tell Babel that we’re using all modern specifications rather than describe each one.

    5a.) Create a .babelrc file in the root directory.

                    Code:        
    $ touch .babelrc


    5b.) In a text editor, add the following code to the .babelrc file:

                    Code:        
    {  "presets": ["env"] }



    6.) Setting up automation


    6a.) After you initialized npm in the root directory, a “package.json” file was created. Go ahead and open this in a text editor.


    6b.) Find the “scripts” property.
    Add this property into the “scripts” object:

                    Code:        
    “build”: “babel src -d lib”


    The “scripts” part will look something like this now:

                    Code:        
    "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "build": "babel src -d lib"  },


    7.) Transpile the code!

    From the command line, run:
                    Code:        
    $ npm run build



    This will transpile all files in your “src” directory from modern Javascript to ES5-compatible Javascript in your “lib” directory. All done!


    Spoiler: ”Bonus: Automate file copy”
    The transpiled code gets put into the “lib” folder of your development directory. But then you would have to copy it into your project in order to test it.
    We can automate this!
    Using the “cp” command, and knowing that “.” refers to the current directory and “..” refers to the level above the current directory.

    Here is an example that copies ‘plugin_name.js’ from the ‘src’ folder into my project plugin folder.

                    Code:        
    cp ./src/plugin_name.js ../Project/MyProject/js/plugins



    So my package.json "scripts" section would look something like this:

                    Code:        
    "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "build": "babel src -d lib && cp ./src/plugin_name.js ../Project/MyProject/js/plugins"  },



    When I use ‘npm run build’ , it will transpile my file and automatically copy it into my project for testing!


    本贴来自国际rpgmaker官方论坛作者:Aloe Guvner处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/how-to-transpile-es6-javascript-to-es5-for-older-versions-of-mv.95200/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-7-10 02:05 , Processed in 0.146913 second(s), 55 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表