전역 변수 나 다른 것을 가지고 싶기 때문에 필요한 경우 쉽게 경로를 변경할 수 있습니다. 나는 또한 그것을 구축 할 때 다른 길을 가고 싶습니다.React - 이미지 가져 오기
require를 사용하지 않고 가져 오는 더 좋은 방법이 있습니까?
const URL="./../../img";
//withURL doesn't work
export const logo1 = require(URL+ "/Global/logo1.png");
//this works but too long
export const logo2 = require("./../../img/Editorial/logo2.jpg");
어떤 아이디어가 있습니까? webpack에 이걸 설정할 수있는 방법이 있나요?
var webpack = require("webpack");
var path = require("path");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var extractPlugin = new ExtractTextPlugin({
filename: "App.css"
});
module.exports = {
entry: './src/js/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['react', 'es2015']
}
}
]
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: ['css-loader']
})
},
{
test: /\.html$/,
use: ['html-loader']
},
{
test: /\.(png|jpg|svg)$/,
use: [
{
loader: 'file-loader',
options: {
name:'[name].[ext]',
outputPath: 'img/'
}
}
]
}
],
},
plugins: [
extractPlugin,
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
};
예, 제 코드가 수정되었습니다. 그것은 요구와 함께 작동하지 않으며 단지 const가없는 URL이어야합니다. – Camila
요청할 때 무엇이 반환되는지 확인해 보셨습니까? console.log 문자열을 반환해야합니다. 그렇지 않으면 웹팩 구성이 잘못되었습니다. – albertfdp
앱이 중단됩니다. 내 웹팩을 추가했습니다. require() 내부에 다른 URL을 쓸 수 있습니까? – Camila