@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.
Files changed (85) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README.md +1 -0
  3. package/bin.js +1 -0
  4. package/dist/bin/index.d.ts +1 -0
  5. package/dist/bin/index.js +32 -0
  6. package/dist/build.d.ts +1 -0
  7. package/dist/build.js +12 -0
  8. package/dist/config.d.ts +51 -0
  9. package/dist/config.js +26 -0
  10. package/dist/index.d.ts +73 -0
  11. package/dist/index.js +229 -0
  12. package/dist/server/index.d.ts +0 -0
  13. package/dist/server/index.js +0 -0
  14. package/example/basic.ts +71 -0
  15. package/example/react-example/index.html +12 -0
  16. package/example/react-example/package.json +17 -0
  17. package/example/react-example/rspack.config.json +13 -0
  18. package/example/react-example/src/app.jsx +44 -0
  19. package/example/react-example/src/base.css +9 -0
  20. package/example/react-example/src/button.jsx +3 -0
  21. package/example/react-example/src/dark.svg +1 -0
  22. package/example/react-example/src/data.json +5 -0
  23. package/example/react-example/src/file.jpg +0 -0
  24. package/example/react-example/src/file.png +0 -0
  25. package/example/react-example/src/file.svg +1 -0
  26. package/example/react-example/src/foo.css +3 -0
  27. package/example/react-example/src/index.html +32 -0
  28. package/example/react-example/src/index.js +1 -0
  29. package/example/react-example/src/light.svg +48 -0
  30. package/example/react-example/src/logo.svg +18 -0
  31. package/example/react-with-sass.ts +28 -0
  32. package/lib/bin/index.d.ts +1 -0
  33. package/lib/bin/index.js +32 -0
  34. package/lib/build.d.ts +1 -0
  35. package/lib/build.js +12 -0
  36. package/lib/config/builtins.d.ts +3 -0
  37. package/lib/config/builtins.js +2 -0
  38. package/lib/config/context.d.ts +2 -0
  39. package/lib/config/context.js +2 -0
  40. package/lib/config/define.d.ts +2 -0
  41. package/lib/config/define.js +2 -0
  42. package/lib/config/dev.d.ts +17 -0
  43. package/lib/config/dev.js +17 -0
  44. package/lib/config/entry.d.ts +2 -0
  45. package/lib/config/entry.js +2 -0
  46. package/lib/config/external.d.ts +2 -0
  47. package/lib/config/external.js +2 -0
  48. package/lib/config/index.d.ts +45 -0
  49. package/lib/config/index.js +37 -0
  50. package/lib/config/mode.d.ts +2 -0
  51. package/lib/config/mode.js +2 -0
  52. package/lib/config/module.d.ts +64 -0
  53. package/lib/config/module.js +121 -0
  54. package/lib/config/output.d.ts +17 -0
  55. package/lib/config/output.js +14 -0
  56. package/lib/config/plugin.d.ts +5 -0
  57. package/lib/config/plugin.js +2 -0
  58. package/lib/config/resolve.d.ts +6 -0
  59. package/lib/config/resolve.js +2 -0
  60. package/lib/config/target.d.ts +5 -0
  61. package/lib/config/target.js +13 -0
  62. package/lib/index.d.ts +39 -0
  63. package/lib/index.js +154 -0
  64. package/lib/server/index.d.ts +0 -0
  65. package/lib/server/index.js +0 -0
  66. package/package.json +49 -0
  67. package/src/bin/index.ts +36 -0
  68. package/src/build.ts +8 -0
  69. package/src/config/builtins.ts +5 -0
  70. package/src/config/context.ts +3 -0
  71. package/src/config/define.ts +3 -0
  72. package/src/config/dev.ts +32 -0
  73. package/src/config/entry.ts +3 -0
  74. package/src/config/external.ts +3 -0
  75. package/src/config/index.ts +80 -0
  76. package/src/config/mode.ts +2 -0
  77. package/src/config/module.ts +240 -0
  78. package/src/config/output.ts +29 -0
  79. package/src/config/plugin.ts +6 -0
  80. package/src/config/resolve.ts +6 -0
  81. package/src/config/target.ts +29 -0
  82. package/src/index.ts +140 -0
  83. package/src/server/index.ts +0 -0
  84. package/tests/config.test.ts +40 -0
  85. 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,6 @@
1
+ import Rspack from "..";
2
+
3
+ export interface Plugin {
4
+ name: string;
5
+ apply(compiler: Rspack): void;
6
+ }
@@ -0,0 +1,6 @@
1
+ export type Resolve = {
2
+ preferRelative?: boolean;
3
+ };
4
+ export type ResolvedResolve = {
5
+ preferRelative?: boolean;
6
+ };
@@ -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
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "CommonJS",
4
+ "target": "ES2018",
5
+ "moduleResolution": "node",
6
+ "esModuleInterop": true,
7
+ "outDir": "lib",
8
+ "declaration": true
9
+ },
10
+ "include": ["src"],
11
+ "ts-node": {
12
+ "transpileOnly": true
13
+ }
14
+ }