提交 e0edf62c 编写于 作者: D Devon Govett

Merge branch 'next-terser-transformer' of github.com:parcel-bundler/parcel...

Merge branch 'next-terser-transformer' of github.com:parcel-bundler/parcel into next-terser-transformer

# Conflicts:
#	packages/transforms/typescript/src/TypescriptAsset.js
const {Asset} = require('@parcel/core');
const localRequire = require('@parcel/utils/localRequire');
class TypeScriptAsset extends Asset {
constructor(name, options) {
super(name, options);
this.type = 'js';
}
async generate() {
// require typescript, installed locally in the app
let typescript = await localRequire('typescript', this.name);
let transpilerOptions = {
compilerOptions: {
module: typescript.ModuleKind.CommonJS,
jsx: typescript.JsxEmit.Preserve,
// it brings the generated output from TypeScript closer to that generated by Babel
// see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html
esModuleInterop: true
},
fileName: this.relativeName
};
let tsconfig = await this.getConfig(['tsconfig.json']);
// Overwrite default if config is found
if (tsconfig) {
transpilerOptions.compilerOptions = Object.assign(
transpilerOptions.compilerOptions,
tsconfig.compilerOptions
);
}
transpilerOptions.compilerOptions.noEmit = false;
transpilerOptions.compilerOptions.sourceMap = this.options.sourceMaps;
// Transpile Module using TypeScript and parse result as ast format through babylon
let transpiled = typescript.transpileModule(
this.contents,
transpilerOptions
);
let sourceMap = transpiled.sourceMapText;
if (sourceMap) {
sourceMap = JSON.parse(sourceMap);
sourceMap.sources = [this.relativeName];
sourceMap.sourcesContent = [this.contents];
// Remove the source map URL
let content = transpiled.outputText;
transpiled.outputText = content.substring(
0,
content.lastIndexOf('//# sourceMappingURL')
);
}
return [
{
type: 'js',
value: transpiled.outputText,
sourceMap
}
];
}
}
module.exports = TypeScriptAsset;
const localRequire = require('@parcel/utils/localRequire');
exports.generate = async function(module, options) {
let typescript = await localRequire('typescript', module.name);
let transpilerOptions = {
compilerOptions: {
module: typescript.ModuleKind.CommonJS,
jsx: typescript.JsxEmit.Preserve,
// it brings the generated output from TypeScript closer to that generated by Babel
// see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html
esModuleInterop: true
},
fileName: module.relativeName
};
// Overwrite default if config is found
// let tsconfig = await this.getConfig(['tsconfig.json']);
/*if (tsconfig) {
transpilerOptions.compilerOptions = Object.assign(
transpilerOptions.compilerOptions,
tsconfig.compilerOptions
);
}*/
transpilerOptions.compilerOptions.noEmit = false;
transpilerOptions.compilerOptions.sourceMap = options.sourceMaps;
let transpiled = typescript.transpileModule(
module.code,
transpilerOptions
);
let sourceMap = transpiled.sourceMapText;
let code = transpiled.outputText;
if (sourceMap) {
sourceMap = JSON.parse(sourceMap);
sourceMap.sources = [module.relativeName];
sourceMap.sourcesContent = [module.code];
// Remove the source map URL
code = code.substring(
0,
code.lastIndexOf('//# sourceMappingURL')
);
}
return {
map: sourceMap || null,
code: code
}
}
const assert = require('assert');
const fs = require('@parcel/fs');
const {bundle, run, assertBundleTree} = require('@parcel/test-utils');
describe('typescript', function() {
it('should produce a ts bundle using ES6 imports', async function() {
let b = await bundle(__dirname + '/fixtures/typescript/index.ts');
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should produce a ts bundle using commonJS require', async function() {
let b = await bundle(
__dirname + '/fixtures/typescript-require/index.ts'
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should support json require', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-json/index.ts');
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should support env variables', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-env/index.ts');
assert.equal(b.assets.size, 1);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.env, 'function');
assert.equal(output.env(), 'test');
});
it('should support importing a URL to a raw asset', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-raw/index.ts');
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.ts', 'test.txt'],
childBundles: [
{
type: 'map'
},
{
type: 'txt',
assets: ['test.txt'],
childBundles: []
}
]
});
let output = await run(b);
assert.equal(typeof output.getRaw, 'function');
assert(/^\/test\.[0-9a-f]+\.txt$/.test(output.getRaw()));
assert(await fs.exists(__dirname + '/dist/' + output.getRaw()));
});
it('should minify in production mode', async function() {
let b = await bundle(
__dirname + '/fixtures/typescript-require/index.ts',
{production: true}
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
let js = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(!js.includes('local.a'));
});
it('should support loading tsconfig.json', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-config/index.ts');
let output = await run(b);
assert.equal(output, 2);
let js = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(!js.includes('/* test comment */'));
});
it('should support compiling JSX', async function() {
await bundle(__dirname + '/fixtures/typescript-jsx/index.tsx');
let file = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(file.includes('React.createElement("div"'));
});
it('should use esModuleInterop by default', async function() {
let b = await bundle(
__dirname + '/fixtures/typescript-interop/index.ts'
);
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.ts', 'commonjs-module.js'],
childBundles: [
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output.test, 'function');
assert.equal(output.test(), 'test passed');
});
});
const assert = require('assert');
const fs = require('@parcel/fs');
const {bundle, run, assertBundleTree} = require('@parcel/test-utils');
const Path = require('path');
const typescriptTransformer = require('../src/typescript-transformer');
describe('typescript', function() {
it('should produce a ts bundle using ES6 imports', async function() {
let b = await bundle(__dirname + '/fixtures/typescript/index.ts');
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should produce a ts bundle using commonJS require', async function() {
let b = await bundle(
__dirname + '/fixtures/typescript-require/index.ts'
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should support json require', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-json/index.ts');
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should support env variables', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-env/index.ts');
assert.equal(b.assets.size, 1);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.env, 'function');
assert.equal(output.env(), 'test');
});
it('should support importing a URL to a raw asset', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-raw/index.ts');
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.ts', 'test.txt'],
childBundles: [
{
type: 'map'
},
{
type: 'txt',
assets: ['test.txt'],
childBundles: []
it('should process a typescript module', async function() {
let dummyModule = {
code: `
function helloworld(count: number) {
return 1 + count;
}
]
});
let output = await run(b);
assert.equal(typeof output.getRaw, 'function');
assert(/^\/test\.[0-9a-f]+\.txt$/.test(output.getRaw()));
assert(await fs.exists(__dirname + '/dist/' + output.getRaw()));
});
it('should minify in production mode', async function() {
let b = await bundle(
__dirname + '/fixtures/typescript-require/index.ts',
{production: true}
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
let js = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(!js.includes('local.a'));
});
it('should support loading tsconfig.json', async function() {
let b = await bundle(__dirname + '/fixtures/typescript-config/index.ts');
let output = await run(b);
assert.equal(output, 2);
let js = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(!js.includes('/* test comment */'));
});
it('should support compiling JSX', async function() {
await bundle(__dirname + '/fixtures/typescript-jsx/index.tsx');
let file = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(file.includes('React.createElement("div"'));
});
it('should use esModuleInterop by default', async function() {
let b = await bundle(
__dirname + '/fixtures/typescript-interop/index.ts'
);
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.ts', 'commonjs-module.js'],
childBundles: [
{
type: 'map'
}
]
});
console.log(helloworld(5));
`,
name: Path.join(__dirname, 'index.ts'),
relativeName: './index.ts'
};
let output = await run(b);
assert.equal(typeof output.test, 'function');
assert.equal(output.test(), 'test passed');
let result = await typescriptTransformer.generate(dummyModule, {});
assert(!result.code.includes('number'));
assert(result.map === null);
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册