@rspack/core 0.0.0-20220909025136
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +1 -0
- package/bin.js +1 -0
- package/dist/bin/index.d.ts +1 -0
- package/dist/bin/index.js +32 -0
- package/dist/build.d.ts +1 -0
- package/dist/build.js +12 -0
- package/dist/config.d.ts +51 -0
- package/dist/config.js +26 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +229 -0
- package/dist/server/index.d.ts +0 -0
- package/dist/server/index.js +0 -0
- package/example/basic.ts +71 -0
- package/example/react-example/index.html +12 -0
- package/example/react-example/package.json +17 -0
- package/example/react-example/rspack.config.json +13 -0
- package/example/react-example/src/app.jsx +44 -0
- package/example/react-example/src/base.css +9 -0
- package/example/react-example/src/button.jsx +3 -0
- package/example/react-example/src/dark.svg +1 -0
- package/example/react-example/src/data.json +5 -0
- package/example/react-example/src/file.jpg +0 -0
- package/example/react-example/src/file.png +0 -0
- package/example/react-example/src/file.svg +1 -0
- package/example/react-example/src/foo.css +3 -0
- package/example/react-example/src/index.html +32 -0
- package/example/react-example/src/index.js +1 -0
- package/example/react-example/src/light.svg +48 -0
- package/example/react-example/src/logo.svg +18 -0
- package/example/react-with-sass.ts +28 -0
- package/lib/bin/index.d.ts +1 -0
- package/lib/bin/index.js +32 -0
- package/lib/build.d.ts +1 -0
- package/lib/build.js +12 -0
- package/lib/config/builtins.d.ts +3 -0
- package/lib/config/builtins.js +2 -0
- package/lib/config/context.d.ts +2 -0
- package/lib/config/context.js +2 -0
- package/lib/config/define.d.ts +2 -0
- package/lib/config/define.js +2 -0
- package/lib/config/dev.d.ts +17 -0
- package/lib/config/dev.js +17 -0
- package/lib/config/entry.d.ts +2 -0
- package/lib/config/entry.js +2 -0
- package/lib/config/external.d.ts +2 -0
- package/lib/config/external.js +2 -0
- package/lib/config/index.d.ts +45 -0
- package/lib/config/index.js +37 -0
- package/lib/config/mode.d.ts +2 -0
- package/lib/config/mode.js +2 -0
- package/lib/config/module.d.ts +64 -0
- package/lib/config/module.js +121 -0
- package/lib/config/output.d.ts +17 -0
- package/lib/config/output.js +14 -0
- package/lib/config/plugin.d.ts +5 -0
- package/lib/config/plugin.js +2 -0
- package/lib/config/resolve.d.ts +6 -0
- package/lib/config/resolve.js +2 -0
- package/lib/config/target.d.ts +5 -0
- package/lib/config/target.js +13 -0
- package/lib/index.d.ts +39 -0
- package/lib/index.js +154 -0
- package/lib/server/index.d.ts +0 -0
- package/lib/server/index.js +0 -0
- package/package.json +49 -0
- package/src/bin/index.ts +36 -0
- package/src/build.ts +8 -0
- package/src/config/builtins.ts +5 -0
- package/src/config/context.ts +3 -0
- package/src/config/define.ts +3 -0
- package/src/config/dev.ts +32 -0
- package/src/config/entry.ts +3 -0
- package/src/config/external.ts +3 -0
- package/src/config/index.ts +80 -0
- package/src/config/mode.ts +2 -0
- package/src/config/module.ts +240 -0
- package/src/config/output.ts +29 -0
- package/src/config/plugin.ts +6 -0
- package/src/config/resolve.ts +6 -0
- package/src/config/target.ts +29 -0
- package/src/index.ts +140 -0
- package/src/server/index.ts +0 -0
- package/tests/config.test.ts +40 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface Output {
|
|
2
|
+
path?: string;
|
|
3
|
+
publicPath?: string;
|
|
4
|
+
assetModuleFilename?: string;
|
|
5
|
+
filename?: string;
|
|
6
|
+
chunkFilename?: string;
|
|
7
|
+
uniqueName?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// TODO: fix it
|
|
11
|
+
export interface ResolvedOutput {
|
|
12
|
+
path?: string;
|
|
13
|
+
publicPath?: string;
|
|
14
|
+
assetModuleFilename?: string;
|
|
15
|
+
filename?: string;
|
|
16
|
+
chunkFilename?: string;
|
|
17
|
+
uniqueName?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resolveOutputOptions(output: Output = {}): ResolvedOutput {
|
|
21
|
+
return {
|
|
22
|
+
path: output.path,
|
|
23
|
+
publicPath: output.publicPath,
|
|
24
|
+
chunkFilename: output.chunkFilename,
|
|
25
|
+
filename: output.publicPath,
|
|
26
|
+
assetModuleFilename: output.assetModuleFilename,
|
|
27
|
+
uniqueName: output.uniqueName
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
type TargetItem =
|
|
2
|
+
| "web"
|
|
3
|
+
| "webworker"
|
|
4
|
+
| "node"
|
|
5
|
+
| "browserslist"
|
|
6
|
+
| "es3"
|
|
7
|
+
| "es5"
|
|
8
|
+
| "es2015"
|
|
9
|
+
| "es2016"
|
|
10
|
+
| "es2017"
|
|
11
|
+
| "es2018"
|
|
12
|
+
| "es2019"
|
|
13
|
+
| "es2020"
|
|
14
|
+
| "es2021"
|
|
15
|
+
| "es2022";
|
|
16
|
+
|
|
17
|
+
export type Target = TargetItem | TargetItem[] | false;
|
|
18
|
+
export type ResolvedTarget = TargetItem[];
|
|
19
|
+
|
|
20
|
+
export function resolveTargetOptions(target: Target = "web"): ResolvedTarget {
|
|
21
|
+
if (!target) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
if (!Array.isArray(target)) {
|
|
25
|
+
return [target];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return target;
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
export * from "./build";
|
|
2
|
+
import * as binding from "@rspack/binding";
|
|
3
|
+
import type { ExternalObject, RspackInternal } from "@rspack/binding";
|
|
4
|
+
import * as tapable from "tapable";
|
|
5
|
+
import {
|
|
6
|
+
RspackOptions,
|
|
7
|
+
ResolvedRspackOptions,
|
|
8
|
+
Assets,
|
|
9
|
+
Asset,
|
|
10
|
+
resolveOptions
|
|
11
|
+
} from "./config";
|
|
12
|
+
|
|
13
|
+
import { RawSource, Source } from "webpack-sources";
|
|
14
|
+
interface RspackThreadsafeContext<T> {
|
|
15
|
+
readonly id: number;
|
|
16
|
+
readonly inner: T;
|
|
17
|
+
}
|
|
18
|
+
interface RspackThreadsafeResult<T> {
|
|
19
|
+
readonly id: number;
|
|
20
|
+
readonly inner: T;
|
|
21
|
+
}
|
|
22
|
+
const createDummyResult = (id: number): string => {
|
|
23
|
+
const result: RspackThreadsafeResult<null> = {
|
|
24
|
+
id,
|
|
25
|
+
inner: null
|
|
26
|
+
};
|
|
27
|
+
return JSON.stringify(result);
|
|
28
|
+
};
|
|
29
|
+
type EmitAssetCallback = (options: { filename: string; asset: Asset }) => void;
|
|
30
|
+
class RspackCompilation {
|
|
31
|
+
#emitAssetCallback: EmitAssetCallback;
|
|
32
|
+
hooks: {
|
|
33
|
+
processAssets: tapable.AsyncSeriesHook<Record<string, Source>>;
|
|
34
|
+
};
|
|
35
|
+
constructor() {
|
|
36
|
+
this.hooks = {
|
|
37
|
+
processAssets: new tapable.AsyncSeriesHook<Record<string, Source>>([
|
|
38
|
+
"assets"
|
|
39
|
+
])
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* unsafe to call out of processAssets
|
|
44
|
+
* @param filename
|
|
45
|
+
* @param asset
|
|
46
|
+
*/
|
|
47
|
+
updateAsset(filename: string, asset: Asset) {
|
|
48
|
+
this.emitAsset(filename, asset);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* unsafe to call out of processAssets
|
|
52
|
+
* @param filename
|
|
53
|
+
* @param asset
|
|
54
|
+
*/
|
|
55
|
+
emitAsset(filename: string, asset: Asset) {
|
|
56
|
+
if (!this.#emitAssetCallback) {
|
|
57
|
+
throw new Error("can't call emitAsset outof processAssets hook for now");
|
|
58
|
+
}
|
|
59
|
+
this.#emitAssetCallback({
|
|
60
|
+
filename: filename,
|
|
61
|
+
asset
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async processAssets(err: Error, value: string, emitAsset: any) {
|
|
65
|
+
this.#emitAssetCallback = emitAsset;
|
|
66
|
+
if (err) {
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
const context: RspackThreadsafeContext<
|
|
70
|
+
Record<string, { source: string | Buffer }>
|
|
71
|
+
> = JSON.parse(value);
|
|
72
|
+
let content: Record<string, { source: string | Buffer }> =
|
|
73
|
+
context.inner ?? {};
|
|
74
|
+
let assets = {};
|
|
75
|
+
for (const [key, value] of Object.entries(content)) {
|
|
76
|
+
// webpack-sources's type definition is wrong, it actually could accept Buffer type
|
|
77
|
+
let source = value.source;
|
|
78
|
+
if (Array.isArray(value.source)) {
|
|
79
|
+
source = Buffer.from(value.source);
|
|
80
|
+
}
|
|
81
|
+
assets[key] = new RawSource(source as string);
|
|
82
|
+
}
|
|
83
|
+
await this.hooks.processAssets.promise(assets);
|
|
84
|
+
return createDummyResult(context.id);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
class Rspack {
|
|
88
|
+
#plugins: RspackOptions["plugins"];
|
|
89
|
+
#instance: ExternalObject<RspackInternal>;
|
|
90
|
+
compilation: RspackCompilation;
|
|
91
|
+
hooks: {
|
|
92
|
+
done: tapable.AsyncSeriesHook<void>;
|
|
93
|
+
compilation: tapable.SyncHook<RspackCompilation>;
|
|
94
|
+
};
|
|
95
|
+
options: ResolvedRspackOptions;
|
|
96
|
+
constructor(options: RspackOptions) {
|
|
97
|
+
this.options = resolveOptions(options);
|
|
98
|
+
// @ts-ignored
|
|
99
|
+
this.#instance = binding.newRspack(this.options, {
|
|
100
|
+
doneCallback: this.#done.bind(this),
|
|
101
|
+
processAssetsCallback: this.#processAssets.bind(this)
|
|
102
|
+
});
|
|
103
|
+
this.hooks = {
|
|
104
|
+
done: new tapable.AsyncSeriesHook<void>(),
|
|
105
|
+
compilation: new tapable.SyncHook<RspackCompilation>(["compilation"])
|
|
106
|
+
};
|
|
107
|
+
this.#plugins = options.plugins ?? [];
|
|
108
|
+
for (const plugin of this.#plugins) {
|
|
109
|
+
plugin.apply(this);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async #done(err: Error, value: string) {
|
|
113
|
+
if (err) {
|
|
114
|
+
throw err;
|
|
115
|
+
}
|
|
116
|
+
const context: RspackThreadsafeContext<void> = JSON.parse(value);
|
|
117
|
+
await this.hooks.done.promise();
|
|
118
|
+
return createDummyResult(context.id);
|
|
119
|
+
}
|
|
120
|
+
async #processAssets(err: Error, value: string, emitAsset: any) {
|
|
121
|
+
return this.compilation.processAssets(err, value, emitAsset);
|
|
122
|
+
}
|
|
123
|
+
#newCompilation() {
|
|
124
|
+
const compilation = new RspackCompilation();
|
|
125
|
+
this.compilation = compilation;
|
|
126
|
+
this.hooks.compilation.call(compilation);
|
|
127
|
+
return compilation;
|
|
128
|
+
}
|
|
129
|
+
async build() {
|
|
130
|
+
const compilation = this.#newCompilation();
|
|
131
|
+
const stats = await binding.build(this.#instance);
|
|
132
|
+
return stats;
|
|
133
|
+
}
|
|
134
|
+
async rebuild(changeFiles: string[]) {
|
|
135
|
+
const stats = await binding.rebuild(this.#instance, changeFiles);
|
|
136
|
+
return stats;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export { Rspack };
|
|
140
|
+
export default Rspack;
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { test } from "uvu";
|
|
2
|
+
import * as assert from "uvu/assert";
|
|
3
|
+
import { Rspack } from "@rspack/core";
|
|
4
|
+
import path from "path";
|
|
5
|
+
|
|
6
|
+
test("default config snapshot", () => {
|
|
7
|
+
const resolvedOptions = new Rspack({}).options;
|
|
8
|
+
|
|
9
|
+
assert.equal(resolvedOptions.context, process.cwd());
|
|
10
|
+
assert.equal(
|
|
11
|
+
resolvedOptions.dev.static.directory,
|
|
12
|
+
path.resolve(process.cwd(), "./dist")
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
// TypeScript will throw `The operand of a 'delete' operator must be optional`.
|
|
16
|
+
// But we remove these configurations with absolute paths.
|
|
17
|
+
// @ts-expect-error
|
|
18
|
+
delete resolvedOptions.context;
|
|
19
|
+
// @ts-expect-error
|
|
20
|
+
delete resolvedOptions.dev.static.directory;
|
|
21
|
+
|
|
22
|
+
assert.snapshot(
|
|
23
|
+
JSON.stringify(resolvedOptions),
|
|
24
|
+
JSON.stringify({
|
|
25
|
+
mode: "development",
|
|
26
|
+
dev: { port: 8080, static: {} },
|
|
27
|
+
entry: {},
|
|
28
|
+
output: {},
|
|
29
|
+
define: {},
|
|
30
|
+
target: ["web"],
|
|
31
|
+
external: {},
|
|
32
|
+
plugins: [],
|
|
33
|
+
builtins: [],
|
|
34
|
+
module: { rules: [] },
|
|
35
|
+
resolve: {}
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test.run();
|
package/tsconfig.json
ADDED