2017-11-28 3 views
1

webpack dev server에서 실행중인 내 각주의 응용 프로그램에 <object> 태그와 data 속성을 사용하여 pdf를로드하려고합니다.webpack을 사용하여 각진 앱에 애셋 (pdf)을 동적으로로드하는 방법은 무엇입니까?

pdf 경로는 런타임에 생성되므로 absoluteC:\test.pdf입니다.

그것은 UI에로드되는 것이 아니라 console가 오류를 기록 -

Not allowed to load local resource: file:///C:/test.jpeg

호스팅 서버하지만 정적 HTML에서 실행되지 않는 응용 프로그램의 생산 빌드가 잘 작동하지만

. 또한 상대 경로도 잘 작동합니다.

어떻게하면 PDF를로드 할 수 있습니까?

webpack.common.js

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

 
const path = require('path'); 
 
const rootDir = path.resolve(__dirname, '..'); 
 

 
module.exports = { 
 
resolve: { 
 
    extensions: ['.js', '.ts'], 
 
    modules: [rootDir, path.join(rootDir, "node_modules")] 
 
}, 
 

 
module: { 
 
    rules: [ 
 
     { 
 
      test: /\.html$/, 
 
      loader: 'html-loader' 
 
     }, 
 
     { 
 
      test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/, 
 
      loader: 'file-loader?name=assets/[name].[ext]' 
 
     }, 
 
     { 
 
      test: /\.(scss|css)$/, 
 
      use: [ 
 
       { loader: "style-loader" }, 
 
       { loader: "css-loader" }, 
 
       { loader: "sass-loader", 
 
        options: { 
 
         includePaths: ["node_modules/"] 
 
        } 
 
       } 
 
      ] 
 
     } 
 
    ]}, 
 
    plugins: [ 
 
     new webpack.optimize.CommonsChunkPlugin({ 
 
      name: ['app', 'vendor', 'polyfills'] 
 
     }), 
 
     new HtmlWebpackPlugin({ 
 
      template: 'src/index.html' 
 
     }), 
 
     new webpack.ContextReplacementPlugin(
 
      /angular(\\|\/)core(\\|\/)@angular/, 
 
      path.resolve(__dirname, '../src') 
 
     ) 
 
    ] 
 
};

webpack.dev.js

var webpackMerge = require('webpack-merge'); 
 
var ExtractTextPlugin = require('extract-text-webpack-plugin'); 
 
var commonConfig = require('./webpack.common.js'); 
 

 
const path = require('path'); 
 
const rootDir = path.resolve(__dirname, '..'); 
 

 
commonConfig.entry = { 
 
    'polyfills': './src/polyfills.ts', 
 
    'vendor': './src/vendor.ts', 
 
    'app': './src/main.ts' 
 
}; 
 

 
commonConfig.module.rules.unshift({ 
 
    test: /\.ts$/, 
 
    loaders: ['awesome-typescript-loader', 'angular2-template-loader', 'angular-router-loader'] 
 
}); 
 

 
module.exports = webpackMerge(commonConfig, { 
 

 
    devtool: 'cheap-module-eval-source-map', 
 

 
    output: { 
 
     path: path.resolve(rootDir, 'dist'), 
 
     publicPath: 'http://localhost:8080/', 
 
     filename: '[name].js', 
 
      chunkFilename: '[name].chunk.js' 
 
     }, 
 

 
     plugins: [ 
 
      new ExtractTextPlugin('[name].css') 
 
     ], 
 

 
     devServer: { 
 
      historyApiFallback: true, 
 
      stats: 'minimal' 
 
     } 
 
});

+0

웹팩 구성 파일의 스 니펫을 제공하고 자산 폴더에서 pdf가 생성되는 위치를 알려주십시오. –

+0

@PraveshKhatri 코드 스 니펫을 추가했습니다. –

+0

파일을 assets 폴더에 넣을 때 webpack.common.js의 파일 로더에'.pdf' 확장자를 추가해야한다고 생각합니다. 당신은 copyWebpackPlugin에 의해 당신의 pdf를 자산의 목적지 폴더에 복사 할 수있는 또 다른 애로를 가지고 있습니다. –

답변

1

당신은 devSe 말할 수 파일을 제공 할 위치의 rver.

devServer: { 
    historyApiFallback: true, 
    stats: 'minimal', 
    contentBase: ['./dist', 'C:\\'] 
} 

그럼 당신은 <... src="/file_name.ext" ... />

사용하여 파일을로드 할 수 있습니다하지만 추가 C 권하고 싶지 않다 : \ contentBase한다. 가능성이 있다면 생성 된 PDF에 대한 다른 출력 디렉토리를 정의하십시오.

+0

감사합니다. 그것은 효과가 있었다. 그런데 왜 그것을 사용하지 않는 것이 좋습니다? –

+1

니스. 왜냐하면 C 드라이브의 모든 파일을 포함시킬 수 있기 때문입니다. – scipper

+0

확인. 그러나 현 재의 유스 케이스는 문제가되지 않을 것이다. 고맙습니다! –

관련 문제