es6-modules

How to declare global variable in module script

纵然是瞬间 提交于 2020-07-03 09:26:08
问题 I have a module script, in which I import some classes and initialize my app. But module scipts declare var myVar = "my"; in module context, what makes init variables unaccessible to main app code. But I don't know how to declare global variable in module script. 回答1: Instead of var myVar = "my"; use window.myVar = "my"; 来源: https://stackoverflow.com/questions/48612021/how-to-declare-global-variable-in-module-script

Can't figure out how to import modules in browser with javascript

别来无恙 提交于 2020-06-27 09:44:25
问题 This is a simple problem. I'm attempting to import modules from one javascript file to another, and then run it on Chrome. I'm using 2 javascript files and an html file, all in the same folder: first js file (testfile1.js): import {test} from 'testfile2.js'; test.func(); second js file (testfile2.js): let f = function() { console.log("TEST"); } let test = { func: f }; export test; The html file is plain, empty html file with a link to testfile1.js script in the header: <script type="text

how to reset module imported between tests

て烟熏妆下的殇ゞ 提交于 2020-06-25 09:12:17
问题 let's say I have a module that needs to be initialized once in the start of the app (to pass on configuration). module will look something like this : MyModule.js let isInitiazlied; const myModule = { init: function() { isInitiazlied = true; }, do: function() { if (!isInitiazlied) throw "error" //DO THINGS } } export default myModule; I want to unittest it, using jest. test file looks something like this : MyModule.test.js import myModule from './MyModule' describe('MyModule', () => {

Access variables defined in script tag with type module?

自作多情 提交于 2020-06-23 06:49:17
问题 I have a script tag inside my HTML with type module, and it exports a variable: <script type="module"> var life = 42; export { life }; </script> How do I access life from other script tags or the Chrome console for debugging? 回答1: First of all, you should be using export in separate js files and not embedded in an html file. I'm not sure if import works on html files. However, for a separate js file in another js file you can just use import {life} from 'path/to/file' to load the variables.

how to determine if javascript module was imported or loaded via script src?

China☆狼群 提交于 2020-05-31 04:29:29
问题 Suppose I have a module named module.js : export default function greet() { console.info( 'hello' ); } Within module.js (either inside or outside of function greet ), how can I determine whether the module was loaded using: <script type=module src="./module.js"> versus: <script type=module> import greet from './module.js'; </script> Either way, import.meta is the same, document.currentScript is null , and NodeJS's require (and therefore require.main ) and module are both undefined . Thanks!

How to export a variable that takes its value from an async function

青春壹個敷衍的年華 提交于 2020-05-29 04:05:48
问题 I apologize if my question sounds stupid, but I found that I need in many situations global variables such as ones that represent database and redis clients to be used in many files, however these variables themselves need to wait to get their values from promises or async functions that initialize the communication to the database or redis servers. I want to do something like that init.js: export default async () => { return await initializeWhatever() } db.js: import init from './init' let

How to make TypeScript output valid ES6 module import statements?

白昼怎懂夜的黑 提交于 2020-05-28 13:19:31
问题 All major browsers have supported ES6 modules for some time. These differ from many of the server-side approaches in that they need to specify the exact file to import from - they can't use file discovery. This makes sense - in Node applications or bundlers like WebPack they only really need the name of the module, and then can spend a bit of extra time discovering the specific file that holds the code. On the web that could be a lot of wasted round trips (is 'library' in library/index.js ,

how to programmatically determine, within the module, if javascript module was loaded via script src (not imported)

流过昼夜 提交于 2020-05-16 22:30:47
问题 Suppose I have a module: // module.js export default function greet() { console.info( 'hello' ); } If the module is loaded via: <script type=module> import greet from './module.js'; ... </script> then I want the calling code to be able to invoke greet() on its own, without it being called automatically (the normal scenario). However, if the module is loaded via: <script type=module src="./module.js"></script> then I want the module to invoke greet() immediately (as a side-effect of being

Export an es6 default class inline with definition or at end of file?

寵の児 提交于 2020-05-14 18:37:08
问题 What is the difference if any between exporting an es6 default class inline with its definition versus at the end of the file after its definition? Following are two examples I came across in React tutorials. Ex. 1: Inline with Class export default class App extends React.Component { // class definition omitted } Ex. 2: End of file class App extends React.Component [ // class definition omitted } export default App; // is this different somehow? If there is no difference, then it seems the

Publish ES module (.mjs) to NPMJS, with backwards compatibility for Node <8.5.0 (Dual Package)

心已入冬 提交于 2020-05-09 19:48:06
问题 Up to Node v8.5.0, publishing a module written in ES6 to NPMJS was a straightforward process: transpile the ES6 code using a tool like Babel, and publish to NPMJS the resulting lib directory, while your GitHub repo contains the src files. With v8.5.0, Node has released experimental support for native modules ( export / import ) via the --experimental-modules flag. It is now possible to publish purely-ES6 modules to NPMJS, and use them without any transpilation, as long as the files involved