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

Production? #20

Open
stokesbga opened this issue Jul 16, 2017 · 3 comments
Open

Production? #20

stokesbga opened this issue Jul 16, 2017 · 3 comments

Comments

@stokesbga
Copy link

Does anyone have an elegant solution for building a sails app with babel for production?

@abecks
Copy link

abecks commented Aug 14, 2018

Hey @stokesbga, were you ever able to find a solution?

@vikr01
Copy link

vikr01 commented Aug 14, 2018

@abecks Don't use sails-hook-babel. It just uses babel-register, which caches your files and should not be used in production. It also won't let you use babel on your config files. It also comes with presets and plugins which may not help you. You lose too much flexibility when using sails-hook-babel.

I've started using a yarn-workspaces (lerna is another good option if you're using npm) monorepo with my modified version of sails, sails-hook-orm, and sails-disk in it that allows me to use babel properly.

You can also do
yarn add https://github.com/vikr01/sails.git#feature/babel-support https://github.com/vikr01/sails-disk.git#fix/tmp-path https://github.com/vikr01/sails-hook-orm.git#feature/babel-support

or
npm i https://github.com/vikr01/sails.git#feature/babel-support https://github.com/vikr01/sails-disk.git#fix/tmp-path https://github.com/vikr01/sails-hook-orm.git#feature/babel-support

to install directly from my repos (I recommend the monorepo approach though, so you can maintainthem yourself).

This is similar to what the structure of my sails project now looks like:

.
├── .babelrc
├── .env
├── .eslintignore
├── .eslintrc
├── .sailsrc
├── assets
├── package.json
├── src
│   ├── api
│   ├── app.js
│   ├── config
│   └── views
└── test

How my project looks like as a monorepo with sails and my project in it:

.
├── package.json
├── packages
│   ├── my-sails-project
│   ├── sails
│   ├── sails-disk
│   └── sails-hook-orm
└── yarn.lock

You'll need to add these scripts in your package.json:

{
  "scripts": {
    "build": "babel src --out-dir dist --source-maps --copy-files",
    "dev": "node -r babel-register src/app.js",
    "start": "node dist/app.js"
  }
}

To start your project in development, you'd run yarn dev or npm run dev. To start your project in production you'd run yarn build && yarn start or npm run build && npm start.

devDependencies you'll need: babel-register, babel-cli.

My app.js looks like:

import 'dotenv/config';
import path from 'path';
import rc from 'sails/accessible/rc';
import sails from 'sails';

const config = rc('sails');
config.appPath = __dirname;
config.paths = Object.assign(config.paths || {}, {
  public: path.join(__dirname, '..', 'assets'),
});
sails.lift(config);

I'm using babel-preset-env and quite a few other plugins in my .babelrc.

I submitted these changes in a PR to sails a while ago, but seems it was missed.

@stokesbga
Copy link
Author

stokesbga commented Aug 14, 2018

Yep, pretty much what I was going to say. Use babel-cli to generate a build folder and deploy that.

I have a package script that creates build folder, ignores some shit I only use in dev, then deploys that to production branch in git. Production branch has a hook that deploys.

"build": "npm run clean && babel -d ./build . -s --ignore build,node_modules --copy-files && rm -r build/keys && rm -r build/config/babel.js && rm -r build/config/babel.js.map",

Clean just empties the build dir and pulls latest from prod branch

Let me/us know how it goes.

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

No branches or pull requests

3 participants