Časť 3: UMD a natívne moduly
Milan Herda, 01/2017, úpravy 09/2022
Som vývojár populárnej javaScriptovej knižnice
Som vývojár populárnej javaScriptovej knižnice
a natívne moduly ešte nie sú "in"
Moji používatelia pracujú
Všetky verzie v jednom súbore
Modul bez závislostí
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.ourModuleName = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
// ...
return { /* ... */ };
}));
Modul so závislosťou na knižnici foo
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['foo'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('foo'));
} else {
root.ourModuleName = factory(root.foo);
}
}(typeof self !== 'undefined' ? self : this, function (foo) {
// ...
return { /* ... */ };
}));
import
Môže sa použiť iba v top-level úrovni (tj. nesmie byť v blokoch) a musí predchádzať iným príkazom
import abc from './utils/module1';
import { foo, bar } from './utils/module2';
foo(abc);
bar();
export
vieme ho použiť viackrát v jednom module
const abc = 'abc';
export abc;
export const bar = 'bar';
const foo = 'foo';
export default foo;
Vytvorenie projektu
yarn create vite js-modules-3 --template=vanilla
# alebo
npm create vite js-modules-3 --template=vanilla
Spustenie projektu
yarn dev
# alebo
npx run dev
src/theme/colors.js
export const PRIMARY = '#000080';
export const SECONDARY = '#008000';
export const WARNING = 'orange';
export const ERROR = '#800000';
export const INFO = '#2266cc';
main.js
import { PRIMARY } from './src/theme/colors.js';
const colorInfo = (name, color) => {
const divStyle = `
width: 300px;
height: 100px;
background-color: ${color};
`;
const titleStyle = `
background-color: rgba(255, 255, 255, 0.5);
`;
return `
<div style="${divStyle}">
<h3 style="${titleStyle}">${name}</h3>
</div>
`;
};
document.querySelector('#app').innerHTML = colorInfo('PRIMARY', PRIMARY);
Named exports
// colors.js
export const PRIMARY = '#0000cc'
// main.js
import { PRIMARY } from './src/theme/colors.js';
Majú viacero "foriem"
export const PRIMARY = '#0000cc';
export const SECONDARY = '#00cc00';
export const WARNING = 'orange';
export const ERROR = '#cc0000';
export const INFO = '#2266cc';
const PRIMARY = '#0000cc';
const SECONDARY = '#00cc00';
const WARNING = 'orange';
const ERROR = '#cc0000';
const INFO = '#2266cc';
export {
PRIMARY,
SECONDARY,
WARNING,
ERROR,
INFO,
};
Tzv. export zoznamu
const PRIMARY = '#0000cc';
const SECONDARY = '#00cc00';
const WARNING = 'orange';
const ERROR = '#cc0000';
const INFO = '#2266cc';
export {
PRIMARY,
SECONDARY,
};
export {
WARNING,
ERROR,
INFO,
};
const PRIMARY = '#0000cc';
const SECONDARY = '#00cc00';
const WARNING = 'orange';
const ERROR = '#cc0000';
const INFO = '#2266cc';
export {
PRIMARY as mainColor,
SECONDARY,
};
export {
WARNING as itsFine,
ERROR as itEscalatedQuickly,
INFO,
};
importujúci modul si ju vloží pod takým menom, akým chce
pri importe sa nepoužívajú { }
const PRIMARY = '#0000cc';
// export const PRIMARY = '#0000cc';
export default PRIMARY;
import mainColor from './src/theme/colors.js';
export default 1 + 1;
import result from './subor.js';
export {
foo,
bar as default,
baz,
};
import result from './subor.js';
import { PRIMARY, SECONDARY, ERROR } from './src/theme/colors.js';
import {
PRIMARY as mainColor,
SECONDARY,
ERROR
} from './src/theme/colors.js';
import * as colors from './src/theme/colors.js';
console.log(colors.PRIMARY);
console.log(colors.default);
import bar from './foo.js';
import mainColor, { PRIMARY, SECONDARY } from './foo.js';
import { PRIMARY, default as mainColor } from './foo.js';
export * from "./subor.js";
export * as foo from "./subor";
export { PRIMARY, SECONDARY } from "./src/theme/colors.js";
export {
PRIMARY as primaryColor,
SECONDARY as secondaryColor
} from "./src/theme/colors.js";
export { default, PRIMARY, SECONDARY } from "./src/theme/colors.js";
Občas potrebujeme naimportovať veci z viacerých modulov len preto, aby sme ich ďalej exportovali.
export {
default,
PRIMARY,
SECONDARY as secondaryColor,
} from "./src/theme/colors.js";
Jediný spôsob, ako reexportovať jedným príkazom default aj pomenový export, je pomocou zoznamu
Ak default nebude premenovaný, stáva sa default exportom pre aktuálny modul