【发布时间】:2018-04-27 16:54:29
【问题描述】:
我想要一个全局变量或其他东西,以便我可以在需要时轻松更改路径。当我构建它时,我也希望有一条不同的路径。
有没有更好的不使用 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 中进行设置吗?
这是我的 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'
})
]
};
【问题讨论】: