2017-04-01 7 views
2

html 로더를 사용할 때, index.html의 <img> 태그가 url-loader를 트리거하지 않았습니다.webpack html 로더 : <img> 태그가 URL 로더를 트리거하지 않음

웹팩 설정

// 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> 

오류 로그 : 여기 내 웹팩 CONFIGS & 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

감사합니다. 위의 방법을 시도했지만 작동하지 않았습니다.

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

하하 모멘트 :

: 그런 다음 갑자기 난 그냥 내 표현 dev에 서버를 구성해야 실현
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을 찾으려고 시도하지만 웹팩 구성에서 판단하면 이미지 경로는 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") 
], 

그와.