script tag text/babel variable scope

后端 未结 1 1985
予麋鹿
予麋鹿 2021-01-02 01:14

Firstly, I understand text/babel is not for use in production, but I found it quite useful for development as when I make a change to my .jsx file

相关标签:
1条回答
  • 2021-01-02 01:59

    Without diving too deeply into the Babel source (looking at https://github.com/babel/babel/blob/master/packages/babel/src/api/browser.js), I'm going to guess that it reads your JSX source, performs transformation on the source, and then evals the source in some way to execute it. The scope is not shared because babel prepends 'use strict'; to the transformed code (standard in ES6).

    If you really need to expose a variable, you can attach it to window (ie use window.mything in your JSX instead of just mything). Ideally, you should make use of modules as you split your code up into multiple files. You can make use of a build step to transform your code through Babel and use browserify/webpack to manage dependencies.

    0 讨论(0)
提交回复
热议问题