@tybys/ts-transform-define
v0.2.2
Published
Similar to `webpack.DefinePlugin`
Downloads
7
Readme
ts-transform-define
Similar to webpack.DefinePlugin
Usage
npm install -D @tybys/ts-transform-define
{
"compilerOptions": {
"plugins": [
{
"transform": "@tybys/ts-transform-define",
"defines": {
"process.env": {
"NODE_ENV": "'development'"
},
"NULL": 0,
"predefinedNestedObject": {
"array": [{ "value": 0 }]
}
}
}
]
}
}
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {
getCustomTransformers (program) {
return {
before: [
require('@tybys/ts-transform-define').default(program, {
defines: {
'process.env': {
NODE_ENV: "'development'"
},
NULL: 0,
predefinedNestedObject: {
array: [{ value: 0 }]
}
}
})
]
}
}
}
}
]
}
]
}
}
// rollup.config.js
import { join } from 'path'
import typescript from '@rollup/plugin-typescript'
export default {
plugins: [
typescript({
transformers: {
before: [
{
type: 'program',
factory: (program) => {
return require('@tybys/ts-transform-define').default(program, {
defines: {
'process.env': {
NODE_ENV: "'development'"
},
NULL: 0,
predefinedNestedObject: {
array: [{ value: 0 }]
}
}
})
}
}
]
}
})
]
}
Input:
console.log(process.env)
console.log(typeof process.env)
console.log(process.env.NODE_ENV)
console.log(typeof process.env.NODE_ENV)
console.log(NULL)
console.log(typeof NULL)
console.log(predefinedNestedObject)
console.log(predefinedNestedObject.array)
console.log(typeof predefinedNestedObject.array)
Output:
console.log(Object({ NODE_ENV: 'development' }));
console.log("object");
console.log('development');
console.log("string");
console.log(0);
console.log("number");
console.log(Object({ array: Object([Object({ value: 0 })]) }));
console.log(Object([Object({ value: 0 })]));
console.log("object");