Skip to content

Commit

Permalink
Clean up typescript and add tsconfig test
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Dec 8, 2017
1 parent acdda54 commit e2cf8a5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/core/parcel-bundler/src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Parser {
if (typeof parser === 'string') {
parser = this.extensions[extension] = require(parser);
}

return parser;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/assets/JSAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JSAsset extends Asset {
}

mightHaveDependencies() {
return IMPORT_RE.test(this.contents) || GLOBAL_RE.test(this.contents);
return !/.js$/.test(this.name) || IMPORT_RE.test(this.contents) || GLOBAL_RE.test(this.contents);
}

async parse(code) {
Expand Down
16 changes: 5 additions & 11 deletions packages/core/parcel-bundler/src/assets/TypeScriptAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@ const config = require('../utils/config');
const localRequire = require('../utils/localRequire');

class TypeScriptAsset extends JSAsset {
async transform() {
super.transform();

await this.parseIfNeeded();
this.isAstDirty = true;
}

async parse(code) {
// require typescript, installed locally in the app
let typescript = localRequire('typescript', this.name);

let transpilerOptions = {
compilerOptions: {
module: typescript.ModuleKind.CommonJS,
jsx: typescript.JsxEmit.Preserve
},
fileName: this.basename
}
};

let tsconfig = await config.load(this.name, ['tsconfig.json']);

// Overwrite default if config is found
if (tsconfig) transpilerOptions.compilerOptions = tsconfig.compilerOptions;
transpilerOptions.compilerOptions.noEmit = false;

// Transpile Module using TypeScript and parse result as ast format through babylon
return await super.parse(typescript.transpileModule(code, transpilerOptions).outputText);
this.contents = typescript.transpileModule(code, transpilerOptions).outputText;
return await super.parse(this.contents);
}
}

module.exports = TypeScriptAsset;
module.exports = TypeScriptAsset;
3 changes: 2 additions & 1 deletion packages/core/parcel-bundler/src/builtins/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require = (function (modules, cache, entry) {
if (previousRequire) {
return previousRequire(name, true);
}

var err = new Error('Cannot find module \'' + name + '\'');
err.code = 'MODULE_NOT_FOUND';
throw err;
Expand Down Expand Up @@ -57,7 +58,7 @@ require = (function (modules, cache, entry) {
newRequire.modules = modules;
newRequire.cache = cache;
newRequire.parent = previousRequire;

for (var i = 0; i < entry.length; i++) {
newRequire(entry[i]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* test comment */
module.exports = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"removeComments": true
}
}
14 changes: 12 additions & 2 deletions packages/core/parcel-bundler/test/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('typescript', function () {
assert(fs.existsSync(__dirname + '/dist/' + output.getRaw()));
});

/*it('should minify in production mode', async function () {
it('should minify in production mode', async function () {
let b = await bundle(__dirname + '/integration/typescript-require/index.ts', { production: true });

assert.equal(b.assets.size, 2);
Expand All @@ -78,5 +78,15 @@ describe('typescript', function () {

let js = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(!js.includes('local.a'));
});*/
});

it('should support loading tsconfig.json', async function () {
let b = await bundle(__dirname + '/integration/typescript-config/index.ts');

let output = run(b);
assert.equal(output, 2);

let js = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(!js.includes('/* test comment */'));
});
});
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4524,6 +4524,10 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"

typescript@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4"

uglify-js@^2.6, uglify-js@^2.8.29:
version "2.8.29"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"
Expand Down

0 comments on commit e2cf8a5

Please sign in to comment.