Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 网页包+;Typescript-如何将导入的非UMD模块隔离到一个文件中?_Javascript_Typescript_Webpack_Umd - Fatal编程技术网

Javascript 网页包+;Typescript-如何将导入的非UMD模块隔离到一个文件中?

Javascript 网页包+;Typescript-如何将导入的非UMD模块隔离到一个文件中?,javascript,typescript,webpack,umd,Javascript,Typescript,Webpack,Umd,在使用Typescript和Webpack的项目中,我希望强制执行通常全局库(例如jQuery)被视为UMD全局库 现在,如果我在引用$的文件中从“jQuery”中将import*作为$省略,则webpack成功,但脚本在运行时失败。但是,import*as uu来自'lodash'具有预期的行为,忽略时会导致网页生成失败 考虑以下文件: first.ts import * as $ from "jquery"; import * as _ from "lodash"; import { se

在使用Typescript和Webpack的项目中,我希望强制执行通常全局库(例如jQuery)被视为UMD全局库

现在,如果我在引用
$
的文件中从“jQuery”中将
import*作为$省略,则webpack成功,但脚本在运行时失败。但是,
import*as uu来自'lodash'
具有预期的行为,忽略时会导致网页生成失败

考虑以下文件:

first.ts

import * as $ from "jquery";
import * as _ from "lodash";

import { second } from "./second";

$(() => {
    const message = _.identity("first.ts");
    $(".first").html(message);
    second.Test();
});
//import * as $ from "jquery";
//import * as _ from "lodash";

export const second = {
    Test: () => {
        const message = _.identity("second.ts");
        $(".second").html(message);
    }
}
const path = require('path');

module.exports = {
    entry: './first.ts',
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname)
    }
}
second.ts

import * as $ from "jquery";
import * as _ from "lodash";

import { second } from "./second";

$(() => {
    const message = _.identity("first.ts");
    $(".first").html(message);
    second.Test();
});
//import * as $ from "jquery";
//import * as _ from "lodash";

export const second = {
    Test: () => {
        const message = _.identity("second.ts");
        $(".second").html(message);
    }
}
const path = require('path');

module.exports = {
    entry: './first.ts',
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname)
    }
}
index.html

<html>
    <head>
        <script type="text/javascript" src="./bundle.js">
        </script>
    </head>
<body>
<div class="first"></div>
<div class="second"></div>
</body>
</html>
tsconfig.json

{
  "name": "webpack-typescript-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/jquery": "^2.0.46",
    "@types/lodash": "^4.14.65",
    "jquery": "^3.2.1",
    "lodash": "^4.17.4",
    "ts-loader": "^2.1.0",
    "typescript": "^2.3.3",
    "webpack": "^2.6.1"
  }
}
{
    "compilerOptions": {
        "target": "ES5",
        "sourceMap": true,
        "module": "commonjs",
        "types": []
    },
    "include": [
        "./*.ts"
    ],
    "exclude": [
        "./node_modules"
    ]
}
webpack.config.js

import * as $ from "jquery";
import * as _ from "lodash";

import { second } from "./second";

$(() => {
    const message = _.identity("first.ts");
    $(".first").html(message);
    second.Test();
});
//import * as $ from "jquery";
//import * as _ from "lodash";

export const second = {
    Test: () => {
        const message = _.identity("second.ts");
        $(".second").html(message);
    }
}
const path = require('path');

module.exports = {
    entry: './first.ts',
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname)
    }
}

是否有一种方法可以在所有.ts文件中强制执行导入语句?

考虑在网页包配置中使用
externals
选项:


我想它的用途与您的用例非常匹配。

很有趣,谢谢您的回答。这看起来像是支持与我的用例相反的东西;当“import”语句丢失时,我试图引发一个错误-我不知道如何使用
externals
选项来实现这一点。