webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
include: [resolve("src")],
use: [
"thread-loader",
"cache-loader",
{
loader: "babel-loader",
},
{
loader: "auto-inject-loader",
options: {
autoImport: [
{
// name: 'localSocket',
paths: "src/lib/core/index.js",
},
],
},
},
],
},
],
},
};
Type: Array
name: 当前在页面使用的变量
paths: 变量定义所在哪个文件路径下
src/lib/core/index.js 里面的函数
export function add () {}
function localSocket () {}
export default localSocket
当前js内容使用到localSocket, add 方法,则会自动生成如下
import localSocket, {add} from 'src/lib/core/index.js'
注意事项:
- 如果 � 项目使用了 eslint 校验,需要在 eslintrc.js 添加
globals: {
localSocket: true; // 此处localSocket即是在options里面定义的name
}