如何使用webpack处理仅在运行时可用的服务器端依赖关系

How to handle serverside dependency available only during runtime with webpack

本文关键字:服务器端 依赖 关系 运行时 webpack 何使用 处理      更新时间:2024-05-31

我们有一个文件需要在运行时在服务器端渲染一个jade模板。

define(['underscore', 'html!/templates/myTemplate'], function (_, template){
    ...
})

其想法是在编译时捆绑其他依赖项(此处为下划线),但仅在运行时需要模板。

我们试着使用这样的外部语言,但没有改变任何东西。

externals: {
    '/templates/myTemplate': '/templates/myTemplate'
}

但当我们编译捆绑包时,会显示相同的错误:

Module not found: Error: Cannot resolve 'file' or 'directory' /templates/myTemplate ...

知道吗?

这是我们的设置文件

module.exports = {
  context: __dirname,
  entry: {
    "pageA": "./pageA.js",
    "pageB": "./pageB.js"
  },
  output: {
    "path": __dirname + '/dist',
    "publicPath": './javascripts/dist/',
    "filename": '[name].js'
  },
  plugins: [
      new webpack.ProvidePlugin({
          $: "jquery",
          jQuery: "jquery"
      })
  ]
};

仅使用Webpack是不可能做到这一点的:https://github.com/webpack/webpack/issues/2326