2016-09-17 8 views
0

.jsonファイルを非同期にロードしようとしています。 .jsファイルの例がありますが、私はtypescriptを使用しており、解決策を見つけることができません。.jsonファイルをwebpackと非同期でロードする

webpack.config.js

var webpack = require('webpack'); 

module.exports = { 
    entry: "./src/app.ts", 
    output: { 
     path: './dist', 
     filename: "bundle.js" 
    }, 
    resolve: { 
     extensions: ['', '.ts', '.tsx', '.js', '.jsx', '.json'] 
    }, 

    module: { 
     loaders: [ 
      { test: /\.css$/, loader: "style!css" }, 
      { test: /\.ts$/, loader: 'ts-loader'}, 
      { test: /\.json$/, loader: 'json-loader'}, 
      { 
       test: /jquery\.min\.js$/, 
       loader: 'expose?jQuery' 
      }, 
      { 
       test: /jquery\.min\.js$/, 
       loader: 'expose?$' 
      } 
     ] 
    }, 
    plugins: [ 
     new webpack.ProvidePlugin({ 
      $: 'jquery', 
      jQuery: 'jquery', 
      'window.jQuery': 'jquery', 
      'window.$': 'jquery' 
     }) 
    ] 
}; 

app.ts

それはコンソールにエラーを与える
declare var require: { 
    <T>(path: string): T; 
    (paths: string[], callback: (...modules: any[]) => void): void; 
    ensure: (paths: string[], callback: (require: <T>(path: string) => T) => void) => void; 
}; 


require(['./assets/data.json'], function(data) { 
    console.log(data); //doesn't log anything. 
}); 

GET http://localhost:5557/1.bundle.js 404 (Not Found) 

しかし、私は非同期的にしようとしていない場合は、それは正常に動作します。

console.log(require('./assets/data.json')); // logs the json just fine 

ありがとうございました

答えて

1

あなたのバンドルがどこにあるかわからないようです。

バンドル分割の使用を開始したら、設定でoutput.publicPathを設定する必要があります。 distディレクトリがブラウザでlocalhost/distと表示されている場合は、output.publicPath/dist/に設定する必要があります(webpackがロードしようとしているファイルの先頭にpublicPathが付加されるため)。

+0

私の設定の出力にpublicPath: './dist'を追加しましたが、それでもエラーは変更されません。 – Anik

+0

'/ dist'を置かずに'/dist'を入れます。あなたのブラウザではpublicPathが使用されています。 – Ambroos

+1

'/ dist'を' localhost:5557/dist1.bundle.js'とした場合、値を '/ dist /'に変更して 'localhost:5557/dist/1.bundle.js'が存在します。答えをありがとう:) – Anik

関連する問題