不使用 webpackJsonp 编译 Webpack typescript

2024-01-21

是否可以使用webpack进行打包而不使用webpack模块加载?

我有一个可以使用 webpack 的所有内容的应用程序,在这个应用程序旁边我有一个小的打字稿文件test.ts应该被编译,缩小等。但是输出应该是一个简单的js文件,没有被包装到webpackJsonp。对于没有外部依赖的几行来说,这增加了太多的开销(96kb)。

test.ts

alert('foo');

test.js 是

webpackJsonp([1],{
/***/ 0:
/***/ function(module, exports, __webpack_require__) {

    __webpack_require__(1);
    __webpack_require__(75);
    module.exports = __webpack_require__(105);

/***/ },

/***/ 105:
/***/ function(module, exports) {

    "use strict";
    alert('test');

/***/ }
});

test.js 应该

alert('foo');

我尝试保留一个生态系统(webpack)来构建。


这不是 webpack 的开销。这种开销是由另一个原因造成的。这里演示webpack配置。 webpack 构建包含内容的源包alert()结果大小仅为 519 字节,而不是所声明的 96kb。项目内容和结果包(缩小和非缩小)是:

包.json

{
  "name": "app",
  "version": "1.0.0",
  "main": "webapp.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-preset-react": "^6.3.13",
    "express": "^4.15.3",
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "ts-loader": "^2.1.0",
    "typescript": "^2.3.3",
    "webpack": "^2.5.1"
  },
  "description": ""
}

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./public/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "allowJs": true
  }
}

alert.ts

alert(1);

索引.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Hello Workd!</title>
    </head>
    <body>
        <div id="root"></div>
        <script type="text/javascript" src="alert.js"></script>
    </body>
</html>

webpack.config.js

var webpack = require('webpack');
var path = require('path');
module.exports = {
  entry: {
    App: './App',
    alert: './alert.ts',
  },
  output: {
    path: path.join(__dirname, './public'),
    filename: '[name].js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015', 'react']
        }
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      }
    ]
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin()
  ]
}

alert.js- 生成迷你版

!function(n){function r(e){if(t[e])return t[e].exports;var o=t[e]={i:e,l:!1,exports:{}};return n[e].call(o.exports,o,o.exports,r),o.l=!0,o.exports}var t={};r.m=n,r.c=t,r.i=function(n){return n},r.d=function(n,t,e){r.o(n,t)||Object.defineProperty(n,t,{configurable:!1,enumerable:!0,get:e})},r.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return r.d(t,"a",t),t},r.o=function(n,r){return Object.prototype.hasOwnProperty.call(n,r)},r.p="",r(r.s=182)}({182:function(n,r){alert(1)}});

alert.js- 生成非缩小的

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // identity function for calling harmony imports with the correct context
/******/    __webpack_require__.i = function(value) { return value; };
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 182);
/******/ })
/************************************************************************/
/******/ ({

/***/ 182:
/***/ (function(module, exports) {

alert(1);


/***/ })

/******/ });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不使用 webpackJsonp 编译 Webpack typescript 的相关文章

随机推荐

  • 用于检查 linq to sql 数据上下文中的唯一性的通用验证属性

    我已经编写 asp net 几天了 这是一个我什至无法自己弄清楚的问题 我希望从代码中可以清楚地看出我想要完成的任务 并且我已经完成了 但这并不漂亮 此外 我想在任何表 任何字段上使用它 即根据我指定的表和字段检查值的唯一性 将其全部传递到
  • 每行中的列数可能不同的 JTable

    我正在寻找 JTable 的解决方案 其中我可以改变每行中的列数 但行的大小 以像素为单位 是恒定的 我发现迄今为止 stackoverflow 引用仅指向旧解决方案的死链接 每行具有不同列数的 JTable https stackover
  • 更新二维计数表

    假设我想要一个 Scala 数据结构来实现一个可以随时间变化的二维计数表 即表中的各个单元格可以递增或递减 我应该用什么来做到这一点 我可以使用二维数组 val x Array fill Int 1 2 0 x 1 2 1 但数组是可变的
  • GIT:错误:pathspec 'xxx 与 git 已知的任何文件都不匹配

    我的 git 存储库遇到一些问题 我找不到错误 事实是 我已经在 PHP 项目中使用了这个存储库 一切都很好 然后 我向其中 添加 了作曲家 也就是说 我将composer文件复制到存储库的根目录 创建了composer json 并使用了
  • 使用 Python/Selenium 切换 iframe

    我正在尝试使用selenium导航使用框架的网站 这是我第 1 部分的工作 python 脚本 from selenium import webdriver import time from urllib import request dr
  • 当应用程序位于*前台*时,红色录音状态栏“闪烁”

    有很多疑问 here https stackoverflow com questions 16878991 hide red recording status bar in ios app when not recording here h
  • 当另一个片段中的数据发生变化时,如何刷新一个片段中的RecyclerView

    数据提交成功后 如何将本地数据库的数据刷新到RecyclerView中 我使用应用程序上的选项卡 2nd Tab函数提交数据 如果成功 数据将存储在localDB Data on localDB我将在3rd Tab 但会发生什么 我必须刷卡
  • 在Excel VBA中,如何检索单元格内文本的格式

    在 Excel VBA 中 我想检索单元格的文本以及每个单词的格式 例如 单元格 A1 的值为 sample text Range A1 Value 属性仅返回纯文本 即 示例文本 我想要的是一个对象 它给我类似 示例 i gt 文本 Ex
  • 无互联网连接时的警报框 - Phonegap

    我试图在设备上没有互联网连接时弹出一个弹出窗口 I got 下面的例子 http docs phonegap com en 1 0 0 phonegap connection connection md html工作 但现在我希望仅在结果为
  • 无法从数据帧列表中获取索引位置

    我试图通过使用 python 中的内置方法索引从数据帧列表中获取数据帧的位置 我的代码如下 df1 pd DataFrame 1 2 3 df2 pd DataFrame 4 5 6 df3 pd DataFrame 7 8 9 dfs d
  • Google Glass Android Studio Gradle 问题

    我正在尝试使用 Android Studio 0 5 4 构建我的第一个 Google Glass 应用程序 但我收到构建错误 Error Module TestApplication TestApplication platform Go
  • 保证表值函数结果的顺序

    PREMISE 应用程序代码无法更改 条件非常具体 我正在寻找一些非书本上的东西 如果可以的话 这是最后的解决方法 我有一个表值函数 内联 可以生成 2 到 7 条记录 有时可能只有 1 个或最多 15 个 但很少 该函数仅由应用程序以这种
  • 特定值的固定颜色

    我正在尝试制作温度图 一切正常 但我不知道如何拥有固定的调色板 其实我有这个 rgb palette lt colorRampPalette c blue green yellow orange red space Lab image pl
  • 如何在java中改变我的框架?

    假设有一个按钮 如果您单击该按钮 则会出现一个新框架 依此类推 The setVisible true 函数用于显示框架 创建所需框架的对象并调用此函数 像这样的东西 The applications first or the main f
  • 打印 Mnesia 表的最佳方式

    我尝试了这个代码片段 print next Current gt case mnesia dirty next muppet Current of end of table gt io format n ok Next gt Muppet
  • 缺少 HttpClient 的依赖项

    我在 Java 桌面应用程序中使用 HttpClient 我已经添加httpclient 4 0 1 jar and httpmime 4 0 1 jar到构建路径 但我收到错误 无法解析类型 org apache http HttpRes
  • 使用标识符或 url 获取 Facebook 群组 ID

    我想通过使用 URL 或特定标识符来获取 Facebook 群组 ID For ex http www facebook com groups chennaifoodies http www facebook com groups chen
  • $.ajax仅在放置alert()时才起作用

    我尝试将内容加载到之前使用 AJAX 加载的内容中 我把代码 使用负载 jQuery contentpage on submit loginform my function contentpage load logincheck jsp a
  • 解析模型后,Redland RDF 中 RDF 节点的生命周期?

    我正在解析 RDF model 使用librdf parser parse string into model 然后我保留librdf model但释放librdf parser 在我看来 模型中的节点似乎也消失了 那么一生会做什么雷德兰R
  • 不使用 webpackJsonp 编译 Webpack typescript

    是否可以使用webpack进行打包而不使用webpack模块加载 我有一个可以使用 webpack 的所有内容的应用程序 在这个应用程序旁边我有一个小的打字稿文件test ts应该被编译 缩小等 但是输出应该是一个简单的js文件 没有被包装