admin管理员组文章数量:1426028
Consider the following file having multiple exports (a library of functions):
lib.js:
export const Funct1 = () => {
...
}
export const Funct2 = () => {
...
}
export const Funct3 = () => {
...
}
In other to work with shared code between environments, I need to build a intermediate file to import all the lib.js functions and make then available to new project as exports (module redirecting). Here is my code:
MyLib.js
export { default as functionlib } from "./src/helpers/lib";
Now I need to import the MyLib.js into my end code:
App.js
import { lib } from "../shared/MyLib.js"
...
// Later use the function
let a = lib.Funct1();
Under that syntax lib
ends being null
at my end code App.js
.
These are variations I've used in MyLib.js
and the result errors on App.js:
import { default as lib } from "../shared/MyLib.js" <--- ERROR: lib ends being null
or
import { * as lib } from "../shared/MyLib.js" <--- ERROR: unexpected token at *
How can I properly redirect all my functions inside MyLib.js
to make available all the lib
functions (without expliciting all the lib`` functions inside
MyLib`) ?
Consider the following file having multiple exports (a library of functions):
lib.js:
export const Funct1 = () => {
...
}
export const Funct2 = () => {
...
}
export const Funct3 = () => {
...
}
In other to work with shared code between environments, I need to build a intermediate file to import all the lib.js functions and make then available to new project as exports (module redirecting). Here is my code:
MyLib.js
export { default as functionlib } from "./src/helpers/lib";
Now I need to import the MyLib.js into my end code:
App.js
import { lib } from "../shared/MyLib.js"
...
// Later use the function
let a = lib.Funct1();
Under that syntax lib
ends being null
at my end code App.js
.
These are variations I've used in MyLib.js
and the result errors on App.js:
import { default as lib } from "../shared/MyLib.js" <--- ERROR: lib ends being null
or
import { * as lib } from "../shared/MyLib.js" <--- ERROR: unexpected token at *
How can I properly redirect all my functions inside MyLib.js
to make available all the lib
functions (without expliciting all the lib`` functions inside
MyLib`) ?
4 Answers
Reset to default 4Is there a way to do it "in a bulk mode" without expliciting all the library functions?
No, dynamically puted value cannot be statically exported: ECMAScript 6 modules: the final syntax:
You may be wondering – why do we need named exports if we could simply default-export objects (like CommonJS)? The answer is that you can’t enforce a static structure via objects and lose all of the associated advantages (described in the next section).
You can either Default export:
//lib.js
const Funct1 = () => {
...
}
export default Funct1;
Default import:
import Funct1 from './lib.js'
import MyFunc from './lib.js'
import Whatever from './lib.js'
You can assign any name when importing, because it resolves to whatever the default export of lib.js
is.
Or Named export:
//lib.js
export const Funct1 = () => {
...
}
Named import
import { Func1 } from './lib.js'
import { MyFunc } from './lib.js' //Doesn't work
import { Whatever } from './lib.js' //Doesn't work
In this case the import name must correspond with the export name since you're importing a specific thing by it's name.
More on the subject
lib.js
export const Funct1 = () => {
...
}
export const Funct2 = () => {
...
}
export const Funct3 = () => {
...
}
MyLib.js
export {
Funct1,
Funct2,
Funct3,
} from "./src/helpers/lib";
//You can easily add more
export {
Foo,
Bar
} from "./src/helpers/fooBar";
App.js
import * as MyLib from "../shared/MyLib.js"
// Usage
MyLib.Funct1()
MyLib.Foo()
Try this in your MyLib.js:
import * as lib from "./src/helpers/lib";
export default lib;
export * from "./src/helpers/lib";
Then in your App.js:
import lib from "../shared/MyLib.js";
// OR
import { Funct1 } from "../shared/MyLib.js";
console.log(lib.Funct1); // function Funct1() {}
console.log(Funct1); // function Funct1() {}
Demo: https://codesandbox.io/s/62jrp3k83k
ECMAScript modules example:
/*
* [package]/src/lib.mjs
*/
export function Func1() {
// ...
}
export function Func2() {
// ...
}
export function Func3() {
// ...
}
/*
* [package]/index.mjs
*/
export * from "./src/lib.mjs";
/*
* App.mjs
*/
import { Func1, Func3 } from '[package]';
Func1();
Func3();
MyLib.js
import { Funct1, Funct2, Funct3 } from "./src/helpers/lib";
const MyLibs = {
Funct1,
Funct2,
Funct3,
};
export default MyLibs;
App.js
import MyLib from "../shared/MyLib.js"
// Usage
MyLib.Funct1()
本文标签: Correct way to import and export javascript function library (module redirect)Stack Overflow
版权声明:本文标题:Correct way to import and export javascript function library (module redirect) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469196a2659671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论