Webpack & TypeScript Setup #5 - Using ES6 Modules
Key Takeaways
Uses ES6 modules with Webpack and TypeScript
Full Transcript
okay then gang so sometimes in our project we might not always want to write all of our code inside one single file because that would make the file really long and hard to manage instead sometimes we like to split our code into different files now this makes our code more modular and more reusable and it makes it much easier to update as well now we saw an example of this when I showed you this diagram before so for example we might have an initial entry type secret file or JavaScript file in this case called a pas but then we can split our code into different modules or different files if you like like so so we could have a file or a module that just contains code that handles forms and we might have some kind of module that just contains code that handles database interactions so we can use different files and different modules in typescript and then have webpack combine all of those modules together into a single bundle file at the end so how do we do this exactly well first of all let me create another type script file and I'm going to call this forms CS and the job oops let me get this right yes the job of this file will be to contain code which is going to handle form submissions now we have a form right here remember with a few different imports and we can see that right here a name an email and an age so all we're going to do is handle the import from that form but it could be any form in the future so I'm actually just gonna paste some code right here which is from my github repo the link to that don't forget it's down below and all this is is a function stored in a constant called form data now if you're unaware of what any of this means definitely check out the typescript tutorial but basically inside this function we're taking in a parameter which were calling form and that must be of type HTML form elements so we pass in some kind of HTML form element that we get by querying the Dom into this thing right here this form data function and inside the function we then create a constant called inputs and we set that equal to the farm we pass in and we scope out a query selector to get all of the inputs from that form so in our case if we pass in the form right here it's going to grab all of these and store them in a collection right here called inputs now right here we say let value be of type and then this is an object where we're saying it can have multiple different properties and each property must be a string and the value of each property it must be a string as well now what ultimately going to be using this empty object to store all of the different key value pairs from our form so then we take the inputs and we cycle through those using for each we take each input in turn and then we take the values object right here and we create a property on this values object called input tag D or it's not called input ID we use the ID property from each inputs to create that property name now we have IDs on those name email and age so it's going to add a name property to the object an age property and an email property in turn and it's going to set the value of each one of those to the value of each input and that's going to be whatever a user types into these three things right here okay so that's all it's doing and then at the end is returning that values object so now we have that and say we want to use it inside this file right here all we need to do is say export like so and that exports this function and marks it for imports so we can now import it into another file so if I save that now and come over here I could import it inside this file and to do that I'd say import then curly braces and inside curly braces what do I want to import well I want to import this thing right here form data so I'm going to copy that and paste it right here and we need to say where this is front by saying from and then dot forward slash to say current directory because we're in the same directory as forms dot CS and then we want the forms file now we don't need to say je s or TS doesn't matter web pack is going to figure that out for us and we'll set up web pack shortly to do that so now we've imported that we could go ahead and use it so first of all we need to pass in a form to the function and I'm going to query the Dom to get that form right here so I'm going to say Const form is equal to documents doctor query select on and we want the form elements and we need to place an exclamation mark right here to say this is definitely going to be found otherwise typescript is going to throw a wobbler later on so now we have that form we can add an event listener to it and in fact so you don't have to watch me write this out because like I said this is not a typescript for beginners tutorial all I'm doing is pasting this in from my repo we have the form add an event listener submit event so when we click on the button we're taking the events parameter automatically and we prevent the default action when they submit occurs that means the page will then not reload as is the default behavior then we initialize a constant called data and set it equal to the form data function and pass in the form so remember whatever is returned from this is going to end up in this and that should be the values right so then we're just logging those values to the console so whatever we now do all of this stuff right here we should see all of those values logged as an object to the console so should this work well no we get an error when I've saved up and that's because it can't resolve forms so we need to make an edit to the webpack config file right here in order for this to work because we're trying to import this type script file and it doesn't know how to resolve it okay so all we need to do is add a property I'm going to add it above output but you can add it anywhere inside this module to export and it's called resolve and this is going to be an object in itself and then we have an extensions property to say what extensions we want webpack to be able to resolve so this is going to be an array and first of all I'm going to say dot es and also dot Jas so JavaScript extension files and also typescript extension files as well and now when we import something it's going to know how to resolve that file right here okay so we now need to restart this server down here because we've changed the webpack config file and going to press ctrl C and then yes to cancel out of this and then I'm gonna run NPM run server again fingers crossed this should work and it looks like we don't have any errors cool looking good and if we now enter in some information so Mario D male Mario that's the net ninja code at UK the age 35 press submit we should see that object down here which we do so we have a name property an email property and an age with those values so now we've seen how we can import different types group files into our entry file and we could do this in multiple different files as well so we could then create a third file and import that into forms or index again it really doesn't matter web pack is gonna then bundle all of those up into a single file and if we built this now it wouldn't build this into two files inside the public folder it would just combine everything and bundle them together into this one bundle dot J's file so that later on the browser only has one Network request to make for that one single file instead of many different files that you might create in the source or right here
Original Description
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
----------------------------------------
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Course Files:
https://github.com/iamshaunjp/webpack-and-typescript
🐱💻 🐱💻 Other Related Free Courses:
+ HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
+ Modern JavaScript - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
+ TypeScript Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gUgr39Q_yD6v-bSyMwKPUI
🐱💻 🐱💻 TypeScript Docs:
https://www.typescriptlang.org/docs/home.html
🐱💻 🐱💻 The Net Ninja Community Boards:
https://community.thenetninja.co.uk/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI