@meteorjs/rspack 0.0.0 → 0.0.2

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/index.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Extend Rspack’s Configuration with Meteor-specific options.
3
+ */
4
+ import {
5
+ defineConfig as _rspackDefineConfig,
6
+ Configuration as _RspackConfig,
7
+ } from '@rspack/cli';
8
+
9
+ export interface MeteorRspackConfig extends _RspackConfig {
10
+ meteor?: {
11
+ packageNamespace?: string;
12
+ };
13
+ }
14
+
15
+ type MeteorEnv = Record<string, any> & {
16
+ isDevelopment: boolean;
17
+ isProduction: boolean;
18
+ isClient: boolean;
19
+ isServer: boolean;
20
+ isTest: boolean;
21
+ isDebug: boolean;
22
+ isRun: boolean;
23
+ isBuild: boolean;
24
+ isReactEnabled: boolean;
25
+ isBlazeEnabled: boolean;
26
+ isBlazeHotEnabled: boolean;
27
+ }
28
+
29
+ export type ConfigFactory = (
30
+ env: MeteorEnv,
31
+ argv: Record<string, any>
32
+ ) => MeteorRspackConfig;
33
+
34
+ export function defineConfig(
35
+ factory: ConfigFactory
36
+ ): ReturnType<typeof _rspackDefineConfig>;
package/index.js ADDED
@@ -0,0 +1,23 @@
1
+ import { defineConfig as rspackDefineConfig } from '@rspack/cli';
2
+
3
+ /**
4
+ * @typedef {import('rspack').Configuration & {
5
+ * meteor?: { packageNamespace?: string }
6
+ * }} MeteorRspackConfig
7
+ */
8
+
9
+ /**
10
+ * @typedef {(env: Record<string, any>, argv: Record<string, any>) => MeteorRspackConfig} ConfigFactory
11
+ */
12
+
13
+ /**
14
+ * Wrap rspack.defineConfig but only accept a factory function.
15
+ * @param {ConfigFactory} factory
16
+ * @returns {ReturnType<typeof rspackDefineConfig>}
17
+ */
18
+ export function defineConfig(factory) {
19
+ return rspackDefineConfig(factory);
20
+ }
21
+
22
+ // Export our helper plus passthrough
23
+ export default defineConfig;
package/lib/swc.js ADDED
@@ -0,0 +1,62 @@
1
+ import fs from 'fs';
2
+ import vm from 'vm';
3
+
4
+ /**
5
+ * Reads and parses the SWC configuration file.
6
+ * @param {string} file - The name of the SWC configuration file (default: '.swcrc')
7
+ * @returns {Object|undefined} The parsed SWC configuration or undefined if an error occurs
8
+ */
9
+ export function getMeteorAppSwcrc(file = '.swcrc') {
10
+ try {
11
+ const filePath = `${process.cwd()}/${file}`;
12
+ if (file.endsWith('.js')) {
13
+ let content = fs.readFileSync(filePath, 'utf-8');
14
+ // Check if the content uses ES module syntax (export default)
15
+ if (content.includes('export default')) {
16
+ // Transform ES module syntax to CommonJS
17
+ content = content.replace(/export\s+default\s+/, 'module.exports = ');
18
+ }
19
+ const script = new vm.Script(`
20
+ (function() {
21
+ const module = {};
22
+ module.exports = {};
23
+ (function(exports, module) {
24
+ ${content}
25
+ })(module.exports, module);
26
+ return module.exports;
27
+ })()
28
+ `);
29
+ const context = vm.createContext({ process });
30
+ return script.runInContext(context);
31
+ } else {
32
+ // For .swcrc and other JSON files, parse as JSON
33
+ return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
34
+ }
35
+ } catch (e) {
36
+ return undefined;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Checks for SWC configuration files and returns the configuration.
42
+ * If the configuration has a baseUrl property, it will be set to process.cwd().
43
+ * @returns {Object|undefined} The SWC configuration or undefined if no configuration exists
44
+ */
45
+ export function getMeteorAppSwcConfig() {
46
+ const hasSwcRc = fs.existsSync(`${process.cwd()}/.swcrc`);
47
+ const hasSwcJs = !hasSwcRc && fs.existsSync(`${process.cwd()}/swc.config.js`);
48
+
49
+ if (!hasSwcRc && !hasSwcJs) {
50
+ return undefined;
51
+ }
52
+
53
+ const swcFile = hasSwcJs ? 'swc.config.js' : '.swcrc';
54
+ const config = getMeteorAppSwcrc(swcFile);
55
+
56
+ // Set baseUrl to process.cwd() if it exists
57
+ if (config?.jsc && config.jsc.baseUrl) {
58
+ config.jsc.baseUrl = process.cwd();
59
+ }
60
+
61
+ return config;
62
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meteorjs/rspack",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "description": "Configuration logic for using Rspack in Meteor projects",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/rspack.config.js CHANGED
@@ -5,6 +5,7 @@ import path from 'path';
5
5
  import { merge } from 'webpack-merge';
6
6
 
7
7
  import { RequireExternalsPlugin } from './plugins/RequireExtenalsPlugin.js';
8
+ import { getMeteorAppSwcConfig } from "./lib/swc.js";
8
9
 
9
10
  const require = createRequire(import.meta.url);
10
11
 
@@ -41,31 +42,38 @@ function createCacheStrategy(mode) {
41
42
  }
42
43
 
43
44
  // SWC loader rule (JSX/JS)
44
- function createSwcConfig({ isRun }) {
45
+ function createSwcConfig({ isRun, isTypeScriptEnabled, isJsxEnabled, isTsxEnabled }) {
46
+ const defaultConfig = {
47
+ jsc: {
48
+ baseUrl: process.cwd(),
49
+ paths: { '/*': ['*'] },
50
+ parser: {
51
+ syntax: isTypeScriptEnabled ? 'typescript' : 'ecmascript',
52
+ ...(isTsxEnabled && { tsx: true }),
53
+ ...(isJsxEnabled && { jsx: true }),
54
+ },
55
+ target: 'es2015',
56
+ transform: {
57
+ react: {
58
+ development: isRun,
59
+ refresh: isRun,
60
+ },
61
+ },
62
+ },
63
+ };
64
+ const customConfig = getMeteorAppSwcConfig() || {};
65
+ const swcConfig = merge(defaultConfig, customConfig);
45
66
  return {
46
67
  test: /\.[jt]sx?$/,
47
68
  exclude: /node_modules|\.meteor\/local/,
48
69
  loader: 'builtin:swc-loader',
49
- options: {
50
- jsc: {
51
- baseUrl: process.cwd(),
52
- paths: { '/*': ['*'] },
53
- parser: { syntax: 'ecmascript', jsx: true },
54
- target: 'es2015',
55
- transform: {
56
- react: {
57
- development: isRun,
58
- refresh: isRun,
59
- },
60
- },
61
- },
62
- },
70
+ options: swcConfig,
63
71
  };
64
72
  }
65
73
 
66
74
  // Watch options shared across both builds
67
75
  const watchOptions = {
68
- ignored: ['**/main.html', '**/dist/**', '**/.meteor/local/**'],
76
+ ignored: ['**/.meteor/local/**', '**/dist/**'],
69
77
  };
70
78
 
71
79
  /**
@@ -93,6 +101,10 @@ export default function (inMeteor = {}, argv = {}) {
93
101
  const isReactEnabled = Meteor.isReactEnabled;
94
102
  const mode = isProd ? 'production' : 'development';
95
103
 
104
+ const isTypeScriptEnabled = Meteor.isTypeScriptEnabled || false;
105
+ const isJsxEnabled = Meteor.isJsxEnabled || false;
106
+ const isTsxEnabled = Meteor.isTsxEnabled || false;
107
+
96
108
  // Determine entry points
97
109
  const entryPath = Meteor.entryPath;
98
110
 
@@ -120,6 +132,27 @@ export default function (inMeteor = {}, argv = {}) {
120
132
  console.log('[i] Meteor flags:', Meteor);
121
133
  }
122
134
 
135
+ const swcConfig = createSwcConfig({
136
+ isRun,
137
+ isTypeScriptEnabled,
138
+ isJsxEnabled,
139
+ isTsxEnabled,
140
+ });
141
+ const externals = [
142
+ /^meteor.*/,
143
+ ...(isReactEnabled ? [/^react$/, /^react-dom$/] : [])
144
+ ];
145
+ const extensions = [
146
+ '.ts',
147
+ '.tsx',
148
+ '.js',
149
+ '.jsx',
150
+ '.mjs',
151
+ '.cjs',
152
+ '.json',
153
+ '.wasm',
154
+ ];
155
+
123
156
  // Base client config
124
157
  let clientConfig = {
125
158
  name: 'meteor-client',
@@ -141,7 +174,7 @@ export default function (inMeteor = {}, argv = {}) {
141
174
  },
142
175
  module: {
143
176
  rules: [
144
- createSwcConfig({ isRun }),
177
+ swcConfig,
145
178
  ...(Meteor.isBlazeEnabled
146
179
  ? [
147
180
  {
@@ -152,8 +185,8 @@ export default function (inMeteor = {}, argv = {}) {
152
185
  : []),
153
186
  ],
154
187
  },
155
- resolve: { extensions: ['.js', '.jsx', '.json'] },
156
- externals: [/^(meteor.*|react$|react-dom$)/],
188
+ resolve: { extensions },
189
+ externals,
157
190
  plugins: [
158
191
  ...(isRun
159
192
  ? [
@@ -202,7 +235,6 @@ export default function (inMeteor = {}, argv = {}) {
202
235
  writeToDisk: false,
203
236
  },
204
237
  },
205
- experiments: { incremental: true },
206
238
  }),
207
239
  };
208
240
 
@@ -221,21 +253,14 @@ export default function (inMeteor = {}, argv = {}) {
221
253
  },
222
254
  optimization: { usedExports: true },
223
255
  module: {
224
- rules: [
225
- {
226
- test: /\.meteor\/local/,
227
- use: 'builtin:empty-loader',
228
- sideEffects: false,
229
- },
230
- createSwcConfig({ isRun }),
231
- ],
256
+ rules: [swcConfig],
232
257
  },
233
258
  resolve: {
234
- extensions: ['.js', '.jsx', '.json'],
259
+ extensions,
235
260
  modules: ['node_modules', path.resolve(process.cwd())],
236
261
  conditionNames: ['import', 'require', 'node', 'default'],
237
262
  },
238
- externals: [/^(meteor.*|react|react-dom)/],
263
+ externals,
239
264
  plugins: [
240
265
  new DefinePlugin({
241
266
  'Meteor.isClient': JSON.stringify(false),
@@ -252,7 +277,8 @@ export default function (inMeteor = {}, argv = {}) {
252
277
  watchOptions,
253
278
  devtool: isRun ? 'source-map' : 'hidden-source-map',
254
279
  ...(isRun &&
255
- merge(createCacheStrategy(mode), { experiments: { incremental: true } })),
280
+ createCacheStrategy(mode)
281
+ ),
256
282
  };
257
283
 
258
284
  // Load and apply project-level overrides for the selected build