Phantom is a research project for HTML, JS, and CSS.
Initial lazy loaded block loader
Added block class cleaning, made eager the default.
This project aims to solve several problems when dealing with HTML, JS, and CSS. It is hyper focused on speed and simplicity.
Specifically:
- Pages only request JS & CSS for the content they actually have. No more un-used JS & CSS.
- All JS & CSS is lazy loaded based on proximity to the viewport.
- Dependencies are handled without chaining requests.
- No bundling, no build systems, and no ramp up. Evergreen browsers only.
The heart of the phantom project. Used to find "blocks" in an HTML file based on CSS selector.
When a match is within the defined area of the viewport, the loader will:
- Inject any dependencies of the block.
- Inject the CSS if the block has not been loaded yet.
- Load the ES Module if it has not been loaded yet.
- Run the default function of the module against the matched element on page.
$ npm install @adobecom/blocks
import blockLoader from './node_modules/@adobecom/blocks/tools/blockLoader.js';
blockLoader(config);
A block is defined by its selector, the location of CSS & JS. Both styles and scripts are optional.
const config = {
blocks: {
'header': {
location: '/blocks/header/',
styles: 'styles.css',
scripts: 'scripts.js',
},
'.home-hero': {
location: '/blocks/home-hero/',
styles: 'styles.css',
},
'footer': {
location: '/blocks/footer/',
styles: 'styles.css',
},
'a[href^="https://www.youtube.com"]': {
location: '/blocks/embed/',
styles: 'youtube.css',
scripts: 'youtube.js',
}
}
};
The following adds the ability to lazy load, spec a margin around the viewport, and have dependencies.
const config = {
lazy: true,
margin: '800px 0px',
blocks: {
'header': {
location: '/blocks/header/',
styles: 'styles.css',
scripts: 'scripts.js',
},
'.accordion': {
deps: ['react'],
location: '/blocks/accordion/',
styles: 'styles.css',
scripts: 'scripts.js',
},
'footer': {
location: '/blocks/footer/',
styles: 'styles.css',
},
'a[href^="https://www.youtube.com"]': {
location: '/blocks/embed/',
styles: 'youtube.css',
scripts: 'youtube.js',
},
'.fragment': {
location: '/blocks/fragment/',
scripts: 'scripts.js',
}
},
deps: {
'react': {
location: 'https://unpkg.com/',
scripts: ['react@17/umd/react.development.js', 'react-dom@17/umd/react-dom.development.js'],
}
}
};