【问题标题】:How to prevent loading Sharp module on Meteor client?如何防止在 Meteor 客户端上加载 Sharp 模块?
【发布时间】:2019-02-21 14:52:11
【问题描述】:

我在服务器的图片集上使用了 npm pkg Sharp 来转换 imgs。服务端代码是这样的:

import * as sharp from 'sharp';

export const Pictures = new Mongo.Collection('pictures');

export const PicturesStore = new UploadFS.store.GridFS({
    collection: Pictures,
    name: 'pictures',
    filter: new UploadFS.Filter({
        contentTypes: [ 'image/*' ],
    }),
    transformWrite(from, to, fileId, file) {
        const transform = sharp().resize(300, 300).min().crop().toFormat('jpeg', { quality });
        from.pipe(transform).pipe(to);
    },
})

但是在客户端却报错:

cannot load native .node modules on the client.

客户端实际上并没有运行尖锐的功能。它仅引用 PicturesStore 并为图片创建一个 minimongo 集合。

在另一个项目中,它在客户端使用 webpack。它可以配置为使用空的虚拟对象解析锐利。

但是如何创建一个空的 Sharp 对象来防止在没有 webpack 的 Meteor 客户端上加载 Sharp 模块?

【问题讨论】:

    标签: meteor webpack sharp


    【解决方案1】:

    事实证明,您必须编写一个 Meteor 包来定义加载在客户端和服务器上的不同文件。在你的 package.js 中,是这样的:

    Package.onUse(function (api) {
        api.mainModule('sharp-client.js', 'client');
        api.mainModule('sharp-server.js', 'server');
    });
    

    在sharp-client.js中是这样的:

    export var Sharp = {};
    

    在sharp-server.js中是这样的:

    import {
        checkNpmVersions
    } from 'meteor/tmeasday:check-npm-versions';
    
    checkNpmVersions({
        'sharp': '^0.20.5'
    }, 'my:awesome-package');
    
    export var Sharp = require('sharp');
    

    完成。

    【讨论】:

      猜你喜欢
      • 2015-11-22
      • 2012-05-17
      • 2011-03-02
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2013-07-30
      • 2014-06-08
      • 1970-01-01
      相关资源
      最近更新 更多