What is the difference between importing a js library or linking to it in the html <script> tag

I am currently working with webpack using js. I am new to this and am wondering about importing dependencies. Traditionally, we import a third-party library from the tag <script>into html. Now I can do it in javascript by running the code below. I wonder what the difference is between these two approaches. Are they imported into the same namespace? Is there any other difference?

import $ from 'jquery'
import React from 'react';
import ReactDOM from 'react-dom';
import load from 'little-loader';
+4
source share
1 answer

You will notice that Webpack generates a JS file into which tag included <script>. This is a "linked" file. There is always a tag on the page <script>.

Webpack/Browserify/etc. do, JS JS <script>. :

<script src="jquery.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>

... :

<script src="bundle.js" type="text/javascript"></script>

jQuery app.js bundle.js. Webpack , , jQuery app.js. :

import $ from 'jquery'

... ECMAScript 5:

var $ = require('jquery');

, jQuery, , 1) 2) .

+1

All Articles