Skip to content

Commit

Permalink
Optimize the worker packaging process (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
deyihu authored Nov 14, 2024
1 parent 8c25d50 commit 227a38e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 60 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"module": "dist/index.js",
"jsnext:main": "dist/index.js",
"scripts": {
"worker": "rollup --environment BUILD:production -c rollup.worker.config.js && node workerinit.js",
"tsc": "npx tsc",
"dev": "rollup --environment BUILD:dev -w -c rollup.config.js",
"build": "npm run tsc && rollup --environment BUILD:production -c rollup.config.js",
Expand Down
35 changes: 32 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import commonjs from '@rollup/plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import mtkWorkerPlugin from './worker-plugin';

const pkg = require('./package.json');
const dev = process.env.BUILD === 'dev';

Expand Down Expand Up @@ -77,7 +79,34 @@ const es5BasePlugins = [
}),
removeGlobal()
];

const workerPlugins = [
json(),
resolve({
module: true,
jsnext: true,
main: true
}),
commonjs(),
babel()
];

let bundleList = [
{
input: 'src/worker/index.js',
plugins: dev ? workerPlugins.concat([mtkWorkerPlugin()]) : workerPlugins.concat([terser(), mtkWorkerPlugin()]),
external: ['maptalks', 'three'],
output: {
'sourcemap': true,
format: 'amd',
name: 'maptalks',
globals: {
'maptalks': 'maptalks'
},
extend: true,
file: 'dist/worker.amd.js'
}
},
{
input: 'src/index.ts',
plugins: basePlugins,
Expand All @@ -102,7 +131,7 @@ let bundleList = [
plugins: basePlugins.concat([terser()]),
external: ['maptalks', 'three'],
output: {
'sourcemap': false,
'sourcemap': true,
'format': 'umd',
'name': 'maptalks',
'banner': banner,
Expand Down Expand Up @@ -140,7 +169,7 @@ let bundleList = [
plugins: es5BasePlugins.concat([terser()]),
external: ['maptalks', 'three'],
output: {
'sourcemap': false,
'sourcemap': true,
'format': 'umd',
'name': 'maptalks',
'banner': banner,
Expand Down Expand Up @@ -169,6 +198,6 @@ let bundleList = [
// }
];
if (dev) {
bundleList = bundleList.slice(0, 1);
bundleList = bundleList.slice(0, 2);
}
module.exports = bundleList;
35 changes: 0 additions & 35 deletions rollup.worker.config.js

This file was deleted.

5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ import * as geometryExtrude from 'deyihu-geometry-extrude';
import * as polyextrude from 'poly-extrude';
import LineMaterial from './util/fatline/LineMaterial';
import { BarOptionType, BaseLayerOptionType, BaseObjectOptionType, ExtrudeLineOptionType, ExtrudeLineTrailOptionType, ExtrudePolygonOptionType, FatLineMaterialType, getBaseObjectMaterialType, HeatMapDataType, HeatMapOptionType, LineMaterialType, LineOptionType, LineStringType, PathOptionType, PointOptionType, PolygonType, SingleLineStringType, TerrainOptionType } from './type/index';
import { getWorkerCode, getWorkerName } from './worker/getworker';
import { getWorkerName } from './worker/worker';
import { BaseObjectTaskManager, BaseObjectTask } from './BaseObjectTaskManager';
import { fetchDataWorkerKey, fetchDataWorkerCode, getFetchDataActor } from './worker/fetchdataworker';
import Path from './Path';
import Paths from './Paths';
import workerCode from './../dist/worker.amd';


const options: BaseLayerOptionType = {
Expand Down Expand Up @@ -1722,6 +1723,6 @@ export {
};

if (maptalks.registerWorkerAdapter) {
maptalks.registerWorkerAdapter(getWorkerName(), getWorkerCode());
maptalks.registerWorkerAdapter(getWorkerName(), workerCode);
maptalks.registerWorkerAdapter(fetchDataWorkerKey, fetchDataWorkerCode);
}
19 changes: 19 additions & 0 deletions worker-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function WorkerPlugin() {
return {
name: 'maptalks-worker-resolve', // 此名称将出现在警告和错误中
// resolveId(source) {
// if (source === 'virtual-module') {
// // 这表示 rollup 不应询问其他插件或
// // 从文件系统检查以找到此 ID
// return source;
// }
// return null; // 其他ID应按通常方式处理
// },
renderChunk(code, chunk) {
code = code.replace('define([\'exports\'],', '');
code = code.replace('define(["exports"],', '');
code = code.substring(0, code.length - 2);
return 'export default `' + code + '`';
}
};
}
19 changes: 0 additions & 19 deletions workerinit.js

This file was deleted.

0 comments on commit 227a38e

Please sign in to comment.