Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a node-compatible export #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"module": "public/build/index.js",
"types": "public/build/index.d.ts",
"type": "module",
"exports": {
".": {
"import": "./public/build/index.js",
"types": "./public/build/index.d.ts"
},
"./node": {
"import": "./public/build/node.js",
"types": "./public/build/node.d.ts"
}
},
"source": "src/index.ts",
"scripts": {
"start": "webpack serve",
Expand All @@ -15,14 +25,15 @@
"license": "MIT",
"devDependencies": {
"@types/three": "^0.169.0",
"three": "^0.169.0",
"source-map-loader": "^3.0.1",
"three": "^0.169.0",
"ts-loader": "^9.2.8",
"typescript": "^4.6.3",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.8.1",
"comlink": "^4.3.1",
"webpack-dev-server": "^4.8.1"
},
"dependencies": {
"xatlasjs": "^0.2.0"
}
}
24 changes: 15 additions & 9 deletions src/UVUnwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,23 @@ export abstract class BaseUVUnwrapper {
this.xAtlas = this._createXAtlas()
}

private _libraryLoaded = false;
private _libraryPromise = null;

async loadLibrary(onProgress: (mode: any, progress: any) => void, wasmFilePath: string, workerFilePath?: string): Promise<void> {
if (this._libraryLoaded) return
await new Promise<void>((resolve, reject) => {
if (this._libraryPromise !== null) return
this._libraryPromise = new Promise<void>((resolve, reject) => {
try {
this.xAtlas.init(resolve, onProgress, wasmFilePath, workerFilePath)
} catch (e) {
reject(e)
}
})
while (!(this.xAtlas.api ? await this.xAtlas.api.loaded : false)) {
await new Promise(r => setTimeout(r, 100)); // wait for load just in case
}
this._libraryLoaded = true;
}).then(async () => {
while (!(this.xAtlas.api ? await this.xAtlas.api.loaded : false)) {
await new Promise(r => setTimeout(r, 100)); // wait for load just in case
}
});

await this._libraryPromise
}

private _isUnwrapping = false;
Expand All @@ -117,9 +119,13 @@ export abstract class BaseUVUnwrapper {
* @param inputUv - Attribute to write the input uv to (if any)
*/
public async packAtlas(nodeList: BufferGeometry[], outputUv: 'uv' | 'uv2' = 'uv2', inputUv: 'uv' | 'uv2' = 'uv'): Promise<Atlas> {
if (!this._libraryLoaded) {
if (this._libraryPromise === null) {
throw new Error('xatlas-three: library not loaded');
}

// wait for the library to finish loading
await this._libraryPromise

if (!nodeList) throw new Error('xatlas-three: nodeList argument not provided');
if (nodeList.length < 1) throw new Error('xatlas-three: nodeList must have non-zero length');
const useUvs = this.chartOptions.useInputMeshUvs;
Expand Down
29 changes: 29 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Atlas, BaseUVUnwrapper} from "./UVUnwrapper";
import {XAtlasNodeWorker} from "./XAtlasNodeWorker";
import {createRequire} from "node:module";
import path from "node:path";

export class UVUnwrapper extends BaseUVUnwrapper{
protected _createXAtlas(): any {
return new XAtlasNodeWorker()
}

constructor(...args:any[]) {
super(...args);
Comment on lines +11 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript seems to not like this spread syntax for passing through the arguments to the parent constructor. I looked it up but there didn't seem to be a clear solution. Do we have to reproduce all the arguments and types for this constructor?


// https://stackoverflow.com/questions/54977743/do-require-resolve-for-es-modules
const require = createRequire(import.meta.url);
const pathName = path.dirname(require.resolve('xatlasjs/package.json'));

// Make sure to wait for the library to load before unwrapping.
this.loadLibrary(
(mode, progress)=>{},
`${pathName}/dist/node/xatlas.wasm`,
`${pathName}/dist/node/worker.mjs`,
);
}

exit(): Promise<void> {
return this.xAtlas?.api.exit();
}
}
12 changes: 0 additions & 12 deletions src/unwrapperNodeWorker.ts

This file was deleted.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"sourceMap": true,
"outDir": "./public/build",
"declarationDir": "./public/build",
"esModuleInterop": true,
/* Module Resolution Options */
"moduleResolution": "node",
/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
10 changes: 7 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ const __dirname = path.dirname(__filename);
export default (env)=>({
mode: 'development',
devtool: 'source-map',
entry: env.no_worker ? './src/unwrapperJS.ts' : './src/unwrapperWorker.ts',
entry: {
index: env.no_worker ? './src/unwrapperJS.ts' : './src/unwrapperWorker.ts',
node: './src/node.ts',
},
experiments: {
outputModule: true
outputModule: true
},
externals: /^node:.+/,
devServer: {
hot: false,
port: 6237,
Expand All @@ -25,7 +29,7 @@ export default (env)=>({
client: false,
},
output: {
filename: !!env.no_worker ? 'index-no-worker.js' : 'index.js',
filename: !!env.no_worker ? '[name]-no-worker.js' : '[name].js',
module: true,
path: path.resolve(__dirname, 'public/build'),
publicPath: '/public/build/',
Expand Down