问答详情
源自:3-2 自动化生成项目中的html页面(中)

写的htmlWebpackPlugin获取它的options和files打包一直出错

indexhtml:

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <title><%= htmlWebpackPlugin.options.title %></title>

    </head>

    <body>

        <%= htmlWebpackPlugin.options.date%>

        <% for( var key in htmlWebpackPlugin){%><%= key%><%= }%>

        <% for( var key in htmlWebpackPlugin.files){%><%= key%>:<%= JSON.stringify(htmlWebpackPlugin.files[key])%><%= }%>

    </body>



打包时候一直提示“ERROR in Template execution failed: TypeError: Cannot read property 'files' of undefined ”



打包后的index.html文件内容:

Html Webpack Plugin:

<pre>

  TypeError: Cannot read property 'files' of undefined

  

  - index.html:91 

    /Users/carol/www/webpackDemo/index.html:91:43

  

  - index.html:104 module.exports

    /Users/carol/www/webpackDemo/index.html:104:3

  

  - index.js:264 

    [webpackDemo]/[html-webpack-plugin]/index.js:264:16

  

  - util.js:16 tryCatcher

    [webpackDemo]/[bluebird]/js/release/util.js:16:23

  

  - promise.js:512 Promise._settlePromiseFromHandler

    [webpackDemo]/[bluebird]/js/release/promise.js:512:31

  

  - promise.js:569 Promise._settlePromise

    [webpackDemo]/[bluebird]/js/release/promise.js:569:18

  

  - promise.js:606 Promise._settlePromiseCtx

    [webpackDemo]/[bluebird]/js/release/promise.js:606:10

  

  - async.js:138 Async._drainQueue

    [webpackDemo]/[bluebird]/js/release/async.js:138:12

  

  - async.js:143 Async._drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:143:10

  

  - async.js:17 Immediate.Async.drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:17:14

  

</pre>

在terminal中出现的错误提示:

Hash: f31b61d7e4a63d32cf9d

Version: webpack 3.6.0

Time: 590ms

                             Asset     Size  Chunks             Chunk Names

./js/main--aea7fcede5dd435d3e28.js  2.51 kB       0  [emitted]  main

   ./js/a--36facad12b17b00910bb.js  2.52 kB       1  [emitted]  a

                        index.html  1.02 kB          [emitted]  

   [0] ./src/script/main.js 21 bytes {0} [built]

   [1] ./src/script/a.js 22 bytes {1} [built]


ERROR in Template execution failed: TypeError: Cannot read property 'files' of undefined


ERROR in   TypeError: Cannot read property 'files' of undefined

  

  - index.html:91 

    /Users/carol/www/webpackDemo/index.html:91:43

  

  - index.html:104 module.exports

    /Users/carol/www/webpackDemo/index.html:104:3

  

  - index.js:264 

    [webpackDemo]/[html-webpack-plugin]/index.js:264:16

  

  - util.js:16 tryCatcher

    [webpackDemo]/[bluebird]/js/release/util.js:16:23

  

  - promise.js:512 Promise._settlePromiseFromHandler

    [webpackDemo]/[bluebird]/js/release/promise.js:512:31

  

  - promise.js:569 Promise._settlePromise

    [webpackDemo]/[bluebird]/js/release/promise.js:569:18

  

  - promise.js:606 Promise._settlePromiseCtx

    [webpackDemo]/[bluebird]/js/release/promise.js:606:10

  

  - async.js:138 Async._drainQueue

    [webpackDemo]/[bluebird]/js/release/async.js:138:12

  

  - async.js:143 Async._drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:143:10

  

  - async.js:17 Immediate.Async.drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:17:14

  


Child html-webpack-plugin for "index.html":

     1 asset

       [0] ./node_modules/html-webpack-plugin/lib/loader.js!./index.html 1.49 kB {0} [built]

       [2] (webpack)/buildin/global.js 509 bytes {0} [built]

       [3] (webpack)/buildin/module.js 517 bytes {0} [built]

        + 1 hidden module


提问者:carolcoral 2017-09-26 19:27

个回答

  • 猪小小屁
    2021-03-22 01:00:44

    <% for( var key in htmlWebpackPlugin){%><%= key%><%= }%>中最后的<%= }%>等号不要

  • carolcoral
    2017-09-26 19:44:26

    打包后的index.html文件内容:

    Html Webpack Plugin:

    <pre>

      TypeError: Cannot read property 'files' of undefined

      

      - index.html:91 

        /Users/carol/www/webpackDemo/index.html:91:43

      

      - index.html:104 module.exports

        /Users/carol/www/webpackDemo/index.html:104:3

      

      - index.js:264 

        [webpackDemo]/[html-webpack-plugin]/index.js:264:16

      

      - util.js:16 tryCatcher

        [webpackDemo]/[bluebird]/js/release/util.js:16:23

      

      - promise.js:512 Promise._settlePromiseFromHandler

        [webpackDemo]/[bluebird]/js/release/promise.js:512:31

      

      - promise.js:569 Promise._settlePromise

        [webpackDemo]/[bluebird]/js/release/promise.js:569:18

      

      - promise.js:606 Promise._settlePromiseCtx

        [webpackDemo]/[bluebird]/js/release/promise.js:606:10

      

      - async.js:138 Async._drainQueue

        [webpackDemo]/[bluebird]/js/release/async.js:138:12

      

      - async.js:143 Async._drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:143:10

      

      - async.js:17 Immediate.Async.drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:17:14

      

    </pre>

    在terminal中出现的错误提示:

    Hash: f31b61d7e4a63d32cf9d

    Version: webpack 3.6.0

    Time: 590ms

                                 Asset     Size  Chunks             Chunk Names

    ./js/main--aea7fcede5dd435d3e28.js  2.51 kB       0  [emitted]  main

       ./js/a--36facad12b17b00910bb.js  2.52 kB       1  [emitted]  a

                            index.html  1.02 kB          [emitted]  

       [0] ./src/script/main.js 21 bytes {0} [built]

       [1] ./src/script/a.js 22 bytes {1} [built]


    ERROR in Template execution failed: TypeError: Cannot read property 'files' of undefined


    ERROR in   TypeError: Cannot read property 'files' of undefined

      

      - index.html:91 

        /Users/carol/www/webpackDemo/index.html:91:43

      

      - index.html:104 module.exports

        /Users/carol/www/webpackDemo/index.html:104:3

      

      - index.js:264 

        [webpackDemo]/[html-webpack-plugin]/index.js:264:16

      

      - util.js:16 tryCatcher

        [webpackDemo]/[bluebird]/js/release/util.js:16:23

      

      - promise.js:512 Promise._settlePromiseFromHandler

        [webpackDemo]/[bluebird]/js/release/promise.js:512:31

      

      - promise.js:569 Promise._settlePromise

        [webpackDemo]/[bluebird]/js/release/promise.js:569:18

      

      - promise.js:606 Promise._settlePromiseCtx

        [webpackDemo]/[bluebird]/js/release/promise.js:606:10

      

      - async.js:138 Async._drainQueue

        [webpackDemo]/[bluebird]/js/release/async.js:138:12

      

      - async.js:143 Async._drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:143:10

      

      - async.js:17 Immediate.Async.drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:17:14

      


    Child html-webpack-plugin for "index.html":

         1 asset

           [0] ./node_modules/html-webpack-plugin/lib/loader.js!./index.html 1.49 kB {0} [built]

           [2] (webpack)/buildin/global.js 509 bytes {0} [built]

           [3] (webpack)/buildin/module.js 517 bytes {0} [built]

            + 1 hidden module