1

I am creating a web page using html,css and js and I am not following any framework so I have only page (that was the requirement). So I have lot of javascript in my page and I want to move my javascript to another script file. So I have two quesions: 1) What's the best way to do that? 2) Is there any way to call my functions as a property like we can do with node_modules.

var printMsg = require('printModule');
printMsg.print();

or in jquery ($.whatever())

Ask
  • 3,076
  • 6
  • 30
  • 63
  • 2
    You might look into Webpack, which allows for strong code organization: https://medium.com/the-node-js-collection/modern-javascript-explained-for-dinosaurs-f695e9747b70 – CertainPerformance Jun 27 '18 at 05:06
  • 1. [Move js script to external file](https://stackoverflow.com/questions/32942778/move-js-script-to-external-file) 2. [How do I include a JavaScript file in another JavaScript file?](https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file) – Saif Jun 27 '18 at 05:07

1 Answers1

1
  1. The currently best way to organize your project is through using Webpack. It's allow you to module your js code easily and they a have plugins for almost everything. Take a look at their guide as knowing webpack will make your life easier. Using their guide, It might take you 5-6 hours depend on your dedication.

  2. That's ES6 and knowing webpack will solve like 99% of your problems.

Here's my webpack-simple-template. You can take a look at how I set thing up.

GTHell
  • 603
  • 1
  • 8
  • 20
  • +1 for webpack. I moved away from grunt and gulp ever since I tried webpack. Popular framework such as Angular-cli adopted it. – Wei-jye Jun 27 '18 at 05:26