es6-modules

Webpack have trouble with scopes

拟墨画扇 提交于 2021-02-10 18:17:02
问题 There are several modules that are connected to app.js, for example the code that is inside: var test = "TEST"; Here is my webpack.config: module.exports = { entry: './src/app.js', output: { filename: './dist/bundle.js' } }; The problem is that when I try to call my test variable in the developer console, I get an error: Something about the scope, when I connect app.js directly - everything works, what's the problem and how to fix it? 回答1: Yes, this is a scope problem. There are two ways to

Webpack have trouble with scopes

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 18:06:11
问题 There are several modules that are connected to app.js, for example the code that is inside: var test = "TEST"; Here is my webpack.config: module.exports = { entry: './src/app.js', output: { filename: './dist/bundle.js' } }; The problem is that when I try to call my test variable in the developer console, I get an error: Something about the scope, when I connect app.js directly - everything works, what's the problem and how to fix it? 回答1: Yes, this is a scope problem. There are two ways to

Can ES6 be used natively in webpack.config.js?

让人想犯罪 __ 提交于 2021-02-10 15:47:47
问题 This answer: How can I use ES6 in webpack.config.js? seems to imply a transpilation step. Can ES6 be used natively? How? For example I tried to convert the simple example here from require to import and receive the following error (function (exports, require, module, __filename, __dirname) { import path from 'path' ^^^^ SyntaxError: Unexpected identifier 回答1: You should use webpack-cli --config-register (-r) to allow that. To use that flag, you must have babel-register installed. webpack -

Why not have `import module-name.export1` in javascript?

。_饼干妹妹 提交于 2021-02-08 12:11:17
问题 What is the design limitation behind the ES6 import statement syntax? import { export1 } from "module-name"; as shown here. Why not have import module-name.export1 If somehow having the 'import' keyword in the first place is so important, why not use it like that? For comparison consider several other popular languages: Java : import package.subpackage.ClassName; Python : from module import SomeClass C#: using System.Text; From left to right: broadScope -> module -> particularItem . While in

Browser accepts “classic” js script-tag, but not ES6 modules — MIME error w/ Node http server

一个人想着一个人 提交于 2021-02-08 08:42:12
问题 I want to play with ES6 modules, so I decided on using Node as a simple web server to avoid all CORS related errors I first encountered when doing it locally. Now I get MIME type related errors in the browser which I can't quite grasp. Here is my server.js file: const http = require('http'), url = require('url'), fs = require('fs'); http.createServer((req, res) => { const q = url.parse(req.url, true), filename = "." + q.pathname; fs.readFile(filename, (err, data) => { if (err) { res.writeHead

Can't call a function from js file imported as type module

♀尐吖头ヾ 提交于 2021-02-08 06:33:37
问题 I am writing my Javascript files in ES6 using module imports. Using type='module' is now supported on most modern browsers to allow for proper parsing of import statements. script type="module https://caniuse.com/#feat=es6-module I built an HTML select element were onchange() calls a function from one of my module files using select onchange="someFunction()" but an error is always thrown saying the function is not defined when the on change event occurs. I tested the same function inline and

ESM importing all files from folder with index.js not working

て烟熏妆下的殇ゞ 提交于 2021-02-04 18:49:25
问题 Consider this very simple project: esm-test Runs on NodeJS 13 . This project has "type":"module" in his package.json so Node will treat by default all files .js as ECMAScript Modules. See: ECMAScript Modules - Enabling. It has the following very simple structure: - package.json - src/ - stuff/ - a.js - b.js - index.js - app.js The file app.js is the package entry point. My purpose is to import all files from stuff/ folder using the index.js file. I know that in ES6, having an index.js file in

Eslint rule to put a new line inside import

旧城冷巷雨未停 提交于 2021-02-04 14:51:13
问题 The rule that I'm looking should show error in this case: import {MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3} And considered as fine in this case: import { MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3 } Is there such eslint rule? 回答1: I was looking for such a rule for both import and export declaration. As a result I've made a plugin with autofix. So plugin transforms the code import { k1, k2 } from 'something' into import { k1, k2 } from 'something' and code export { name1, name2, nameN } into

Eslint rule to put a new line inside import

点点圈 提交于 2021-02-04 14:51:05
问题 The rule that I'm looking should show error in this case: import {MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3} And considered as fine in this case: import { MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3 } Is there such eslint rule? 回答1: I was looking for such a rule for both import and export declaration. As a result I've made a plugin with autofix. So plugin transforms the code import { k1, k2 } from 'something' into import { k1, k2 } from 'something' and code export { name1, name2, nameN } into

Is it possible to compile to commonjs from es6 with webpack

最后都变了- 提交于 2021-01-29 16:11:00
问题 The question is as the title suggests- if you have written your source code as es6 modules ( import ... from ... ) can you then compile this source back to node.js style commonjs modules ( const ... = require(...) ) using Webpack? 回答1: You sure can. Here is my webkack.config.js which is doing exactly as you ask for a legacy project that we maintain: var path = require("path"); var webpack = require('webpack'); var HardSourceWebpackPlugin = require("hard-source-webpack-plugin"); module.exports