Skip to content

Commit

Permalink
convert to module
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Nov 15, 2024
1 parent db2d346 commit b11b172
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 50 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pnpm-debug.log*
.pnpm-debug.log

node_modules
*~

example/pnpm-lock.yaml
example/dist
example/dist-xdc
*~
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
example/dist
example/dist-xdc
emulator/webxdc.js
src/webxdc.js
pnpm-lock.yaml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ export default defineConfig({
});
```

For a full example check the [example](https://github.com/webxdc/vite-plugins/tree/master/example) folder.
For a full example check the [example](https://github.com/webxdc/vite-plugins/tree/main/example) folder.
10 changes: 7 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
<h1>Hello World</h1>
<script>
window.onload = () =>
window.webxdc.sendUpdate({
payload: "hello from " + window.webxdc.selfName,
});
window.webxdc.sendUpdate(
{
payload: null,
info: "hello from " + window.webxdc.selfName,
},
"",
);
</script>
</body>
</html>
8 changes: 1 addition & 7 deletions example/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// example vite configuration

import {
buildXDC,
eruda,
mockWebxdc,
legacy,
injectScript,
} from "@webxdc/vite-plugins";
import { buildXDC, eruda, mockWebxdc, legacy } from "@webxdc/vite-plugins";
import { defineConfig } from "vite";

// https://vitejs.dev/config/
Expand Down
26 changes: 25 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
{
"name": "@webxdc/vite-plugins",
"description": "Collection of vite plugins for webxdc development",
"version": "0.3.0",
"license": "MIT",
"author": "adbenitez",
"keywords": [
"webxdc",
"deltachat"
],
"repository": {
"type": "git",
"url": "https://github.com/webxdc/vite-plugins.git"
},
"bugs": {
"url": "https://github.com/webxdc/vite-plugins/issues"
},
"homepage": "https://github.com/webxdc/vite-plugins",
"main": "./src/index.js",
"module": "./src/index.js",
"source": "./src/index.js",
"type": "module",
"exports": {
".": {
"import": "./src/index.js"
}
},
"scripts": {
"fix": "prettier --write .",
"check": "prettier --check ."
},
"main": "src/index.js",
"dependencies": {
"@vitejs/plugin-legacy": "^5.4.3",
"eruda": "^3.4.1",
"terser": "^5.36.0",
"vite-plugin-zip-pack": "^1.2.4"
},
"devDependencies": {
Expand Down
27 changes: 15 additions & 12 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions src/build_xdc.js → src/build-xdc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const zipPack = require("vite-plugin-zip-pack").default;
import zipPack from "vite-plugin-zip-pack";

function defaultFilter(fileName, filePath, isDirectory) {
return !fileName.endsWith("~");
}

// package your app as .xdc file on build
exports.buildXDC = function (opts = {}) {
export function buildXDC(opts = {}) {
opts = Object.assign(
{ outDir: "dist-xdc", outFileName: "app.xdc", filter: defaultFilter },
opts,
);
return zipPack(opts);
};
}
9 changes: 6 additions & 3 deletions src/eruda.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const readFileSync = require("node:fs").readFileSync;
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);

// inject eruda dev tools in your app for debugging in end devices if NODE_ENV=debug is set
exports.eruda = function (debug = undefined) {
export function eruda(debug = undefined) {
const erudaSrc = readFileSync(require.resolve("eruda"), "utf-8");
return {
name: "vite-plugin-eruda",
Expand Down Expand Up @@ -38,4 +41,4 @@ exports.eruda = function (debug = undefined) {
}
},
};
};
}
19 changes: 8 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const buildXDC = require("./build_xdc").buildXDC;
const eruda = require("./eruda").eruda;
const mockWebxdc = require("./mock_webxdc").mockWebxdc;
const legacy = require("./legacy").legacy;

exports.buildXDC = buildXDC;
exports.eruda = eruda;
exports.mockWebxdc = mockWebxdc;
exports.legacy = legacy;
import { buildXDC } from "./build-xdc.js";
import { eruda } from "./eruda.js";
import { mockWebxdc } from "./mock-webxdc.js";
import { legacy } from "./legacy.js";

/**
* The recommended Vite config for a Webxdc app.
*
* @returns The recommended Vite config for a Webxdc app.
*/
exports.webxdcViteConfig = function (options = {}) {
export const webxdcViteConfig = function (options = {}) {
options = Object.assign(
{ plugins: [buildXDC(), eruda(), legacy(), mockWebxdc()] },
options,
Expand All @@ -26,10 +21,12 @@ exports.webxdcViteConfig = function (options = {}) {
*
* @returns Vite config for a Webxdc app.
*/
exports.webxdcViteConfigNoLegacy = function (options = {}) {
export const webxdcViteConfigNoLegacy = function (options = {}) {
options = Object.assign(
{ plugins: [buildXDC(), eruda(), mockWebxdc()] },
options,
);
return options;
};

export { buildXDC, eruda, mockWebxdc, legacy };
6 changes: 3 additions & 3 deletions src/legacy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const _legacy = require("@vitejs/plugin-legacy");
import _legacy from "@vitejs/plugin-legacy";

// convert your code to be compatible with legacy browsers and adds polyfills
exports.legacy = function (opts = {}) {
export function legacy(opts = {}) {
opts = Object.assign(
{ targets: ["ChromeAndroid >=37", "iOS >=11"], renderModernChunks: false },
opts,
);
return _legacy(opts);
};
}
8 changes: 4 additions & 4 deletions src/mock_webxdc.js → src/mock-webxdc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const readFileSync = require("node:fs").readFileSync;
import { readFileSync } from "node:fs";

// serve a mock webxdc.js file in development mode to fake webxdc API while developing in the browser
exports.mockWebxdc = function (
path = "./node_modules/@webxdc/vite-plugins/emulator/webxdc.js",
export function mockWebxdc(
path = "./node_modules/@webxdc/vite-plugins/src/webxdc.js",
) {
const scriptSrc = readFileSync(path, "utf-8");
return {
Expand All @@ -17,4 +17,4 @@ exports.mockWebxdc = function (
});
},
};
};
}
File renamed without changes.

0 comments on commit b11b172

Please sign in to comment.