gpt4 book ai didi

javascript - 从 Node_Modules(带有 TypeScript 的 .Net Core 项目) bundle/复制 Javascript 库

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:03 24 4
gpt4 key购买 nike

这个问题对很多人来说可能是微不足道且容易的,但我很难配置一切以正常工作,想了解它是如何工作的。

我想在 VS2015 中创建一个客户端项目,但是我想使用 TypeScript 以及一些外部 javascript 库,例如 backbone 和 marionette。

我已经配置了大部分内容,但是在运行该页面时,我在控制台上看到一条错误消息,提示“Marionette is not defined”,这让我认为这些库(我之前使用 NPM 在项目中安装的)是未正确复制到 wwwroot。

我正在使用带有加载器的 Webpack 来转换我的 TypeScript 文件。

有人可以指导我走上正确的道路吗?或者请给我一些文件?

这是我的 package.json

{
"version": "1.0.0",
"name": "yourappname",
"private": true,
"devDependencies": {
"clean-webpack-plugin": "^0.1.19",
"ts-loader": "^4.0.1",
"typescript": "^2.7.2",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.11"
},
"scripts": {
"webpack-script": "webpack"
},
"-vs-binding": {
"BeforeBuild": [
"webpack-script"
]
},
"dependencies": {
"@types/backbone": "^1.3.42",
"@types/backbone.marionette": "^3.3.2",
"@types/jquery": "^3.3.1",
"@types/underscore": "^1.8.8",
"backbone": "^1.3.3",
"backbone.marionette": "^3.5.1",
"jquery": "^3.3.1",
"underscore": "^1.8.3"
}
}

我的 webpack.config.js

"use strict"
{
// Required to form a complete output path
let path = require('path');

// Plagin for cleaning up the output folder (bundle) before creating a new one
const CleanWebpackPlugin = require('clean-webpack-plugin');

// Path to the output folder
const bundleFolder = "wwwroot/bundle/";

module.exports = {
// Application entry point
entry: {
app: ["./Scripts/main.ts"],
underscore: ['underscore'],
jquery: ['jquery'],
backbone: ['backbone'],
backbonemarionette: ['backbone.marionette']
},

// Output file
output: {
filename: '[name].js',
path: path.resolve(__dirname, bundleFolder)
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/,
},
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
plugins: [
new CleanWebpackPlugin([bundleFolder])
],
// Include the generation of debugging information within the output file
// (Required for debugging client scripts)
devtool: "inline-source-map"
};
}

我的 tsconfig.json

{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,

"alwaysStrict": true,

"allowSyntheticDefaultImports": true,
"lib": [
"dom",
"es5",
"es2015.promise"
],

"allowJs": true,
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true
},

"include": [ "./Scripts/*" ],
"compileOnSave": false
}

我的 main.ts

class MarionetteApp extends Marionette.Application {  //That's the thing not defined in chrome console 'Marionette'
constructor() {
super();
this.on("start", this.initializeAfter);
}
initializeAfter() {
console.log("i am here");
alert("initialiseAfter called");
}
}

但是从 chrome 控制台来看,那里似乎存在一个文件,正如您在这里看到的:

enter image description here

我注意到的另一件事是,我在 VS 的 Dependencies 文件夹和 Marionette 中看到一个警告,它有一个语法错误,它没有定义,为什么?这里:

enter image description here

有人可以给我一些见解吗?我究竟做错了什么?愿意学习如何正确地做到这一点!

最佳答案

使用Webpack.ProvidePlugin定义全局变量

 const Webpack = require('webpack');
//...
plugins: [
new CleanWebpackPlugin([bundleFolder]),
new Webpack.ProvidePlugin({
_: 'underscore',
$: 'jquery',
jQuery: 'jquery',
Backbone: 'backbone',
Bb: 'backbone',
Marionette: 'backbone.marionette',
Mn: 'backbone.marionette',
}),

]

像这样去掉多个入口点

entry: "./Scripts/main.ts",

关于javascript - 从 Node_Modules(带有 TypeScript 的 .Net Core 项目) bundle/复制 Javascript 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205021/

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