最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - script tag textbabel variable scope - Stack Overflow

programmeradmin5浏览0评论

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 django's dev webserver will reload without me having to do anything (i.e. pile the JSX to JS after every change).

I am not in control of the build environment (e.g. django) as this is a small plugin for a larger system that I am not developing.

The problem is this:

<script type="text/babel" src="{% static "myapp/js/main.jsx" %}"></script>

<script>
    $(function() {
      console.log(mything);
    }
</script>

Where mything is in main.jsx, something as simple as:

var mything = "hello";

If main.jsx is javascript (and the type of the script tags is changed accordingly) then this will work just fine. As text/babel though, it will not work because mything is not in scope.

Uncaught ReferenceError: mything is not defined

This makes sense to me as I wouldn't expect script tags of different types to share a scope, but I'm wondering if there is some clever way around this to aid development?

I previously had all the code in a single text/babel block, but as it grows, it would be nice to separate it out into several JSX files.

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 django's dev webserver will reload without me having to do anything (i.e. pile the JSX to JS after every change).

I am not in control of the build environment (e.g. django) as this is a small plugin for a larger system that I am not developing.

The problem is this:

<script type="text/babel" src="{% static "myapp/js/main.jsx" %}"></script>

<script>
    $(function() {
      console.log(mything);
    }
</script>

Where mything is in main.jsx, something as simple as:

var mything = "hello";

If main.jsx is javascript (and the type of the script tags is changed accordingly) then this will work just fine. As text/babel though, it will not work because mything is not in scope.

Uncaught ReferenceError: mything is not defined

This makes sense to me as I wouldn't expect script tags of different types to share a scope, but I'm wondering if there is some clever way around this to aid development?

I previously had all the code in a single text/babel block, but as it grows, it would be nice to separate it out into several JSX files.

Share Improve this question asked Oct 13, 2015 at 17:55 dpwrdpwr 2,8121 gold badge24 silver badges41 bronze badges 1
  • 1 For pleteness. I ended up removing the use of text/babel and instead building my code with webpack. – dpwr Commented Apr 19, 2016 at 15:42
Add a ment  | 

1 Answer 1

Reset to default 11

Without diving too deeply into the Babel source (looking at https://github./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)

  1. 暂无评论