# sewing_kit [![Build status](https://badge.buildkite.com/e7cdc87e61b9fe80e91e8686b6bfba53ca26a36366eb811a50.svg)](https://buildkite.com/shopify/sewing-kit-gem-ci) Zero configuration, high performance front end development at organization scale. `sewing_kit` facilitates legacy Rails integration using ERB tags. For a more complete modern stack with performance-as-a-feature, consider [quilt_rails](https://github.com/Shopify/quilt/tree/master/gems/quilt_rails). For details of the `sewing-kit` node package's configuration and usage, see the [sewing-kit README](/README.md). ## Quick Start Create a Rails project using `dev init` then: ### Install Sewing Kits ```sh # Add Ruby/Node dependencies bundle add sewing_kit yarn add @shopify/sewing-kit # Optional - add Polaris yarn add @shopify/polaris react react-dom yarn dev up ``` ### Add JavaScript sewing_kit looks for JavaScript in `app/ui/index.js`. The code in `index.js` (and any imported JS/CSS) will be built into a `main` bundle. ### Link to JS/CSS with `erb` Helpers The `main` bundle is imported into `erb` files using Rails helpers: ```erb <%= sewing_kit_link_tag *sewing_kit_assets('main', extension: 'css') %> <%= sewing_kit_script_tag *sewing_kit_assets('main') %> ``` **Note:** CSS `` tags appear only in production; in development, CSS is embedded within the `main.js` bundle. ## Minimal Project Layout ``` ├── Gemfile (must contain "gem 'sewing_kit") ├── package.json (must specify '@shopify/sewing-kit' as a 'devDependency') │ └── app └── ui │ └─- index.js └── views └─- layouts └─- application.html.erb (must link to JS / CSS using sewing_kit_script_tag / sewing_kit_link_tag ``` ## Rails, Polaris, and React Layout A typical Polaris app will use React to render views and components. The following layout shows best practice locations for: - Global SCSS settings - App sections (roughly analogous to Rails routes) - Components - Co-located CSS modules - Co-located unit tests ``` └── app └── ui ├─- index.js (renders React into the DOM) ├── styles (optional) │ └── settings.scss (global vars and @polaris overrides) │ └── tests (optional) │ └── each-test.ts │ └── setup.ts └── components (optional) ├── App │ ├── index.js │ ├── App.js │ └── tests │ └── App.test.js │ ├-─ MyComponent │ ├-─ index.js │ ├-─ MyComponent.js │ ├── MyComponent.scss (optional; component-scoped CSS styles, mixins, etc) │ └── tests │ └── MyComponent.test.js │ └── sections (optional; container views that compose presentation components into UI blocks) └── Home ├-─ index.js └── Home.js ``` ## React Boilerplate - Create a React app in `app/ui/App.js` ([example](https://github.com/Shopify/sewing-kit-gem-example/blob/master/app/ui/App.jsx)) - In an `erb` view, add a placeholder for React content ([example](https://github.com/Shopify/sewing-kit-gem-example/blob/master/app/views/home/index.html.erb#L4)) - In `index.js`, render a React component into the placeholder element ([example](https://github.com/Shopify/sewing-kit-gem-example/blob/master/app/ui/index.js)) - Use `sewing_kit_script_tag`/`sewing_kit_link_tag` helpers to link erb/js ([example](https://github.com/Shopify/sewing-kit-gem-example/blob/master/app/views/layouts/application.html.erb#L8)) ## Testing the front end For fast tests with consistent results, test front-end components using Jest instead of Rails integration tests. Use [`sewing-kit test`](https://github.com/Shopify/sewing-kit/blob/master/docs/commands/test.md#L3) to run all `.test.tsx` files in the `app/ui` directory. [Jest](https://jestjs.io) is used as a test runner, with customization available via [its sewing-kit plugin](https://github.com/Shopify/sewing-kit/blob/master/docs/plugins/jest.md). ### Testing React For testing React applications we provide and support [`@shopify/react-testing`](https://github.com/Shopify/quilt/tree/master/packages/react-testing). #### Example Given a component `MyComponent.tsx` ```tsx // app/components/MyComponent/MyComponent.tsx export function MyComponent({name}: {name: string}) { return