Code Monkey home page Code Monkey logo

Comments (30)

appedemic avatar appedemic commented on August 18, 2024

This is caused by the babel parser. babel 6 doesn't allow toplevel this and destroys references (#15) so the parser wraps code in a function. The import statements are executed within this wrapper function and that is causing the error.

I can't think of an easy solution right now. The two hard solutions I can think of right now are:

  • Remove the wrapper function. This implies that preset es2015 cannot be used because the es2015-plugin-modules-commonjs was causing #15. This also implies that end-users cannot simply do "npm install babel-preset-es2015" but have to install all the plugins from the preset manually (except commonjs). Or maybe we can do some trick witha .babelrc?
  • Add some dirty code to extract/hoist import statements from the code block and move them out of the wrapper function.

from compiler.

txchen avatar txchen commented on August 18, 2024

I think previously it works because of the blacklist 'strict' setting, not sure if babel 6 still have something similar.

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

@appedemic , it is true. I'm not a babel user, but in tests babel-core 5.8.x was the cleaner and faster solution, v6 was complicated and slower.

from compiler.

txchen avatar txchen commented on August 18, 2024

@aMarCruz right, I also noticed that v6 is slower, but seems v5 is not maintained any more, I think we still need to go with v6.

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

It seems so. I'm closing this issue by now.

from compiler.

txchen avatar txchen commented on August 18, 2024

@aMarCruz why close this? We don't want to support babel v6, then?

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

@aMarCruz I want to reopen this issue I think we can fix this as well.
@txchen have you tried also using the include outside of your tags?

import foo from './bar'
<mytag>
  <script type="babel">
  // some other fancy code
  </script>
</mytag>

I will try asking support on the babel repo

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

I have just seen that babeljs has already fixed the issue we just need to implement this option 🎉

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

👍

from compiler.

appedemic avatar appedemic commented on August 18, 2024

Yeah, "Add allowTopLevelThis option to babel-plugin-transform-es2015-modules-commonjs" 💃

I can probably create a patch today

from compiler.

appedemic avatar appedemic commented on August 18, 2024

Currently there is no way to add plugin options to a preset, so this allowTopLevelThis cannot be used directly with presets:['es2015']. There is an ongoing discussion about how to support this...

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

@appedemic , thanks. we hope your PR.

from compiler.

txchen avatar txchen commented on August 18, 2024

@GianlucaGuarini putting import in the very beginning of the tag file does not work, riot-loader will throw error:

ERROR in ./src/view/detail-view.html
Module parse failed: /Users/txchen/GithubCode/feplay/riot_webpack/node_modules/riotjs-loader/index.js!/Users/txchen/GithubCode/feplay/riot_webpack/src/view/detail-view.html Line 1: Unexpected token
You may need an appropriate loader to handle this file type.
| import stores from '../stores'
| riot.tag2('detail-view', '<h2>{_post.title}</h2> <p>{_post.content}</p> <p>{_post.likes} Likes</p> <a if="{opts.data > 1}" href="#detail/{opts.data - 1}">Previous Post</a> | <a if="{opts.data < _total}" href="#detail/{opts.data - -1}">Next Post</a>', '', '', function(opts) {
|   var _this = this;
 @ ./src/riotTags.js 9:0-34

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

I've asked support to the babel maintainers babel/babel#3055 I hope to get a valid answer soon

from compiler.

appedemic avatar appedemic commented on August 18, 2024

For fun I created a modified es2015 preset: https://github.com/appedemic/babel-preset-es2015-riot

However, npm has some trouble accepting logins right now so I can't publish this as a package right now

from compiler.

andyrj avatar andyrj commented on August 18, 2024

is babel-preset-es2015-riot still going to be pushed to npm?

Just tested and as far as I can tell even using this allowTopLevelThis option doesn't seem to fix this error. :(

So for now stay with babel v5.x I suppose?

from compiler.

appedemic avatar appedemic commented on August 18, 2024

Right now it's not possible to send the allowTopLevelThis option to the right plugin within the preset. I've been thinking, if we add an option to the compiler where the end user can supply a list of presets/plugins to use, it would be the most flexible.

For example, I really like to use some ES7 features (async/await and Object.entries) and they are not part of the es2015 preset, but stage-0 and stage-3. For these features I need to add a preset/plugin to the babel compiler.

I could create a pr that takes the compiler plugins/preset options from the configuration and defaults to es2015. Then, I can supply my desired configuration into riotify and it will be passed on nicely.

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

There is an idea of a possible system of plugins that should solve some issues like this in riot/riot#1272.
It is likely that something like that is implemented.

from compiler.

appedemic avatar appedemic commented on August 18, 2024

Ok, so the plan is to make packages for the parsers? So to have some npm packages like riot-compiler-parser-babel and riot-compiler-parser-jade, etc.? And provide some mechanism to configure the compiler with said packages?

riot.compiler.use(require('riot-compiler-parser-babel')({ presets: ['es2015'] }))

If implemented correctly it would be a nice future-ready solution

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

@appedemic , yeah, I hope soon, after stabilizing the current version, the changes have been many.

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

Ok this issue was solved in the es6 branch @aMarCruz can you review my changes and make the new releases of the riot-tmpl and riot-compiler? I would like compile riotjs using rollup and also update the cli as well adding the debug compiler option

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

@GianlucaGuarini sure. the dist/riot.*.js files will be generated anyway?

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

Let's keep it for now we will delete it if everything will run smooth

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

ok

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

@aMarCruz can we infer the url param for the compile method from the first one ( src ) at least on node?

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

@appedemic can we use your riot-bable preset in our official repo? I woul like to publish it on npm with the new release of the compiler

from compiler.

aMarCruz avatar aMarCruz commented on August 18, 2024

@GianlucaGuarini , no way to infer the url from the src parameter, it can come from anywhere.

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

@appedemic I have published on npm the riot-preset based on your setup thank you and feel free to pull request on this repo

from compiler.

creatorrr avatar creatorrr commented on August 18, 2024

@appedemic I thought the function wrapper was only necessary for this bug. The top-level this issue should be resolved now with the babel-riot preset. Removing the wrapper function from the compiled templates should allow imports.

If not, for now we could revert to using require( _module ) in tags instead of import.

from compiler.

GianlucaGuarini avatar GianlucaGuarini commented on August 18, 2024

@creatorrr you are right this bug was fixed and it can be closed

from compiler.

Related Issues (20)

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.