网站搜索

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


这个问题对很多人来说可能很简单和容易,但我在配置一切以正常工作时遇到了困难,想学习它是如何工作的。

我想在 VS2015 中创建一个客户端项目,不过我想使用 TypeScript,并且还想使用一些外部的 JavaScript 库,比如 Backbone 和 Marionette。

我已经配置了大部分内容,但在运行页面时,我在控制台看到一个错误,显示 'Marionette 未定义',这让我认为之前使用 NPM 安装到项目中的库没有被正确复制到 wwwroot。

我正在使用 Webpack 和一个 loader 来转译我的 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 有一个语法错误,它没有定义,为什么?这里:

请问有人能给我一些见解吗?我做错了什么?我愿意学习如何正确地做这件事!

答案#1

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