Skip to content
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

Rollup for JS, Babel, Typescript and React with code formatting apis #69

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

theajr
Copy link
Contributor

@theajr theajr commented Oct 23, 2019

@jakoblind

Added support for Rollup

  • Javascript
  • Babel
  • Typescript
  • React + Babel
  • React + Typescript

Added code formatting apis, which are being used in formatting generated code snippets. We should use the same in webpack and parcel code generation as well. That will in be another PR along with changing sample typescript code in webpack and parcel as mentioned in last comments.

@theajr
Copy link
Contributor Author

theajr commented Oct 23, 2019

#46

@jakoblind
Copy link
Owner

Wow, impressive work @theajr !

I have trouble running the projects. React+typescripts gives the following error when I run npm start:

rollup v1.25.2
bundles src/index.tsx → dist/bundle.js...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/index.tsx (6:33)
4: import Button from "./button";
5: 
6: class App extends React.Component<{}, {}> {
                                    ^
7:   render() {
8:     return (
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (/home/jlind/Downloads/empty-project (12)/node_modules/rollup/dist/rollup.js:5330:30)
    at Module.error (/home/jlind/Downloads/empty-project (12)/node_modules/rollup/dist/rollup.js:9606:9)
    at tryParse (/home/jlind/Downloads/empty-project (12)/node_modules/rollup/dist/rollup.js:9517:16)
    at Module.setSource (/home/jlind/Downloads/empty-project (12)/node_modules/rollup/dist/rollup.js:9835:33)
    at Promise.resolve.catch.then.then.then (/home/jlind/Downloads/empty-project (12)/node_modules/rollup/dist/rollup.js:12615:20)

React+babel gives me the following error when running npm start

rollup v1.25.2
bundles src/index.js → dist/bundle.js...
[!] (plugin commonjs) SyntaxError: Unexpected token (9:6) in /home/jlind/Downloads/empty-project (11)/src/index.js
src/index.js (9:6)
 7:   render() {
 8:     return (
 9:       <div>
          ^
10:         <Button text="Sample Button" />
11:       </div>
SyntaxError: Unexpected token (9:6) in /home/jlind/Downloads/empty-project (11)/src/index.js
    at Object.pp$4.raise (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:2836:15)
    at Object.pp.unexpected (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:689:10)
    at Object.pp$3.parseExprAtom (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:2260:12)
    at Object.parseExprAtom (/home/jlind/Downloads/empty-project (11)/node_modules/rollup/dist/rollup.js:11574:30)
    at Object.pp$3.parseExprSubscripts (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:2089:21)
    at Object.pp$3.parseMaybeUnary (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:2066:19)
    at Object.pp$3.parseExprOps (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:2010:21)
    at Object.pp$3.parseMaybeConditional (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:1993:21)
    at Object.pp$3.parseMaybeAssign (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:1968:21)
    at Object.pp$3.parseParenAndDistinguishExpression (/home/jlind/Downloads/empty-project (11)/node_modules/acorn/dist/acorn.js:2332:30)

@theajr
Copy link
Contributor Author

theajr commented Oct 23, 2019

@jakoblind let me check once.

@theajr
Copy link
Contributor Author

theajr commented Oct 23, 2019

I am facing some issue with gatsby. Server is not starting. Do you have any idea how to fix this? I tried google and deleted nodule modules etc.

Screenshot 2019-10-23 at 12 39 55 PM

@jakoblind
Copy link
Owner

I have never seen that issue. Did it work before and suddenly stopped working? Which node version are you using?

gatsby-node.js Outdated
};

exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment describing why we do this would be nice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, as I mentioned, I was facing some error while using prettier api to format the code snippet. So this piece of code should be added here so that it works. I will keep all these comments and update the PR once my local systems issue is resolved.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I see! a comment about this in the code will be very valuable once you get your systems up and running again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! My system issue is happening only with last commit. If I reset hard to previous commit and start the server, it is working fine. I tested the same branch with latest commit in my friend's mac, it is working fine! this is weird! I am setting up a ubuntu virutal machine to see if it helps.

@@ -259,6 +263,7 @@ export default class Features extends React.Component {
onMouseLeave={onMouseLeave}
selectedBuildTool={selectedBuildTool}
key={group}
expandByDefault={_.size(groupedFeatures) === 1}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but now that we have React, we don't need passing in expandByDefault and the implementation in FeatureGruop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can remove it. But having it won't hurt, in case we are going to add another tab in future this logic may be useful ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a valid point, however, in my experience all line of code adds up to creating complexity so I'm very careful not to have more code than necessary.

If you create a new commit reverting these lines, we'll have the code in git history and we can easily get them back if needed.

export const getIndex = configItems => {
const isBabel = _.includes(configItems, 'Babel');

return getFormattedCode(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this call, in theory, be slow? If it makes external API calls or something? If it is then we need to consider doing caching.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's just calling a js function which is inside utils folder. So it won't cause any delay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These apis are removed in latest commit, somehow the this commit is making some issue in my system. So I corrected the indentation manually. I will try to analyse more once this PR is merged.

@theajr
Copy link
Contributor Author

theajr commented Oct 23, 2019

It used to work my mac got crashed this morning, so restarted. From then, I am unable to start gatsby server. I was using node 10.x LTS version, I tried upgrading to 12.x LTS. still not working. It may take some time.

@jakoblind
Copy link
Owner

ok maybe your error comes from the crash. It works fine for me on 10.x LTS. good luck fixing the mac, hope it isn't too difficult to fix

@theajr
Copy link
Contributor Author

theajr commented Oct 23, 2019

@jakoblind I will let you once this PR is totally ready. I am working on it now.

@theajr
Copy link
Contributor Author

theajr commented Oct 23, 2019

@jakoblind It looks good now. I tested all rollup features from deployed netlify.
You can check now.

@jakoblind
Copy link
Owner

I can reproduce the error you showed me yesterday on my mac. On my linux machine it works fine. Netlify also runs linux I think, that's why it also works there probably. I will help debugging this mac issue.

@theajr
Copy link
Contributor Author

theajr commented Oct 24, 2019

@jakoblind Thanks. I tried the same in my friend's Mac, it was not throwing the error. I created a new Ubuntu virtual machine and there also I could see the issue.

@jakoblind
Copy link
Owner

I pushed a fix to master. Please checkout and test. You might have to remove the node_module folder first.

This was the issue: There was a mismatch between version of NPM dependency sharp

[email protected] /home/jlind/dev/webpack-autoconf
├─┬ [email protected]
│ └─┬ [email protected] (git+https://github.com/Creatiwity/favicons-webpack-plugin.git#0872de414061baaad0a2853c77ae75bc38b8bbbf)
│   └─┬ [email protected]
│     └── [email protected] 
└─┬ [email protected]
  └── [email protected] 

After some research I realized that the gatsby-plugin-manifest also provides favicon functionality, so I deleted gatsby-plugin-favicon.

If everything works, you can merge in master to this branch

@jakoblind
Copy link
Owner

@theajr I tested running React+babel and still get the same error as before, did you get a chance to look into why this error happens? Let me know if you need help debugging

rollup v1.25.2
bundles src/index.js → dist/bundle.js...
[!] (plugin commonjs) SyntaxError: Unexpected token (9:6) in /home/jlind/Downloads/empty-project (13)/src/index.js
src/index.js (9:6)
 7:   render() {
 8:     return (
 9:       <div>
          ^
10:         <Button text="Sample Button" />
11:       </div>
SyntaxError: Unexpected token (9:6) in /home/jlind/Downloads/empty-project (13)/src/index.js
    at Object.pp$4.raise (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:2836:15)
    at Object.pp.unexpected (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:689:10)
    at Object.pp$3.parseExprAtom (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:2260:12)
    at Object.parseExprAtom (/home/jlind/Downloads/empty-project (13)/node_modules/rollup/dist/rollup.js:11574:30)
    at Object.pp$3.parseExprSubscripts (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:2089:21)
    at Object.pp$3.parseMaybeUnary (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:2066:19)
    at Object.pp$3.parseExprOps (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:2010:21)
    at Object.pp$3.parseMaybeConditional (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:1993:21)
    at Object.pp$3.parseMaybeAssign (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:1968:21)
    at Object.pp$3.parseParenAndDistinguishExpression (/home/jlind/Downloads/empty-project (13)/node_modules/acorn/dist/acorn.js:2332:30)

@jakoblind
Copy link
Owner

I experimented a bit and I got it to work. Moving up babel call to the top seemed to help. Also I needed to add resolve({extensions: ['.js', '.jsx', '.json']}), to the resolver call.

plugins: [
        babel({
            exclude: "node_modules/**"
        }),
        resolve({extensions: ['.js', '.jsx', '.json']}),
        commonjs({
            exclude: "src/**"
        }),
        globals(),
    ]

@theajr
Copy link
Contributor Author

theajr commented Oct 25, 2019

@jakoblind Thanks, I don't know why it is working at my end. Still I will add your changes and merge it.

}
plugins.push(`resolve({
extensions:${JSON.stringify(resolveExtensions)}
}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a missing ) for resolve

@jakoblind
Copy link
Owner

default file should be rollup.config.js. Currently it's package.json

@theajr
Copy link
Contributor Author

theajr commented Oct 27, 2019

@jakoblind Fixed both now.

@jakoblind
Copy link
Owner

Now React+Babel works for me! Awesome! But I still cannot get React+typescript to work. Could you see if you can reproduce this error? running npm start

rollup v1.26.0
bundles src/index.tsx → dist/bundle.js...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/index.tsx (7:33)
5: import Button from './button';
6: 
7: class App extends React.Component<{}, {}> {
                                    ^
8:   render() {
9:     return (
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (/home/jlind/Downloads/empty-project (15)/node_modules/rollup/dist/rollup.js:5351:30)
    at Module.error (/home/jlind/Downloads/empty-project (15)/node_modules/rollup/dist/rollup.js:9627:9)
    at tryParse (/home/jlind/Downloads/empty-project (15)/node_modules/rollup/dist/rollup.js:9535:16)
    at Module.setSource (/home/jlind/Downloads/empty-project (15)/node_modules/rollup/dist/rollup.js:9852:33)
    at Promise.resolve.catch.then.then.then (/home/jlind/Downloads/empty-project (15)/node_modules/rollup/dist/rollup.js:12641:20)

@theajr
Copy link
Contributor Author

theajr commented Nov 4, 2019

@jakoblind Sure, I will look into it.

@jakoblind
Copy link
Owner

I managed to reproduce on two machines now. But have not yet figured out how to fix it. If this takes to long, we could ship this without typescript support for now just to get it out.

@jakoblind jakoblind mentioned this pull request Nov 6, 2019
@theajr
Copy link
Contributor Author

theajr commented Nov 6, 2019

@jakoblind I will sit tonight to fix this. I wasn't getting much time recently.

@theajr
Copy link
Contributor Author

theajr commented Nov 8, 2019

@jakoblind I found the fix. I will be pushing the code tomorrow.

@jakoblind
Copy link
Owner

awesome @theajr ! looking forward to read the code. Have a good weekend!

@theajr
Copy link
Contributor Author

theajr commented Nov 9, 2019

@jakoblind I will let you know once PR is ready to review. Have a good weekend.

@theajr
Copy link
Contributor Author

theajr commented Nov 10, 2019

@jakoblind Please check now.

@jakoblind
Copy link
Owner

Hmm. I still get the same error:

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/index.ts (2:24)
1: 
2:   const sayHello = (name:string): string => `Hello, ${name}`;
                           ^
3: 
4:   export default sayHello;
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)

does it work for you?

@theajr
Copy link
Contributor Author

theajr commented Nov 11, 2019

It's weird. It's always working for me. I never faced this issue. I wonder what is the deal here 😇

@jakoblind
Copy link
Owner

that's weird. what version of npm and node are you using?

@theajr
Copy link
Contributor Author

theajr commented Nov 11, 2019

I am using node 12.13.0 and npm 6.12.0

Screenshot 2019-11-11 at 2 56 42 PM

@theajr
Copy link
Contributor Author

theajr commented Nov 11, 2019

@jakoblind I could reproduce the issue in a actual windows machine(Working fine in windows in VM somehow). If I change the content of index.ts to below content, It is working fine. i.e. removed the data types from code.

Code causing issue:

const sayHello = (name:string): string => `Hello, ${name}`;

export default sayHello;  

After removing data types, it is working fine. Can you please check the same and provide your thoughts?

const sayHello = (name) => `Hello, ${name}`;

export default sayHello;

It should support data types as well but seems to be an issue or I am missing something.

@jakoblind
Copy link
Owner

Yes when I do that it's working fine. But then no typescript-specific code is running. I guess the problem is that this file is not run through the typescript compiler for some reason. That's why it works to remove the types.

I'm still confused here. I can reproduce the error on two of my machines. I got a friend to run it and he couldn't reproduce the error.

Anyway, I'll ask around a bit

@gengjiawen
Copy link

const sayHello = (name:string): string => Hello, ${name};

Is this the only blocker here ?

@gengjiawen
Copy link

@jakoblind Looks like last merge break build.

@jakoblind
Copy link
Owner

@gengjiawen yeah this PR is now a bit outdated. There have been some refactorings in master. I have limited time, and will not prioritize fix this currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants