-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6 target : 'import' and 'export' may appear only with 'sourceType: module' #86
Comments
|
This is a common enough issue that I've added some details to the README. |
I think it's a great idea to add some detail about it, because I had seen mention of "ES6 mode" in the tsify documentation that implied that using ES6 target should 'just work'. But... Yes, I have tried using Babelify. I've tried expressly setting ... And, while I was about to post some code example, I double-checked that it was really effectively the same as my actual code and discovered this surprising thing: Additionally, the default configuration for babelify will not work and you must explicitly configure it to accept ".ts" as an extension. Therefore, a WORKING minimal configuration for babelify, tsify and browserify together using ES6 is: browserify(browserifyConfig)
.plugin(tsify, {target: 'es6'})
.transform(babelify.configure({extensions: [".ts",".js"]}); Maybe this should have been obvious to some people, but it wasn't to me since it appeared in the debugger that the tsify was being called before babelify was being called (babelify was most definitely receiving the output of tsify and not the raw typescript). Could you please put that snippet in the documentation so that folks wanting to use ES6 output have a straightforward example? Please close afterwords, thanks! |
That is very surprising! I didn't even think to check whether order-of-setup would matter, because tsify always registers its transform first in the list. I'll investigate more. Thanks for the additional details! |
I found the same to be true (basarat/globalize-so-what-cha-want-typescript@2c60355), but I wasn't sure if it was just me. Guess its verified now 🌹 |
Actually, reading over the code, I was completely wrong. It's been dependent on order-of-setup since |
I'm trying to compile from
Before I was just doing:
I want to be able to use source maps. My tsconfig.json is the following:
Since I added
My Gulp tasks is the following:
I just added the ES6 compilation, before I was compiling TS into ES5 and everything worked fine (including source maps). I'm using |
I have the same problem. The documentation does not provide a solution here. I've resolve to compiling to es5 for now, but the |
I'm using ts -> ES6 -> ES5 with babel without a problem. If you're using the latest versions of babel and babelify, you must be sure to specify the es2015 preset, since babel no longer, by default, has any plugins specified. I am currently only using inline sourcemaps, though. If you're still getting "'import' and 'export' may appear only with 'sourceType: module'" it means that babelify is not actually transforming your output into commonjs module form, probably because you're missing the es2015 preset. |
@cherrydev , could you share a repo with your config? I just made a minimal working example that reproduces this issue: https://github.com/LaurensRietveld/TsifyIssue . |
@LaurensRietveld, add |
excellent, thanks a bunch! |
That's nice, now going one step further.... If we need to reference external modules + typings, what's the approach?
If I try to add typings to browserify:
I get: Did anyone get tsify working with typings.json + external module refs? Here's the tsconfig:
|
@Davste93 First of all, I don't see that you're actually using babelify in your config. tsify will ignore your 'module:' configuration in your tsconfig if it sees that you're targeting es6 and will automatically output es6 modules. If browserify gets an es6 module (or anything other than commonjs) as an output from a plugin or transform it will punt with |
Thank you so much for your fast response! I'm still getting the same issue after using babelify however:
|
I solved it by doing in
and gulpfile without babelify:
|
While using typescript 1.6.2 and the target: "ES6" option, I receive the error
while trying to build. There is no stack trace indicating where it came from, but the conversation in issue #73 led me to discover tsify itself to be the problem.
I think with the release of typescript 1.6 this is going to rapidly become a major problem; in the past there has been very little use for ES6 output for TSify, which is expressly designed for code targeting browsers:
This is no longer the case, though. To a certain extent, using ES6 natively works for newer web browsers. More importantly, though, Typescript 1.6 introduces to major features (generators and async functions) that ONLY work with ES6 targeted. For people writing browser-targeted code to be able to take advantage of it, they will need to have typescript output ES6 and then have another transpiler (Babel for example) compile it back down to ES5.
The text was updated successfully, but these errors were encountered: