Skip to content

Commit

Permalink
Merge pull request #140 from TypeStrong/dont-force-module
Browse files Browse the repository at this point in the history
Don't force module output when targeting ES6
  • Loading branch information
jbrantly committed Jan 20, 2016
2 parents 7b4e18f + 57732d1 commit 57c813e
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 14 deletions.
19 changes: 7 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
};

var compilerOptions: typescript.CompilerOptions = {
module: 1 /* CommonJS */
};

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

var libFileName = 'lib.d.ts';

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

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

if (!compilerOptions.noLib) {
filesToLoad.push(libFileName);
}

// Load initial files (core lib files, any files specified in tsconfig.json)
filesToLoad.forEach(filePath => {
filePath = path.normalize(filePath);
Expand Down Expand Up @@ -356,7 +351,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
},
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => compilerOptions,
getDefaultLibFileName: options => libFileName,
getDefaultLibFileName: options => compiler.getDefaultLibFilePath(options),
getNewLine: () => newLine,
log: log,
resolveModuleNames: (moduleNames: string[], containingFile: string) => {
Expand Down
1 change: 1 addition & 0 deletions test/es6withCJS/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'a';
6 changes: 6 additions & 0 deletions test/es6withCJS/expectedOutput-1.6/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

ERROR in ./.test/es6withCJS/app.ts
Module parse failed: index.js!app.ts Line 1: Unexpected token
You may need an appropriate loader to handle this file type.
| export default 'a';
|
51 changes: 51 additions & 0 deletions test/es6withCJS/expectedOutput-1.7/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/******/ (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] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = 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;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {

exports.default = 'a';


/***/ }
/******/ ]);
4 changes: 4 additions & 0 deletions test/es6withCJS/expectedOutput-1.7/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 1.41 kB 0 [emitted] main
chunk {0} bundle.js (main) 23 bytes [rendered]
[0] ./.test/es6withCJS/app.ts 23 bytes {0} [built]
53 changes: 53 additions & 0 deletions test/es6withCJS/expectedOutput-1.8/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/******/ (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] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = 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;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = 'a';


/***/ }
/******/ ]);
4 changes: 4 additions & 0 deletions test/es6withCJS/expectedOutput-1.8/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 1.49 kB 0 [emitted] main
chunk {0} bundle.js (main) 100 bytes [rendered]
[0] ./.test/es6withCJS/app.ts 100 bytes {0} [built]
6 changes: 6 additions & 0 deletions test/es6withCJS/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs"
}
}
17 changes: 17 additions & 0 deletions test/es6withCJS/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
entry: './app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.ts', '.js']
},
module: {
loaders: [
{ test: /\.ts$/, loader: 'ts-loader' }
]
}
}

// for test harness purposes only, you would not need this in a normal project
module.exports.resolveLoader = { alias: { 'ts-loader': require('path').join(__dirname, "../../index.js") } }
3 changes: 1 addition & 2 deletions test/ignoreDiagnostics/expectedOutput-1.8/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ chunk {0} bundle.js (main) 247 bytes [rendered]
[0] ./.test/ignoreDiagnostics/app.ts 247 bytes {0} [built] [1 error]

ERROR in ./.test/ignoreDiagnostics/app.ts
(9,5): error TS2322: Type 'string' is not assignable to type 'Number'.
Property 'toFixed' is missing in type 'String'.
(9,5): error TS2322: Type 'string' is not assignable to type 'Number'.

0 comments on commit 57c813e

Please sign in to comment.