javascript - WebPacker - 通过 ES6 Import 与 webpack.ProvidePlugin() 加载 NPM 模块

标签 javascript webpack webpacker ruby-on-rails-5.2

我正在尝试在 Rails 内运行多个 WebApp。

每个 WebApp 都有完全不同的依赖项,我希望将每个 WebApp 的包大小保持在最小大小。

我正在构建的第一个应用程序使用 Reveal.js我已经使用 yarn add Reveal

添加了它

我原以为我可以通过 packs/presentation.js 导入来包含它

console.log('Presentation System');
import 'reveal';

Reveal.initialize({
  controls: true,
  progress: true,
  history: true,
  center: true,
  transition: 'none',
});

enter image description here

然后我尝试了一个细微的变化 import { Reveal } from 'reveal';

  console.log('Presentation System');
  import { Reveal } from 'reveal';

  Reveal.initialize({
    controls: true,
    progress: true,
    history: true,
    center: true,
    transition: 'none',
  });

enter image description here

通过更改侧轨中的 WebPacker 配置,我终于让 Reveal.js 正常工作

config/webpack/environment.js

我使用以下代码将 jQuery 和 Reveal.js 添加为 webpack 插件

我不确定这是否是包含 Reveal 的最佳方式,因为它似乎会通过向每个应用程序添加 jQuery 和 Reveal 来影响我的所有应用程序,这不是我想要的,因为其他应用程序正在使用 VUE 或 Angular

// https://joey.io/how-to-use-jquery-in-rails-5-2-using-webpack/
const { environment } = require('@rails/webpacker');
const typescript =  require('./loaders/typescript');
const vue =  require('./loaders/vue');

const webpack = require('webpack')

environment.loaders.append('vue', vue);
environment.loaders.append('typescript', typescript);

module.exports = environment;

environment.plugins.prepend(
    'Provide',
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      jquery: 'jquery',
      Reveal: 'reveal'
    })
);

以及我现在运行来显示演示文稿的代码

$(document).ready(function() {

  Reveal.initialize({
    controls: true,
    progress: true,
    history: true,
    center: true,
    transition: 'none',
  });

});

最佳答案

怎么样import Reveal from 'reveal'

https://babeljs.io/docs/en/learn/#modules

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

通过做import { Reveal } from 'reveal'您正在尝试导入 Reveal Reveal 的命名导出模块。

但是Revealdefault导出。

关于javascript - WebPacker - 通过 ES6 Import 与 webpack.ProvidePlugin() 加载 NPM 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53439087/

相关文章:

ruby-on-rails - 在 Rails 6 中使用 webpacker 导入 CSS

ruby-on-rails - 如何解决rails中的机架应用程序错误?

javascript - 排序二维 Javascript 数组

ruby-on-rails - Heroku 应用程序错误 - 部署时超出内存配额 - Scout 显示 4,804,423 个分配

ruby-on-rails - 使用 webpack 部署到 Heroku 时预编译 Assets 时出错

javascript - 导入 Bootstrap JavaScript、Webpack

reactjs - 如何使用React + ES6 + webpack导入导出组件?

javascript - 如何为用户创建 Dialogflow 登录?

javascript - 如何替换 vue.js 中的斜杠

javascript - markdown深度图片上传