Skip to content

Commit 32fa70c

Browse files
authored
Merge pull request #671 from maier49/compile-bundled-files-and-typing-files
Compile bundled files and typing files
2 parents cc1c037 + 705741e commit 32fa70c

File tree

22 files changed

+393
-6
lines changed

22 files changed

+393
-6
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,15 @@ Or if you want to use only tsx, just use the `appendTsxSuffixTo` option only:
401401
{ test: /\.tsx$/, loader: 'babel-loader!ts-loader', options: { appendTsxSuffixTo: [/\.vue$/] } }
402402
```
403403
404+
#### onlyCompileBundledFiles *(boolean) (default=false)*
405+
406+
The default behavior of ts-loader is to act as a drop-in replacement for the `tsc` command,
407+
so it respects the `include`, `files`, and `exclude` options in your `tsconfig.json`, loading
408+
any files specified by those options. The `onlyCompileBundledFiles` option modifies this behavior,
409+
loading only those files that are actually bundled by webpack, as well as any `.d.ts` files included
410+
by the `tsconfig.json` settings. `.d.ts` files are still included because they may be needed for
411+
compilation without being explicitly imported, and therefore not picked up by webpack.
412+
404413
### `LoaderOptionsPlugin`
405414
406415
[There's a known "gotcha"](https://github.com/TypeStrong/ts-loader/issues/283) if you are using webpack 2 with the `LoaderOptionsPlugin`. If you are faced with the `Cannot read property 'unsafeCache' of undefined` error then you probably need to supply a `resolve` object as below: (Thanks @jeffijoe!)

src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function getLoaderOptions(loader: Webpack) {
110110
}
111111

112112
type ValidLoaderOptions = keyof LoaderOptions;
113-
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'happyPackMode', 'getCustomTransformers'];
113+
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers'];
114114

115115
/**
116116
* Validate the supplied loader options.
@@ -146,7 +146,8 @@ function makeLoaderOptions(instanceName: string, configFileOptions: Partial<Load
146146
transformers: {},
147147
entryFileCannotBeJs: false,
148148
happyPackMode: false,
149-
colors: true
149+
colors: true,
150+
onlyCompileBundledFiles: false
150151
} as Partial<LoaderOptions>, configFileOptions, loaderOptions);
151152

152153
options.ignoreDiagnostics = arrify(options.ignoreDiagnostics).map(Number);

src/instances.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import chalk, { Chalk } from 'chalk';
55

66
import { makeAfterCompile } from './after-compile';
77
import { getConfigFile, getConfigParseResult } from './config';
8-
import { EOL } from './constants';
8+
import { EOL, dtsDtsxRegex } from './constants';
99
import { getCompilerOptions, getCompiler } from './compilerSetup';
1010
import { hasOwnProperty, makeError, formatErrors, registerWebpackErrors } from './utils';
1111
import * as logger from './logger';
@@ -118,7 +118,7 @@ function successfulTypeScriptInstance(
118118
// Load initial files (core lib files, any files specified in tsconfig.json)
119119
let normalizedFilePath: string;
120120
try {
121-
const filesToLoad = configParseResult.fileNames;
121+
const filesToLoad = loaderOptions.onlyCompileBundledFiles ? configParseResult.fileNames.filter(fileName => dtsDtsxRegex.test(fileName)) : configParseResult.fileNames;
122122
filesToLoad.forEach(filePath => {
123123
normalizedFilePath = path.normalize(filePath);
124124
files[normalizedFilePath] = {

src/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ export interface LoaderOptions {
269269
transpileOnly: boolean;
270270
ignoreDiagnostics: number[];
271271
errorFormatter: (message: ErrorInfo, colors: Chalk) => string;
272+
onlyCompileBundledFiles: boolean;
272273
colors: boolean;
273274
compilerOptions: typescript.CompilerOptions;
274275
appendTsSuffixTo: RegExp[];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import submodule = require('./submodule/submodule');
2+
import externalLib = require('externalLib');
3+
externalLib.doSomething(submodule);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // define getter function for harmony exports
37+
/******/ __webpack_require__.d = function(exports, name, getter) {
38+
/******/ if(!__webpack_require__.o(exports, name)) {
39+
/******/ Object.defineProperty(exports, name, {
40+
/******/ configurable: false,
41+
/******/ enumerable: true,
42+
/******/ get: getter
43+
/******/ });
44+
/******/ }
45+
/******/ };
46+
/******/
47+
/******/ // getDefaultExport function for compatibility with non-harmony modules
48+
/******/ __webpack_require__.n = function(module) {
49+
/******/ var getter = module && module.__esModule ?
50+
/******/ function getDefault() { return module['default']; } :
51+
/******/ function getModuleExports() { return module; };
52+
/******/ __webpack_require__.d(getter, 'a', getter);
53+
/******/ return getter;
54+
/******/ };
55+
/******/
56+
/******/ // Object.prototype.hasOwnProperty.call
57+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
58+
/******/
59+
/******/ // __webpack_public_path__
60+
/******/ __webpack_require__.p = "";
61+
/******/
62+
/******/ // Load entry module and return exports
63+
/******/ return __webpack_require__(__webpack_require__.s = 1);
64+
/******/ })
65+
/************************************************************************/
66+
/******/ ([
67+
/* 0 */
68+
/***/ (function(module, exports) {
69+
70+
module.exports = {
71+
doSomething: function() { }
72+
}
73+
74+
/***/ }),
75+
/* 1 */
76+
/***/ (function(module, exports, __webpack_require__) {
77+
78+
"use strict";
79+
80+
exports.__esModule = true;
81+
var submodule = __webpack_require__(2);
82+
var externalLib = __webpack_require__(0);
83+
externalLib.doSomething(submodule);
84+
85+
86+
/***/ }),
87+
/* 2 */
88+
/***/ (function(module, exports, __webpack_require__) {
89+
90+
"use strict";
91+
92+
var externalLib = __webpack_require__(0);
93+
externalLib.doSomething("");
94+
var message = "Hello from submodule";
95+
module.exports = message;
96+
97+
98+
/***/ })
99+
/******/ ]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 2.99 kB 0 [emitted] main
3+
[0] ./.test/onlyCompileBundledFiles/lib/externalLib.js 55 bytes {0} [built]
4+
[1] ./.test/onlyCompileBundledFiles/app.ts 169 bytes {0} [built]
5+
[2] ./.test/onlyCompileBundledFiles/submodule/submodule.ts 149 bytes {0} [built]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // define getter function for harmony exports
37+
/******/ __webpack_require__.d = function(exports, name, getter) {
38+
/******/ if(!__webpack_require__.o(exports, name)) {
39+
/******/ Object.defineProperty(exports, name, {
40+
/******/ configurable: false,
41+
/******/ enumerable: true,
42+
/******/ get: getter
43+
/******/ });
44+
/******/ }
45+
/******/ };
46+
/******/
47+
/******/ // getDefaultExport function for compatibility with non-harmony modules
48+
/******/ __webpack_require__.n = function(module) {
49+
/******/ var getter = module && module.__esModule ?
50+
/******/ function getDefault() { return module['default']; } :
51+
/******/ function getModuleExports() { return module; };
52+
/******/ __webpack_require__.d(getter, 'a', getter);
53+
/******/ return getter;
54+
/******/ };
55+
/******/
56+
/******/ // Object.prototype.hasOwnProperty.call
57+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
58+
/******/
59+
/******/ // __webpack_public_path__
60+
/******/ __webpack_require__.p = "";
61+
/******/
62+
/******/ // Load entry module and return exports
63+
/******/ return __webpack_require__(__webpack_require__.s = 1);
64+
/******/ })
65+
/************************************************************************/
66+
/******/ ([
67+
/* 0 */
68+
/***/ (function(module, exports) {
69+
70+
module.exports = {
71+
doSomething: function() { }
72+
}
73+
74+
/***/ }),
75+
/* 1 */
76+
/***/ (function(module, exports, __webpack_require__) {
77+
78+
"use strict";
79+
80+
exports.__esModule = true;
81+
var submodule = __webpack_require__(2);
82+
var externalLib = __webpack_require__(0);
83+
externalLib.doSomething2(submodule);
84+
85+
86+
/***/ }),
87+
/* 2 */
88+
/***/ (function(module, exports, __webpack_require__) {
89+
90+
"use strict";
91+
92+
var externalLib = __webpack_require__(0);
93+
externalLib.doSomething("");
94+
var message = "Hello from submodule";
95+
module.exports = message;
96+
97+
98+
/***/ })
99+
/******/ ]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 2.99 kB 0 [emitted] main
3+
[0] ./.test/onlyCompileBundledFiles/lib/externalLib.js 55 bytes {0}
4+
[1] ./.test/onlyCompileBundledFiles/app.ts 170 bytes {0} [built]
5+
[2] ./.test/onlyCompileBundledFiles/submodule/submodule.ts 149 bytes {0}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 2.99 kB 0 [emitted] main
3+
[0] ./.test/onlyCompileBundledFiles/lib/externalLib.js 55 bytes {0}
4+
[1] ./.test/onlyCompileBundledFiles/app.ts 170 bytes {0} [built] [1 error]
5+
[2] ./.test/onlyCompileBundledFiles/submodule/submodule.ts 149 bytes {0}
6+
7+
ERROR in ./.test/onlyCompileBundledFiles/app.ts
8+
[tsl] ERROR in app.ts(3,13)
9+
 TS2551: Property 'doSomething2' does not exist on type 'typeof externalLib'. Did you mean 'doSomething'?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // define getter function for harmony exports
37+
/******/ __webpack_require__.d = function(exports, name, getter) {
38+
/******/ if(!__webpack_require__.o(exports, name)) {
39+
/******/ Object.defineProperty(exports, name, {
40+
/******/ configurable: false,
41+
/******/ enumerable: true,
42+
/******/ get: getter
43+
/******/ });
44+
/******/ }
45+
/******/ };
46+
/******/
47+
/******/ // getDefaultExport function for compatibility with non-harmony modules
48+
/******/ __webpack_require__.n = function(module) {
49+
/******/ var getter = module && module.__esModule ?
50+
/******/ function getDefault() { return module['default']; } :
51+
/******/ function getModuleExports() { return module; };
52+
/******/ __webpack_require__.d(getter, 'a', getter);
53+
/******/ return getter;
54+
/******/ };
55+
/******/
56+
/******/ // Object.prototype.hasOwnProperty.call
57+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
58+
/******/
59+
/******/ // __webpack_public_path__
60+
/******/ __webpack_require__.p = "";
61+
/******/
62+
/******/ // Load entry module and return exports
63+
/******/ return __webpack_require__(__webpack_require__.s = 1);
64+
/******/ })
65+
/************************************************************************/
66+
/******/ ([
67+
/* 0 */
68+
/***/ (function(module, exports) {
69+
70+
module.exports = {
71+
doSomething: function() { }
72+
}
73+
74+
/***/ }),
75+
/* 1 */
76+
/***/ (function(module, exports, __webpack_require__) {
77+
78+
"use strict";
79+
80+
exports.__esModule = true;
81+
var submodule = __webpack_require__(2);
82+
var externalLib = __webpack_require__(0);
83+
externalLib.doSomething(submodule);
84+
85+
86+
/***/ }),
87+
/* 2 */
88+
/***/ (function(module, exports, __webpack_require__) {
89+
90+
"use strict";
91+
92+
var externalLib = __webpack_require__(0);
93+
externalLib.doSomething("");
94+
var message = "Hello from submodule";
95+
module.exports = message;
96+
97+
98+
/***/ })
99+
/******/ ]);

0 commit comments

Comments
 (0)