Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transform): support useDefineForClassFields Config #1181

Merged
merged 10 commits into from
May 29, 2024
17 changes: 16 additions & 1 deletion crates/mako/src/build/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ impl Transform {
})));
// TODO: is it a problem to clone comments?
let comments = origin_comments.get_swc_comments().clone();
let assumptions = context.assumptions_for(file);

folders.push(Box::new(swc_preset_env::preset_env(
unresolved_mark,
Some(comments),
Expand All @@ -152,7 +154,7 @@ impl Transform {
)),
..Default::default()
},
Assumptions::default(),
assumptions,
&mut FeatureFlag::default(),
)));
// simplify, but keep top level dead code
Expand Down Expand Up @@ -227,6 +229,19 @@ impl Transform {
}
}

impl Context {
pub fn assumptions_for(&self, file: &File) -> Assumptions {
let is_ts = file.extname == "ts" || file.extname == "tsx";

let mut assumptions = Assumptions::default();
assumptions.set_public_class_fields |= !self.config.use_define_for_class_fields;
if is_ts {
assumptions.set_class_methods |= !self.config.use_define_for_class_fields;
}
assumptions
}
}

// TODO: use visitor instead
// Why do this?
// 为了修复 @import url() 会把 css 当 asset 处理,返回 base64 的问题
Expand Down
2 changes: 2 additions & 0 deletions crates/mako/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ pub struct Config {
pub rsc_client: Option<RscClientConfig>,
pub experimental: ExperimentalConfig,
pub watch: WatchConfig,
pub use_define_for_class_fields: bool,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -657,6 +658,7 @@ const DEFAULT_CONFIG: &str = r#"
"rscServer": false,
"rscClient": false,
"experimental": { "webpackSyntaxValidate": [] },
"useDefineForClassFields": true,
"watch": { "ignorePaths": [] },
"devServer": { "host": "127.0.0.1", "port": 3000 }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require("assert");
const { parseBuildResult, trim, moduleReg, injectSimpleJest } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

injectSimpleJest();

require("./dist/index.js");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class T{
a;
b(){
return this.a
}
}

it('define the field with descriptor by default',()=>{
let t = new T();
expect(Object.getOwnPropertyDescriptor(t,'a')).toStrictEqual({
value: undefined,
writable: true,
enumerable: true,
configurable: true
});
});




Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require("assert");
const { parseBuildResult, trim, moduleReg, injectSimpleJest } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

injectSimpleJest();

require("./dist/index.js");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"useDefineForClassFields": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class T{
a;
b(){
return this.a
}
}

it('has no descriptor when disable use define for class fields',()=>{
let t = new T();
expect(Object.getOwnPropertyDescriptor(t,'a')).toStrictEqual(undefined);
});




1 change: 1 addition & 0 deletions packages/bundler-mako/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/**/dist
1 change: 1 addition & 0 deletions packages/bundler-mako/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
scripts
examples
5 changes: 5 additions & 0 deletions packages/bundler-mako/examples/simple/base-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"useDefineForClassFields": true
}
}
25 changes: 25 additions & 0 deletions packages/bundler-mako/examples/simple/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const bundler = require('../../');
const noop = () => {};

bundler
.build({
cwd: __dirname,
config: {
entry: {
index: 'index.ts',
},
alias: {},
jsMinifier: 'none',
hash: false,
targets: {
chrome: 40,
},
},
onBuildComplete: noop,
chainWebpack: noop,
watch: false,
})
.then(
() => console.log('Build completed'),
(e) => console.log(e),
);
16 changes: 16 additions & 0 deletions packages/bundler-mako/examples/simple/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const assert = require('assert');

class T {
a;
b() {
return this.a;
}
}

let t = new T();
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(t, 'a'), {
value: undefined,
writable: true,
enumerable: true,
configurable: true,
});
7 changes: 7 additions & 0 deletions packages/bundler-mako/examples/simple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./base-tsconfig.json",
"compilerOptions": {
"allowJs": true,
"outDir": "dist2"
}
}
29 changes: 29 additions & 0 deletions packages/bundler-mako/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const assert = require('assert');
const { createProxy, createHttpsServer } = require('@umijs/bundler-utils');
const lodash = require('lodash');
const chalk = require('chalk');
const { parseTsconfig } = require('get-tsconfig');
const {
createProxyMiddleware,
} = require('@umijs/bundler-utils/compiled/http-proxy-middleware');
Expand Down Expand Up @@ -510,6 +511,7 @@ async function getMakoConfig(opts) {
{},
);
const outputPath = path.resolve(opts.cwd, opts.config.outputPath || 'dist');
const tsConfig = getTsConfig(opts);

const makoConfig = {
entry: opts.entry,
Expand Down Expand Up @@ -541,6 +543,8 @@ async function getMakoConfig(opts) {
emotion,
forkTSChecker: !!forkTSChecker,
...(opts.disableCopy ? { copy: [] } : { copy: ['public'].concat(copy) }),
useDefineForClassFields:
tsConfig.compilerOptions.useDefineForClassFields ?? true,
stormslowly marked this conversation as resolved.
Show resolved Hide resolved
};

return makoConfig;
Expand All @@ -565,3 +569,28 @@ function normalizeDefineValue(val) {
}, {});
}
}

const DEFAULT_TS_CONFIG = {
compilerOptions: {
useDefineForClassFields: true,
},
};

function getTsConfig(opts) {
let root = opts.cwd;
const tsConfigPath = path.resolve(root, 'tsconfig.json');

if (fs.existsSync(tsConfigPath)) {
try {
return parseTsconfig(tsConfigPath);
} catch (e) {
console.error(
'parsing tsconfig.json failed, fallback to default tsconfig\n',
e,
);
return DEFAULT_TS_CONFIG;
}
} else {
return DEFAULT_TS_CONFIG;
}
}
3 changes: 2 additions & 1 deletion packages/bundler-mako/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"name": "@umijs/bundler-mako",
"version": "0.4.17",
"dependencies": {
"@umijs/mako": "0.4.18-canary.20240521.4",
"@umijs/bundler-utils": "^4.0.81",
"@umijs/mako": "0.4.18-canary.20240521.4",
"chalk": "^4.1.2",
"compression": "^1.7.4",
"connect-history-api-fallback": "^2.0.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"get-tsconfig": "4.7.5",
"lodash": "^4.17.21",
"rimraf": "5.0.1",
"webpack-5-chain": "8.0.1"
Expand Down
21 changes: 8 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading