Skip to content

Commit

Permalink
feat(dynamic-imports): support module: 'esnext'
Browse files Browse the repository at this point in the history
* tolerate ModuleKind.ESNext

As outlined in #208, this change just recognizes `ModuleKind.ESNext`, a superset of `ModuleKind.ES2015` introduced in TypeScript 2.4.0.

* npm:typescript: ^2.2.0 -> ^2.4.0

* ts-node: ^2.0.0 -> ^3.0.0 (fixes test runner)

* fix test failure on Windows

* Add a test for builder output under `"module": "esnext"`

* fixup test and add a test for bundling
  • Loading branch information
aluanhaddad authored and frankwallis committed Aug 11, 2017
1 parent ac10942 commit d184e47
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "https://github.com/frankwallis/plugin-typescript.git"
},
"peerDependencies": {
"typescript": "^2.0.0"
"typescript": "^2.4.0"
},
"devDependencies": {
"@types/chai": "^3.4.35",
Expand All @@ -44,9 +44,9 @@
"rollup": "^0.41.4",
"rollup-plugin-includepaths": "^0.2.1",
"sinon": "^1.17.7",
"ts-node": "^2.0.0",
"ts-node": "^3.0.0",
"tslib": "^1.4.0",
"typescript": "^2.2.0"
"typescript": "^2.4.0"
},
"scripts": {
"bundle": "tsc && rollup -c && tsc ./tmp/plugin.js -m system -lib es6 -allowJs -allowUnreachableCode -removeComments -outDir ./lib",
Expand All @@ -62,7 +62,7 @@
"registry": "github",
"name": "frankwallis/plugin-typescript",
"peerDependencies": {
"typescript": "npm:typescript@^2.0.0"
"typescript": "npm:typescript@^2.4.0"
},
"directories": {
"lib": "lib",
Expand Down
2 changes: 1 addition & 1 deletion src/resolve-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function getEnum(enumValue: any, enumType: any, defaultValue: number): number {
function validateOptions(options: CombinedOptions) {
/* The only time you don't want to output in 'm format is when you are using rollup or babel
downstream to compile es6 output (e.g. for async/await support) */
if ((options.module != ts.ModuleKind.System) && (options.module != ts.ModuleKind.ES2015)) {
if ((options.module !== ts.ModuleKind.System) && (options.module !== ts.ModuleKind.ES2015) && (options.module !== ts.ModuleKind.ESNext)) {
logger.warn(`transpiling to ${ts.ModuleKind[options.module]}, consider setting module: "system" in typescriptOptions to transpile directly to System.register format`)
}

Expand Down
34 changes: 32 additions & 2 deletions test/builder-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ chai.use(chaiAsPromised)
const should = chai.should()

describe('Builder', () => {
let builder = null
interface Builder {
builderStatic(build: string, options?: object): Promise<{source: string}>
reset(): void
bundle(bundleSpec: string, options?: object): Promise<{source: string}>
buildStatic(bundleSpec: string, options?: object): Promise<{source: string}>
config<T extends SystemJSLoader.Config | {transpiler}>(config?: T): void
}

let builder: Builder = null

beforeEach(() => {
global['tsHost'] = undefined
Expand All @@ -26,7 +34,7 @@ describe('Builder', () => {
"jsx": "react",
"noImplicitAny": false,
"tsconfig": false
} as any,
},
packages: {
"testsrc": {
"main": "index",
Expand Down Expand Up @@ -225,4 +233,26 @@ describe('Builder', () => {
result.source.indexOf('counter.elided += 1').should.be.equal(-1);
})
})

it('supports dynamic import when bundling to esnext modules', async () => {
const config = defaultConfig()
config.map["testsrc"] = "test/fixtures-es6/plugin/dynamic"
config.typescriptOptions.module = "esnext"
config.typescriptOptions.target = "es5"
builder.config(config)
const result = await builder.bundle('testsrc', {})
console.log(result.source)
result.source.should.contain('_context.import(\'')
})

it('supports dynamic import when building to esnext modules', async () => {
const config = defaultConfig()
config.map["testsrc"] = "test/fixtures-es6/plugin/dynamic"
config.typescriptOptions.module = "esnext"
config.typescriptOptions.target = "es5"
builder.config(config)
const result = await builder.buildStatic('testsrc', {})
console.log(result.source)
result.source.should.contain('_context.import(\'')
})
})
1 change: 1 addition & 0 deletions test/fixtures-es6/plugin/dynamic/dynamic-dependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1
3 changes: 3 additions & 0 deletions test/fixtures-es6/plugin/dynamic/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function () {
return import('./dynamic-dependency')
}
3 changes: 2 additions & 1 deletion test/plugin-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os = require('os')
import chai = require('chai')
import chaiAsPromised = require('chai-as-promised')
import * as ts from 'typescript'
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('Plugin', () => {
System.config(config)
const mod = await System.import('testsrc')
.should.be.fulfilled
mod.default.should.equal('<div>hello</div>\n')
mod.default.should.equal('<div>hello</div>' + os.EOL)
})

it('compiles js commonjs files', async () => {
Expand Down

0 comments on commit d184e47

Please sign in to comment.