Skip to content

Commit

Permalink
support official wq plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Oct 13, 2023
1 parent 50263bc commit e77e66d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/rollup-plugin/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/rollup-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wq/rollup-plugin",
"version": "2.0.0-alpha.3",
"version": "2.0.0-beta.1",
"description": "Build custom wq plugins that integrate with wq.js",
"type": "module",
"main": "src/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import app from "@wq/app";
import material from "@wq/material";
import mapgl from "@wq/map-gl";
import analyst from "@wq/analyst";
import wizard from "@wq/wizard";
import myPlugin from "./plugin.js";

app.use([material, mapgl, myPlugin]);
app.use([material, mapgl, analyst, wizard, myPlugin]);
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { modules } from "./wq.js";
import analyst from "./analyst.js";
import wizard from "./wizard.js";
import myPlugin from "./plugin.js";

const { "@wq/app": app } = modules;
Expand All @@ -9,4 +11,4 @@ const materialPlugin = material.default;
const { "@wq/map-gl": mapgl } = modules;
const mapglPlugin = mapgl.default;

app.use([materialPlugin, mapglPlugin, myPlugin]);
app.use([materialPlugin, mapglPlugin, analyst, wizard, myPlugin]);
21 changes: 14 additions & 7 deletions packages/rollup-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import modules from "./modules.js";

const prefix = "\0wq-bundle:";
const prefix = "\0wq-bundle:",
defaultConfig = {urlBase: "."};

export default function wq() {
export default function wq(config) {
const { urlBase } = {...defaultConfig,...config};
return {
name: "@wq/rollup-plugin",
resolveId(id) {
if (id == "./wq.js") {
if (id == `${urlBase}/wq.js`) {
return { id, external: true };
}
if (id.match(/\?commonjs-proxy$/)) {
Expand All @@ -16,33 +18,38 @@ export default function wq() {
return {
id: `${prefix}${id}`,
};
} else if (id.startsWith("@wq/")) {
return {
id: id.replace(/^@wq/, urlBase) + ".js",
external: true,
};
}
},
load(id) {
if (id.startsWith(prefix)) {
return createVirtualModule(id.replace(prefix, ""));
return createVirtualModule(id.replace(prefix, ""), urlBase);
}
},
enforce: "pre", // Vite
};
}

function createVirtualModule(id) {
function createVirtualModule(id, urlBase) {
const { name, hasDefault, exports } = modules[id],
exportStr = exports.join(", "),
importStr = exports
.map((exp) => `const { ${exp} } = ${name};`)
.join("\n");
if (hasDefault) {
return `import { modules } from './wq.js';
return `import { modules } from '${urlBase}/wq.js';
const { '${id}': ${name} } = modules;
const ${name}Plugin = ${name}.default;
export default ${name}Plugin;
${importStr}
export { ${exportStr} };
`;
} else {
return `import { modules } from './wq.js';
return `import { modules } from '${urlBase}/wq.js';
const { '${id}': ${name} } = modules;
export default ${name};
${importStr}
Expand Down
1 change: 0 additions & 1 deletion packages/rollup-plugin/src/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const modules = {
"exact",
"checkPropTypes",
"resetWarningCache",
"PropTypes",
],
hasDefault: false,
},
Expand Down
4 changes: 3 additions & 1 deletion packages/rollup-plugin/update_modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ async function update_modules() {
name:
depNames[name] ||
camelCase(name.replace(/\//g, "-").replace("@", "")),
exports: Object.keys(exports).filter((e) => e !== "default"),
exports: Object.keys(exports).filter(
(e) => e !== "default" && e !== depNames[name]
),
hasDefault,
};
});
Expand Down

0 comments on commit e77e66d

Please sign in to comment.