I'm trying to use BeePlugin package in a Rails project and it raised the question of how to import properly a library using Webpacker?
I added jQuery but somehow I can't add bee-plugin.
I wanna be able to import just what I need and only in the file I need it
So far what I did was
- Install the library with yarn
yarn add @mailupinc/bee-plugin
- Created a new file to add my code and import it in application.js
import ./bee
- In the new file import my library. I have tried
import "@mailupinc/bee-plugin"
import Bee from "@mailupinc/bee-plugin"
import * as Bee from "@mailupinc/bee-plugin"
const Bee = require "@mailupinc/bee-plugin"
None of them seem to work. Why?
I always get Uncaught ReferenceError: Bee is not defined
For reference
application.js
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
import "./bee";
webpack/environment.js
const { environment } = require('@rails/webpacker')
const webpack = require("webpack");
// Avoid using require and import and alias jquery
environment.plugins.append(
"Provide",
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
})
);
module.exports = environment
bee.js
import Bee from "@mailupinc/bee-plugin";
function initBee() {
$.ajax({ url: "/editor/token", success: startBee });
}
function beeConfig() {
return {...} // Config params
}
function startBee(auth) {
const beeInstance = Bee.create(auth, beeConfig(), (instance) => {
$.ajax({
url: $("#bee-plugin-container").data("template"),
success: (template) => instance.start(template),
});
return instance;
});
}
$(document).on("turbolinks:load", initBee);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…