@meteorjs/rspack 0.0.1 → 0.0.3
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 +62 -0
- package/package.json +1 -1
- package/rspack.config.js +55 -23
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
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,28 @@ 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
|
+
console.log("--> (rspack.config.js-Line: 141)\n swcConfig: ", swcConfig?.options?.jsc);
|
|
142
|
+
const externals = [
|
|
143
|
+
/^meteor.*/,
|
|
144
|
+
...(isReactEnabled ? [/^react$/, /^react-dom$/] : [])
|
|
145
|
+
];
|
|
146
|
+
const extensions = [
|
|
147
|
+
'.ts',
|
|
148
|
+
'.tsx',
|
|
149
|
+
'.js',
|
|
150
|
+
'.jsx',
|
|
151
|
+
'.mjs',
|
|
152
|
+
'.cjs',
|
|
153
|
+
'.json',
|
|
154
|
+
'.wasm',
|
|
155
|
+
];
|
|
156
|
+
|
|
123
157
|
// Base client config
|
|
124
158
|
let clientConfig = {
|
|
125
159
|
name: 'meteor-client',
|
|
@@ -141,7 +175,7 @@ export default function (inMeteor = {}, argv = {}) {
|
|
|
141
175
|
},
|
|
142
176
|
module: {
|
|
143
177
|
rules: [
|
|
144
|
-
|
|
178
|
+
swcConfig,
|
|
145
179
|
...(Meteor.isBlazeEnabled
|
|
146
180
|
? [
|
|
147
181
|
{
|
|
@@ -152,8 +186,8 @@ export default function (inMeteor = {}, argv = {}) {
|
|
|
152
186
|
: []),
|
|
153
187
|
],
|
|
154
188
|
},
|
|
155
|
-
resolve: { extensions
|
|
156
|
-
externals
|
|
189
|
+
resolve: { extensions },
|
|
190
|
+
externals,
|
|
157
191
|
plugins: [
|
|
158
192
|
...(isRun
|
|
159
193
|
? [
|
|
@@ -220,16 +254,14 @@ export default function (inMeteor = {}, argv = {}) {
|
|
|
220
254
|
},
|
|
221
255
|
optimization: { usedExports: true },
|
|
222
256
|
module: {
|
|
223
|
-
rules: [
|
|
224
|
-
createSwcConfig({ isRun }),
|
|
225
|
-
],
|
|
257
|
+
rules: [swcConfig],
|
|
226
258
|
},
|
|
227
259
|
resolve: {
|
|
228
|
-
extensions
|
|
260
|
+
extensions,
|
|
229
261
|
modules: ['node_modules', path.resolve(process.cwd())],
|
|
230
262
|
conditionNames: ['import', 'require', 'node', 'default'],
|
|
231
263
|
},
|
|
232
|
-
externals
|
|
264
|
+
externals,
|
|
233
265
|
plugins: [
|
|
234
266
|
new DefinePlugin({
|
|
235
267
|
'Meteor.isClient': JSON.stringify(false),
|