es6-modules

How do I transpile React JSX and ES6 modules with babel and use browser native ES6

风流意气都作罢 提交于 2019-12-23 05:15:37
问题 The problem I have is when using the "react-app" Babel presets, Babel prevents me from using browser native ES6 features. How do I use the ES6 browser native features available in the latest Chrome http://kangax.github.io/compat-table/es6/ While also using the ES6 module system , which currently has no support https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/import And additionally keep using the JSX syntax while writing React Components? 回答1: The solution I found was

Socket.io-client no default export

浪尽此生 提交于 2019-12-22 10:17:10
问题 I'm currently working on a project in polymer 3, one of the components needs to import socket.io-client but whatever i try i can't get it to work. I have tried: import io from 'socket.io-client'; what i get back: Uncaught SyntaxError: The requested module '../../node_modules/socket.io-client/lib/index.js' does not provide an export named 'default' same for this: import io from 'socket.io-client/dist/socket.io.js'; what i get back: Uncaught SyntaxError: The requested module '../../node_modules

How to access functions defined in ES6 module in a browser's JavaScript console?

和自甴很熟 提交于 2019-12-22 04:02:33
问题 I have a function that is defined in an ES6 module ( sender.js ) as follows: function send() { // do stuff } export {send}; This module is then used in the application's main JavaScript file app.js as follows: import {send} from "./sender" The send function is available in the app.js file, however it is not in Firefox's Javascript console: > send ReferenceError: send is not defined How can I import the send function in the JavaScript console? 回答1: You can set the specific function as global

Conditional export in ES2015

允我心安 提交于 2019-12-21 07:18:13
问题 Let's say you're developing a polyfill and don't want to shim a class if it already exists in the browser. How can this be done in ES6? The following is not valid because exports is not a statement: if (typeof Foo === 'undefined') { export class Foo { ... } } If the condition above evaluates to false , the importing script should get the browser built-in. 回答1: export should be static. For conditional exports CommonJS modules and exports can be used. It should be handled with ES6 modules this

ES6 (EcmaScript 2015) modules: import index.js

我与影子孤独终老i 提交于 2019-12-21 07:10:52
问题 Looking on the internet I'm confused with the special "index.js" module file. Using babelJS + nodeJS or Browserify/Webpack I can import an "index.js" module inside a "libs" directory using import myLib from "./libs" (ie. omitting the /index or /index.js part). Is the "index.js" module resolution (specifying the containing folder) supported by the ES6 (EcmaScript 2015) modules official standard? Or is it just "custom" NodeJS/CommonJS transpiling behaviour? Will it be possible to omit the

How do I resolve eslint import/no-named-as-default

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 07:06:38
问题 After looking at the documentation for the import/no-named-as-default eslint rule, I'm still confused about what exactly I'm doing wrong. I have the following file structure . ├── ButtonBack.css ├── ButtonBack.jsx ├── __tests__ │ └── ButtonBack.test.jsx └── index.js The ButtonBack.jsx contains the following code import React from 'react'; import PropTypes from 'prop-types'; export default class ButtonBack extends React.Component { ... code removed to keep example short ... } __tests__

How to import jest?

旧巷老猫 提交于 2019-12-21 06:56:07
问题 I would like to get rid of globals in my jest test code. Specifically describe , it and expect describe('Welcome (Snapshot)', () => { it('Welcome renders hello world', () => { ... }); }); So I tried add import {describe,it} from 'jest'; and import jest from 'jest'; jest.describe( ... jest.it( ... and other variations.. But no luck. How should I get it working? 回答1: The simplest solution for this is adding jest: true to your env config in eslint, like so: "env": { "browser": true, "node": true

What’s the purpose of the HTML “nomodule” attribute for script elements if the default is text/javascript?

五迷三道 提交于 2019-12-21 06:46:12
问题 I am not clearly understanding why the nomodule attribute exists in the new browsers that support ES6 modules. In HTML 5, the type attribute is optional and defaults to text/javascript: The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript". It doesn't default to <script type="module" src=

Garbage collect unused modules

三世轮回 提交于 2019-12-21 01:14:51
问题 I am using dynamic import to load scripts written by the user in the browser. I start by placing the script contents into a blob, then I use dynamic import() to load the script as a module. Over time, I expect these scripts to change and be destroyed, and thus for the corresponding modules to be garbage collected. However, based on memory profiling in Chrome, that is not happening. The reason why seems to be related to something called the ModuleMap . Here's a screenshot from a memory

Using import fs from 'fs'

走远了吗. 提交于 2019-12-20 09:48:01
问题 I want to use import fs from 'fs' in JavaScript. Here is a sample: import fs from 'fs' var output = fs.readFileSync('someData.txt') console.log(output) The error I get when I run my file using node main.js is: (function (exports, require, module, __filename, __dirname) { import fs from 'fs ' ^^^^^^ SyntaxError: Unexpected token import What should I install in node in order to achieve importing modules and functions from other places? 回答1: ES6 modules support in node.js is fairly recent; even