Skip to content

Commit

Permalink
Fix rollup watch isses
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Dec 5, 2023
1 parent 96d6ab1 commit 2dc2560
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ function compilePluginFeatures(
{ connectors = [], events = [], routeMaps = [], markdownFeatures = true } = {}
) {
return {
name: "collect-plugin-features",
name: "compile-plugin-features",

buildStart() {
this.addWatchFile(pluginsDir);

emptyDirSync("./src");

const packageJson = readJsonSync("package.json");
Expand Down Expand Up @@ -158,8 +161,6 @@ function compilePluginFeatures(
}

if (isPlugin(depJson)) {
this.addWatchFile(depPath);

if (depJson.exports) {
for (const feature of features) {
feature.process(depJson.name, depJson.exports);
Expand Down
1 change: 0 additions & 1 deletion plugins/chat-lite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
],
"exports": {
"./addon-main.js": "./addon-main.cjs",
"./connectors/below-site-header": "./dist/connectors/below-site-header.js",
"./connectors/extra-header-icons": "./dist/connectors/extra-header-icons.js",
"./connectors/sidebar-footer-actions": "./dist/connectors/sidebar-footer-actions.js",
"./markdown-features/scramble": "./dist/markdown-features/scramble.js",
Expand Down
11 changes: 0 additions & 11 deletions plugins/chat-lite/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@ export default {
// You can augment this if you need to.
output: plugin.output(),

watch: "src/**/*",

plugins: [
// These are the modules that users should be able to import from your
// addon. Anything not listed here may get optimized away.
// By default all your JavaScript modules (**/*.js) will be importable.
// But you are encouraged to tweak this to only cover the modules that make
// up your addon's public API. Also make sure your package.json#exports
// is aligned to the config here.
// See https://github.com/embroider-build/embroider/blob/main/docs/v2-faq.md#how-can-i-define-the-public-exports-of-my-addon
plugin.publicEntrypoints(["**/*.js", "index.js"]),

// Ensure that any plugin features are exported from package.json.
plugin.exportPluginFeatures(),

Expand Down
3 changes: 2 additions & 1 deletion plugins/discourse-plugin-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"dependencies": {
"@embroider/addon-dev": "^4.1.2",
"fs-extra": "^11.1.1"
"fs-extra": "^11.1.1",
"walk-sync": "^3.0.0"
},
"engines": {
"node": "16.* || >= 18",
Expand Down
63 changes: 51 additions & 12 deletions plugins/discourse-plugin-dev/src/rollup/export-plugin-features.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import fsx from "fs-extra";
import path from "path";
import walkSync from "walk-sync";

const { readJsonSync, writeJsonSync } = fsx;

const PluginFeatures = [
"./connectors/",
"./events/",
"./markdown-features/",
"./route-maps/",
"connectors/",
"events/",
"markdown-features/",
"route-maps/",
];

function isPluginFeature(module) {
Expand All @@ -19,28 +22,64 @@ function isPluginFeature(module) {
return false;
}

export default function exportPluginFeatures() {
function normalizeFileExt(fileName) {
return fileName.replace(/(?<!\.d)\.ts|\.gjs|\.gts$/, ".js");
}

// Loosely based on publicEntrypoints from @embroider/addon-dev
export default function exportPluginFeatures({ srcDir, destDir }) {
return {
name: "export-plugin-features",

async buildStart() {
this.addWatchFile(srcDir);

const matches = walkSync(srcDir, {
directories: false,
globs: ["**/*.js", "**/*.ts", "**/*.gjs", "**/*.gts"],
});

for (const name of matches) {
// the matched file, but with the extension swapped with .js
const normalizedName = normalizeFileExt(name);

if (isPluginFeature(normalizedName)) {
this.emitFile({
type: "chunk",
id: path.join(srcDir, name),
fileName: normalizedName,
});
}
}
},

generateBundle(_, bundle) {
const packageJson = readJsonSync("package.json");
const originalExports = packageJson.exports;
const exports = { ...originalExports };
const exports =
typeof originalExports === "string"
? { ".": originalExports }
: { ...originalExports };

for (const moduleName of Object.keys(exports)) {
if (isPluginFeature(moduleName)) {
if (!moduleName.startsWith("./")) {
// Other than ".", this would be illegal. Should we warn about it?
continue;
}

const normalizedName = moduleName.slice(2) + ".js";

if (isPluginFeature(normalizedName)) {
// we'll add this back later, as needed
delete exports[moduleName];
}
}

for (const moduleName of Object.keys(bundle)) {
const qualifiedName = `./${moduleName}`;

if (isPluginFeature(qualifiedName)) {
if (isPluginFeature(moduleName)) {
// without .js
const entryName = qualifiedName.slice(0, -3);
exports[entryName] = `./dist/${moduleName}`;
const entryName = "./" + moduleName.slice(0, -3);
exports[entryName] = "./" + path.join(destDir, moduleName);
}
}

Expand Down
13 changes: 8 additions & 5 deletions plugins/discourse-plugin-dev/src/rollup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import exportPluginFeatures from "./export-plugin-features.js";

export default class Plugin {
#addon;
#srcDir;
#destDir;

constructor({ srcDir = "src", destDir = "dist" } = {}) {
this.#addon = new Addon({ srcDir, destDir });
}

publicEntrypoints(patterns) {
return this.#addon.publicEntrypoints(patterns);
this.#srcDir = srcDir;
this.#destDir = destDir;
}

exportPluginFeatures() {
return exportPluginFeatures();
return exportPluginFeatures({
srcDir: this.#srcDir,
destDir: this.#destDir,
});
}

dependencies() {
Expand Down

0 comments on commit 2dc2560

Please sign in to comment.