[英]How can I include a global JS file to my React project using webpack?
我第一次使用 React,我发现我需要在多个 JSX 文件中使用一些 JS 函数。
我查看了一些示例,但无法弄清楚如何正确包含我的global.js
文件。
我的文件夹结构:
/dist
/src
/components
/sass
/img
index.js
global.js
webpack.config.js
这是我的 webpack 文件:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: "./src/index.js",
output: {
path: __dirname + "/js/",
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
cacheDirectory: true,
presets: ['es2015', 'react']
}
}
]
}
};
谁能告诉我如何正确包含这个文件以及它在 webpack 代码中的位置?
我不确定你需要什么,但 webpack 是你的构建系统,所以它在你想要的层次上发挥作用。 如果要在各种文件中包含函数,则需要在需要的任何位置包含该文件:
global.js
module.export = {
func1: ..,
func2: ..
}
需要函数的文件:
var globals = require('path/to/global.js')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.