【问题标题】:Webpack only bundling a single Typescript file instead of all dependenciesWebpack 仅捆绑单个 Typescript 文件而不是所有依赖项
【发布时间】:2017-04-20 15:15:36
【问题描述】:

我正在尝试将我的 Typescript 项目文件(包括来自 node_modules 的所有依赖项)捆绑到一个文件中,但我在使用 Webpack 时遇到了问题。我尝试了很多选项,但无论我做什么,它似乎只是在输出中包含入口文件 only

一些具有依赖关系的示例 Typescript 文件:

app/test1.ts:

import {x} from 'app/test2';

let y = x + 1;

app/test2.ts:

export let x = 123;

Webpack 文件:

module.exports = {
entry: {
 'app/startup' : './app/test1'
},
output: {
  filename: 'bundle.js'
},
module: {
  loaders: [
    { test: /\.ts$/, loader: 'ts-loader' }
  ]
},
resolve: {
  modules: [
    'node_modules'
  ],
  extensions: ['.ts', '.js']
},
};

tsconfig.json 文件:

 {
  "atom": {
      "rewriteTsconfig": false
  },
  "compilerOptions": {
      "skipLibCheck": true,
      "pretty": false,
      "allowJs": false,
      "outDir": "tsDist",
      "baseUrl": ".",
      "moduleResolution": "node",
      "target": "es5",
      "module": "es2015",
      "sourceMap": false,
      "strictNullChecks": true,
      "noImplicitThis": true,
      "noImplicitAny": false,
      "declaration": false
  },
  "filesGlob": [
      "app/**/*.ts",
  ],
  "exclude": [
      "node_modules",
  ],
  "files": [
  ]
 }

除了捆绑包顶部的标准内容外,它仅包含缺少“app/test2”的内容:

System.register(["app/test2"], function (exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var test2_1, y;
    return {
        setters: [
            function (test2_1_1) {
                test2_1 = test2_1_1;
            }
        ],
        execute: function () {
            y = test2_1.x + 1;
        }
    };
});

我在这里做错了什么?为什么 test2 不包含在捆绑包中?我还需要做什么才能包含任何 node_module 依赖项?

我尝试从这个示例项目中复制设置,但得到了相同的结果: https://github.com/blacksonic/typescript-webpack-tree-shaking

【问题讨论】:

  • 能否请您也包括您的项目文件结构?

标签: javascript typescript webpack


【解决方案1】:

可能是因为您引用了错误的链接?

import {x} from './test2'; // you enter 'app/test2'
let y = x + 1;

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多