Skip to content

Commit 0b6d9dd

Browse files
committed
Add support for specifying target: es6 and module: commonjs at the same time.
Additionally, simplify logic for loading the default lib.
1 parent 7b4e18f commit 0b6d9dd

File tree

9 files changed

+149
-12
lines changed

9 files changed

+149
-12
lines changed

index.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
197197
};
198198

199199
var compilerOptions: typescript.CompilerOptions = {
200-
module: 1 /* CommonJS */
201200
};
202201

203202
// Load any available tsconfig.json file
@@ -275,14 +274,14 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
275274
instance.compilerOptions = objectAssign<typescript.CompilerOptions>(compilerOptions, configParseResult.options);
276275
filesToLoad = configParseResult.fileNames;
277276

278-
var libFileName = 'lib.d.ts';
279-
280-
// Special handling for ES6 targets
281-
if (compilerOptions.target == 2 /* ES6 */) {
277+
// if `module` is not specified and not using ES6 target, default to CJS module output
278+
if (compilerOptions.module == null && compilerOptions.target !== 2 /* ES6 */) {
279+
compilerOptions.module = 1 /* CommonJS */
280+
}
281+
// special handling for TS 1.6 and target: es6
282+
else if (compilerCompatible && semver.lt(compiler.version, '1.7.3-0') && compilerOptions.target == 2 /* ES6 */) {
282283
compilerOptions.module = 0 /* None */;
283-
libFileName = 'lib.es6.d.ts';
284284
}
285-
libFileName = path.join(path.dirname(require.resolve(loaderOptions.compiler)), libFileName);
286285

287286
if (loaderOptions.transpileOnly) {
288287
// quick return for transpiling
@@ -297,10 +296,6 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
297296
return { instance: instances[loaderOptions.instance] = { compiler, compilerOptions, loaderOptions, files }};
298297
}
299298

300-
if (!compilerOptions.noLib) {
301-
filesToLoad.push(libFileName);
302-
}
303-
304299
// Load initial files (core lib files, any files specified in tsconfig.json)
305300
filesToLoad.forEach(filePath => {
306301
filePath = path.normalize(filePath);
@@ -356,7 +351,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
356351
},
357352
getCurrentDirectory: () => process.cwd(),
358353
getCompilationSettings: () => compilerOptions,
359-
getDefaultLibFileName: options => libFileName,
354+
getDefaultLibFileName: options => compiler.getDefaultLibFilePath(options),
360355
getNewLine: () => newLine,
361356
log: log,
362357
resolveModuleNames: (moduleNames: string[], containingFile: string) => {

test/es6withCJS/app.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'a';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
ERROR in ./.test/es6withCJS/app.ts
3+
Module parse failed: index.js!app.ts Line 1: Unexpected token
4+
You may need an appropriate loader to handle this file type.
5+
| export default 'a';
6+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/******/ exports: {},
15+
/******/ id: moduleId,
16+
/******/ loaded: false
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.loaded = 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+
/******/ // __webpack_public_path__
37+
/******/ __webpack_require__.p = "";
38+
39+
/******/ // Load entry module and return exports
40+
/******/ return __webpack_require__(0);
41+
/******/ })
42+
/************************************************************************/
43+
/******/ ([
44+
/* 0 */
45+
/***/ function(module, exports) {
46+
47+
exports.default = 'a';
48+
49+
50+
/***/ }
51+
/******/ ]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 1.41 kB 0 [emitted] main
3+
chunk {0} bundle.js (main) 23 bytes [rendered]
4+
[0] ./.test/es6withCJS/app.ts 23 bytes {0} [built]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
/******/ exports: {},
15+
/******/ id: moduleId,
16+
/******/ loaded: false
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.loaded = 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+
/******/ // __webpack_public_path__
37+
/******/ __webpack_require__.p = "";
38+
39+
/******/ // Load entry module and return exports
40+
/******/ return __webpack_require__(0);
41+
/******/ })
42+
/************************************************************************/
43+
/******/ ([
44+
/* 0 */
45+
/***/ function(module, exports) {
46+
47+
"use strict";
48+
Object.defineProperty(exports, "__esModule", { value: true });
49+
exports.default = 'a';
50+
51+
52+
/***/ }
53+
/******/ ]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 1.49 kB 0 [emitted] main
3+
chunk {0} bundle.js (main) 100 bytes [rendered]
4+
[0] ./.test/es6withCJS/app.ts 100 bytes {0} [built]

test/es6withCJS/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs"
5+
}
6+
}

test/es6withCJS/webpack.config.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
entry: './app.ts',
3+
output: {
4+
filename: 'bundle.js'
5+
},
6+
resolve: {
7+
extensions: ['', '.ts', '.js']
8+
},
9+
module: {
10+
loaders: [
11+
{ test: /\.ts$/, loader: 'ts-loader' }
12+
]
13+
}
14+
}
15+
16+
// for test harness purposes only, you would not need this in a normal project
17+
module.exports.resolveLoader = { alias: { 'ts-loader': require('path').join(__dirname, "../../index.js") } }

0 commit comments

Comments
 (0)