-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(loader): implement tsconfig compatible loader
- Loading branch information
1 parent
e89ca14
commit 8c1cd85
Showing
21 changed files
with
668 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+105 Bytes
(160%)
packages/integrate/__tests__/react-pragma/__snapshots__/react-pragma.spec.ts.snap
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# `@swc-node/loader` | ||
|
||
> 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn) | ||
<a href="https://npmcharts.com/compare/@swc-node/loader?minimal=true"><img src="https://img.shields.io/npm/dm/@swc-node/loader.svg?sanitize=true" alt="Downloads" /></a> | ||
|
||
## Usage | ||
|
||
```js | ||
{ | ||
test: /\.tsx?$/, | ||
use: [ | ||
{ | ||
loader: '@swc-node/loader', | ||
// If options not passed | ||
// `@swc-node/loader` will read the project `tsconfig.json` as compile options | ||
// If the default `tsconfig.json` parse failed or not existed | ||
// The default options will be used | ||
// `compilerOptions` is the same with `compilerOptions in tsconfig` | ||
options: { | ||
// if `compilerOptions` provided, `configFile` will be ignored | ||
compilerOptions: { | ||
target: 'ES5', | ||
module: 'esnext', | ||
sourceMap: true, | ||
jsx: true, | ||
}, | ||
// absolute path for tsconfig.json | ||
configFile: path.join(process.cwd(), 'tsconfig.build.json'), | ||
// enable react fast refresh | ||
fastRefresh: true | ||
} | ||
} | ||
], | ||
exclude: /node_modules/, | ||
} | ||
``` | ||
|
||
## Differences between [swc-loader](https://github.com/swc-project/swc-loader) | ||
|
||
This `loader` is compatible with `tsconfig.json` and `compilerOptions` in `tsconfig.json`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib').loader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"name": "@swc-node/loader", | ||
"version": "1.0.0", | ||
"description": "Webpack loader powered by swc", | ||
"keywords": [ | ||
"swc", | ||
"babel", | ||
"loader", | ||
"ts-loader", | ||
"babel-loader", | ||
"swc-loader", | ||
"napi-rs", | ||
"napi", | ||
"node-api", | ||
"tsc" | ||
], | ||
"author": "LongYinan <longyinan.brooklyn@bytedance.com>", | ||
"homepage": "https://github.com/Brooooooklyn/swc-node", | ||
"license": "MIT", | ||
"main": "./index.js", | ||
"files": ["lib", "LICENSE"], | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@swc-node/core": "^1.4.0", | ||
"@swc-node/register": "^1.2.1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Brooooooklyn/swc-node.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Brooooooklyn/swc-node/issues" | ||
}, | ||
"funding": { | ||
"type": "github", | ||
"url": "https://github.com/sponsors/Brooooooklyn" | ||
}, | ||
"devDependencies": { | ||
"@types/webpack": "^5.28.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { transform } from '@swc-node/core' | ||
import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from '@swc-node/register/read-default-tsconfig' | ||
import { CompilerOptions, convertCompilerOptionsFromJson } from 'typescript' | ||
|
||
import type { LoaderContext } from 'webpack' | ||
|
||
export function loader( | ||
this: LoaderContext<{ | ||
compilerOptions?: CompilerOptions | ||
configFile?: string | ||
fastRefresh?: boolean | ||
}>, | ||
source: string, | ||
) { | ||
const callback = this.async() | ||
const { compilerOptions, configFile, fastRefresh } = this.getOptions() ?? {} | ||
const options = convertCompilerOptionsFromJson(compilerOptions, '').options ?? readDefaultTsConfig(configFile) | ||
const swcOptions = tsCompilerOptionsToSwcConfig(options, this.resourcePath) | ||
if (fastRefresh) { | ||
if (swcOptions.react) { | ||
swcOptions.react.refresh = true | ||
} else { | ||
swcOptions.react = { | ||
refresh: true, | ||
} | ||
} | ||
} | ||
transform(source, this.resourcePath, swcOptions) | ||
.then(({ code, map }) => callback(null, code, map)) | ||
.catch((err) => callback(err)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"composite": true, | ||
"rootDir": "./src", | ||
"outDir": "./lib" | ||
}, | ||
"include": ["."], | ||
"exclude": ["lib"], | ||
"references": [ | ||
{ "path": "../core" }, | ||
{ "path": "../register" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": [], | ||
"files": ["src/index.ts"], | ||
"references": [ | ||
{ "path": "../core/tsconfig.project.json" }, | ||
{ "path": "../register/tsconfig.project.json" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.