From 227a38eb9656e29da26da5c925f5fa7b5065d88d Mon Sep 17 00:00:00 2001 From: hu de yi Date: Thu, 14 Nov 2024 16:05:44 +0800 Subject: [PATCH] Optimize the worker packaging process (#651) --- package.json | 1 - rollup.config.js | 35 ++++++++++++++++++++++++++++++++--- rollup.worker.config.js | 35 ----------------------------------- src/index.ts | 5 +++-- worker-plugin.js | 19 +++++++++++++++++++ workerinit.js | 19 ------------------- 6 files changed, 54 insertions(+), 60 deletions(-) delete mode 100644 rollup.worker.config.js create mode 100644 worker-plugin.js delete mode 100644 workerinit.js diff --git a/package.json b/package.json index 7968bd8..f8ac889 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rollup.config.js b/rollup.config.js index db65568..591a0f3 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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'; @@ -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, @@ -102,7 +131,7 @@ let bundleList = [ plugins: basePlugins.concat([terser()]), external: ['maptalks', 'three'], output: { - 'sourcemap': false, + 'sourcemap': true, 'format': 'umd', 'name': 'maptalks', 'banner': banner, @@ -140,7 +169,7 @@ let bundleList = [ plugins: es5BasePlugins.concat([terser()]), external: ['maptalks', 'three'], output: { - 'sourcemap': false, + 'sourcemap': true, 'format': 'umd', 'name': 'maptalks', 'banner': banner, @@ -169,6 +198,6 @@ let bundleList = [ // } ]; if (dev) { - bundleList = bundleList.slice(0, 1); + bundleList = bundleList.slice(0, 2); } module.exports = bundleList; diff --git a/rollup.worker.config.js b/rollup.worker.config.js deleted file mode 100644 index aa59bc2..0000000 --- a/rollup.worker.config.js +++ /dev/null @@ -1,35 +0,0 @@ -import { nodeResolve as resolve } from '@rollup/plugin-node-resolve'; -import { babel } from '@rollup/plugin-babel'; -import commonjs from '@rollup/plugin-commonjs'; -import { terser } from 'rollup-plugin-terser'; -import json from '@rollup/plugin-json'; - -module.exports = [ - { - input: 'src/worker/index.js', - plugins: [ - json(), - resolve({ - module: true, - jsnext: true, - main: true - }), - commonjs(), - babel(), - terser() - ], - external: ['maptalks'], - output: { - format: 'amd', - name: 'maptalks', - globals: { - 'maptalks': 'maptalks' - }, - extend: true, - file: 'dist/worker.amd.js' - }, - // watch: { - // include: 'src/worker/**' - // } - } -]; diff --git a/src/index.ts b/src/index.ts index c32c3f9..90cf115 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = { @@ -1722,6 +1723,6 @@ export { }; if (maptalks.registerWorkerAdapter) { - maptalks.registerWorkerAdapter(getWorkerName(), getWorkerCode()); + maptalks.registerWorkerAdapter(getWorkerName(), workerCode); maptalks.registerWorkerAdapter(fetchDataWorkerKey, fetchDataWorkerCode); } diff --git a/worker-plugin.js b/worker-plugin.js new file mode 100644 index 0000000..64e446b --- /dev/null +++ b/worker-plugin.js @@ -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 + '`'; + } + }; +} diff --git a/workerinit.js b/workerinit.js deleted file mode 100644 index 02540ef..0000000 --- a/workerinit.js +++ /dev/null @@ -1,19 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const chalk = require('chalk'); - -const bundlePath = path.join(__dirname, './dist/worker.amd.js'); -let code = fs.readFileSync(bundlePath).toString(); -console.log(chalk.greenBright(`read ${bundlePath} worker bundle success`)); -code = code.replace(`define(['exports'],`, ''); -code = code.replace(`define(["exports"],`, ''); -code = code.substring(0, code.length - 3); - -const workerTempPath = path.join(__dirname, './src/worker/worker.ts'); -let worker = fs.readFileSync(workerTempPath).toString(); -worker = worker.replace('{code}', code); -console.log(chalk.greenBright(`read ${workerTempPath} workte template success`)); - -const getWorkerPath = path.join(__dirname, './src/worker/getworker.ts'); -fs.writeFileSync(getWorkerPath, worker); -console.log(chalk.greenBright(`write ${getWorkerPath} worker success`)); \ No newline at end of file