Skip to content

Commit c110f0e

Browse files
committed
fix webpack-plugin
1 parent caea02d commit c110f0e

File tree

12 files changed

+1518
-90
lines changed

12 files changed

+1518
-90
lines changed

examples/webpack-demo/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "webpack-demo",
33
"private": true,
44
"scripts": {
5+
"start": "webpack serve --config webpack.dev.js",
56
"build": "webpack --config webpack.config.js"
67
},
78
"devDependencies": {
@@ -11,8 +12,10 @@
1112
"@babel/preset-typescript": "7.16.7",
1213
"@babel/runtime": "7.16.7",
1314
"babel-loader": "8.2.3",
15+
"html-webpack-plugin": "^5.5.0",
1416
"ts-paths-resolve-plugin": "../../packages/ts-paths-resolve-plugin",
1517
"webpack": "5.65.0",
16-
"webpack-cli": "4.9.1"
18+
"webpack-cli": "4.9.1",
19+
"webpack-dev-server": "^4.7.4"
1720
}
1821
}

examples/webpack-demo/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import { value } from "he"
22
console.log(value)
3+
import "./test"

examples/webpack-demo/src/test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { value } from "he"
2+
console.log(value)

examples/webpack-demo/src/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"allowSyntheticDefaultImports": true,
77
"esModuleInterop": true,
88
"paths": {
9-
"he": ["./hello"]
9+
"he": ["./hello"],
10+
"ro": ["./world"],
1011
}
1112
}
1213
}

examples/webpack-demo/src/world.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 100
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
const path = require("path")
22
const glob = require("glob")
33
const { TsPathsResolvePlugin } = require("ts-paths-resolve-plugin")
4-
4+
const HtmlWebpackPlugin = require("html-webpack-plugin")
55
const src = path.join(__dirname, "src")
6+
process.env.NODE_ENV = "development"
67

78
/** @type {import("webpack").Configuration} */
89
const config = {
9-
mode: "production",
10+
mode: "development",
11+
devtool: "inline-source-map",
1012
entry: glob.sync("index.*", { cwd: src }).map(i => path.join(src, i)),
1113
output: {
1214
path: path.join(__dirname, "build"),
1315
filename: "js/[name].js?[fullhash]",
1416
clean: true,
1517
},
16-
plugins: [
17-
new TsPathsResolvePlugin({ tsConfigPath: "src/tsconfig.json" }),
18-
],
1918
module: {
2019
rules: [{ test: /\.(j|t)s?/, loader: "babel-loader" }],
2120
},
21+
plugins: [new HtmlWebpackPlugin()],
22+
resolve: {
23+
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
24+
plugins: [new TsPathsResolvePlugin({ tsConfigPath: "src/tsconfig.json", logLevel: "debug" })],
25+
},
26+
devServer: {
27+
hot: true,
28+
open: false,
29+
historyApiFallback: true,
30+
},
2231
}
2332

2433
module.exports = config

packages/ts-paths-resolve-plugin/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Configurate in `webpack.config.js`:
1818
const { TsPathsResolvePlugin } = require('ts-paths-resolve-plugin');
1919

2020
module.exports = {
21-
plugins: [new TsPathsResolvePlugin()],
2221
resolve: {
2322
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
23+
plugins: [new TsPathsResolvePlugin()],
2424
}
2525
}
2626
```
+25-30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Resolver } from "enhanced-resolve"
12
import fs from "fs"
23
import ts from "typescript"
34
import { convertLogLevel, createHandler, createLogger, LogFunc, LogLevel, RegisterOptions } from "typescript-paths"
@@ -15,8 +16,8 @@ interface Request {
1516
descriptionFileRoot: string
1617
descriptionFileData: unknown
1718
relativePath: string
18-
context: {
19-
issuer: string
19+
context?: {
20+
issuer?: string
2021
}
2122
}
2223

@@ -37,36 +38,30 @@ export class TsPathsResolvePlugin {
3738
falllback: moduleName => fs.existsSync(moduleName),
3839
})
3940
}
40-
apply(compiler: Compiler) {
41-
compiler.resolverFactory.hooks.resolver.for("normal").tap(PLUGIN_NAME, resolver => {
42-
resolver.hooks.resolve.tapAsync(PLUGIN_NAME, (request, context, callback) => {
43-
const innerRequest = request.request || request.path
44-
if (!innerRequest || request.module === false) {
45-
return callback()
46-
}
47-
48-
const importer = (request as Request).context.issuer
49-
if (!importer) {
50-
return callback()
51-
}
52-
53-
const moduleName = this.handler?.(innerRequest, importer)
54-
if (!moduleName) {
55-
return callback()
56-
}
57-
58-
this.log(LogLevel.Debug, `${innerRequest} -> ${moduleName}`)
59-
60-
return resolver.doResolve(
61-
resolver.hooks.resolve,
62-
{ ...request, request: moduleName },
63-
"",
64-
context,
65-
callback,
66-
)
67-
})
41+
apply(c: Compiler | Resolver) {
42+
if (this.isRsolver(c)) {
43+
this.setup(c)
44+
return
45+
}
46+
c.resolverFactory.hooks.resolver.for("normal").tap(PLUGIN_NAME, this.setup.bind(this))
47+
}
48+
setup(resolver: Resolver) {
49+
const target = resolver.ensureHook("resolve")
50+
const hook = resolver.getHook("described-resolve")
51+
hook.tapAsync(PLUGIN_NAME, (request, resolveContext, callback) => {
52+
const innerRequest = request.request || request.path
53+
if (!innerRequest || !request.module) return callback()
54+
const importer = (request as Request).context?.issuer
55+
if (!importer) return callback()
56+
const moduleName = this.handler?.(innerRequest, importer)
57+
if (!moduleName) return callback()
58+
this.log(LogLevel.Debug, `${innerRequest} -> ${moduleName}`)
59+
return resolver.doResolve(target, { ...request, request: moduleName }, "", resolveContext, callback)
6860
})
6961
}
62+
isRsolver(obj: any): obj is Resolver {
63+
return typeof obj?.doResolve === "function"
64+
}
7065
}
7166

7267
export default TsPathsResolvePlugin

packages/ts-paths-resolve-plugin/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"webpack": "^5"
3232
},
3333
"devDependencies": {
34+
"enhanced-resolve": "^5.9.0",
3435
"webpack": "^5"
3536
},
3637
"keywords": [
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import type { Resolver } from "enhanced-resolve"
12
import { createHandler, LogFunc, RegisterOptions } from "typescript-paths"
23
import type { Compiler } from "webpack"
34
export interface PluginOptions extends Omit<RegisterOptions, "loggerID"> {}
45
export declare class TsPathsResolvePlugin {
56
handler: ReturnType<typeof createHandler>
67
log: LogFunc
78
constructor({ tsConfigPath, respectCoreModule, logLevel, colors }?: Partial<PluginOptions>)
8-
apply(compiler: Compiler): void
9+
apply(c: Compiler | Resolver): void
10+
setup(resolver: Resolver): void
11+
isRsolver(obj: any): obj is Resolver
912
}
1013
export default TsPathsResolvePlugin

packages/typescript-paths/types/paths.d.ts

-35
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,5 @@
11
import ts from "typescript"
22
import { LogFunc } from "./logger"
3-
export declare const coreModules: {
4-
assert: string
5-
buffer: string
6-
child_process: string
7-
console: string
8-
cluster: string
9-
crypto: string
10-
dgram: string
11-
dns: string
12-
events: string
13-
fs: string
14-
http: string
15-
http2: string
16-
https: string
17-
net: string
18-
os: string
19-
path: string
20-
perf_hooks: string
21-
process: string
22-
querystring: string
23-
readline: string
24-
repl: string
25-
stream: string
26-
string_decoder: string
27-
timers: string
28-
tls: string
29-
tty: string
30-
url: string
31-
util: string
32-
v8: string
33-
vm: string
34-
wasi: string
35-
worker: string
36-
zlib: string
37-
}
383
export interface Mapping {
394
pattern: string
405
prefix: string

0 commit comments

Comments
 (0)