earthsdk3-assets 3.0.27 → 3.0.28
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/package.json +1 -1
- package/src/plugins/rspack.cjs +38 -40
- package/src/plugins/webpack.cjs +39 -41
package/package.json
CHANGED
package/src/plugins/rspack.cjs
CHANGED
|
@@ -4,6 +4,8 @@ const { downloadAssets } = require('../core/downloadAssets.cjs');
|
|
|
4
4
|
const { copyAssetsToDest, getAssetsPaths } = require('../core/copyAssets.cjs');
|
|
5
5
|
|
|
6
6
|
const ROOT_DIR = path.resolve(__dirname, '../..');
|
|
7
|
+
const ASSETS_DIR = path.resolve(ROOT_DIR, 'assets');
|
|
8
|
+
const IGNORE_FILES = new Set(['.assets-version', '.assets-manifest.json']);
|
|
7
9
|
let hasDownloaded = false;
|
|
8
10
|
|
|
9
11
|
async function ensureAssets(assetsDir, cacheFile, manifestCacheFile) {
|
|
@@ -14,6 +16,25 @@ async function ensureAssets(assetsDir, cacheFile, manifestCacheFile) {
|
|
|
14
16
|
await downloadAssets(assetsDir, cacheFile, manifestCacheFile);
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
function addDirToCompilation(srcDir, assetPrefix, compilation) {
|
|
20
|
+
const items = fs.readdirSync(srcDir);
|
|
21
|
+
items.forEach(item => {
|
|
22
|
+
if (IGNORE_FILES.has(item)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const srcPath = path.join(srcDir, item);
|
|
26
|
+
if (fs.statSync(srcPath).isDirectory()) {
|
|
27
|
+
addDirToCompilation(srcPath, assetPrefix + '/' + item, compilation);
|
|
28
|
+
} else {
|
|
29
|
+
const content = fs.readFileSync(srcPath);
|
|
30
|
+
compilation.assets[assetPrefix + '/' + item] = {
|
|
31
|
+
source: () => content,
|
|
32
|
+
size: () => content.length
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
17
38
|
class EarthsdkAssetsRspackPlugin {
|
|
18
39
|
constructor(options = {}) {
|
|
19
40
|
this.options = options;
|
|
@@ -31,10 +52,6 @@ class EarthsdkAssetsRspackPlugin {
|
|
|
31
52
|
await ensureAssets(assetsDir, cacheFile, manifestCacheFile);
|
|
32
53
|
});
|
|
33
54
|
|
|
34
|
-
compiler.hooks.afterEnvironment.tap('EarthsdkAssetsRspackPlugin', () => {
|
|
35
|
-
this.outDir = compiler.options.output.path || 'dist';
|
|
36
|
-
});
|
|
37
|
-
|
|
38
55
|
compiler.hooks.compilation.tap('EarthsdkAssetsRspackPlugin', (compilation) => {
|
|
39
56
|
let HtmlPlugin;
|
|
40
57
|
try {
|
|
@@ -66,49 +83,30 @@ class EarthsdkAssetsRspackPlugin {
|
|
|
66
83
|
}
|
|
67
84
|
});
|
|
68
85
|
|
|
86
|
+
compiler.hooks.afterEnvironment.tap('EarthsdkAssetsRspackPlugin', () => {
|
|
87
|
+
this.outDir = compiler.options.output.path || 'dist';
|
|
88
|
+
});
|
|
89
|
+
|
|
69
90
|
compiler.hooks.afterEmit.tapPromise('EarthsdkAssetsRspackPlugin', async () => {
|
|
70
91
|
if (compiler.options.mode === 'production') {
|
|
71
92
|
copyAssetsToDest(assetsDir, ROOT_DIR, this.outDir);
|
|
72
93
|
}
|
|
73
94
|
});
|
|
74
95
|
|
|
75
|
-
compiler.hooks.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const filePath = path.join(ROOT_DIR, fileName);
|
|
86
|
-
|
|
87
|
-
if (!fs.existsSync(filePath)) {
|
|
88
|
-
return next();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const ext = path.extname(filePath).slice(1).toLowerCase();
|
|
92
|
-
const mimeTypes = {
|
|
93
|
-
js: 'application/javascript',
|
|
94
|
-
json: 'application/json',
|
|
95
|
-
png: 'image/png',
|
|
96
|
-
jpg: 'image/jpeg',
|
|
97
|
-
jpeg: 'image/jpeg',
|
|
98
|
-
webp: 'image/webp',
|
|
99
|
-
glb: 'model/gltf-binary',
|
|
100
|
-
bin: 'application/octet-stream'
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
res.setHeader('Content-Type', mimeTypes[ext] || 'application/octet-stream');
|
|
104
|
-
res.setHeader('Cache-Control', 'max-age=3600');
|
|
105
|
-
fs.createReadStream(filePath).pipe(res);
|
|
106
|
-
});
|
|
96
|
+
compiler.hooks.emit.tapPromise('EarthsdkAssetsRspackPlugin', async (compilation) => {
|
|
97
|
+
if (compiler.options.mode === 'development') {
|
|
98
|
+
const mainJsSource = path.join(ROOT_DIR, 'earthsdk3-assets.js');
|
|
99
|
+
if (fs.existsSync(mainJsSource)) {
|
|
100
|
+
const mainJsContent = fs.readFileSync(mainJsSource, 'utf-8');
|
|
101
|
+
compilation.assets['js/earthsdk3-assets/earthsdk3-assets.js'] = {
|
|
102
|
+
source: () => mainJsContent,
|
|
103
|
+
size: () => Buffer.byteLength(mainJsContent, 'utf-8')
|
|
104
|
+
};
|
|
105
|
+
}
|
|
107
106
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
};
|
|
107
|
+
if (fs.existsSync(ASSETS_DIR)) {
|
|
108
|
+
addDirToCompilation(ASSETS_DIR, 'js/earthsdk3-assets/assets', compilation);
|
|
109
|
+
}
|
|
112
110
|
}
|
|
113
111
|
});
|
|
114
112
|
}
|
package/src/plugins/webpack.cjs
CHANGED
|
@@ -4,6 +4,8 @@ const { downloadAssets } = require('../core/downloadAssets.cjs');
|
|
|
4
4
|
const { copyAssetsToDest, getAssetsPaths } = require('../core/copyAssets.cjs');
|
|
5
5
|
|
|
6
6
|
const ROOT_DIR = path.resolve(__dirname, '../..');
|
|
7
|
+
const ASSETS_DIR = path.resolve(ROOT_DIR, 'assets');
|
|
8
|
+
const IGNORE_FILES = new Set(['.assets-version', '.assets-manifest.json']);
|
|
7
9
|
let hasDownloaded = false;
|
|
8
10
|
|
|
9
11
|
async function ensureAssets(assetsDir, cacheFile, manifestCacheFile) {
|
|
@@ -14,6 +16,25 @@ async function ensureAssets(assetsDir, cacheFile, manifestCacheFile) {
|
|
|
14
16
|
await downloadAssets(assetsDir, cacheFile, manifestCacheFile);
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
function addDirToCompilation(srcDir, assetPrefix, compilation) {
|
|
20
|
+
const items = fs.readdirSync(srcDir);
|
|
21
|
+
items.forEach(item => {
|
|
22
|
+
if (IGNORE_FILES.has(item)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const srcPath = path.join(srcDir, item);
|
|
26
|
+
if (fs.statSync(srcPath).isDirectory()) {
|
|
27
|
+
addDirToCompilation(srcPath, assetPrefix + '/' + item, compilation);
|
|
28
|
+
} else {
|
|
29
|
+
const content = fs.readFileSync(srcPath);
|
|
30
|
+
compilation.assets[assetPrefix + '/' + item] = {
|
|
31
|
+
source: () => content,
|
|
32
|
+
size: () => content.length
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
17
38
|
class EarthsdkAssetsWebpackPlugin {
|
|
18
39
|
constructor(options = {}) {
|
|
19
40
|
this.options = options;
|
|
@@ -31,10 +52,6 @@ class EarthsdkAssetsWebpackPlugin {
|
|
|
31
52
|
await ensureAssets(assetsDir, cacheFile, manifestCacheFile);
|
|
32
53
|
});
|
|
33
54
|
|
|
34
|
-
compiler.hooks.afterEnvironment.tap('EarthsdkAssetsWebpackPlugin', () => {
|
|
35
|
-
this.outDir = compiler.options.output.path || 'dist';
|
|
36
|
-
});
|
|
37
|
-
|
|
38
55
|
compiler.hooks.compilation.tap('EarthsdkAssetsWebpackPlugin', (compilation) => {
|
|
39
56
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
40
57
|
|
|
@@ -56,53 +73,34 @@ class EarthsdkAssetsWebpackPlugin {
|
|
|
56
73
|
}
|
|
57
74
|
});
|
|
58
75
|
|
|
76
|
+
compiler.hooks.afterEnvironment.tap('EarthsdkAssetsWebpackPlugin', () => {
|
|
77
|
+
this.outDir = compiler.options.output.path || 'dist';
|
|
78
|
+
});
|
|
79
|
+
|
|
59
80
|
compiler.hooks.afterEmit.tapPromise('EarthsdkAssetsWebpackPlugin', async () => {
|
|
60
81
|
if (compiler.options.mode === 'production') {
|
|
61
82
|
copyAssetsToDest(assetsDir, ROOT_DIR, this.outDir);
|
|
62
83
|
}
|
|
63
84
|
});
|
|
64
85
|
|
|
65
|
-
compiler.hooks.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const filePath = path.join(ROOT_DIR, fileName);
|
|
76
|
-
|
|
77
|
-
if (!fs.existsSync(filePath)) {
|
|
78
|
-
return next();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const ext = path.extname(filePath).slice(1).toLowerCase();
|
|
82
|
-
const mimeTypes = {
|
|
83
|
-
js: 'application/javascript',
|
|
84
|
-
json: 'application/json',
|
|
85
|
-
png: 'image/png',
|
|
86
|
-
jpg: 'image/jpeg',
|
|
87
|
-
jpeg: 'image/jpeg',
|
|
88
|
-
webp: 'image/webp',
|
|
89
|
-
glb: 'model/gltf-binary',
|
|
90
|
-
bin: 'application/octet-stream'
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
res.setHeader('Content-Type', mimeTypes[ext] || 'application/octet-stream');
|
|
94
|
-
res.setHeader('Cache-Control', 'max-age=3600');
|
|
95
|
-
fs.createReadStream(filePath).pipe(res);
|
|
96
|
-
});
|
|
86
|
+
compiler.hooks.emit.tapPromise('EarthsdkAssetsWebpackPlugin', async (compilation) => {
|
|
87
|
+
if (compiler.options.mode === 'development') {
|
|
88
|
+
const mainJsSource = path.join(ROOT_DIR, 'earthsdk3-assets.js');
|
|
89
|
+
if (fs.existsSync(mainJsSource)) {
|
|
90
|
+
const mainJsContent = fs.readFileSync(mainJsSource, 'utf-8');
|
|
91
|
+
compilation.assets['js/earthsdk3-assets/earthsdk3-assets.js'] = {
|
|
92
|
+
source: () => mainJsContent,
|
|
93
|
+
size: () => Buffer.byteLength(mainJsContent, 'utf-8')
|
|
94
|
+
};
|
|
95
|
+
}
|
|
97
96
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
};
|
|
97
|
+
if (fs.existsSync(ASSETS_DIR)) {
|
|
98
|
+
addDirToCompilation(ASSETS_DIR, 'js/earthsdk3-assets/assets', compilation);
|
|
99
|
+
}
|
|
102
100
|
}
|
|
103
101
|
});
|
|
104
102
|
}
|
|
105
103
|
}
|
|
106
104
|
|
|
107
105
|
module.exports = EarthsdkAssetsWebpackPlugin;
|
|
108
|
-
module.exports.default = EarthsdkAssetsWebpackPlugin;
|
|
106
|
+
module.exports.default = EarthsdkAssetsWebpackPlugin;
|