Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 98b0ddc

Browse files
committed
feat: support webpack4
1 parent 149ce3d commit 98b0ddc

File tree

10 files changed

+2180
-285
lines changed

10 files changed

+2180
-285
lines changed

appveyor.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
environment:
22
nodejs_version: "6"
33
matrix:
4-
- TYPESCRIPT: [email protected]
5-
- TYPESCRIPT: [email protected]
4+
- TYPESCRIPT: [email protected]
65
cache:
76
- "%LOCALAPPDATA%\\Yarn"
87
install:

lib/runtime.d.ts

-20
This file was deleted.

package.json

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"build": "rimraf dist && tsc --pretty",
1414
"lint": "tslint src/*.ts",
1515
"release": "standard-version",
16-
"test": "rimraf .test && mocha --timeout 30000 dist/__test__",
16+
"test": "rimraf .test && mocha --trace-warnings --timeout 30000 dist/__test__",
1717
"precommit": "lint-staged"
1818
},
1919
"author": "Stanislav Panferov <[email protected]> (http://panferov.me/)",
@@ -34,29 +34,29 @@
3434
"homepage": "https://github.com/s-panferov/awesome-typescript-loader",
3535
"dependencies": {
3636
"chalk": "^2.3.1",
37-
"enhanced-resolve": "3.3.0",
37+
"enhanced-resolve": "^4.0.0",
3838
"loader-utils": "^1.1.0",
39-
"lodash": "^4.17.4",
40-
"micromatch": "^3.0.3",
39+
"lodash": "^4.17.5",
40+
"micromatch": "^3.1.9",
4141
"mkdirp": "^0.5.1",
4242
"source-map-support": "^0.5.3"
4343
},
4444
"peerDependencies": {
45-
"typescript": "^2"
45+
"typescript": "^2.7"
4646
},
4747
"devDependencies": {
4848
"@types/chai": "^4.0.1",
4949
"@types/lodash": "^4.14.67",
50-
"@types/micromatch": "^2.3.29",
50+
"@types/micromatch": "^3.1.0",
5151
"@types/mocha": "^2.2.41",
5252
"@types/node": "^8.0.5",
53-
"@types/shelljs": "^0.7.2",
53+
"@types/shelljs": "^0.7.8",
5454
"@types/sinon": "^2.3.2",
55-
"@types/webpack": "^3.0.1",
55+
"@types/webpack": "^4.0.0",
5656
"bluebird": "^3.5.0",
5757
"chai": "^4.0.2",
5858
"empty-module": "0.0.2",
59-
"fs-extra": "^3.0.1",
59+
"fs-extra": "^5.0.0",
6060
"mocha": "^3.4.2",
6161
"ps-node": "^0.1.6",
6262
"rimraf": "^2.6.1",
@@ -68,6 +68,7 @@
6868
"lint-staged": "^7.0.0",
6969
"husky": "^0.14.3",
7070
"typescript": "^2.7.2",
71-
"webpack": "^3.11.0"
71+
"webpack": "^4.1.0",
72+
"webpack-cli": "^2.0.10"
7273
}
7374
}

src/__test__/compile-output.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export function config(env) {
66
`
77
const path = require('path')
88
module.exports = {
9-
entry: { index: path.join(process.cwd(), '${env.SRC_DIR}', 'index.ts') },
9+
entry: { index: path.join(process.cwd(), '${env.SRC_DIR}', 'index.ts') },
10+
mode: 'development',
1011
output: {
1112
path: path.join(process.cwd(), '${env.OUT_DIR}'),
1213
filename: '[name].js'
@@ -15,7 +16,7 @@ export function config(env) {
1516
extensions: ['.ts', '.tsx', '.js', '.jsx'],
1617
},
1718
module: {
18-
loaders: [
19+
rules: [
1920
{
2021
test: /\.(tsx?|jsx?)/,
2122
loader: path.resolve(process.cwd(), '..', '..', 'index.js'),

src/__test__/utils.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import * as child from 'child_process'
55
import * as webpack from 'webpack'
66
import { exec as shellExec } from 'shelljs'
77

8+
process.on('unhandledRejection', e => {
9+
throw e
10+
})
11+
812
import { LoaderConfig } from '../interfaces'
913

1014
require('source-map-support').install()
@@ -32,8 +36,9 @@ export interface ConfigOptions {
3236
const TEST_DIR = path.join(process.cwd(), '.test')
3337
const SRC_DIR = './src'
3438
const OUT_DIR = './out'
39+
3540
const WEBPACK = path.join(
36-
path.dirname(path.dirname(require.resolve('webpack'))),
41+
path.dirname(path.dirname(require.resolve('webpack-cli'))),
3742
'bin',
3843
'webpack.js'
3944
)
@@ -50,13 +55,13 @@ export function entry(file: string) {
5055

5156
export function query(q: any) {
5257
return config => {
53-
_.merge(config.module.loaders.find(loader => loader.loader === LOADER).query, q)
58+
_.merge(config.module.rules.find(loader => loader.loader === LOADER).query, q)
5459
}
5560
}
5661

5762
export function include(...folders: string[]) {
5863
return config => {
59-
config.module.loaders.find(loader => loader.loader === LOADER).include.push(
64+
config.module.rules.find(loader => loader.loader === LOADER).include.push(
6065
...folders.map(f => {
6166
return path.join(process.cwd(), f)
6267
})
@@ -65,7 +70,8 @@ export function include(...folders: string[]) {
6570
}
6671

6772
export function webpackConfig(...enchance: any[]): webpack.Configuration {
68-
const config = {
73+
const config: webpack.Configuration = {
74+
mode: 'development',
6975
entry: { index: path.join(process.cwd(), SRC_DIR, 'index.ts') },
7076
output: {
7177
path: path.join(process.cwd(), OUT_DIR),
@@ -75,7 +81,7 @@ export function webpackConfig(...enchance: any[]): webpack.Configuration {
7581
extensions: ['.ts', '.tsx', '.js', '.jsx']
7682
},
7783
module: {
78-
loaders: [
84+
rules: [
7985
{
8086
test: /\.(tsx?|jsx?)/,
8187
loader: LOADER,
@@ -285,7 +291,7 @@ export function checkOutput(fileName: string, fragment: string) {
285291
process.exit()
286292
}
287293

288-
expect(source.replace(/\s/g, '')).include(fragment.replace(/\s/g, ''))
294+
expect(source.replace(/([\s\n]|\\n)/g, '')).include(fragment.replace(/([\s\n]|\\n)/g, ''))
289295
}
290296

291297
export function readOutput(fileName: string) {

src/checker/runtime.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
490490
if (loaderConfig.reportFiles) {
491491
filters.push(file => {
492492
const fileName = path.relative(context, file.fileName)
493-
return micromatch(fileName, loaderConfig.reportFiles).length > 0
493+
return micromatch([fileName], loaderConfig.reportFiles).length > 0
494494
})
495495
}
496496
const diagnosticsCollected: boolean[] = new Array(sourceFiles.length)

src/instance.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { CompilerInfo, LoaderConfig, TsConfig } from './interfaces'
88
import { WatchModeSymbol } from './watch-mode'
99
import { createHash } from 'crypto'
1010

11+
import { Compiler } from 'webpack'
12+
1113
import chalk from 'chalk'
1214

1315
let pkg = require('../package.json')
@@ -364,12 +366,10 @@ const filterMtimes = (mtimes: any) => {
364366
}
365367

366368
function setupWatchRun(compiler, instanceName: string) {
367-
compiler.hooks.watchRun.tap('at-loader', function (compiler, callback) {
369+
compiler.hooks.watchRun.tapAsync('at-loader', function(compiler, callback) {
368370
const instance = resolveInstance(compiler, instanceName)
369371
const checker = instance.checker
370-
const watcher =
371-
compiler.watchFileSystem.watcher ||
372-
compiler.watchFileSystem.wfs.watcher
372+
const watcher = compiler.watchFileSystem.watcher || compiler.watchFileSystem.wfs.watcher
373373

374374
const startTime = instance.startTime || compiler.startTime
375375
const times = filterMtimes(watcher.getTimes())
@@ -442,7 +442,7 @@ function isWatching(compiler: any): WatchMode {
442442
}
443443

444444
function setupAfterCompile(compiler, instanceName, forkChecker = false) {
445-
compiler.hooks.afterCompile.tap('at-loader', function (compilation, callback) {
445+
compiler.hooks.afterCompile.tapAsync('at-loader', function(compilation, callback) {
446446
// Don"t add errors for child compilations
447447
if (compilation.compiler.isChild()) {
448448
callback()

src/paths-plugin.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ResolverPlugin {
4141
}
4242

4343
export interface Resolver {
44+
hooks: any
4445
apply(plugin: ResolverPlugin): void
4546
plugin(source: string, cb: ResolverCallback)
4647
doResolve(target: string, req: Request, desc: string, Callback)
@@ -63,8 +64,6 @@ export interface PathPluginOptions {
6364
}
6465

6566
export class PathPlugin implements ResolverPlugin {
66-
source: string
67-
target: string
6867
ts: typeof ts
6968
configFilePath: string
7069
options: ts.CompilerOptions
@@ -74,9 +73,6 @@ export class PathPlugin implements ResolverPlugin {
7473
absoluteBaseUrl: string
7574

7675
constructor(config: LoaderConfig & ts.CompilerOptions & PathPluginOptions = {} as any) {
77-
this.source = 'described-resolve'
78-
this.target = 'resolve'
79-
8076
this.ts = setupTs(config.compiler).tsImpl
8177

8278
let context = config.context || process.cwd()
@@ -117,13 +113,16 @@ export class PathPlugin implements ResolverPlugin {
117113
let { baseUrl, mappings } = this
118114

119115
if (baseUrl) {
120-
resolver.apply(new ModulesInRootPlugin('module', this.absoluteBaseUrl, 'resolve'))
116+
new ModulesInRootPlugin('module', this.absoluteBaseUrl, 'resolve').apply(resolver)
121117
}
122118

123119
mappings.forEach(mapping => {
124120
// skip "phantom" type references
125121
if (!this.isTyping(mapping.target)) {
126-
resolver.plugin(this.source, this.createPlugin(resolver, mapping))
122+
resolver.hooks.describedResolve.tapAsync(
123+
'at-loader',
124+
this.createPlugin(resolver, mapping)
125+
)
127126
}
128127
})
129128
}
@@ -133,7 +132,7 @@ export class PathPlugin implements ResolverPlugin {
133132
}
134133

135134
createPlugin(resolver: Resolver, mapping: Mapping) {
136-
return (request, callback) => {
135+
return (request, info, callback) => {
137136
let innerRequest = getInnerRequest(resolver, request)
138137
if (!innerRequest) {
139138
return callback()
@@ -158,7 +157,7 @@ export class PathPlugin implements ResolverPlugin {
158157
}) as Request
159158

160159
return resolver.doResolve(
161-
this.target,
160+
'resolve',
162161
newRequest,
163162
"aliased with mapping '" +
164163
innerRequest +

src/watch-mode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ export const WatchModeSymbol = Symbol('WatchMode')
22

33
export class CheckerPlugin {
44
apply(compiler) {
5-
compiler.hooks.run.tap('at-loader', function (params, callback) {
5+
compiler.hooks.run.tapAsync('at-loader', function(params, callback) {
66
compiler[WatchModeSymbol] = false
77
callback()
88
})
99

10-
compiler.hooks.watchRun.tap('at-loader', function (params, callback) {
10+
compiler.hooks.watchRun.tapAsync('at-loader', function(params, callback) {
1111
compiler[WatchModeSymbol] = true
1212
callback()
1313
})

0 commit comments

Comments
 (0)