ES6+ export of an import within export default - ecmascript-6

I'm trying to do this
state.js
export default {
someValue: 'fooBar'
}
index.js
export default {
export {default as state} from './state',
export {default as actions} from './actions'
}
but I am not allowed, Unexpected keyword 'export' (2:2). Is there any way to achieve this?

You can only use import and export at the top level of your module. (You can use dynamic import() elsewhere, but not the static versions.)
So to have that export, you have to do what you've said you're trying to avoid in the comments:
import {default as state} from './state';
import {default as actions} from './actions';
export default {
state, actions
};
but, beware what you're exporting there: an object with state and actions properties whose initial values come from the imported state and action, but which are not connected to them. Code importing that object can change those properties. You could maintain the live binding:
import {default as state} from './state';
import {default as actions} from './actions';
export default {
get state() {
return state;
},
get actions() {
return actions;
}
};
but at that point you're kind of reinventing the module namespace object. You might prefer to simply:
export {default as state} from './state.js';
export {default as actions} from './actions.js';
and then either use those named exports:
import {state, actions} from "./index";
or use the module namespace object
import * as index from "./index";
// Use `index.state` and `index.actions`

Related

Create an "Import Menu" in TypeScript

I'm trying to work on a project with a numerous amount of files, and I thought an import object would be helpful. For example, here would be menu.ts on the top-level, which every program will reference to:
import router from "./router/index";
import controllers from "./controllers/index";
import config from "./config";
export default {
router: router,
controllers: controllers
config: config
}
This would be a sample controllers/index.ts:
import database from "./database";
import accounts from "./accounts";
import a_controller from "./a_controller";
export default {
database: database,
accounts: accounts,
a_controller: a_controller
}
Obviously, this would raise some circular dependency issues with controllers referencing to menu. This is asserted with a TypeError: cannot read property controllers of undefined error message. Is there a way to do this?
Thank you for your time.
in your case it is better to use named import, in controllers/index.ts: you can do like:
export { database } from "./database";
export { accounts } from "./accounts";
export { a_controller } from "./a_controller";
and in your menu.ts you can import them like:
import { database, accounts, a_controller } from './controller'

when to combining multiple imports into a single file

As I understand then combine multiple export statement into a single file is considered a good practice iff source file comes under the same directory
so
import { Observable } from 'rxjs/Observable';
import { Observable, BehaviorSubject, Subject, ReplaySubject } from 'rxjs';
can be re-written to
import { Observable, BehaviorSubject, Subject, ReplaySubject } from 'rxjs';
But what If I have to import only one module than which is preferred way? using a complete file or a specific one.
import { Observable } from 'rxjs/Observable';
OR
import { Observable } from 'rxjs';
does that cost anything extra?
ts v 2.3.4
angular v 4.4.6
import { Observable } from 'rxjs/Observable';
This is the efficient way to import a single module, because you are providing the exact path to only fetch the required module.
Whereas in this case,
import { Observable } from 'rxjs';
It will import a lot of unnecessary stuff from the rxjs library that will increase the page load time and/or the code base.

How to use date filter in ng2-smart-table in angular?

I used ng2-smart-table module in my angular project, everything working fine, but i don't know how to filer a date column based on from and to date. any one have example or link to correct solution please give. Thanks
The way to do it is by using 'renderComponent' on your field. You can see an example here : https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/custom-edit-view/advanced-example-custom-editor.component.ts
Here is what I did it :
Create a new component, here SimpleDateComponent
Add it to your module declarations and entryComponents arrays
Use it from your 'setting', used by your ng2-smart-table
1) The new component
import {Component, Input} from '#angular/core';
#Component({
selector: 'app-simple-date',
template: `{{value |date: 'dd.MM.yyyy'}}`,
})
export class SimpleDateComponent {
#Input() value: Date;
}
2) The import
import {NgModule} from '#angular/core';
import {PrestationListComponent} from './prestation-list/prestation-list.component';
import {SimpleDateComponent} from './simple-date-component/simple-date-component';
#NgModule({
declarations: [
PrestationListComponent,
SimpleDateComponent],
entryComponents: [SimpleDateComponent]
})
export class PrestationsModule {
}
3) The settings object modified:
settings = {
columns: {
dueDate: {
title: 'Deadline',
type: 'custom',
renderComponent: SimpleDateComponent
}
}
};
This issue seems to be still open [1] on GitHub and a potential PR is still in pending merge requests. PRECAUTION: For this specific solution, you need to explicitly modify few files in ng2-smart-table folder under node_modules\ folder in your project and in case you happen to install the package again (npm i ng2-smart-table) then it'll override any changes you did in the ng2-smart-table folder. Take necessary measures like-- (generally node_modules\ folder are part of your .gitignore file and not commited to the repo but you can hack it by) explicitly commiting this folder or take a backup of ng2-smart-table folder before performing any package update.
PS: Do remember ng2-smart-table is the only boilerplate to accomplish this task for e.g. there exists other better options too

es6 import 'destructuring' not working

Hey there I have this uiTypes.js file like this:
export default {
SELECT_ITEM: 'SELECT_ITEM',
DESELECT_ITEM: 'DESELECT_ITEM',
TOGGLE_ITEM: 'TOGGLE_ITEM',
}
And when I try importing its content like so, it works:
import uiTypes from './uiTypes';
console.log(uiTypes.SELECT_ITEM) // 'SELECT_ITEM'
But not like this:
import { SELECT_ITEM, DESELECT_ITEM, TOGGLE_ITEM } from './uiTypes';
console.log(SELECT_ITEM) // undefined,
Am I missing something?
There is no destructuring for imports (notice also that exports and imports use the keyword as instead of a colon to avoid confusion with objects). You can either import the default export, individual named exports, or the module namespace object.
Your attempt is trying to import three named exports, while there is only a default export; that's why it's failing.
You should use named exports:
export const SELECT_ITEM = 'SELECT_ITEM';
export const DESELECT_ITEM = 'DESELECT_ITEM';
export const TOGGLE_ITEM = 'TOGGLE_ITEM';
or use "real" destructuring after importing the object:
import uiTypes from './uiTypes';
const {SELECT_ITEM, DESELECT_ITEM, TOGGLE_ITEM} = uiTypes;

How to troubleshoot es6 module dependencies?

I am developing a React & Reflux app, which is bundled by webpack with babel-loader (v6), and I am experiencing es6 modules dependencies issues
For example, I have a component that use the reflux .connect() mixin :
import MyStore from '../stores/my-store';
const Component = React.createClass({
mixins: [Reflux.connect(MyStore)]
});
When I import all modules individually in each file like this, everything's fine.
I then tried to improve my code by using deconstructed import statements :
...in a component :
//import One from '../js/one';
//import Two from '../js/two';
//import Three from '../js/three';
import { One, Two, Three } from '../js'; // Instead
...and in js/index.js :
import One from './one';
import Two from './two';
import Three from './three';
export { One, Two, Three };
App source code files are more concise using the above technique, because I can import all components in one import line.
But when I use this, some dependencies end up beeing undefined when I use them
If I use the same updated example...
//import MyStore from '../stores/my-store';
import { MyStore } from '../stores'; // Instead
const Component = React.createClass({
mixins: [Reflux.connect(MyStore)]
});
...MyStore parameter ends up undefined in Reflux.connect method.
I tried to troubleshoot in the debugger, but I'm not really aware of what's going on with the __webpack_require__(xxx) statements in the generated bundle. There must be a circular dependency that babel-loader or webpack require could not figure out when there are the index.js files re-exporting individual modules.
Do you know any tool that can help me figure this out? I tried madge but it does not work with es6 modules, and I could not find anything that would tell me where anything is wrong
In order to get extended info about build, run:
webpack --profile --display-modules --display-reasons
It will give you bunch of information for optimisation/profiling.
import statement is used to import functions, objects or primitives that have been exported from an external module.
As per MDN doc, you can import the Modules not the directory.
import name from "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as alias from "module-name";
import defaultMember from "module-name";
import "module-name";
Reference URL:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
http://es6-features.org/#ValueExportImport
https://github.com/lukehoban/es6features#modules
http://www.2ality.com/2014/09/es6-modules-final.html
As a workaround keep one file as base.js and include all your 3 files.