浏览代码

dev: update puter.js imports to work in cli

This is a step towards getting puter.js to run in node instead of just
in the browser. In node, all the relative imports need to end with the
file extension, and the "type" option in package.json needs to match the
import mechanism used. It was also necessary to migrate
webpack.config.js to ESM syntax, which I would not have done if the
tooling didn't force me to.
KernelDeimos 2 月之前
父节点
当前提交
be59f57d68

+ 1 - 0
src/puter-js/package.json

@@ -3,6 +3,7 @@
   "version": "1.0.0",
   "version": "1.0.0",
   "description": "",
   "description": "",
   "main": "index.js",
   "main": "index.js",
+  "type": "module",
   "scripts": {
   "scripts": {
     "start-server": "npx http-server --cors -c-1",
     "start-server": "npx http-server --cors -c-1",
     "start-webpack": "webpack && webpack --output-filename puter.dev.js --watch --devtool source-map",
     "start-webpack": "webpack && webpack --output-filename puter.dev.js --watch --devtool source-map",

+ 1 - 0
src/puter-js/src/clitest.js

@@ -0,0 +1 @@
+import puter from './index.js';

+ 1 - 1
src/puter-js/src/index.js

@@ -33,7 +33,7 @@ import Perms from './modules/Perms.js';
 //       (using defaultGUIOrigin breaks locally-hosted apps)
 //       (using defaultGUIOrigin breaks locally-hosted apps)
 const PROD_ORIGIN = 'https://puter.com';
 const PROD_ORIGIN = 'https://puter.com';
 
 
-window.puter = (function() {
+export default window.puter = (function() {
     'use strict';
     'use strict';
 
 
     class Puter{
     class Puter{

+ 1 - 1
src/puter-js/src/lib/filesystem/APIFS.js

@@ -1,6 +1,6 @@
 import * as utils from '../utils.js';
 import * as utils from '../utils.js';
 import putility from "@heyputer/putility";
 import putility from "@heyputer/putility";
-import { TeePromise } from "@heyputer/putility/src/libs/promise";
+import { TeePromise } from "@heyputer/putility/src/libs/promise.js";
 import getAbsolutePathForApp from '../../modules/FileSystem/utils/getAbsolutePathForApp.js';
 import getAbsolutePathForApp from '../../modules/FileSystem/utils/getAbsolutePathForApp.js';
 import { TFilesystem } from './definitions.js';
 import { TFilesystem } from './definitions.js';
 
 

+ 3 - 3
src/puter-js/src/lib/filesystem/CacheFS.js

@@ -1,7 +1,7 @@
 import putility from "@heyputer/putility";
 import putility from "@heyputer/putility";
-import { RWLock } from "@heyputer/putility/src/libs/promise";
-import { ProxyFilesystem, TFilesystem } from "./definitions";
-import { uuidv4 } from "../utils";
+import { RWLock } from "@heyputer/putility/src/libs/promise.js";
+import { ProxyFilesystem, TFilesystem } from "./definitions.js";
+import { uuidv4 } from "../utils.js";
 
 
 export const ROOT_UUID = '00000000-0000-0000-0000-000000000000';
 export const ROOT_UUID = '00000000-0000-0000-0000-000000000000';
 const TTL = 5 * 1000;
 const TTL = 5 * 1000;

+ 1 - 1
src/puter-js/src/lib/filesystem/PostMessageFS.js

@@ -1,5 +1,5 @@
 import putility from "@heyputer/putility";
 import putility from "@heyputer/putility";
-import { TFilesystem } from "./definitions";
+import { TFilesystem } from "./definitions.js";
 
 
 const example =     {
 const example =     {
         "id": "f485f1ba-de07-422c-8c4b-c2da057d4a44",
         "id": "f485f1ba-de07-422c-8c4b-c2da057d4a44",

+ 1 - 1
src/puter-js/src/lib/http.js

@@ -1,5 +1,5 @@
 import putility from "@heyputer/putility";
 import putility from "@heyputer/putility";
-import EventListener from "./EventListener";
+import EventListener from "./EventListener.js";
 
 
 // TODO: this inheritance is an anti-pattern; we should use
 // TODO: this inheritance is an anti-pattern; we should use
 //       a trait or mixin for event emitters.
 //       a trait or mixin for event emitters.

+ 1 - 1
src/puter-js/src/modules/PuterDialog.js

@@ -417,4 +417,4 @@ class PuterDialog extends HTMLElement {
 }
 }
 customElements.define('puter-dialog', PuterDialog);
 customElements.define('puter-dialog', PuterDialog);
 
 
-module.exports = PuterDialog;
+export default PuterDialog;

+ 1 - 1
src/puter-js/src/modules/Util.js

@@ -1,4 +1,4 @@
-import { $SCOPE, CallbackManager, Dehydrator, Hydrator } from "../lib/xdrpc";
+import { $SCOPE, CallbackManager, Dehydrator, Hydrator } from "../lib/xdrpc.js";
 
 
 /**
 /**
  * The Util module exposes utilities within puter.js itself.
  * The Util module exposes utilities within puter.js itself.

+ 1 - 1
src/puter-js/src/modules/networking/PTLS.js

@@ -2,7 +2,7 @@
  * This file uses https://github.com/MercuryWorkshop/rustls-wasm authored by GitHub:@r58Playz under the MIT License
  * This file uses https://github.com/MercuryWorkshop/rustls-wasm authored by GitHub:@r58Playz under the MIT License
  */
  */
 
 
-import { PSocket } from "./PSocket";
+import { PSocket } from "./PSocket.js";
 
 
 let rustls = undefined;
 let rustls = undefined;
 
 

+ 3 - 3
src/puter-js/src/services/Filesystem.js

@@ -1,7 +1,7 @@
 import putility from "@heyputer/putility";
 import putility from "@heyputer/putility";
-import { PuterAPIFilesystem } from "../lib/filesystem/APIFS";
-import { CachedFilesystem } from "../lib/filesystem/CacheFS";
-import { ProxyFilesystem, TFilesystem } from "../lib/filesystem/definitions";
+import { PuterAPIFilesystem } from "../lib/filesystem/APIFS.js";
+import { CachedFilesystem } from "../lib/filesystem/CacheFS.js";
+import { ProxyFilesystem, TFilesystem } from "../lib/filesystem/definitions.js";
 import io from '../lib/socket.io/socket.io.esm.min.js';
 import io from '../lib/socket.io/socket.io.esm.min.js';
 import { PostMessageFilesystem } from "../lib/filesystem/PostMessageFS.js";
 import { PostMessageFilesystem } from "../lib/filesystem/PostMessageFS.js";
 
 

+ 12 - 3
src/puter-js/webpack.config.js

@@ -1,9 +1,18 @@
-const path = require('path');
-const webpack = require('webpack');
+import path from 'node:path';
+import webpack from 'webpack';
+
+// '__dirname' isn't defined by default in ES modules.
+// We didn't really want to migrate this file to ESM because
+// it's config for tooling that only runs in node, but alas
+// if package.json says "type": "module" then we have to use
+// ESM syntax everywhere unless we rename this to a .cjs file
+// and add an extra flag everywhere we use webpack.
+import { fileURLToPath } from 'url';
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
 
 
 console.log('ENV CHECK!!!', process.env.PUTER_ORIGIN, process.env.PUTER_API_ORIGIN);
 console.log('ENV CHECK!!!', process.env.PUTER_ORIGIN, process.env.PUTER_API_ORIGIN);
 
 
-module.exports = {
+export default {
   entry: './src/index.js',
   entry: './src/index.js',
   output: {
   output: {
     filename: 'puter.js',
     filename: 'puter.js',