12.12.2018 Views

Web_Designer_UK__May_2018

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Developer tutorials<br />

Deploy TypeScript with React<br />

Below Thanks to the use of the ‘./’ syntax, the ‘config’ file can sit above the source folders<br />

“target”: “es5”,<br />

“jsx”: “react”<br />

},<br />

“include”: [<br />

“./src/**/*”<br />

]}<br />

6. Understand the structure<br />

Oneinterestingaspectofthe‘tsconfig’fileinvolvesthe<br />

useof‘./.’Asingledotrepresentsthelocalfolder,while..<br />

representsthefolder‘above’thecurrentone.Inourcase,<br />

‘./dist/’pointsatthedistfolderbelowthelocationofthe<br />

configuration file.<br />

7. Configure webpack<br />

Asworkstationsage,yourNode.JSconfigcangetmessed<br />

up.Thisisparticularlyproblematicforwebpack:ifthe<br />

productcan’tberunfromyourshell,installitagainlocally.<br />

Next,usethenpmbintofindthefoldercontainingit,and<br />

invokeitfromtheretogettheerrormessageshown<br />

accompanying this step.<br />

8. Get configuring<br />

Createafilecalled‘webpack.config.js’intherootfolderof<br />

the current project. Next, replace its contents with the<br />

onesshownaccompanyingthisstep–wewilldiscuss<br />

relevantpartsoftheconfigurationfilenext.<br />

module.exports = {<br />

entry: “./src/index.tsx”,<br />

output: {<br />

filename: “bundle.js”,<br />

path: __dirname + “/dist”<br />

},<br />

devtool: “source-map”,<br />

resolve: {<br />

extensions: [“.ts”, “.tsx”, “.js”, “.<br />

json”]<br />

},<br />

module: {<br />

rules: [<br />

{ test: /\.tsx?$/, loader: “awesometypescript-loader”<br />

},<br />

{ enforce: “pre”, test: /\.js$/, loader:<br />

“source-map-loader” }<br />

]<br />

},<br />

externals: {<br />

“react”: “React”,<br />

“react-dom”: “ReactDOM” },<br />

};<br />

9. Do the module!<br />

webpack’s packaging process can be expanded by<br />

loading so-called modules. Think of them like expansion<br />

projects for XNA’s legendary content pipeline. In our case,<br />

both JS and TS files are declared to be processable.<br />

10. Externalise stuff<br />

Bundling large libraries into your webpack output is not<br />

sensible because browser caching and CDNs get<br />

disabled. Using the externals module allows you to mark<br />

parts of the codebase as ‘foreign’, which changes the<br />

mode used for their inclusion.<br />

Even more options!<br />

The‘AwesomeTypeScriptLoader’canbe<br />

consideredtheSwissArmyknifeofTypeScript–<br />

look at its options described at https://github.<br />

com/s-panferov/awesome-typescript-loader to<br />

adjust its behaviour even further.<br />

Above Index.htm must stand outside of the ‘/src’ folder and its contents<br />

Top<br />

If webpack cannot be<br />

found, re-install it and<br />

run it from the npm<br />

bin folder<br />

Bottom<br />

Microsoft’s preferred<br />

arrangement does not<br />

work with recent<br />

versions of webpack<br />

88 _________________________________________________tutorial

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!