Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38818

Re: Using a single module

Subject Re: Using a single module
Newsgroups comp.lang.javascript
References <67Wdnb4wtM6Kr_nCnZ2dnUU7-aPNnZ2d@westnet.com.au> <d2c6b62b-e3cc-4d6c-b711-01bc3d6be789n@googlegroups.com>
From Andrew Poulos <ap_prog@hotmail.com>
Date 2020-09-20 15:27 +1000
Message-ID <44Odnb2C14DRdfvCnZ2dnUU7-IVQAAAA@westnet.com.au> (permalink)

Show all headers | View raw


On 19/09/2020 4:06 am, Michael Haufe (TNO) wrote:
> On Thursday, September 17, 2020 at 11:05:58 PM UTC-5, Andrew Poulos wrote:
>> I'm trying my first use of modules and there some concepts that escape
>> me despite goog's help.
>>
>> If I find an ES module I'd like to use I can add it to the head of the
>> page eg
>> <script type="module" src="modules/jspdf.es.min.js"></script>
>>
>> To use it in my code I need the script element that includes index.js to
>> have type="module" eg
>> <script type="module" src="scripts/index.js"></script>
>>
>> In index.js I have: import { ...
>>
>> If I have other script files referenced in my page eg
>> <script src="scripts/two.js"></script>
>>
>> and two.js wants to reference functions in index.js then it seems I need to
>> a. export the functions in index.js
>> b. add type="module" to the two.js' script element
>>
>> Then if there's another js file, third.js, that references a function in
>> index.js then I need to do the same. And so on for every other js file
>> that references function in files that are included as modules.
>>
>> So I end up with every script element having type="module" and every js
>> file exporting functions even though there's only one actual module I'd
>> like to use. Is that correct or am I missing something?
> 
> I'd suggest you use only the index.js module.
> 
> ```
> <!DOCTYPE html>
> <html>
> <head>
>      <meta charset='utf-8'>
>      <meta http-equiv='X-UA-Compatible' content='IE=edge'>
>      <title>Hello World</title>
>      <meta name='viewport' content='width=device-width, initial-scale=1'>
>      <script type="module" src='./main.js' defer></script>
> </head>
> <body>
>    <noscript>JavaScript must be enabled</noscript>
> </body>
> </html>
> ```
> 
> Inside that entry point module import the other dependencies.
> For importing other modules use:
> 
> ```
> import a from './module-a.js'
> ```
> 
> For importing code from non-modules:
> 
> ```
> import './foo.js'

Hmm, so using a module where the rest of the code has not been designed 
to use modules is not just a case of drop a file into a folder...

> As Julio has mentioned, you can do this dynamically as well:
> 
> ```
> (async () => {
>      if (somethingIsTrue) {
>        // import module for side effects
>        await import('/modules/my-module.js');
>      }
> })();

If "somethingIsTrue" necessary because importing a module multiple times 
cause issues?

> Some more examples are available on MDN:
> 
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import>
> 
> If your non-module script wants to use a module the quickest hack might be assigning to the window object,
> but a better question might be: why can't the other files be converted to modules as well?

Perhaps if I'd known about modules before I started but I'm not going to 
introduce such a change midway.

Andrew Poulos

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using a single module Andrew Poulos <ap_prog@hotmail.com> - 2020-09-18 14:05 +1000
  Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-18 04:40 -0700
    Re: Using a single module Andrew Poulos <ap_prog@hotmail.com> - 2020-09-20 15:27 +1000
      Re: Using a single module Arno Welzel <usenet@arnowelzel.de> - 2020-09-20 11:27 +0200
        Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-20 02:38 -0700
          Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-20 02:51 -0700
  Re: Using a single module "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-09-18 11:06 -0700
    Re: Using a single module Andrew Poulos <ap_prog@hotmail.com> - 2020-09-20 15:27 +1000
    Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-20 02:46 -0700
  Re: Using a single module Arno Welzel <usenet@arnowelzel.de> - 2020-09-20 11:26 +0200
    Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-20 02:42 -0700
      Re: Using a single module Arno Welzel <usenet@arnowelzel.de> - 2020-09-24 19:34 +0200
        Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-24 11:16 -0700
          Re: Using a single module Arno Welzel <usenet@arnowelzel.de> - 2020-09-24 20:44 +0200
            Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-24 11:59 -0700
              Re: Using a single module Scott Sauyet <scott.sauyet@gmail.com> - 2020-09-24 19:55 -0700
                Re: Using a single module Julio Di Egidio <julio@diegidio.name> - 2020-09-24 20:13 -0700
                Re: Using a single module Arno Welzel <usenet@arnowelzel.de> - 2020-09-26 23:37 +0200
                Re: Using a single module Scott Sauyet <scott.sauyet@gmail.com> - 2020-09-26 15:41 -0700
                Re: Using a single module Jon Ribbens <jon+usenet@unequivocal.eu> - 2020-09-26 23:04 +0000
              Re: Using a single module Arno Welzel <usenet@arnowelzel.de> - 2020-09-26 23:36 +0200

csiph-web