【问题标题】:serverless webpack config, manually add directoryserverless webpack 配置,手动添加目录
【发布时间】:2018-02-02 00:11:16
【问题描述】:

我正在运行的以下代码无法运行,因为当我上传到亚马逊时找不到模型文件夹。

exports.setModels = function(connection,modelPath){
  //Import all the known models for the project.

  //Proof of Stage being set.
  console.log("stage for models="+stage);

  const fs = require('fs');
  const dir = modelPath;

  var models = {};

  //@JA - Wait until this function finishes ~ hence readdirSync vs regular readdir which is async
  fs.readdirSync(dir).forEach(file => {
    console.log("file="+file);
    //Split the .js part of the filename
    var arr = file.split(".");
    var name = arr[0].toLowerCase();
    //Create a modle object using the filename as the reference without the .js pointing to a created sequelize instance of the file.

    var modelPath = "../models/"+file; //default assumes same directory that was used to scan.
    if(process.env.DOMAIN_NAME){ //If this enviroment variable is detected then change the modelPath.
      modelPath = "../../../../models/"+file;
    }
    models[name] = connection.import(modelPath);
  })
  return models;
}

经过调查,发现是因为models文件夹没有被serverless webpack插件打包。

我最近发现了如何在我的无服务器文件中使用此代码强制某些包上传。

webpackIncludeModules:
    forceInclude:
      - mysql
      - mysql2

但这似乎只包含包,当我尝试引用模型文件夹以自动包含我所有的续集模型时,我收到一个错误,说它不是包,这当然是有道理的。

这确实给我留下了一个问题:如何在不手动为每个模型执行要求的情况下将其打包模型目录。我写了一个动态函数来在运行时获取它们并将其导入到sequelize。

关于插件的信息在这里(https://github.com/serverless-heaven/serverless-webpack),我浏览了所有内容,但似乎找不到答案。

使用无服务器打包的输出如下所示,缺少包含我所有 sequelize 模型的模型文件夹。

我在做webpack之前的主目录是这样的

这也是我的 webpack.config.js。

const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");

module.exports = {
  entry: slsw.lib.entries,
  target: "node",
  // Since 'aws-sdk' is not compatible with webpack,
  // we exclude all node dependencies
  externals: [nodeExternals()],
  // Run babel on all .js files and skip those in node_modules
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader",
        include: __dirname,
        exclude: /node_modules/
      }
    ]
  }
};

【问题讨论】:

  • 现在继续前进,我已经手动创建了对模型的引用,并在从文件加载后通过导入的辅助选项和 sequelize 加载。这是我目前唯一的解决方法。

标签: node.js webpack sequelize.js serverless-framework serverless-webpack-plugin


【解决方案1】:

您可以使用copy-webpack-plugin 来包含模型而不是引用。

const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    entry: slsw.lib.entries,
    target: "node",
    // Since 'aws-sdk' is not compatible with webpack,
    // we exclude all node dependencies
    externals: [nodeExternals()],
    // Run babel on all .js files and skip those in node_modules
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: "babel-loader",
                include: __dirname,
                exclude: /node_modules/
            }
        ]
    },
    plugin: [
        new CopyWebpackPlugin(filesToCopy, {})
    ]
};

确保从正确的文件夹中导入模型,因为您是从相对路径导入的。如果您的捆绑包位于不同的文件夹中,这可能会改变。

【讨论】:

    猜你喜欢
    • 2018-05-30
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多