javascript - Webpack file-loader not loading files with complex paths -
i've got project uses typescript 2.4.1, webpack 2.6.1, , i'm having issues getting relative paths load images correctly.
if put image in same folder .ts
file loads it, can work:
const background = require("./background.png"); console.log(background);
so works:
src book.ts background.png
but can't figure out how make file loader find image structure:
src book.ts assets background.png
i tried setting publicpath
in file-loader options described here couldn't file load @ way no matter combinations of absolute , relative paths tried.
here's webpack.config.js file. (i removed parts because long.)
module.exports = { entry: { app: [ path.resolve(__dirname, './src/main.ts') ], vendor: ['pixi', 'p2', 'phaser', 'webfontloader'] }, output: { pathinfo: true, path: path.resolve(__dirname, 'dist'), publicpath: './dist/', filename: 'bundle.js' }, module: { rules: [ {test: /\.ts$/, enforce: 'pre', loader: 'tslint-loader', options: {emiterrors: true, failonhint: true}}, {test: /\.ts$/, loader: 'ts-loader'}, {test: /pixi\.js/, use: ['expose-loader?pixi']}, {test: /phaser-split\.js$/, use: ['expose-loader?phaser']}, {test: /p2\.js/, use: ['expose-loader?p2']}, {test: /\.(png|svg|jpg|gif|mp3|wav|mp4)$/, use: [{ loader: 'file-loader', options: { outputpath: 'assets/' } }]}, {enforce: "pre", test: /\.js$/, loader: "source-map-loader"} ] }, node: { fs: 'empty', net: 'empty', tls: 'empty' } }
and here's typescript require declaration:
declare let require: { <t>(path: string): t; (paths: string[], callback: (...modules: any[]) => void): void; ensure: (paths: string[], callback: (require: <t>(path: string) => t) => void) => void; };
(i tried getting nice es6 imports working, didn't go well.)
i tried file-loader , didnt work...
try using "url-loader" in webpack.config:
// "url" loader works "file" loader except embeds assets // smaller specified limit in bytes data urls avoid requests. // missing `test` equivalent match. { test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/], loader: require.resolve('url-loader'), options: { limit: 10000, name: 'static/media/[name].[hash:8].[ext]', }, },
then can "just" use path can use:
const imagesrc = import('./relativepathtoimgfile');
edit: , dont forget add url-loader package.json :)
Comments
Post a Comment