# Import a specific item from a source module, with its original name. import { something } from'./module.js';
# Import a specific item from a source module, with a custom name assigned upon import. import { something as somethingElse } from'./module.js';
# Import everything from the source module as an object which exposes all the source module's named exports as properties and methods. import * asmodulefrom'./module.js'
# Import the default export of the source module. import something from'./module.js';
# Load the module code, but don't make any new objects available. # This is useful for polyfills, or when the primary purpose of the imported code is to muck about with prototypes. import'./module.js';
# Import modules using the dynamic import API. # This is useful for code-splitting applications and using modules on-the-fly. import('./modules.js').then(({ default: DefaultExport, NamedExport })=> { // do something with modules. })
# Export a value that has been previously declared const something = true; export { something };
# Rename on export export { something as somethingElse };
# Export a value immediately upon declaration // this works with `var`, `let`, `const`, `class`, and `function` exportconst something = true;
# Export a single value as the source module's default export # This practice is only recommended if your source module only has one export. # It is bad practice to mix default and named exports in the same module, though it is allowed by the specification. exportdefault something;
// Uses AMD or browser globals to create a module.
// If you want something that will also work in Node, see returnExports.js // If you want to support other stricter CommonJS environments, // or if you need to create a circular dependency, see commonJsStrict.js
// Defines a module "amdWeb" that depends on another module called "b". // Note that the name of the module is implied by the file name. It is best // if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then // in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you // can remove the `root` use and the passing of `this` as the first arg to // the top function.
(function(root, factory) { if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. define(["b"], factory); } else { // Browser globals root.amdWeb = factory(root.b); } })(typeof self !== "undefined" ? self : this, function(b) { // Use b in some fashion.
// Just return a value to define the module export. // This example returns an object, but the module // can return a function as the exported value. return {}; });
// Uses AMD or browser globals to create a module. This example creates a // global even when AMD is used. This is useful if you have some scripts // that are loaded by an AMD loader, but they still want access to globals. // If you do not need to export a global for the AMD case, see amdWeb.js.
// If you want something that will also work in Node, and still export a // global in the AMD case, see returnExportsGlobal.js // If you want to support other stricter CommonJS environments, // or if you need to create a circular dependency, see commonJsStrictGlobal.js
// Defines a module "amdWebGlobal" that depends another module called "b". // Note that the name of the module is implied by the file name. It is best // if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then // in the browser, it will create a global .b that is used below.
(function(root, factory) { if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. define(["b"], function(b) { // Also create a global in case some scripts // that are loaded still are looking for // a global even when an AMD loader is in use. return (root.amdWebGlobal = factory(b)); }); } else { // Browser globals root.amdWebGlobal = factory(root.b); } })(typeof self !== "undefined" ? self : this, function(b) { // Use b in some fashion.
// Just return a value to define the module export. // This example returns an object, but the module // can return a function as the exported value. return {}; });
// Defines a module that works in CommonJS and AMD.
// This version can be used as common boilerplate for a library module // that you only want to expose to CommonJS and AMD loaders. It will not work // well for defining browser globals.
// If you only want to target Node and AMD or a CommonJS environment that // supports assignment to module.exports and you are not defining a module // that has a circular dependency, see nodeAdapter.js
// Help Node out by setting up define. if ( typeof exports === "object" && typeof exports.nodeName !== "string" && typeof define !== "function" ) { var define = function(factory) { factory(require, exports, module); }; }
define(function(require, exports, module) { var b = require("b");
// Only attach properties to the exports object to define // the module's properties. exports.action = function() {}; });
// Uses CommonJS, AMD or browser globals to create a module.
// If you just want to support Node, or other CommonJS-like environments that // support module.exports, and you are not creating a module that has a // circular dependency, then see returnExports.js instead. It will allow // you to export a function as the module value.
// Defines a module "commonJsStrict" that depends another module called "b". // Note that the name of the module is implied by the file name. It is best // if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then // in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you // can remove the `root` use and the passing `this` as the first arg to // the top function.
(function(root, factory) { if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. define(["exports", "b"], factory); } elseif ( typeof exports === "object" && typeof exports.nodeName !== "string" ) { // CommonJS factory(exports, require("b")); } else { // Browser globals factory((root.commonJsStrict = {}), root.b); } })(typeof self !== "undefined" ? self : this, function(exports, b) { // Use b in some fashion.
// attach properties to the exports object to define // the exported module properties. exports.action = function() {}; });
// Uses CommonJS, AMD or browser globals to create a module. This example // creates a global even when AMD is used. This is useful if you have some // scripts that are loaded by an AMD loader, but they still want access to // globals. If you do not need to export a global for the AMD case, see // commonjsStrict.js.
// If you just want to support Node, or other CommonJS-like environments that // support module.exports, and you are not creating a module that has a // circular dependency, then see returnExportsGlobal.js instead. It will allow // you to export a function as the module value.
// Defines a module "commonJsStrictGlobal" that depends another module called // "b". Note that the name of the module is implied by the file name. It is // best if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then // in the browser, it will create a global .b that is used below.
(function(root, factory) { if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. define(["exports", "b"], function(exports, b) { factory((root.commonJsStrictGlobal = exports), b); }); } elseif ( typeof exports === "object" && typeof exports.nodeName !== "string" ) { // CommonJS factory(exports, require("b")); } else { // Browser globals factory((root.commonJsStrictGlobal = {}), root.b); } })(typeof self !== "undefined" ? self : this, function(exports, b) { // Use b in some fashion.
// attach properties to the exports object to define // the exported module properties. exports.action = function() {}; });
// This version can be used as common boilerplate for a library module // that you only want to expose to Node and AMD loaders. It will not work // well for defining browser globals.
// If you need a version of this file that works CommonJS-like environments // that do not support module.exports or if you want to define a module // with a circular dependency, see commonjsAdapter.js
(function(define) { define(function(require, exports, module) { var b = require("b");
returnfunction() {}; }); })( // Help Node out by setting up define. typeofmodule === "object" && module.exports && typeof define !== "function" ? function(factory) { module.exports = factory(require, exports, module); } : define );
// Uses Node, AMD or browser globals to create a module.
// If you want something that will work in other stricter CommonJS environments, // or if you need to create a circular dependency, see commonJsStrict.js
// Defines a module "returnExports" that depends another module called "b". // Note that the name of the module is implied by the file name. It is best // if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then // in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you // can remove the `root` use and the passing `this` as the first arg to // the top function.
(function(root, factory) { if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. define(["b"], factory); } elseif (typeofmodule === "object" && module.exports) { // Node. Does not work with strict CommonJS, but // only CommonJS-like environments that support module.exports, // like Node. module.exports = factory(require("b")); } else { // Browser globals (root is window) root.returnExports = factory(root.b); } })(typeof self !== "undefined" ? self : this, function(b) { // Use b in some fashion.
// Just return a value to define the module export. // This example returns an object, but the module // can return a function as the exported value. return {}; });
// if the module has no dependencies, the above pattern can be simplified to (function(root, factory) { if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. define([], factory); } elseif (typeofmodule === "object" && module.exports) { // Node. Does not work with strict CommonJS, but // only CommonJS-like environments that support module.exports, // like Node. module.exports = factory(); } else { // Browser globals (root is window) root.returnExports = factory(); } })(typeof self !== "undefined" ? self : this, function() { // Just return a value to define the module export. // This example returns an object, but the module // can return a function as the exported value. return {}; });
// Uses Node, AMD or browser globals to create a module. This example creates // a global even when AMD is used. This is useful if you have some scripts // that are loaded by an AMD loader, but they still want access to globals. // If you do not need to export a global for the AMD case, // see returnExports.js.
// If you want something that will work in other stricter CommonJS environments, // or if you need to create a circular dependency, see commonJsStrictGlobal.js
// Defines a module "returnExportsGlobal" that depends another module called // "b". Note that the name of the module is implied by the file name. It is // best if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then // in the browser, it will create a global .b that is used below.
(function(root, factory) { if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. define(["b"], function(b) { return (root.returnExportsGlobal = factory(b)); }); } elseif (typeofmodule === "object" && module.exports) { // Node. Does not work with strict CommonJS, but // only CommonJS-like environments that support module.exports, // like Node. module.exports = factory(require("b")); } else { // Browser globals root.returnExportsGlobal = factory(root.b); } })(typeof self !== "undefined" ? self : this, function(b) { // Use b in some fashion.
// Just return a value to define the module export. // This example returns an object, but the module // can return a function as the exported value. return {}; });
ES6 处理“循环加载”与 CommonJS 有本质的不同。ES6 模块是动态引用,如果使用import从一个模块加载变量(即import foo from ‘foo’),那些变量不会被缓存,而是成为一个指向被加载模块的引用,需要开发者自己保证,真正取值的时候能够取到值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// a.mjs import { bar } from './b'; console.log('a.mjs'); console.log(bar); export let foo = 'foo'; // b.mjs import { foo } from './a'; console.log('b.mjs'); console.log(foo); export let bar = 'bar'; $ node --experimental-modules a.mjs b.mjs ReferenceError: foo is not defined
Maybe you could buy me a cup of coffee.
Scan this qrcode
Open alipay app scan this qrcode, buy me a coffee!
Scan this qrcode
Open wechat app scan this qrcode, buy me a coffee!