@cocreate/lazy-loader 1.3.12 → 1.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/FUNDING.yml +3 -3
- package/.github/workflows/automated.yml +95 -95
- package/.github/workflows/manual.yml +44 -44
- package/CHANGELOG.md +625 -610
- package/CONTRIBUTING.md +96 -96
- package/CoCreate.config.js +26 -26
- package/LICENSE +21 -21
- package/README.md +84 -84
- package/demo/index.html +20 -20
- package/docs/index.html +254 -254
- package/package.json +4 -4
- package/release.config.js +21 -21
- package/src/index.js +60 -60
- package/webpack.config.js +84 -84
package/src/index.js
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import observer from '@cocreate/observer';
|
|
2
|
-
|
|
3
|
-
function listen(callback, selector) {
|
|
4
|
-
|
|
5
|
-
function observerCallback({ target }) {
|
|
6
|
-
// let isInit = target.querySelector(selector)
|
|
7
|
-
// if (isInit) {
|
|
8
|
-
callback()
|
|
9
|
-
// console.log('lazyloaded', selector)
|
|
10
|
-
observer.uninit(observerCallback)
|
|
11
|
-
// }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
observer.init({
|
|
15
|
-
name: 'lazyloadObserver',
|
|
16
|
-
observe: ['childList'],
|
|
17
|
-
target: selector,
|
|
18
|
-
callback: observerCallback
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
let selectorAttributes = [];
|
|
22
|
-
let attributes = selector.split(",")
|
|
23
|
-
for (let attribute of attributes){
|
|
24
|
-
let attr = attribute.trim()
|
|
25
|
-
if (attr.startsWith("[")) {
|
|
26
|
-
let pos = attr.indexOf("*")
|
|
27
|
-
if (pos == -1)
|
|
28
|
-
pos = attr.indexOf("=")
|
|
29
|
-
if (pos !== -1) {
|
|
30
|
-
attr = attr.slice(1, pos)
|
|
31
|
-
} else {
|
|
32
|
-
attr = attr.slice(1, -1)
|
|
33
|
-
}
|
|
34
|
-
selectorAttributes.push(attr)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
if (selectorAttributes.length > 0)
|
|
39
|
-
observer.init({
|
|
40
|
-
name: 'lazyloadAttributeObserver',
|
|
41
|
-
observe: ['attributes'],
|
|
42
|
-
attributeName: selectorAttributes,
|
|
43
|
-
target: selector,
|
|
44
|
-
callback: observerCallback
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export async function lazyLoad(name, selector, callback) {
|
|
50
|
-
if (document.querySelector(selector))
|
|
51
|
-
await dependency(name, await callback())
|
|
52
|
-
else
|
|
53
|
-
listen(callback, selector)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export async function dependency(name, promise) {
|
|
57
|
-
let component = await promise;
|
|
58
|
-
Object.assign(window.CoCreate, {
|
|
59
|
-
[name]: component.default
|
|
60
|
-
});
|
|
1
|
+
import observer from '@cocreate/observer';
|
|
2
|
+
|
|
3
|
+
function listen(callback, selector) {
|
|
4
|
+
|
|
5
|
+
function observerCallback({ target }) {
|
|
6
|
+
// let isInit = target.querySelector(selector)
|
|
7
|
+
// if (isInit) {
|
|
8
|
+
callback()
|
|
9
|
+
// console.log('lazyloaded', selector)
|
|
10
|
+
observer.uninit(observerCallback)
|
|
11
|
+
// }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
observer.init({
|
|
15
|
+
name: 'lazyloadObserver',
|
|
16
|
+
observe: ['childList'],
|
|
17
|
+
target: selector,
|
|
18
|
+
callback: observerCallback
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
let selectorAttributes = [];
|
|
22
|
+
let attributes = selector.split(",")
|
|
23
|
+
for (let attribute of attributes){
|
|
24
|
+
let attr = attribute.trim()
|
|
25
|
+
if (attr.startsWith("[")) {
|
|
26
|
+
let pos = attr.indexOf("*")
|
|
27
|
+
if (pos == -1)
|
|
28
|
+
pos = attr.indexOf("=")
|
|
29
|
+
if (pos !== -1) {
|
|
30
|
+
attr = attr.slice(1, pos)
|
|
31
|
+
} else {
|
|
32
|
+
attr = attr.slice(1, -1)
|
|
33
|
+
}
|
|
34
|
+
selectorAttributes.push(attr)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
if (selectorAttributes.length > 0)
|
|
39
|
+
observer.init({
|
|
40
|
+
name: 'lazyloadAttributeObserver',
|
|
41
|
+
observe: ['attributes'],
|
|
42
|
+
attributeName: selectorAttributes,
|
|
43
|
+
target: selector,
|
|
44
|
+
callback: observerCallback
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function lazyLoad(name, selector, callback) {
|
|
50
|
+
if (document.querySelector(selector))
|
|
51
|
+
await dependency(name, await callback())
|
|
52
|
+
else
|
|
53
|
+
listen(callback, selector)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function dependency(name, promise) {
|
|
57
|
+
let component = await promise;
|
|
58
|
+
Object.assign(window.CoCreate, {
|
|
59
|
+
[name]: component.default
|
|
60
|
+
});
|
|
61
61
|
}
|
package/webpack.config.js
CHANGED
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
const path = require("path")
|
|
2
|
-
const TerserPlugin = require("terser-webpack-plugin")
|
|
3
|
-
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
|
|
4
|
-
let isProduction = process.env.NODE_ENV === "production"
|
|
5
|
-
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
|
|
6
|
-
|
|
7
|
-
module.exports = {
|
|
8
|
-
entry: {
|
|
9
|
-
"CoCreate-lazy-loader": "./src/index.js",
|
|
10
|
-
},
|
|
11
|
-
output: {
|
|
12
|
-
path: path.resolve(__dirname, "dist"),
|
|
13
|
-
filename: isProduction ? "[name].min.js" : "[name].js",
|
|
14
|
-
libraryTarget: "umd",
|
|
15
|
-
libraryExport: "default",
|
|
16
|
-
library: ["CoCreate", "lazy-loader"],
|
|
17
|
-
globalObject: "this",
|
|
18
|
-
},
|
|
19
|
-
|
|
20
|
-
plugins: [
|
|
21
|
-
new CleanWebpackPlugin(),
|
|
22
|
-
new MiniCssExtractPlugin({
|
|
23
|
-
filename: "[name].css",
|
|
24
|
-
}),
|
|
25
|
-
],
|
|
26
|
-
// Default mode for Webpack is production.
|
|
27
|
-
// Depending on mode Webpack will apply different things
|
|
28
|
-
// on final bundle. For now we don't need production's JavaScript
|
|
29
|
-
// minifying and other thing so let's set mode to development
|
|
30
|
-
mode: isProduction ? "production" : "development",
|
|
31
|
-
module: {
|
|
32
|
-
rules: [
|
|
33
|
-
{
|
|
34
|
-
test: /.js$/,
|
|
35
|
-
exclude: /(node_modules)/,
|
|
36
|
-
use: {
|
|
37
|
-
loader: "babel-loader",
|
|
38
|
-
options: {
|
|
39
|
-
plugins: ["@babel/plugin-transform-modules-commonjs"],
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
test: /.css$/i,
|
|
45
|
-
use: [
|
|
46
|
-
{ loader: "style-loader", options: { injectType: "linkTag" } },
|
|
47
|
-
"file-loader",
|
|
48
|
-
],
|
|
49
|
-
},
|
|
50
|
-
],
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
// add source map
|
|
54
|
-
...(isProduction ? {} : { devtool: "eval-source-map" }),
|
|
55
|
-
|
|
56
|
-
optimization: {
|
|
57
|
-
minimize: true,
|
|
58
|
-
minimizer: [
|
|
59
|
-
new TerserPlugin({
|
|
60
|
-
extractComments: true,
|
|
61
|
-
// cache: true,
|
|
62
|
-
parallel: true,
|
|
63
|
-
// sourceMap: true, // Must be set to true if using source-maps in production
|
|
64
|
-
terserOptions: {
|
|
65
|
-
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
|
|
66
|
-
// extractComments: 'all',
|
|
67
|
-
compress: {
|
|
68
|
-
drop_console: true,
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
}),
|
|
72
|
-
],
|
|
73
|
-
splitChunks: {
|
|
74
|
-
chunks: "all",
|
|
75
|
-
minSize: 200,
|
|
76
|
-
// maxSize: 99999,
|
|
77
|
-
//minChunks: 1,
|
|
78
|
-
|
|
79
|
-
cacheGroups: {
|
|
80
|
-
defaultVendors: false,
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
}
|
|
1
|
+
const path = require("path")
|
|
2
|
+
const TerserPlugin = require("terser-webpack-plugin")
|
|
3
|
+
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
|
|
4
|
+
let isProduction = process.env.NODE_ENV === "production"
|
|
5
|
+
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
entry: {
|
|
9
|
+
"CoCreate-lazy-loader": "./src/index.js",
|
|
10
|
+
},
|
|
11
|
+
output: {
|
|
12
|
+
path: path.resolve(__dirname, "dist"),
|
|
13
|
+
filename: isProduction ? "[name].min.js" : "[name].js",
|
|
14
|
+
libraryTarget: "umd",
|
|
15
|
+
libraryExport: "default",
|
|
16
|
+
library: ["CoCreate", "lazy-loader"],
|
|
17
|
+
globalObject: "this",
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
plugins: [
|
|
21
|
+
new CleanWebpackPlugin(),
|
|
22
|
+
new MiniCssExtractPlugin({
|
|
23
|
+
filename: "[name].css",
|
|
24
|
+
}),
|
|
25
|
+
],
|
|
26
|
+
// Default mode for Webpack is production.
|
|
27
|
+
// Depending on mode Webpack will apply different things
|
|
28
|
+
// on final bundle. For now we don't need production's JavaScript
|
|
29
|
+
// minifying and other thing so let's set mode to development
|
|
30
|
+
mode: isProduction ? "production" : "development",
|
|
31
|
+
module: {
|
|
32
|
+
rules: [
|
|
33
|
+
{
|
|
34
|
+
test: /.js$/,
|
|
35
|
+
exclude: /(node_modules)/,
|
|
36
|
+
use: {
|
|
37
|
+
loader: "babel-loader",
|
|
38
|
+
options: {
|
|
39
|
+
plugins: ["@babel/plugin-transform-modules-commonjs"],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
test: /.css$/i,
|
|
45
|
+
use: [
|
|
46
|
+
{ loader: "style-loader", options: { injectType: "linkTag" } },
|
|
47
|
+
"file-loader",
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
// add source map
|
|
54
|
+
...(isProduction ? {} : { devtool: "eval-source-map" }),
|
|
55
|
+
|
|
56
|
+
optimization: {
|
|
57
|
+
minimize: true,
|
|
58
|
+
minimizer: [
|
|
59
|
+
new TerserPlugin({
|
|
60
|
+
extractComments: true,
|
|
61
|
+
// cache: true,
|
|
62
|
+
parallel: true,
|
|
63
|
+
// sourceMap: true, // Must be set to true if using source-maps in production
|
|
64
|
+
terserOptions: {
|
|
65
|
+
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
|
|
66
|
+
// extractComments: 'all',
|
|
67
|
+
compress: {
|
|
68
|
+
drop_console: true,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
splitChunks: {
|
|
74
|
+
chunks: "all",
|
|
75
|
+
minSize: 200,
|
|
76
|
+
// maxSize: 99999,
|
|
77
|
+
//minChunks: 1,
|
|
78
|
+
|
|
79
|
+
cacheGroups: {
|
|
80
|
+
defaultVendors: false,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}
|