Skip to content

Commit 917973d

Browse files
committed
fix
1 parent e3abfc2 commit 917973d

5 files changed

Lines changed: 82 additions & 0 deletions

File tree

rspack/inline-const/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "example-builtin-swc-loader",
3+
"version": "1.0.0",
4+
"private": true,
5+
"license": "MIT",
6+
"main": "index.js",
7+
"scripts": {
8+
"build": "rspack build"
9+
},
10+
"devDependencies": {
11+
"@rspack/cli": "latest",
12+
"@rspack/core": "latest"
13+
}
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
import { rspack } from "@rspack/core";
3+
4+
/** @type {import("@rspack/core").Configuration} */
5+
export default {
6+
entry: {
7+
main: "./src/index.ts",
8+
},
9+
experiments: {
10+
inlineConst: true,
11+
},
12+
mode: "production",
13+
optimization: {
14+
// disable minimize so you can understand the output
15+
minimize: false,
16+
},
17+
resolve: {
18+
extensions: [".ts", "..."],
19+
},
20+
plugins: [
21+
new rspack.DefinePlugin({
22+
ENV: JSON.stringify("mobile"),
23+
}),
24+
],
25+
module: {
26+
rules: [
27+
{
28+
test: /\.ts$/,
29+
use: {
30+
loader: "builtin:swc-loader",
31+
/** @type {import("@rspack/core").SwcLoaderOptions} */
32+
options: {
33+
jsc: {
34+
parser: {
35+
syntax: "typescript",
36+
},
37+
target: "es2015", // use target es2015 or greater so swc won't transform const to var
38+
},
39+
},
40+
},
41+
type: "javascript/auto",
42+
},
43+
],
44+
},
45+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const NULL = null; // null can be inlined
2+
export const UNDEFINED = undefined; // undefined can be inlined
3+
export const isMobile = ENV === 'mobile'; // boolean can be inlined
4+
export const shortString = 'hhh'; // lenght <= 6 string can be inlined
5+
export const longString = 'string len greater than 6'; // lenght > 6 string can't be inlined
6+
export const shortNumber = 1.2345; // length <= 6 number can be inlined
7+
export const longNumber = 1.23456; // length > 6 number can't be inlined
8+
const a = 1 << 0;
9+
const b = 1 << 1;
10+
const c = 1 << 2;
11+
export const all = a | b | c; // since 'a', 'b', 'c' are all constants, so 'all' can also be inlined

rspack/inline-const/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
NULL,
3+
UNDEFINED,
4+
isMobile,
5+
shortString,
6+
longString,
7+
shortNumber,
8+
longNumber,
9+
all,
10+
} from './reexports';
11+
console.log(NULL, UNDEFINED, isMobile, shortString, longString, shortNumber, longNumber, all);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './constants';

0 commit comments

Comments
 (0)