Webpack:ENOENT:没有这样的文件或目录,打开' head.ejs'

时间:2017-04-13 18:59:44

标签: javascript webpack ejs webpack-2 html-webpack-plugin

我有以下webpack.config.js文件:

'use strict';

let path = require('path');
let webpack = require('webpack');
let HtmlWebpackPlugin = require('html-webpack-plugin');
let ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: [
    './index.js'
  ],
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  devtool: 'inline-source-map',
  watch: true,
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        use: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: [ 'style-loader', 'css-loader?modules', ],
          publicPath: '/dist'
        })
      },
      {
        test: /\.ejs$/,
        use: 'ejs-compiled-loader'
      }
    ],
  },
  plugins: [
    new ExtractTextPlugin({
      filename: 'styles.css'
    }),

    new webpack.HotModuleReplacementPlugin(),

    new webpack.NamedModulesPlugin(),

    new HtmlWebpackPlugin({
      minify: {
        collapseWhitespace: true
      },
      hash: true,
      template: 'ejs-render-loader!./views/index.ejs'
    }),


  ]
};

尝试加载包含<% somefile %>的ejs文件时,无法找到该文件..这是我收到的错误:

Child html-webpack-plugin for "index.html":
         Asset     Size  Chunks  Chunk Names
    index.html  26.2 kB       0
    chunk    {0} index.html 554 bytes [entry]
     [./node_modules/ejs-render-loader/index.js!./views/index.ejs] ./~/ejs-render-loader!./views/index.ejs 554 bytes {0} [built] [failed] [1 error]

    ERROR in ./~/ejs-render-loader!./views/index.ejs
    Module build failed: Error: ENOENT: no such file or directory, open 'head.ejs'
        at Object.fs.openSync (fs.js:584:18)
        at fs.readFileSync (fs.js:491:33)
        at Object.exports.parse (/var/www/rio-olympics-3/monitor/node_modules/ejs-compiled-loader/node_modules/ejs/lib/ejs.js:168:19)
        at Object.exports.compile (/var/www/rio-olympics-3/monitor/node_modules/ejs-compiled-loader/node_modules/ejs/lib/ejs.js:245:15)
        at Object.module.exports (/var/www/rio-olympics-3/monitor/node_modules/ejs-compiled-loader/index.js:7:22)
webpack: Failed to compile.

我尝试了很多文件路径格式,但没有一个工作,这是我的ejs文件,我的head.ejs在同一个文件夹中:

<!DOCTYPE html>
    <html lang="en">

    <% include head %>

    <body>
        <div id="navbar-app"></div>
        <p> Welcome, more coming soon! </p>
    </body>


   <!-- insert component scripts below here -->
   <script src="dist/js/NavBarMain.js"></script>

    </html>

1 个答案:

答案 0 :(得分:0)

您为index.ejs配置了两个不同的加载器,这会导致冲突。

所有.ejs的一般规则:

{
  test: /\.ejs$/,
  use: 'ejs-compiled-loader'
}

html-webpack-plugin中模板的内联规则:

template: 'ejs-render-loader!./views/index.ejs'

如果您只想使用内联加载程序,则可以添加前导!,因此webpack不会应用其他加载程序:

template: '!ejs-render-loader!./views/index.ejs'

以上解决了您的问题,但为什么您有两个不同的ejs加载器有点可疑。说实话,我并不完全了解加载器的差异,因为两者都指向相同的GitHub repository,尽管在我看来ejs-render-loader只是一个早期版本。他们实际上处理不同的包含,这就是你得到错误的原因。

来自Usage of ejs-compiled-loader中的示例:

// Child Templates 
// path is relative to where webpack is being run 
<%- include templates/child -%>

这意味着如果您想对所有ejs-compiled-loader使用.ejs,则需要将包含更改为:

<% include views/head %>

您可以在html-webpack-plugin

中删除内联加载程序
template: './views/index.ejs'

由您决定是否要更改此设置,我会尽量避免加载程序冲突,我不认为ejs-render-loader将会更新。

实际上有一个v2.2.0 ejs-compiled-loader回到另一个包含行为,相对于当前文件,这明确地更直观。您可以使用以下命令安装它:

npm install --save-dev ejs-compiled-loader@2.2.0