Home
Forums
New posts
Search forums
What's new
New posts
New resources
New profile posts
Latest activity
Resources
Latest reviews
Search resources
Members
Current visitors
New profile posts
Search profile posts
DMCA Policy
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
FEEL FREE TO SHARE TUTORIALS, YOUR SKILS & KNOWLEDGE ON CODING, SCRIPTS, THEMES, PLUGINS OR ANY RESOURCES YOU HAVE WITH THE COMMUNITY-
Click Here To Post Your Request,
JOIN COMPUTER REPAIR FORUM
Home
Forums
TUTORIALS
CODING TUTORIALS
Node.js
Modern JavaScript Apps with Neutrino
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="codeguru" data-source="post: 53" data-attributes="member: 2"><p><img src="https://cdn-images-1.medium.com/max/1600/1*xArsbAlIa6VmtK0u-VuxTA.png" alt="Mozilla Neutrino" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>Utilize tooling that harnesses the power of Webpack with ease of simple presets to quickly start JavaScript projects, all without upfront configuration.</p><p></p><p>Taking the plunge into starting a new JS project often brings along a significant effort into preparing your environment prior to starting development on the project. Many developers have a preference for using cutting-edge features and modern tooling workflows to make the process enjoyable. Unfortunately, this approach can often have quite a bit of learning curve as people spend time researching best practices, following complex configuration tutorials, and experimenting with boilerplates. What if we could take the work that the community has put into configuring their own projects, and capture that into shareable packages that anyone can use? This is why we created Neutrino.</p><p></p><p><a href="https://neutrino.js.org" target="_blank"><strong>Neutrino</strong></a> is a tool that combines the build and configuration power of Webpack and bolts on the capability to build JavaScript-based projects with presets. A preset is the fundamental building block of Neutrino. With it you can supplement a project with a number of features including how it is compiled, ensuring code quality, and even quickly add testing. By breaking up configuration into composable units we can foster an ecosystem where customizing a project becomes plug and play.</p><p></p><p>To get an idea of how easy it is to get started with Neutrino, I’ll walk through creating a couple simple projects.</p><p></p><p><em>Note: In the upcoming examples I am using the </em><a href="https://yarnpkg.com" target="_blank"><em>Yarn package manager</em></a><em> for installing dependencies and creating scripts. This is only my personal preference, and you are free to use the npm client if you desire.</em></p><p></p><h3>Node.js Quickstart</h3><p></p><p></p><p>To get started with our first Neutrino-based Node.js project, we are going to be using <a href="https://www.npmjs.com/package/neutrino-preset-node" target="_blank">neutrino-preset-node</a>. According to its documentation it enables:</p><p></p><ul> <li data-xf-list-type="ul">No upfront configuration, and easy overrides if necessary</li> <li data-xf-list-type="ul">Compiles to support Node.js v6.9+, ES Modules, Async Functions</li> <li data-xf-list-type="ul">Auto-wired sourcemaps</li> </ul><p></p><p>Cool, let’s get started!</p><p></p><p>First up, we need a directory to start working from. From your terminal, create a new directory and change into it. Then we are going to install [ICODE]neutrino[/ICODE] and [ICODE]neutrino-preset-node[/ICODE] as development dependencies.</p><p></p><p>❯ mkdir project && cd project</p><p>❯ yarn add --dev neutrino neutrino-preset-node</p><p></p><p>By default the Node.js preset looks for source code in a [ICODE]src[/ICODE] directory, with the main entry point being [ICODE]index.js[/ICODE]. Let’s create this file and edit it, just with a simple HTTP server that echoes whatever we send to it.</p><p></p><p>import { createServer } from 'http';</p><p></p><p>const port = process.env.PORT || 3000;</p><p></p><p>createServer(({ url }, response) => {</p><p> console.log(`Received message at ${url}`);</p><p> response.end(url.slice(1));</p><p>})</p><p>.listen(port, () => console.log(`Running on :${port}`));</p><p></p><p>Next, let’s add a couple scripts to our package.json which will give us some easy commands to start and build our app:</p><p></p><p>{</p><p> "scripts": {</p><p> "start": "neutrino start --presets neutrino-preset-node",</p><p> "build": "neutrino build --presets neutrino-preset-node",</p><p> "serve": "yarn start && node build"</p><p></p><p> },</p><p> "devDependencies": {</p><p> "neutrino": "^4.0.1",</p><p> "neutrino-preset-node": "^4.0.1"</p><p> }</p><p>}</p><p></p><p>We are ready to start our app! Using [ICODE]yarn serve[/ICODE] in one terminal, and [ICODE]curl[/ICODE] in another, we can see our code in action:</p><p></p><p>❯ yarn serve</p><p>Warning: This preset does not support watch compilation. Falling back to a one-time build. Hash: 8fa3faf9cbe8ca235884 Version: webpack 2.2.1 Time: 632ms Asset Size Chunks Chunk Names index.js 3.6 kB 0 [emitted] index index.js.map 3.53 kB 0 [emitted] index Running on :3000 --- ❯ curl <a href="http://localhost:3000/Hello\" target="_blank">http://localhost:3000/Hello\</a>! Hello!</p><p></p><p></p><p>Yep. That’s it.</p><p></p><p>No upfront cost needed to start your project with a completely modern toolchain.</p><p></p><h3>React Quickstart</h3><p></p><p></p><p>For fun, let’s just change this project from Node.js to React. According to the <a href="https://neutrino.js.org/presets/neutrino-preset-react/" target="_blank">Neutrino documentation,</a> the React preset features:</p><p></p><ul> <li data-xf-list-type="ul">JSX syntax, ES Modules, support for last 2 browser versions, and Async Functions</li> <li data-xf-list-type="ul">Support for import CSS, HTML, images, fonts, and icons directly from JavaScript</li> <li data-xf-list-type="ul">Hot module replacement, no HTML templating, and much more</li> </ul><p></p><p>Let’s swap presets and install some React dependencies.</p><p></p><p>❯ yarn remove neutrino-preset-node && yarn add --dev neutrino-preset-react</p><p>❯ yarn add react react-dom</p><p></p><p>Our package.json commands need to be changed to use the React preset now:</p><p></p><p>{</p><p> "scripts": {</p><p> "start": "neutrino start --presets neutrino-preset-react",</p><p> "build": "neutrino build --presets neutrino-preset-react"</p><p> },</p><p>}</p><p></p><p>Instead of creating a Node.js server, let’s render some content to a web app. By convention our preset allows us to mount our application at ID “root”:</p><p></p><p>import React from 'react';</p><p>import { render } from 'react-dom';</p><p></p><p>render((</p><p> <main></p><p> <h1>Hello! <img src="https://s.w.org/images/core/emoji/11/72x72/1f60e.png" alt="😎" class="fr-fic fr-dii fr-draggable " style="" /></h1></p><p> </main></p><p>), document.getElementById('root'));</p><p></p><p>Back to the terminal, and we can start up our app, and load it up in the browser:</p><p></p><p>❯ yarn start</p><p>✔ Development server running on: <a href="http://localhost:5000" target="_blank">http://localhost:5000</a> ✔ Build completed </p><p><img src="https://cdn-images-1.medium.com/max/1600/1*sQzz8Rbn90uCFqZDkD9lWA.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>Hopefully that demonstrates how simple it is to get up and running with a new React project! If you aren’t working with React for your web project, consider using [ICODE]neutrino-preset-web[/ICODE] for other libraries or generic web applications.</p><p></p><h3>Multiple presets</h3><p></p><p>Neutrino makes it simple to compose multiple presets together. To demonstrate, let’s add a linting preset which will conform our project to the <a href="https://github.com/airbnb/javascript" target="_blank">Airbnb style guide</a>. Install [ICODE]neutrino-preset-airbnb-base[/ICODE]:</p><p></p><p>❯ yarn add --dev neutrino-preset-airbnb-base</p><p></p><p>To reduce some repetition, we are going to take advantage of a Neutrino feature which will pull from an array of presets in our package.json. This saves us from having to name all the presets we want to use for every command. Remove the presets from the script commands and move them to [ICODE]config.presets[/ICODE].</p><p></p><p>{</p><p> "config": {</p><p> "presets": [</p><p> "neutrino-preset-airbnb-base",</p><p> "neutrino-preset-react"</p><p> ]</p><p> },</p><p> "scripts": {</p><p> "start": "neutrino start",</p><p> "build": "neutrino build"</p><p> }</p><p>}</p><p></p><p>Note: [ICODE]neutrino-preset-airbnb-base[/ICODE] needs to be loaded before our React preset <a href="https://neutrino.js.org/presets/neutrino-preset-airbnb-base/" target="_blank">according to the documentation</a>.</p><p></p><p>If we modify our code and introduce something in violation of the preset, we are clearly notified in the console:</p><p></p><p>❯ yarn start</p><p>✔ Development server running on: <a href="http://localhost:5000" target="_blank">http://localhost:5000</a> ✔ Build completed ERROR in ./src/index.js /node-project/src/index.js 6:10 error Strings must use singlequote quotes ✖ 1 problem (1 error, 0 warnings)</p><p></p><h4>Testing, too!</h4><p></p><p>Let’s quickly add a simple Jest test, because why not? The <a href="https://neutrino.js.org/presets/neutrino-preset-jest/" target="_blank">Neutrino preset </a>[ICODE][URL='https://neutrino.js.org/presets/neutrino-preset-jest/']neutrino-preset-jest[/URL][/ICODE] uses a convention of a [ICODE]test[/ICODE] directory, with some expectations on file extensions:</p><p></p><p>❯ yarn add --dev neutrino-preset-jest</p><p>❯ mkdir test && touch test/add.test.js</p><p>❯ touch src/add.js</p><p></p><p>Let’s write a quick test which verifies a function correctly performs simple addition, which we will shortly create:</p><p></p><p>import add from '../src/add';</p><p></p><p>describe('addition', () => {</p><p> it('adds 2 numbers', () => {</p><p> expect(add(3, 5)).toBe(8);</p><p> });</p><p>});</p><p></p><p>Now our addition module in [ICODE]src/add.js[/ICODE]:</p><p></p><p>export default (x, y) => x + y;</p><p></p><p>Edit the package.json once more, adding the Jest preset to our list, along with a command to run tests:</p><p></p><p>{</p><p> "config": {</p><p> "presets": [</p><p> "neutrino-preset-airbnb-base",</p><p> "neutrino-preset-react",</p><p> "neutrino-preset-jest"</p><p> ]</p><p> },</p><p> "scripts": {</p><p> "start": "neutrino start",</p><p> "build": "neutrino build",</p><p> "test": "neutrino test"</p><p> }</p><p>}</p><p></p><p>Let’s run the test!</p><p></p><p>❯ yarn test</p><p>PASS test/add.test.js addition ✓ adds 2 numbers (3ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.228s Ran all test suites. </p><p>If we had made a mistake writing our addition module and accidentally used multiplication:</p><p></p><p>export default (x, y) => x * y;</p><p></p><p>This would have caused the test to fail:</p><p></p><p>❯ yarn test</p><p>FAIL test/add.test.js ● addition › adds 2 numbers expect(received).toBe(expected) Expected value to be (using ===): 8 Received: 15 at Object.<anonymous> (test/add.test.js:5:38) at process._tickCallback (internal/process/next_tick.js:103:7) addition ✕ adds 2 numbers (5ms) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 1.221s Ran all test suites.</p><h3>Modifying the build</h3><p></p><p>One of the great features of Neutrino is that you don’t have to trade simplicity for lock-in or lack of extensibility. By following the <a href="https://neutrino.js.org/customization/" target="_blank">documentation</a> you can supplement your project build process with additional features that don’t come with your preset. If you find you use these features or changes across multiple projects, you can roll that into your <a href="https://neutrino.js.org/creating-presets.html" target="_blank">own preset</a>, and share it with your team and the community!</p><p></p><h3>Conclusion</h3><p></p><p>Making Neutrino into the tool it is today has been a lot of hard work, but we hope you enjoy it. Try it in your projects, give feedback, create your own presets, and share with others. We want to see everyone be successful with Neutrino.</p><p></p><p>If you would like to get involved with development or documentation, please visit the contributing section of the docs for complete details, or visit our GitHub repo.</p><p></p><p><strong>Neutrino Documentation:</strong> <a href="https://neutrino.js.org/" target="_blank">https://neutrino.js.org/</a></p><p></p><p><strong>Neutrino GitHub:</strong> <a href="https://github.com/mozilla-neutrino/neutrino-dev" target="_blank">https://github.com/mozilla-neutrino/neutrino-dev</a></p><p></p><p>Thanks! </p><p><em>Eli Perelman & Hassan Ali — Mozilla</em></p><p></p><p>The post <a href="https://davidwalsh.name/neutrino" target="_blank">Modern JavaScript Apps with Neutrino</a> appeared first on <a href="https://davidwalsh.name" target="_blank">David Walsh Blog</a>.</p><p></p><p><a href="https://tkjs.us/dwb" target="_blank"><img src="https://davidwalsh.name/demo/tjs_block-1.svg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></a></p><p></p><p><a href="https://davidwalsh.name/neutrino" target="_blank">Continue reading...</a></p></blockquote><p></p>
[QUOTE="codeguru, post: 53, member: 2"] [IMG alt="Mozilla Neutrino"]https://cdn-images-1.medium.com/max/1600/1*xArsbAlIa6VmtK0u-VuxTA.png[/IMG] Utilize tooling that harnesses the power of Webpack with ease of simple presets to quickly start JavaScript projects, all without upfront configuration. Taking the plunge into starting a new JS project often brings along a significant effort into preparing your environment prior to starting development on the project. Many developers have a preference for using cutting-edge features and modern tooling workflows to make the process enjoyable. Unfortunately, this approach can often have quite a bit of learning curve as people spend time researching best practices, following complex configuration tutorials, and experimenting with boilerplates. What if we could take the work that the community has put into configuring their own projects, and capture that into shareable packages that anyone can use? This is why we created Neutrino. [URL='https://neutrino.js.org'][B]Neutrino[/B][/URL] is a tool that combines the build and configuration power of Webpack and bolts on the capability to build JavaScript-based projects with presets. A preset is the fundamental building block of Neutrino. With it you can supplement a project with a number of features including how it is compiled, ensuring code quality, and even quickly add testing. By breaking up configuration into composable units we can foster an ecosystem where customizing a project becomes plug and play. To get an idea of how easy it is to get started with Neutrino, I’ll walk through creating a couple simple projects. [I]Note: In the upcoming examples I am using the [/I][URL='https://yarnpkg.com'][I]Yarn package manager[/I][/URL][I] for installing dependencies and creating scripts. This is only my personal preference, and you are free to use the npm client if you desire.[/I] [HEADING=2]Node.js Quickstart[/HEADING] To get started with our first Neutrino-based Node.js project, we are going to be using [URL='https://www.npmjs.com/package/neutrino-preset-node']neutrino-preset-node[/URL]. According to its documentation it enables: [LIST] [*]No upfront configuration, and easy overrides if necessary [*]Compiles to support Node.js v6.9+, ES Modules, Async Functions [*]Auto-wired sourcemaps [/LIST] Cool, let’s get started! First up, we need a directory to start working from. From your terminal, create a new directory and change into it. Then we are going to install [ICODE]neutrino[/ICODE] and [ICODE]neutrino-preset-node[/ICODE] as development dependencies. ❯ mkdir project && cd project ❯ yarn add --dev neutrino neutrino-preset-node By default the Node.js preset looks for source code in a [ICODE]src[/ICODE] directory, with the main entry point being [ICODE]index.js[/ICODE]. Let’s create this file and edit it, just with a simple HTTP server that echoes whatever we send to it. import { createServer } from 'http'; const port = process.env.PORT || 3000; createServer(({ url }, response) => { console.log(`Received message at ${url}`); response.end(url.slice(1)); }) .listen(port, () => console.log(`Running on :${port}`)); Next, let’s add a couple scripts to our package.json which will give us some easy commands to start and build our app: { "scripts": { "start": "neutrino start --presets neutrino-preset-node", "build": "neutrino build --presets neutrino-preset-node", "serve": "yarn start && node build" }, "devDependencies": { "neutrino": "^4.0.1", "neutrino-preset-node": "^4.0.1" } } We are ready to start our app! Using [ICODE]yarn serve[/ICODE] in one terminal, and [ICODE]curl[/ICODE] in another, we can see our code in action: ❯ yarn serve Warning: This preset does not support watch compilation. Falling back to a one-time build. Hash: 8fa3faf9cbe8ca235884 Version: webpack 2.2.1 Time: 632ms Asset Size Chunks Chunk Names index.js 3.6 kB 0 [emitted] index index.js.map 3.53 kB 0 [emitted] index Running on :3000 --- ❯ curl [URL]http://localhost:3000/Hello\[/URL]! Hello! Yep. That’s it. No upfront cost needed to start your project with a completely modern toolchain. [HEADING=2]React Quickstart[/HEADING] For fun, let’s just change this project from Node.js to React. According to the [URL='https://neutrino.js.org/presets/neutrino-preset-react/']Neutrino documentation,[/URL] the React preset features: [LIST] [*]JSX syntax, ES Modules, support for last 2 browser versions, and Async Functions [*]Support for import CSS, HTML, images, fonts, and icons directly from JavaScript [*]Hot module replacement, no HTML templating, and much more [/LIST] Let’s swap presets and install some React dependencies. ❯ yarn remove neutrino-preset-node && yarn add --dev neutrino-preset-react ❯ yarn add react react-dom Our package.json commands need to be changed to use the React preset now: { "scripts": { "start": "neutrino start --presets neutrino-preset-react", "build": "neutrino build --presets neutrino-preset-react" }, } Instead of creating a Node.js server, let’s render some content to a web app. By convention our preset allows us to mount our application at ID “root”: import React from 'react'; import { render } from 'react-dom'; render(( <main> <h1>Hello! [IMG alt="😎"]https://s.w.org/images/core/emoji/11/72x72/1f60e.png[/IMG]</h1> </main> ), document.getElementById('root')); Back to the terminal, and we can start up our app, and load it up in the browser: ❯ yarn start ✔ Development server running on: [URL]http://localhost:5000[/URL] ✔ Build completed [IMG]https://cdn-images-1.medium.com/max/1600/1*sQzz8Rbn90uCFqZDkD9lWA.png[/IMG] Hopefully that demonstrates how simple it is to get up and running with a new React project! If you aren’t working with React for your web project, consider using [ICODE]neutrino-preset-web[/ICODE] for other libraries or generic web applications. [HEADING=2]Multiple presets[/HEADING] Neutrino makes it simple to compose multiple presets together. To demonstrate, let’s add a linting preset which will conform our project to the [URL='https://github.com/airbnb/javascript']Airbnb style guide[/URL]. Install [ICODE]neutrino-preset-airbnb-base[/ICODE]: ❯ yarn add --dev neutrino-preset-airbnb-base To reduce some repetition, we are going to take advantage of a Neutrino feature which will pull from an array of presets in our package.json. This saves us from having to name all the presets we want to use for every command. Remove the presets from the script commands and move them to [ICODE]config.presets[/ICODE]. { "config": { "presets": [ "neutrino-preset-airbnb-base", "neutrino-preset-react" ] }, "scripts": { "start": "neutrino start", "build": "neutrino build" } } Note: [ICODE]neutrino-preset-airbnb-base[/ICODE] needs to be loaded before our React preset [URL='https://neutrino.js.org/presets/neutrino-preset-airbnb-base/']according to the documentation[/URL]. If we modify our code and introduce something in violation of the preset, we are clearly notified in the console: ❯ yarn start ✔ Development server running on: [URL]http://localhost:5000[/URL] ✔ Build completed ERROR in ./src/index.js /node-project/src/index.js 6:10 error Strings must use singlequote quotes ✖ 1 problem (1 error, 0 warnings) [HEADING=3]Testing, too![/HEADING] Let’s quickly add a simple Jest test, because why not? The [URL='https://neutrino.js.org/presets/neutrino-preset-jest/']Neutrino preset [/URL][ICODE][URL='https://neutrino.js.org/presets/neutrino-preset-jest/']neutrino-preset-jest[/URL][/ICODE] uses a convention of a [ICODE]test[/ICODE] directory, with some expectations on file extensions: ❯ yarn add --dev neutrino-preset-jest ❯ mkdir test && touch test/add.test.js ❯ touch src/add.js Let’s write a quick test which verifies a function correctly performs simple addition, which we will shortly create: import add from '../src/add'; describe('addition', () => { it('adds 2 numbers', () => { expect(add(3, 5)).toBe(8); }); }); Now our addition module in [ICODE]src/add.js[/ICODE]: export default (x, y) => x + y; Edit the package.json once more, adding the Jest preset to our list, along with a command to run tests: { "config": { "presets": [ "neutrino-preset-airbnb-base", "neutrino-preset-react", "neutrino-preset-jest" ] }, "scripts": { "start": "neutrino start", "build": "neutrino build", "test": "neutrino test" } } Let’s run the test! ❯ yarn test PASS test/add.test.js addition ✓ adds 2 numbers (3ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.228s Ran all test suites. If we had made a mistake writing our addition module and accidentally used multiplication: export default (x, y) => x * y; This would have caused the test to fail: ❯ yarn test FAIL test/add.test.js ● addition › adds 2 numbers expect(received).toBe(expected) Expected value to be (using ===): 8 Received: 15 at Object.<anonymous> (test/add.test.js:5:38) at process._tickCallback (internal/process/next_tick.js:103:7) addition ✕ adds 2 numbers (5ms) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 1.221s Ran all test suites. [HEADING=2]Modifying the build[/HEADING] One of the great features of Neutrino is that you don’t have to trade simplicity for lock-in or lack of extensibility. By following the [URL='https://neutrino.js.org/customization/']documentation[/URL] you can supplement your project build process with additional features that don’t come with your preset. If you find you use these features or changes across multiple projects, you can roll that into your [URL='https://neutrino.js.org/creating-presets.html']own preset[/URL], and share it with your team and the community! [HEADING=2]Conclusion[/HEADING] Making Neutrino into the tool it is today has been a lot of hard work, but we hope you enjoy it. Try it in your projects, give feedback, create your own presets, and share with others. We want to see everyone be successful with Neutrino. If you would like to get involved with development or documentation, please visit the contributing section of the docs for complete details, or visit our GitHub repo. [B]Neutrino Documentation:[/B] [URL]https://neutrino.js.org/[/URL] [B]Neutrino GitHub:[/B] [URL]https://github.com/mozilla-neutrino/neutrino-dev[/URL] Thanks! [I]Eli Perelman & Hassan Ali — Mozilla[/I] The post [URL='https://davidwalsh.name/neutrino']Modern JavaScript Apps with Neutrino[/URL] appeared first on [URL='https://davidwalsh.name']David Walsh Blog[/URL]. [URL='https://tkjs.us/dwb'][IMG]https://davidwalsh.name/demo/tjs_block-1.svg[/IMG][/URL] [url="https://davidwalsh.name/neutrino"]Continue reading...[/url] [/QUOTE]
Insert quotes…
Verification
Post reply
Richest Freecoded User
Most Freecoin
freecoded
4,838 Freecoin
Davy200
590 Freecoin
J
Johnhendrick
575 Freecoin
S
Smith16
527 Freecoin
nathan69
426 Freecoin
Laureine
415 Freecoin
A
anajeen
370 Freecoin
C
codeguru
287 Freecoin
Tekera
267 Freecoin
A
Akubay
170 Freecoin
Home
Forums
TUTORIALS
CODING TUTORIALS
Node.js
Modern JavaScript Apps with Neutrino
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top