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
ありがとうございました
私の設定の出力にpublicPath: './dist'を追加しましたが、それでもエラーは変更されません。 – Anik
'/ dist'を置かずに'/dist'を入れます。あなたのブラウザではpublicPathが使用されています。 – Ambroos
'/ dist'を' localhost:5557/dist1.bundle.js'とした場合、値を '/ dist /'に変更して 'localhost:5557/dist/1.bundle.js'が存在します。答えをありがとう:) – Anik