typescript - 加载一个与捆绑包分开的库
问题描述
我正在尝试使用这个库,但这取决于THREE.js
已经在全球范围内加载。我正在使用typescript
,webpack
和webpack-dev-server
. 是否可以在主包之前将 webpackTHREE.js
作为单独的脚本加载?我还需要THREE
在我自己的源代码中使用,所以我也想将它从主包中完全排除。
这是我的 webpack.config.js:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.ts',
devtool: 'inline-sourcemap',
mode: 'development',
devServer: {
contentBase: path.join(__dirname, 'dist'),
port: 4500
},
module: {
rules: [
{
test: /\.tsx?$/i,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.obj$/i,
use: {
loader: 'file-loader'
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
}
],
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist'
},
plugins: [new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.ejs'
})]
};
还有我的 tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist/",
"noImplicitAny": false,
"module": "es6",
"target": "es2015",
"sourceMap": true,
"moduleResolution": "node",
"allowJs": true,
"typeRoots": [
"src/types",
"node_modules/@types"
],
"paths": {
"@custom-modules/*": ["src/types/*"]
}
},
"exclude": [
"node_modules",
"src/types"
]
}
解决方案
可能值得将 CDN 链接three.js
添加到您绑定到的 html 模板中webpack
。看起来这是您的index.ejs
文件。
我不确定你是如何构建这个文件的,但在 html 中它看起来像:
<!DOCTYPE html>
<html>
<head>
...
// globally add three.js to your site
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>
// put the rest of your scripts below
...
</head>
<body><!-- Your web--></body>
</html>
推荐阅读
- python-3.x - 找到从 1 到 N 的 L 个数字的组合,使它们的和等于 N
- java - Kafka - 将消息分配给特定的消费者组
- java - 如何创建实现 Map 接口的向量?
- reactjs - 绑定元素“字符串”隐式具有“任何”类型
- phpmyadmin - 解析错误:语法错误,意外的 'const' (T_CONST),在 Phpmyadmin 上期望变量 (T_VARIABLE)
- regex - 将不同的正则表达式可能性合并为一个
- javascript - 在类函数中编辑变量如何工作?
- haskell - 我怎么能提前知道 cabal 将生成的可执行文件放在哪里?
- java - Spring Boot v2.1.0.RELEASE - 没有名为“entityManagerFactory”的bean可用
- javascript - 窗口调整大小不应影响角度 8 中的打印屏幕内容