@meteorjs/rspack 0.0.1 → 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/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.1",
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,25 +42,32 @@ 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
 
@@ -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
  ? [
@@ -220,16 +253,14 @@ export default function (inMeteor = {}, argv = {}) {
220
253
  },
221
254
  optimization: { usedExports: true },
222
255
  module: {
223
- rules: [
224
- createSwcConfig({ isRun }),
225
- ],
256
+ rules: [swcConfig],
226
257
  },
227
258
  resolve: {
228
- extensions: ['.js', '.jsx', '.json'],
259
+ extensions,
229
260
  modules: ['node_modules', path.resolve(process.cwd())],
230
261
  conditionNames: ['import', 'require', 'node', 'default'],
231
262
  },
232
- externals: [/^(meteor.*|react|react-dom)/],
263
+ externals,
233
264
  plugins: [
234
265
  new DefinePlugin({
235
266
  'Meteor.isClient': JSON.stringify(false),