How to use babel-plugin-transform-define - 10 common examples

To help you get started, we’ve selected a few babel-plugin-transform-define examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NervJS / taro / packages / taro-cli / src / rn / transformJS.ts View on Github external
}
      }
    }
  })
  try {
    const constantsReplaceList = Object.assign({
      'process.env.TARO_ENV': BUILD_TYPES.RN
    }, Util.generateEnvList(projectConfig.env || {}), Util.generateConstantsList(projectConfig.defineConstants || {}))
    // TODO 使用 babel-plugin-transform-jsx-to-stylesheet 处理 JSX 里面样式的处理,删除无效的样式引入待优化

    const plugins = [
      [require('babel-plugin-transform-jsx-to-stylesheet'), {filePath}],
      require('babel-plugin-transform-class-properties'),
      require('babel-plugin-transform-decorators-legacy').default,
      [require('babel-plugin-danger-remove-unused-import'), {ignore: ['@tarojs/taro', 'react', 'react-native', 'nervjs']}],
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]

    // const babelConfig = projectConfig.plugins.babel
    // const plugins = babelConfig.plugins.concat(extraBabelPlugins)
    const newBabelConfig = Object.assign({}, {plugins})

    ast = babel.transformFromAst(ast, code, newBabelConfig).ast
  } catch (e) {
    throw e
  }

  return {
    code: unescape(generate(ast).code.replace(/\\u/g, '%u')),
    styleFiles
  }
}
github Tencent / omi / packages / omip / taro-cli / src / util / resolve_npm_files.js View on Github external
try {
    const constantsReplaceList = Object.assign({
      'process.env.TARO_ENV': buildAdapter
    }, generateEnvList(projectConfig.env || {}))
    const transformResult = wxTransformer({
      code: fileContent,
      sourcePath: filePath,
      outputPath: outputNpmPath,
      isNormal: true,
      adapter: buildAdapter,
      isTyped: REG_TYPESCRIPT.test(filePath),
      env: constantsReplaceList
    })
    const ast = babel.transformFromAst(transformResult.ast, '', {
      plugins: [
        [require('babel-plugin-transform-define').default, constantsReplaceList]
      ]
    }).ast
    fileContent = parseAst(ast, filePath, files, isProduction, npmConfig, buildAdapter, compileInclude)
  } catch (err) {
    console.log(err)
  }
  if (!copyedFiles[outputNpmPath]) {
    if (compileInclude && compileInclude.length) {
      const filePathArr = filePath.split(path.sep)
      const nodeModulesIndex = filePathArr.indexOf('node_modules')
      const npmPkgName = filePathArr[nodeModulesIndex + 1]
      if (compileInclude.indexOf(npmPkgName) >= 0) {
        const compileScriptRes = await npmProcess.callPlugin('babel', fileContent, filePath, babelConfig)
        fileContent = compileScriptRes.code
      }
    }
github NervJS / taro / packages / taro-cli / src / mini / astProcess.ts View on Github external
const isQuickApp = buildAdapter === BUILD_TYPES.QUICKAPP
  const cannotRemoves = [taroJsFramework, 'react', 'nervjs']
  let hasComponentDidHide
  let hasComponentDidShow
  let hasComponentWillMount
  let hasEnablePageScroll
  let needSetConfigFromHooks = false
  let configFromHooks
  if (isQuickApp) {
    cannotRemoves.push(taroJsComponents)
  }
  const taroSelfComponents = new Set()
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-danger-remove-unused-import'), { ignore: cannotRemoves }],
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast as t.File
  traverse(ast, {
    ClassDeclaration (astPath) {
      const node = astPath.node
      let hasCreateData = false
      if (node.superClass) {
        astPath.traverse({
          ClassMethod (astPath) {
            if (astPath.get('key').isIdentifier({ name: '_createData' })) {
              hasCreateData = true
            }
          }
        })
        if (hasCreateData) {
          needExportDefault = true
github Tencent / omi / packages / cax-omip / scripts / taro-cli / src / weapp.js View on Github external
function parseComponentExportAst (ast, componentName, componentPath, componentType) {
  let componentRealPath = null
  let importExportName
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast
  traverse(ast, {
    ExportNamedDeclaration (astPath) {
      const node = astPath.node
      const specifiers = node.specifiers
      const source = node.source
      if (source && source.type === 'StringLiteral') {
        specifiers.forEach(specifier => {
          const exported = specifier.exported
          if (_.kebabCase(exported.name) === componentName) {
            componentRealPath = Util.resolveScriptPath(path.resolve(path.dirname(componentPath), source.value))
          }
        })
      } else {
        specifiers.forEach(specifier => {
github Tencent / omi / packages / omi-cloud / scripts / taro-cli / src / weapp.js View on Github external
function parseComponentExportAst (ast, componentName, componentPath, componentType) {
  let componentRealPath = null
  let importExportName
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast
  traverse(ast, {
    ExportNamedDeclaration (astPath) {
      const node = astPath.node
      const specifiers = node.specifiers
      const source = node.source
      if (source && source.type === 'StringLiteral') {
        specifiers.forEach(specifier => {
          const exported = specifier.exported
          if (_.kebabCase(exported.name) === componentName) {
            componentRealPath = Util.resolveScriptPath(path.resolve(path.dirname(componentPath), source.value))
          }
        })
      } else {
        specifiers.forEach(specifier => {
github Tencent / omi / packages / omip / my-app / scripts / taro-cli / src / weapp.js View on Github external
function parseAst (type, ast, depComponents, sourceFilePath, filePath, npmSkip = false) {
  const styleFiles = []
  const scriptFiles = []
  const jsonFiles = []
  const mediaFiles = []
  let configObj = {}
  let componentClassName = null
  let taroJsReduxConnect = null
  let taroMiniAppFramework = `@tarojs/taro-${buildAdapter}`
  let taroImportDefaultName
  let needExportDefault = false
  let exportTaroReduxConnected = null
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-danger-remove-unused-import'), { ignore: ['@tarojs/taro', 'react', 'nervjs'] }],
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast
  traverse(ast, {
    ClassDeclaration (astPath) {
      const node = astPath.node
      let hasCreateData = false
      if (node.superClass) {
        astPath.traverse({
          ClassMethod (astPath) {
            if (astPath.get('key').isIdentifier({ name: '_createData' })) {
              hasCreateData = true
            }
          }
        })
        if (hasCreateData) {
          needExportDefault = true
github NervJS / taro / packages / taro-cli / src / quick / astProcess.ts View on Github external
buildAdapter,
    constantsReplaceList,
    isProduction,
    npmConfig,
    alias: pathAlias,
    projectConfig
  } = getBuildData()
  const notExistNpmList = getNotExistNpmList()
  const taroMiniAppFramework = `@tarojs/taro-${buildAdapter}`
  let needExportDefault = false
  let configObj: IConfig = {}
  let componentClassName: string = ''
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-danger-remove-unused-import'), { ignore: ['@tarojs/taro', 'react', 'nervjs'] }],
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast as t.File
  traverse(ast, {
    ClassDeclaration (astPath) {
      const node = astPath.node
      let hasCreateData = false
      if (astPath.isProperty({ superClass: true })) {
        astPath.traverse({
          ClassMethod (astPath) {
            if (astPath.get('key').isIdentifier({ name: '_createData' })) {
              hasCreateData = true
            }
          }
        })
        if (hasCreateData) {
          needExportDefault = true
github NervJS / taro / packages / taro-cli / src / weapp.js View on Github external
function parseComponentExportAst (ast, componentName, componentPath, componentType) {
  let componentRealPath = null
  let importExportName
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast
  traverse(ast, {
    ExportNamedDeclaration (astPath) {
      const node = astPath.node
      const specifiers = node.specifiers
      const source = node.source
      if (source && source.type === 'StringLiteral') {
        specifiers.forEach(specifier => {
          const exported = specifier.exported
          if (_.kebabCase(exported.name) === componentName) {
            componentRealPath = Util.resolveScriptPath(path.resolve(path.dirname(componentPath), source.value))
          }
        })
      } else {
        specifiers.forEach(specifier => {
github NervJS / taro / packages / taro-transformer-wx / src / options.ts View on Github external
export const buildBabelTransformOptions: () => TransformOptions = () => {
  Status.isSFC = false
  let plugins = [
    require('babel-plugin-transform-do-expressions'),
    require('babel-plugin-transform-export-extensions'),
    require('babel-plugin-transform-flow-strip-types'),
    [require('babel-plugin-transform-define').default, transformOptions.env]
  ]
  if (!transformOptions.isNormal) {
    plugins.push(buildVistor())
  }
  return {
    filename: transformOptions.sourcePath,
    babelrc: false,
    parserOpts: {
      sourceType: 'module',
      plugins: [
        'classProperties',
        'jsx',
        'flow',
        'flowComment',
        'trailingFunctionCommas',
        'asyncFunctions',
github Tencent / omi / packages / mps / _scripts / jsx2wxml / options.js View on Github external
'classProperties',
                'jsx',
                'flow',
                'flowComment',
                'trailingFunctionCommas',
                'asyncFunctions',
                'exponentiationOperator',
                'asyncGenerators',
                'objectRestSpread',
                'decorators',
                'dynamicImport'
            ]
        },
        plugins: [
            require('babel-plugin-transform-flow-strip-types'),
            [require('babel-plugin-transform-define').default, exports.transformOptions.env]
        ].concat(process.env.ESLINT === 'false' || exports.transformOptions.isNormal || exports.transformOptions.isTyped ? [] : eslint_1.eslintValidation)
            .concat((process.env.NODE_ENV === 'test') ? [] : require('babel-plugin-remove-dead-code').default)
    };
};
//# sourceMappingURL=options.js.map

babel-plugin-transform-define

Babel plugin that replaces member expressions and typeof statements with strings

MIT
Latest version published 1 year ago

Package Health Score

75 / 100
Full package analysis

Popular babel-plugin-transform-define functions