Bundle/CopyJavascript 来自 Node_Modules 的库(带有 TypeScript 的 .Net Core 项目)

Bundle/CopyJavascript Libraries from Node_Modules (.Net Core proj with TypeScript)

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

我想在 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 控制台来看,那里似乎存在一个文件,正如您在此处看到的那样:

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

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

使用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",