【问题标题】:Fails to compile, when I try to use include with EJS files. (Error: Child compilation failed.)当我尝试将包含与 EJS 文件一起使用时,编译失败。 (错误:子编译失败。)
【发布时间】:2020-02-16 06:09:03
【问题描述】:

我已经将此作为 github 问题,但我怀疑可能存在一些我不知道的微不足道的问题。

非常感谢一些帮助我进行调试的意见。

演示:

https://github.com/andrasna/html-ejs-webpack-config-issue-demo

index.ejs

<% const title = 'Hello' %>

<%- include('partial') -%>

<h1><%= `${title} World` %></h1> 

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.ejs',
    })
  ]
}

预期行为

我正在尝试根据文档将一个 EJS 文件包含到另一个文件中:

https://ejs.co/#docs

<%- include('header'); -%>
<h1>
  Title
</h1>
<p>
  My page
</p>
<%- include('footer'); -%>

我期望的是,partial.ejs 中的 HTML 将包含在 index.ejs 文件中。

当前行为

我收到“错误:子编译失败”(请参阅​​下面的完整错误消息)。

但是,如果我删除 index.ejs 中带有 include 的行,EJS 就会被编译。这就是为什么我认为问题是特定于包含的。

有什么想法吗?

错误信息

> npm start

> html-ejs-webpack-config-issue-demo@1.0.0 start /Users/user/Dev/html-ejs-webpack-config--issue-demo
> webpack-dev-server --mode development

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from /Users/user/Dev/html-ejs-webpack-config--issue-demo
✖ 「wdm」: Hash: f9f8666f87ab7acd22cd
Version: webpack 4.41.2
Time: 595ms
Built at: 19/10/2019 15:34:18
     Asset      Size  Chunks             Chunk Names
index.html  1.72 KiB          [emitted]  
   main.js   360 KiB    main  [emitted]  main
Entrypoint main = main.js
[0] multi (webpack)-dev-server/client?http://localhost:8080 ./src 40 bytes {main} [built]
[./node_modules/ansi-html/index.js] 4.16 KiB {main} [built]
[./node_modules/ansi-regex/index.js] 135 bytes {main} [built]
[./node_modules/html-entities/index.js] 231 bytes {main} [built]
[./node_modules/loglevel/lib/loglevel.js] 7.68 KiB {main} [built]
[./node_modules/strip-ansi/index.js] 161 bytes {main} [built]
[./node_modules/webpack-dev-server/client/index.js?http://localhost:8080] (webpack)-dev-server/client?http://localhost:8080 4.29 KiB {main} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.51 KiB {main} [built]
[./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.53 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/createSocketUrl.js] (webpack)-dev-server/client/utils/createSocketUrl.js 2.89 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-server/client/utils/log.js 964 bytes {main} [built]
[./node_modules/webpack-dev-server/client/utils/reloadApp.js] (webpack)-dev-server/client/utils/reloadApp.js 1.59 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/sendMessage.js] (webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main} [built]
[./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
[./src/index.js] 0 bytes {main} [built]
    + 18 hidden modules

ERROR in   Error: Child compilation failed:
  Module build failed (from ./node_modules/html-webpack-plugin/lib/loader.js):
  SyntaxError: Unexpected token )

  - Function

  - lodash.js:14870 
    [html-ejs-webpack-config--issue-demo]/[lodash]/lodash.js:14870:16

  - lodash.js:473 apply
    [html-ejs-webpack-config--issue-demo]/[lodash]/lodash.js:473:27

  - lodash.js:15254 
    [html-ejs-webpack-config--issue-demo]/[lodash]/lodash.js:15254:16

  - lodash.js:475 apply
    [html-ejs-webpack-config--issue-demo]/[lodash]/lodash.js:475:27

  - lodash.js:6563 
    [html-ejs-webpack-config--issue-demo]/[lodash]/lodash.js:6563:16

  - lodash.js:14869 Function.template
    [html-ejs-webpack-config--issue-demo]/[lodash]/lodash.js:14869:20


  - SyntaxError: Unexpected token )

  - compiler.js:79 childCompiler.runAsChild
    [html-ejs-webpack-config--issue-demo]/[html-webpack-plugin]/lib/compiler.js:79:16

  - Compiler.js:343 compile
    [html-ejs-webpack-config--issue-demo]/[webpack]/lib/Compiler.js:343:11

  - Compiler.js:681 hooks.afterCompile.callAsync.err
    [html-ejs-webpack-config--issue-demo]/[webpack]/lib/Compiler.js:681:15


  - Hook.js:154 AsyncSeriesHook.lazyCompileHook
    [html-ejs-webpack-config--issue-demo]/[tapable]/lib/Hook.js:154:20

  - Compiler.js:678 compilation.seal.err
    [html-ejs-webpack-config--issue-demo]/[webpack]/lib/Compiler.js:678:31


  - Hook.js:154 AsyncSeriesHook.lazyCompileHook
    [html-ejs-webpack-config--issue-demo]/[tapable]/lib/Hook.js:154:20

  - Compilation.js:1423 hooks.optimizeAssets.callAsync.err
    [html-ejs-webpack-config--issue-demo]/[webpack]/lib/Compilation.js:1423:35



Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = index.html
    [./node_modules/html-webpack-plugin/lib/loader.js!./index.ejs] 922 bytes {0} [built] [failed] [1 error]

    ERROR in ./index.ejs (./node_modules/html-webpack-plugin/lib/loader.js!./index.ejs)
    Module build failed (from ./node_modules/html-webpack-plugin/lib/loader.js):
    SyntaxError: Unexpected token )
        at Function (<anonymous>)
        at /Users/user/Dev/html-ejs-webpack-config--issue-demo/node_modules/lodash/lodash.js:14870:16
        at apply (/Users/user/Dev/html-ejs-webpack-config--issue-demo/node_modules/lodash/lodash.js:473:27)
        at /Users/user/Dev/html-ejs-webpack-config--issue-demo/node_modules/lodash/lodash.js:15254:16
        at apply (/Users/user/Dev/html-ejs-webpack-config--issue-demo/node_modules/lodash/lodash.js:475:27)
        at /Users/user/Dev/html-ejs-webpack-config--issue-demo/node_modules/lodash/lodash.js:6563:16
        at Function.template (/Users/user/Dev/html-ejs-webpack-config--issue-demo/node_modules/lodash/lodash.js:14869:20)
        at Object.module.exports (/Users/user/Dev/html-ejs-webpack-config--issue-demo/node_modules/html-webpack-plugin/lib/loader.js:28:22)
ℹ 「wdm」: Failed to compile.

【问题讨论】:

  • 你能更新 repo 以便在提交中看到对你有用的解决方案吗?下面的答案对我不起作用

标签: javascript webpack ejs webpack-dev-server html-webpack-plugin


【解决方案1】:

ejs-compiled-loader 与 HtmlWebpackPlugin 一起使用

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

将 webpack.config.js 更改为:

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: '!!ejs-compiled-loader!index.ejs', //Here is the Loader plugged in
    })
  ]
}

(as described here)

然后将您的模板更改为有效的 EJS。 HtmlWebpackPlugin 说:"By default (if you don't specify any loader in any way) a fallback lodash loader kicks in." 它使用 lodash 作为简约加载器呈现模板。这就是您的错误来自 lodash.js 的原因。

index.ejs:

<% title = 'Hello' %>

<%- include partial -%>

<h1><%= title%> World</h1> 

然后npm start 和您新编译的“Lorem Ipsum ...Hello World”将在您的本地主机上提供。

npx webpack --mode=development 让您的“dist/index.html”享受源代码:)

【讨论】:

  • "然后将您的模板更改为有效的 EJS。"它已经是有效的 EJS,并且在没有 include 的情况下编译,因为 html-webpack-plugin 使用 ejs 后备加载器。尽管如此,看起来“ejs-compiled-loader”仍然有效。但是这个存储库似乎不再维护了,而且与默认的 ejs 后备加载器不同,它无法编译有效的 JS(如模板文字)。
  • 你是对的。使用 ejs.compile 对您的模板进行独立编译可以正常工作。该软件包使用 ejs-compiled-loader 的 v1.x 将我困住,因此使用 EJS v1。 (要使用 v2.x,您需要安装 ejs-compiled-loader-webpack4,它引用同一个 repo (github.com/bazilio91/ejs-compiled-loader) 但安装 v2.x 分支。这里编译失败并出现错误“include is not a function”已在此 repo github.com/testerum/ejs-compiled-loader-webpack4-nodeps 中提到,但未修复。我仍在查看错误发生的来源。祝我好运
  • npm 错误! enoent 执行时出错:npm ERR! enoent 未定义 ls-remote -h -t github.com/nemanjan00/ejs.git
  • ejs-compiled-loader 不再维护。 AFAIS 它也缺少模板参数管理。
【解决方案2】:

你的用例的问题是你的 webpack 文件你没有使用 ejs 编译器。

我做了一些更改并重构了你的一些不良做法。

您可以在下面找到最终代码:

https://gitlab.com/akhileshcoder/ejs-webpack-starter

现在您的 webpack.config 如下所示(作为客户端的任务/项目处理程序)

module.exports = {
  devServer: {
    inline:true,
    port: 8080
  },
  entry: './src/index.js',
  output: {
    path: __dirname + '/public/dist',
    filename: 'bundle.js',
    publicPath: '/public/dist',
    library: 'bundle',
    libraryTarget: 'var'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['es2015']
            }
          }
        ]
      }
    ]
  }
};

ejb 在 express 上渲染如下:

app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'ejs');
app.get('/', function(req, res, next) {
  res.render('index', {title: 'Hello World'});
});

【讨论】:

  • "你没有使用 ejs 编译器" 正如我所指出的,它确实在没有include 的情况下编译。这是因为 html-webpack-plugin 使用 ejs 后备加载器:github.com/jantimon/html-webpack-plugin/blob/master/docs/…“我做了一些更改并重构了你的一些不良做法。”如果您能指出不好的做法,这将有所帮助。我共享的存储库仅包含用于重现问题的最低限度。
  • 感谢这个例子,虽然它用 express 代替了 webpack-dev-server,所以这个例子缺少了我以前拥有的 webpack-dev-server 功能,比如热重载和在内存中运行。
猜你喜欢
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多