webpack/examples/typescript/README.md

176 lines
4.3 KiB
Markdown
Raw Normal View History

# example.js
```javascript
console.log(require("./index"));
```
# index.ts
```typescript
2019-06-16 21:06:50 +08:00
const myName: string = "Junya";
const age: number = 22;
function getArray<T>(...args: T[]): T[] {
return [...args];
}
console.log(getArray("foo", "bar"));
console.log(getArray(1, 2, 3));
```
# webpack.config.js
```javascript
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
module.exports = (env = "development") => ({
mode: env,
entry: {
output: "./index.ts"
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
transpileOnly: true
}
}
]
},
resolve: {
extensions: [".ts", ".js", ".json"]
},
plugins: [new ForkTsCheckerWebpackPlugin({ async: env === "production" })]
});
```
# dist/output.js
```javascript
2019-10-09 05:45:47 +08:00
/******/ ((modules, runtime) => { // webpackBootstrap
2019-06-16 21:06:50 +08:00
/******/ "use strict";
/******/ // 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;
/******/ }
/******/
/******/
2019-06-16 21:06:50 +08:00
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ };
/******/
2019-06-16 21:06:50 +08:00
/******/ // run startup
/******/ return startup();
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/*!********************!*\
!*** ./example.js ***!
\********************/
2019-06-16 21:06:50 +08:00
/*! exports [maybe provided (runtime-defined)] [no usage info] */
/*! runtime requirements: __webpack_require__ */
2019-10-09 05:45:47 +08:00
/***/ ((__unusedmodule, __unusedexports, __webpack_require__) => {
console.log(__webpack_require__(/*! ./index */ 1));
/***/ }),
/* 1 */
/*!******************!*\
!*** ./index.ts ***!
\******************/
2019-06-16 21:06:50 +08:00
/*! exports [maybe provided (runtime-defined)] [no usage info] */
2019-10-09 05:45:47 +08:00
/*! runtime requirements: top-level-this-exports */
2019-06-16 21:06:50 +08:00
/***/ (function() {
2019-10-09 05:45:47 +08:00
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
var myName = "Junya";
var age = 22;
function getArray() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
2019-10-09 05:45:47 +08:00
return __spreadArrays(args);
}
console.log(getArray("foo", "bar"));
console.log(getArray(1, 2, 3));
/***/ })
/******/ ]);
```
# Info
## Unoptimized
```
Starting type checking service...
Using 1 worker with 2048MB memory limit
Hash: 0a1b2c3d4e5f6a7b8c9d
2019-10-09 05:45:47 +08:00
Version: webpack 5.0.0-alpha.30
2019-06-16 21:06:50 +08:00
Asset Size Chunks Chunk Names
2019-10-09 05:45:47 +08:00
output.js 2.41 KiB {0} [emitted] main
Entrypoint main = output.js
2019-10-09 05:45:47 +08:00
chunk {0} output.js (main) 652 bytes [entry] [rendered]
> ./example.js main
[0] ./example.js 33 bytes {0} [built]
2019-06-16 21:06:50 +08:00
[used exports unknown]
entry ./example.js main
2019-10-09 05:45:47 +08:00
[1] ./index.ts 619 bytes {0} [built]
2019-06-16 21:06:50 +08:00
[used exports unknown]
cjs require ./index [0] ./example.js 1:12-30
```
## Production mode
```
Starting type checking service...
Using 1 worker with 2048MB memory limit
Hash: 0a1b2c3d4e5f6a7b8c9d
2019-10-09 05:45:47 +08:00
Version: webpack 5.0.0-alpha.30
2019-06-16 21:06:50 +08:00
Asset Size Chunks Chunk Names
2019-10-09 05:45:47 +08:00
output.js 576 bytes {179} [emitted] main
Entrypoint main = output.js
2019-10-09 05:45:47 +08:00
chunk {179} output.js (main) 652 bytes [entry] [rendered]
> ./example.js main
2019-06-16 21:06:50 +08:00
[144] ./example.js 33 bytes {179} [built]
entry ./example.js main
2019-10-09 05:45:47 +08:00
[862] ./index.ts 619 bytes {179} [built]
2019-06-16 21:06:50 +08:00
cjs require ./index [144] ./example.js 1:12-30
```