|
1 | 1 | # JavaScript
|
2 | 2 |
|
3 |
| -```js |
4 |
| -mix.js(src | [src], output); |
5 |
| -``` |
6 |
| - |
7 |
| -With a single line of code, Laravel Mix allows you to trigger a number of vital actions. |
| 3 | +- [Basic Usage](#basic-usage) |
| 4 | +- [TypeScript Support](#typescript-support) |
8 | 5 |
|
9 |
| -- ES2017 + modules compilation |
10 |
| -- Build and compile `.vue` components \(via `vue-loader`\) |
11 |
| -- Hot module replacement |
12 |
| -- Tree-shaking, new in webpack 2 \(removes unused library code\) |
13 |
| -- Extract vendor libraries \(via `mix.extract()`\), for improved long-term caching |
14 |
| -- Automatic versioning \(query string hash\), via `mix.version()` |
15 |
| - |
16 |
| -### Usage |
| 6 | +### Basic Usage |
17 | 7 |
|
18 | 8 | ```js
|
19 |
| -let mix = require('laravel-mix'); |
20 |
| - |
21 |
| -// 1. A single src and output path. |
22 | 9 | mix.js('src/app.js', 'dist/app.js');
|
23 |
| - |
24 |
| -// 2. For additional src files that should be |
25 |
| -// bundled together: |
26 |
| -mix.js(['src/app.js', 'src/another.js'], 'dist/app.js'); |
27 |
| - |
28 |
| -// 3. For multiple entry/output points: |
29 |
| -mix.js('src/app.js', 'dist/').js('src/forum.js', 'dist/'); |
30 |
| -``` |
31 |
| - |
32 |
| -### Laravel Example |
33 |
| - |
34 |
| -Consider a typical Laravel install. By default, your JavaScript entry point will be located at `./resources/assets/js/app.js`. Let's prepare a `webpack.mix.js` file to compile that to `./public/js/app.js`. |
35 |
| - |
36 |
| -```js |
37 |
| -let mix = require('laravel-mix'); |
38 |
| - |
39 |
| -mix.js('resources/assets/js/app.js', 'public/js'); |
40 |
| -``` |
41 |
| - |
42 |
| -Done! Now, all of the bullet items above are available to you, and it required exactly one method call. |
43 |
| - |
44 |
| -To trigger the compilation, run `npm run dev` from the command line. |
45 |
| - |
46 |
| -### Vue Components |
47 |
| - |
48 |
| -Laravel Mix is mildly opinionated, and ships with compilation support for `.vue` component files. Don't worry: if you don't use Vue, you can ignore this entire section. |
49 |
| - |
50 |
| -Single file components are one of Vue's neatest features. One file to declare the template, script, and styling for a component? Yes, please! Let's try it out. |
51 |
| - |
52 |
| -##### ./resources/assets/js/app.js |
53 |
| - |
54 |
| -```js |
55 |
| -import Vue from 'vue'; |
56 |
| -import Notification from './components/Notification.vue'; |
57 |
| - |
58 |
| -new Vue({ |
59 |
| - el: '#app', |
60 |
| - components: { Notification } |
61 |
| -}); |
62 |
| -``` |
63 |
| - |
64 |
| -Above, we import Vue \(you'll want to first run `npm install vue --save-dev`\), require a `Notification` Vue component, and then build up our root Vue instance. |
65 |
| - |
66 |
| -**./resources/assets/js/components/Notification.vue** |
67 |
| - |
68 |
| -```js |
69 |
| -<template> |
70 |
| - <div class="notification"> |
71 |
| - {{ body }} |
72 |
| - </div> |
73 |
| -</template> |
74 |
| - |
75 |
| -<script> |
76 |
| - export default { |
77 |
| - data() { |
78 |
| - return { |
79 |
| - body: 'I am a notification.' |
80 |
| - } |
81 |
| - } |
82 |
| - } |
83 |
| -</script> |
84 |
| - |
85 |
| -<style> |
86 |
| - .notification { |
87 |
| - background: grey; |
88 |
| - } |
89 |
| -</style> |
90 | 10 | ```
|
91 | 11 |
|
92 |
| -If you're familiar with Vue, this should all look very familiar, so we'll move on. |
| 12 | +With a single method call, Laravel Mix allows you to trigger a variety of powerful actions. |
93 | 13 |
|
94 |
| -**./webpack.mix.js** |
| 14 | +- Compile the [latest JavaScript syntax](https://babeljs.io/docs/en/babel-preset-env). |
| 15 | +- Trigger hot module replacement (via the `npx mix watch --hot` command). |
| 16 | +- Enable tree-shaking (where supported) for smaller builds. |
| 17 | +- Automatically optimizate and minify, when building for production (`npx mix --production`). |
95 | 18 |
|
96 | 19 | ```js
|
97 | 20 | let mix = require('laravel-mix');
|
98 | 21 |
|
99 |
| -mix.js('resources/assets/js/app.js', 'public/js'); |
100 |
| -``` |
101 |
| - |
102 |
| -And that should do it! Run `npm run dev` to compile it all down. At this point, simply create an HTML file, import your `./js/app.js` bundle, and load the browser. Tada! |
| 22 | +// 1. Compile src/app.js to dist/app.js |
| 23 | +mix.js('src/app.js', 'dist'); |
103 | 24 |
|
104 |
| -### React Support |
| 25 | +// 2. Compile src/app.js to dist/foo.js |
| 26 | +mix.js('src/app.js', 'dist/foo.js'); |
105 | 27 |
|
106 |
| -Laravel Mix also ships with basic React support. Simply update your `mix.js()` call to `mix.react()`, and then use the exact same set of arguments. Behind the scenes, Mix will pull in and reference any necessary Babel plugins for React. |
| 28 | +// 3. Merge and compile multiple scripts to dist/app.js |
| 29 | +mix.js(['src/app.js', 'src/another.js'], 'dist/app.js'); |
107 | 30 |
|
108 |
| -```js |
109 |
| -mix.react('resources/assets/js/app.jsx', 'public/js/app.js'); |
| 31 | +// 4. Compile src/app.js to dist/app.js and src/forum.js to dist/forum.js |
| 32 | +mix.js('src/app.js', 'dist/').js('src/forum.js', 'dist/'); |
110 | 33 | ```
|
111 | 34 |
|
112 |
| -Of course, you'll still want to install React and ReactDOM through NPM, per usual, but everything else should be taken care of. |
113 |
| - |
114 | 35 | ### Typescript Support
|
115 | 36 |
|
116 | 37 | Laravel Mix also ships with basic Typescript support. Simply update your `mix.js()` call to `mix.ts()`, and then use the exact same set of arguments.
|
117 | 38 |
|
118 | 39 | ```js
|
119 |
| -mix.ts('resources/assets/js/app.ts', 'public/js/app.js'); |
| 40 | +mix.ts('src/app.js', 'dist'); |
120 | 41 | ```
|
121 | 42 |
|
122 |
| -Of course, you'll still want to do the necessary tweeks like creating `tsconfig.json` file and installing [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped), but everything else should be taken care of. |
| 43 | +Of course, you'll still want to handle any TypeScript-specific tweeks like creating a `tsconfig.json` file and installing [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped), but everything else should be taken care of. |
0 commit comments