gpt4 book ai didi

javascript - React-loadable 仅从相对路径获取 block

转载 作者:行者123 更新时间:2023-11-29 17:42:26 29 4
gpt4 key购买 nike

我正在使用 webpack 4.6.0babel 7.0.0 构建 react 16.3 网络应用。

我的js资源在https://domain/js/app.bundle.js下

我使用 react-loadable 将组件拆分成 block 。这些 block 也位于/js/下,但是,react 从相对路径中获取它们,即:

https: //domain/1.app.bundle.js

这当然行不通。

问题:如何让 react 或 webpack 使用绝对路径/js/来获取 block ?

这是我的 webpack.config.json

var path = require('path');

module.exports = {
mode: 'production',
devtool: 'eval-source-map',
entry: './js/src/app.js',
watch: false,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},
output: {
path: path.resolve(__dirname, "output/js"),
filename: 'app.bundle.js',
chunkFilename: '[name].app.bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: path.resolve(__dirname, "js/src"),
query: {
presets: ['@babel/preset-react'],//, 'es2015'],
plugins: [
'transform-class-properties',
'@babel/plugin-syntax-dynamic-import'
]
}
},
{
test: /\.scss$/,
include: path.resolve(__dirname, "sass"),
loaders: ['style', 'css', 'sass']
}
]
}
};

这是我在 package.json 中的包

    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@material-ui/core": "^3.0.3",
"@material-ui/icons": "^3.0.1",
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.1",
"babel-loader": "^8.0.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-es2015": "^6.9.0",
"@babel/preset-react": "^7.0.0",
"node-sass": "^4.9.2",
"react": "^16.3.2",
"react-addons-css-transition-group": "^15.6.2",
"react-dom": "^16.3.2",
"react-loadable": "^5.5.0",
"react-swipeable-views": "^0.12.13",
"react-tap-event-plugin": "^3.0.3",
"scss-compile": "^0.1.7",
"webpack": "^4.6.0",
"webpack-cli": "^2.1.2",

这是我为测试代码拆分而更改的 react 代码

import React from 'react';
import Loadable from 'react-loadable';

const PV = Loadable({
loader: () => import('./PV/PV'),
loading: () => <div>Loading...</div>,
});

最佳答案

已解决

output: {
path: path.resolve(__dirname, "output/js"),
filename: 'app.bundle.js',
chunkFilename: '[name].app.bundle.js',
publicPath: '/js/' <------------------ this was the missing piece.
},

关于javascript - React-loadable 仅从相对路径获取 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52329156/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com