Why&How tree-shaking this code after build? #19063
-
BackgroundI have a front-end project that has a git submodule directory, and this submodule project is also referenced by other back-end projects, but the front-end only imports the enum variable. The packaged code contains typeorm schema code that was not imported in the front end Reproductionhttps://github.com/Yuliang-Lee/vite-treeshake-test Problem codebuilded code includes typeorm entity which project not imported? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The easier way would be to separate the file and avoid importing the parts that is not needed in the front-end side. The harder way would be to tell rollup the statements as side-effect free. In this case, export const TTest = /* #__PURE__ */ (() => {
@Index('t_id', ['id'], { unique: true })
@Index('Finsert_time', ['finsertTime'], {})
@Entity('t_test', { schema: 'db_a' })
class r {
@PrimaryGeneratedColumn({ type: 'int', name: 'id' })
id: number
@Column('varchar', { name: 'Fcreate_user', length: 255 })
fcreateUser?: string
@Column('int', { name: 'Fis', comment: '', default: () => "'0'" })
fis: IS_ENUM
@Column('datetime', { name: 'Finsert_time', default: () => "'1970-01-01 00:00:00'" })
finsertTime?: Date
@Column('datetime', { name: 'Fmodify_time', default: () => "'1970-01-01 00:00:00'" })
fmodifyTime?: Date
}
return r
})() |
Beta Was this translation helpful? Give feedback.
The easier way would be to separate the file and avoid importing the parts that is not needed in the front-end side.
For example, extract the enums in a separate file called
src/git_submodule/typeorm/enums.ts
and import that file from the front-end side andsrc/git_submodule/typeorm/test.ts
.The harder way would be to tell rollup the statements as side-effect free. In this case,
import 'typeorm'
and@Entity('t_test', { schema: 'db_a' })
call is assumed as side-effectful. For the former one, you can setbuild.rollupOptions.treeshake.moduleSideEffects: (id) => { if (id === 'typeorm') { return false } }
. For the latter one, you can wrap it with IIFE and mark it with#__PURE__
.