2017-04-01 7 views
2

html-loaderを使用している場合、index.htmlの<img>タグはurl-loaderを起動しませんでした。webpack html-loader:<img> url-loaderを起動していないタグ

WebPACKのコンフィグ

// webpack config 
module.exports = { 
    entry: { 
    "index": path.join(__dirname, "./src/js/app.js"), 
    "guide_index": path.join(__dirname, './src/js/guide_app.js') 
    }, 
    output: { 
    path: path.join(__dirname, '/dist'), 
    publicPath: '/', 
    filename: '[name].[hash].js' 
    }, 
    // loaders 
    module: { 
    rules: [ 
     { 
     test: /\.(gif|jpe?g|png|svg|mp3|ttf)$/i, 
     loader: "url-loader", 
     include: [ 
      "/Users/chrishu/Projects/Wind/guideios/Travel/Travel/Resource/assets/image", 
      path.resolve(__dirname, "/src/image"), 
      path.resolve(__dirname, "/../Travel/Resource/assets/image"), 
      path.resolve(__dirname, "/../Travel/Resource/assets") 
     ], 
     query: { 
      limit: 5000, 
      name: '[name].[hash:16].[ext]' 
      //name: "./assets/[name].[hash:16].[ext]" 
     } 
     }, 
     { 
     test: /\.css$/, 
     use: ["style-loader", "css-loader"] 
     }, 
     { 
     test: /\.(js|jsx)$/, 
     loader: "babel-loader", 
     // exclude: /node_modules/, 
     query: { cacheDirectory: true } 
     }, 
     { 
     test: /\.html$/, 
     use: [ { 
      loader: 'html-loader', 
      options: { 
      minimize: false, 
      removeComments: false, 
      removeCommentsFromCDATA: false, 
      removeCDATASectionsFromCDATA: false, 
      collapseWhitespace: false, 
      conservativeCollapse: false, 
      // removeAttributeQuotes: false, 
      // useShortDoctype: false, 
      // keepClosingSlash: false, 
      minifyJS: false, 
      minifyCSS: false, 
      // removeScriptTypeAttributes: false, 
      // removeStyleTypeAttributes: false 
      } 
     }] 
     } 
    ] 
    }, 
    // Plugins 
    plugins: [ 
    new HtmlWebpackPlugin({ 
     filename: 'guide_index.html', 
     template: path.join(__dirname, './src/html/guide_index.html'), 
     inject: true, 
     chunks: ["guide_index"], 
     minify: false, 
     chunksSortMode: 'dependency' 
    }), 
    new HtmlWebpackPlugin({ 
     filename: 'index.html', 
     template: path.join(__dirname, './src/html/index.html'), 
     inject: true, 
     chunks: ["index"], 
     minify: false, 
     chunksSortMode: 'dependency' 
    }), 
    new webpack.LoaderOptionsPlugin({ 
     debug: true 
    }), 
    new webpack.DefinePlugin({ 
     'process.env.NODE_ENV': "'development'" 
    }) 
    ], 
    resolve: { 
    extensions: [ '.web.js', '.js', '.jsx' ] 
    } 
} 

のindex.html:

<div class="button" ng-click="$backHistory()" ng-if="navbarType == 'backHistory'"> 
    <img src="back.png"> 
</div> 

エラーログ:ここに私のWebPACKのコンフィグ& HTMLです

Module not found: Error: Can't resolve './back.png' in '/Users/chrishu/Projects/Wind/guideios/Travel/Travel/Resource/assets/html': 
Error: Can't resolve './back.png' in '/Users/chrishu/Projects/Wind/guideios/Travel/Travel/Resource/assets/html' 

    - compiler.js:76 
    [Travel]/[[email protected]]/lib/compiler.js:76:16 

    - Compiler.js:291 Compiler.<anonymous> 
    [Travel]/[[email protected]]/lib/Compiler.js:291:10 

    - Compiler.js:494 
    [Travel]/[[email protected]]/lib/Compiler.js:494:13 

    - Tapable.js:138 next 
    [Travel]/[[email protected]]/lib/Tapable.js:138:11 

    - CachePlugin.js:62 Compiler.<anonymous> 
    [Travel]/[[email protected]]/lib/CachePlugin.js:62:5 

    - Tapable.js:142 Compiler.applyPluginsAsyncSeries 
    [Travel]/[[email protected]]/lib/Tapable.js:142:13 

    - Compiler.js:491 
    [Travel]/[[email protected]]/lib/Compiler.js:491:10 

    - Tapable.js:131 Compilation.applyPluginsAsyncSeries 
    [Travel]/[[email protected]]/lib/Tapable.js:131:46 

    - Compilation.js:645 self.applyPluginsAsync.err 
    [Travel]/[[email protected]]/lib/Compilation.js:645:19 

    - Tapable.js:131 Compilation.applyPluginsAsyncSeries 
    [Travel]/[[email protected]]/lib/Tapable.js:131:46 

    - Compilation.js:636 self.applyPluginsAsync.err 
    [Travel]/[[email protected]]/lib/Compilation.js:636:11 

    - Tapable.js:131 Compilation.applyPluginsAsyncSeries 
    [Travel]/[[email protected]]/lib/Tapable.js:131:46 

    - Compilation.js:631 self.applyPluginsAsync.err 
    [Travel]/[[email protected]]/lib/Compilation.js:631:10 

    - Tapable.js:131 Compilation.applyPluginsAsyncSeries 
    [Travel]/[[email protected]]/lib/Tapable.js:131:46 

    - Compilation.js:627 sealPart2 
    [Travel]/[[email protected]]/lib/Compilation.js:627:9 

    - Tapable.js:131 Compilation.applyPluginsAsyncSeries 
    [Travel]/[[email protected]]/lib/Tapable.js:131:46 

...... 

Child html-webpack-plugin for "index.html": 
     Asset  Size Chunks Chunk Names 
    index.html 7.51 kB  0 

ERROR in ./~/[email protected]/lib/loader.js!./Travel/Resource/assets/html/index.html 
Module not found: Error: Can't resolve './back.png' in '/Users/chrishu/Projects/Wind/guideios/Travel/Travel/Resource/assets/html' 
    @ ./~/[email protected]/lib/loader.js!./Travel/Resource/assets/html/index.html 1:2182-2203 1:2367-2388 

答えて

0

ありがとうございましたが、私は上記の方法を試しましたが、うまくいきませんでした。それから私は突然、私はちょうど私の急行のdevのサーバーを設定する必要が実現:

app.use(express.static(...)) 

母瞬間:)

0

あなたは拡張子リストに追加する必要があります。

// extensions: [ '.web.js', '.js', '.jsx' ] 
extensions: [ '.web.js', '.js', '.jsx', '.png' ] 
2

ファイルが存在しないので、それはurl-loaderに取得していません。あなたのHTMLファイルはTravel/Resource/assets/html/で、HTMLファイルにはback.pngというソースを持つ<img>タグがありますので、同じディレクトリ内にあるので、Travel/Resource/assets/html/back.pngを見つけようとしますが、Webpackの設定から判断すると、画像へのパスはTravel/Resource/assets/image/back.png 。それはあなたが../image/back.pngとしてインポートしなければならないことを意味:

<div class="button" ng-click="$backHistory()" ng-if="navbarType == 'backHistory'"> 
    <img src="../image/back.png"> 
</div> 

これは、ファイルを見つける必要があり、正しくurl-loaderを適用されますが、url-loaderであなたのルールは全く正しくありません。 path.resolve(__dirname, "/src/image")が含まれていて、path.resolveに絶対パスがあると、残りの部分は無視されます。したがって、あなたが含むパスは/src/imageではなく、/path/to/project/src/imageです。あなたは一流/を削除する必要があり、あなたがにあなたのincludeを変更することができます。それは今二path.resolveによってカバーされていますので、あなたは、あなたが手動で追加した絶対パスを必要としないことで

include: [ 
    path.resolve(__dirname, "src/image"), 
    path.resolve(__dirname, "../Travel/Resource/assets/image"), 
    path.resolve(__dirname, "../Travel/Resource/assets") 
], 

関連する問題