1 | <table>
|
2 | <tr><th>Compatible Runners</th><td>
|
3 |
|
4 | - [Gulp](05_Packages/02_crafty-runner-gulp.md)
|
5 | - [rollup.js](05_Packages/02_crafty-runner-rollup.md)
|
6 | - [Webpack](05_Packages/02_crafty-runner-webpack.md)
|
7 |
|
8 | </td></tr>
|
9 | <tr><th>Test Runners</th><td>
|
10 |
|
11 | - [Jest](05_Packages/05_crafty-preset-jest.md)
|
12 |
|
13 | </td></tr>
|
14 | </table>
|
15 |
|
16 | [TOC]
|
17 |
|
18 | ## Description
|
19 |
|
20 | **Babel** is the leading tool to compile EcmaScript 2015+ to EcmaScript 5, combined with **ESLint** to lint your code, you get the best preset to get started.
|
21 |
|
22 | ## Features
|
23 |
|
24 | `@swissquote/crafty-preset-babel` is able to configure **Babel** with **Webpack** and **rollup.js**. This preset also supports **Gulp** but in this case it concatenates and minifies the files, it doesn't resolve imports.
|
25 |
|
26 | [Our Babel preset](05_Packages/10_babel-preset-swissquote.md)
|
27 |
|
28 | [Read more](./JavaScript_Features.md)
|
29 |
|
30 | ## Linting
|
31 |
|
32 | In `@swissquote/crafty-preset-babel` JavaScript is linted with **ESLint**, a powerful linter that supports plugins, our configuration follows the Swissquote JavaScript Guideline through our `@swissquote/crafty-preset-eslint` preset.
|
33 |
|
34 | [Read more](../05_crafty-preset-eslint/JavaScript_Linting.md)
|
35 |
|
36 | ## Installation
|
37 |
|
38 | ```bash
|
39 | npm install @swissquote/crafty-preset-babel --save
|
40 | ```
|
41 |
|
42 | ```javascript
|
43 | module.exports = {
|
44 | presets: [
|
45 | "@swissquote/crafty-preset-babel",
|
46 | "@swissquote/crafty-runner-webpack", // optional
|
47 | "@swissquote/crafty-runner-gulp" // optional
|
48 | ],
|
49 | js: {
|
50 | app: {
|
51 | runner: "webpack", // Webpack, Gulp or rollup.js (optional if you have a single runner defined)
|
52 | source: "js/app.js"
|
53 | }
|
54 | }
|
55 | };
|
56 | ```
|
57 |
|
58 | ## Usage
|
59 |
|
60 | ### With Webpack / rollup.js
|
61 |
|
62 | Both runners have the same features in regards to Babel support.
|
63 | They will resolve your modules recursively and bundle them in one file or more if you do some code-splitting.
|
64 |
|
65 | #### JavaScript External assets
|
66 |
|
67 | By default, all bundlers include all external dependencies in the final bundle, this works fine for applications, but if you wish to build a multi-tenant application or a library, you don't wish to include all dependencies, because you'll end up with the same dependency duplicated.
|
68 |
|
69 | The `externals` option allows you to define a list of libraries that are provided and should not be embedded in the build, here is an example :
|
70 |
|
71 | ```javascript
|
72 | module.exports = {
|
73 | ...
|
74 | // patterns are strings or globs
|
75 | externals: ["react", "react-dom", "squp", "squp/**"],
|
76 | ...
|
77 | js: {
|
78 | app: {
|
79 | // You can provide more items here, they will be merged with the main list for this bundle
|
80 | externals: ["my-plugin", "my-plugin/**"]
|
81 | ...
|
82 | }
|
83 | }
|
84 | ...
|
85 | }
|
86 | ```
|
87 |
|
88 | In this example `react`, `react-dom` and all modules starting with `squp/` will be treated as external
|
89 |
|
90 | > You can see that globs were used here, note that they work for Webpack but rollup.js needs complete strings.
|
91 |
|
92 | ### With Gulp
|
93 |
|
94 | Gulp will not bundle your files like Webpack and rollup.js do, instead it will generate one output file per input file.
|
95 | This is useful if you are creating a library as it's the role of the final application to tree-shake what it doesn't need from your library.
|
96 |
|
97 | Tree-shaking is powerful but is sub-optimal on big files as some code patterns are recognized as side-effects and thus aren't removed from your bundle even if they aren't used.
|
98 |
|
99 | ## Usage with Jest
|
100 |
|
101 | If you include both `crafty-preset-babel` and `crafty-preset-jest`.
|
102 | When running your tests with `crafty test` this preset will be use to convert all `.js` and `.jsx` files (source and test files)
|
103 |
|
104 | ## Configuration
|
105 |
|
106 | ### Bundle options
|
107 |
|
108 | | Option | Type | Optional ? | Runner | Description |
|
109 | | -------- | ------- | ---------- | ------ | ------------------------------------------------------------------------------------------------------------------------------- |
|
110 | | `concat` | Boolean | Yes | Gulp | This will merge all files together, outputting a single file. (This doesn't resolve imports, use Webpack or rollup.js for this) |
|
111 |
|
112 | ### Adding Babel plugins and presets
|
113 |
|
114 | You can add, replace or remove plugins and add options to our default Babel configuration.
|
115 | To see which plugins are already included, you can go to the [Swissquote Preset for Babel](05_Packages/10_babel-preset-swissquote.md) page.
|
116 |
|
117 | ```javascript
|
118 | module.exports = {
|
119 | /**
|
120 | * Represents the extension point for Babel configuration
|
121 | * @param {Crafty} crafty - The instance of Crafty.
|
122 | * @param {Object} bundle - The bundle that is being prepared for build (name, input, source, destination)
|
123 | * @param {Object} babelConfig - The current Babel configuration
|
124 | */
|
125 | babel(crafty, bundle, babelConfig) {
|
126 | babelConfig.plugins.push(
|
127 | require.resolve("@babel/plugin-transform-property-literals")
|
128 | );
|
129 | }
|
130 | };
|
131 | ```
|
132 |
|
133 | After you did `npm install --save-dev babel-plugin-transform-es5-property-mutators` before, Babel will now use this plugin as well in each run.
|
134 |
|
135 | This method is called once per bundle, so you can customize each bundle's configuration differently.
|