es6-modules

How to make a class-based custom element side-effect-free so webpack only bundles the explicitly imported components

我与影子孤独终老i 提交于 2019-12-03 16:51:33
问题 I have a set of spec v1 custom elements which I'm using webpack 4 to bundle (and babel-loader to transpile). The components all look similar to this: export class CompDiv extends HTMLDivElement { constructor(...args) { const self = super(...args); self.property = null; return self; } connectedCallback() { console.log('connected CompDiv'); } } customElements.define('comp-div', CompDiv, { extends: 'div' }); Now to be able to create custom packages from these components using selective, named

ts-jest does not recognize es6 imports

倖福魔咒の 提交于 2019-12-03 16:16:37
问题 I'm adding typescript support to a react codebase, and while the app is working ok, jest tests are failing all over the place, apparently not recognizing something about es6 syntax. We're using ts-jest for this. Below is the error message I'm getting, right off the bat when trying to process jest's tests setup file. FAIL src/data/reducers/reducers.test.js ● Test suite failed to run /Users/ernesto/code/app/react/setupTests.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,_

Is there any way to mock private functions with Jest?

一个人想着一个人 提交于 2019-12-03 09:46:10
The ES6 module that I want to test looks as follows: function privateFunction() { ... } export function publicFunction() { ... does something ... privateFunction() ... does something else ... } I am using JEST for my unit tests and I am trying to find a way to test publicFunction and avoiding the execution of privateFunction by mocking it but I couldn't succeed in the mock attempt. Any idea? I found out a way to mock my private function by using the babel-plugin-rewire module. In package.json I have the following: "devDependencies": { ... "babel-plugin-rewire": "1.0.0-beta-5", "babel-jest":

pm2 not working with experimental-modules flag

倖福魔咒の 提交于 2019-12-03 07:15:18
I am using ES6 modules by adding the --experimental-modules arguments to Node. Running node --experimental-modules app.mjs works perfectly fine. However, when I run the same command with pm2 I get the following error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module My current pm2 config file looks like this: "apps": [ { "name": "api", "script": "app.mjs", "exec_mode": "cluster", "instances": "max", "node_args": "--experimental-modules", "env": { variables here.. } } ], I have also tried using esm instead like this: "node_args": "-r esm" In both cases they return the same [ERR

How to make a class-based custom element side-effect-free so webpack only bundles the explicitly imported components

﹥>﹥吖頭↗ 提交于 2019-12-03 07:01:35
I have a set of spec v1 custom elements which I'm using webpack 4 to bundle (and babel-loader to transpile). The components all look similar to this: export class CompDiv extends HTMLDivElement { constructor(...args) { const self = super(...args); self.property = null; return self; } connectedCallback() { console.log('connected CompDiv'); } } customElements.define('comp-div', CompDiv, { extends: 'div' }); Now to be able to create custom packages from these components using selective, named imports I need to mark these files as side-effect-free . The component registration, though, takes place

How do I cache bust imported modules in es6?

ぐ巨炮叔叔 提交于 2019-12-03 04:55:48
问题 ES6 modules allows us to create a single point of entry like so: // main.js import foo from 'foo'; foo() <script src="scripts/main.js" type="module"></script> foo.js will be stored in the browser cache. This is desirable until I push a new version of foo.js to production. It is common practice to add a query string param with a unique id to force the browser to fetch a new version of a js file ( foo.js?cb=1234 ) How can this be achieved using the es6 module pattern? 回答1: HTTP headers to the

ts-jest does not recognize es6 imports

丶灬走出姿态 提交于 2019-12-03 04:50:00
I'm adding typescript support to a react codebase, and while the app is working ok, jest tests are failing all over the place, apparently not recognizing something about es6 syntax. We're using ts-jest for this. Below is the error message I'm getting, right off the bat when trying to process jest's tests setup file. FAIL src/data/reducers/reducers.test.js ● Test suite failed to run /Users/ernesto/code/app/react/setupTests.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import './polyfills'; ^^^^^^^^^^^^^ SyntaxError: Unexpected string at

Idiomatic Revealing Module Pattern for ES6

感情迁移 提交于 2019-12-03 03:51:19
问题 In the past I've used the revealing module pattern. function myModule() { function foo() ... function bar() ... return { foo: foo, bar: bar }; } With ES6, this was improved with object shorthand. function myModule() { function foo() ... function bar() ... return { foo, bar }; } Now with built-in module syntax, I'm struggling to find the preferred pattern that is most similar to the above. Option #1 named exports // export file function foo() ... function bar() ... export { foo, bar }; //

Does webpack make ES6 modules compatible with ES5 browsers?

三世轮回 提交于 2019-12-02 19:50:57
问题 If I use an ES6 import in a JS file like: import { tempates } from "./templates.js"; webpack converts this to something like: __webpack_require__.r(__webpack_exports__); /* harmony import */ var _templates_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./templates.js */ "./static/js/templates.js"); Does this mean that I can use ES6 modules and due to the transformation of webpack they will also work in old browsers that do not support ES6 modules? If yes: Whats the difference

How do I cache bust imported modules in es6?

微笑、不失礼 提交于 2019-12-02 19:16:54
ES6 modules allows us to create a single point of entry like so: // main.js import foo from 'foo'; foo() <script src="scripts/main.js" type="module"></script> foo.js will be stored in the browser cache. This is desirable until I push a new version of foo.js to production. It is common practice to add a query string param with a unique id to force the browser to fetch a new version of a js file ( foo.js?cb=1234 ) How can this be achieved using the es6 module pattern? From my point of view dynamic imports could be a solution here. Step 1) Create a manifest file with gulp or webpack. There you