Babel输出路径,用于维护输入路径的目录结构

qacovj5a  于 2023-04-27  发布在  Babel
关注(0)|答案(2)|浏览(243)

给定以下示例目录结构:

srcDir/file1.js
srcDir/subDir1/file2.js
srcDir/subDir2/file3.js

我想用babel-cli处理这些文件,以便输出文件最终在destDir中具有相同的相对目录结构。
即:

destDir/file1.js
destDir/subDir1/file2.js
destDir/subDir2/file3.js

我不想处理srcDir中的所有文件,只想处理其中的一部分。
我想我必须指定一个输入目录,一个文件路径列表和一个输出目录。
但是Babel的命令行帮助并没有解释如何做到这一点。

Usage: babel [options] <files ...>

Options:
  -f, --filename [filename]                   filename to use when reading from stdin - this will be used in source-maps, errors etc
  --presets [list]                            comma-separated list of preset names
  --plugins [list]                            comma-separated list of plugin names
  --config-file [path]                        Path to a .babelrc file to use
  --env-name [name]                           The name of the 'env' to use when loading configs and plugins. Defaults to the value of BABEL_ENV, or else NODE_ENV, or else 'development'.
  --root-mode [mode]                          The project-root resolution mode. One of 'root' (the default), 'upward', or 'upward-optional'.
  --source-type [script|module]
  --no-babelrc                                Whether or not to look up .babelrc and .babelignore files
  --ignore [list]                             list of glob paths to **not** compile
  --only [list]                               list of glob paths to **only** compile
  --no-highlight-code                         enable/disable ANSI syntax highlighting of code frames (on by default)
  --no-comments                               write comments to generated output (true by default)
  --retain-lines                              retain line numbers - will result in really ugly code
  --compact [true|false|auto]                 do not include superfluous whitespace characters and line terminators
  --minified                                  save as much bytes when printing [true|false]
  --auxiliary-comment-before [string]         print a comment before any injected non-user code
  --auxiliary-comment-after [string]          print a comment after any injected non-user code
  -s, --source-maps [true|false|inline|both]
  --source-map-target [string]                set `file` on returned source map
  --source-file-name [string]                 set `sources[0]` on returned source map
  --source-root [filename]                    the root from which all sources are relative
  --module-root [filename]                    optional prefix for the AMD module formatter that will be prepend to the filename on module definitions
  -M, --module-ids                            insert an explicit id for modules
  --module-id [string]                        specify a custom name for module ids
  -x, --extensions [extensions]               List of extensions to compile when a directory has been input [.es6,.js,.es,.jsx,.mjs]
  --keep-file-extension                       Preserve the file extensions of the input files
  -w, --watch                                 Recompile files on changes
  --skip-initial-build                        Do not compile files before watching
  -o, --out-file [out]                        Compile all input files into a single file
  -d, --out-dir [out]                         Compile an input directory of modules into an output directory
  --relative                                  Compile into an output directory relative to input directory or file. Requires --out-dir [out]
  -D, --copy-files                            When compiling a directory copy over non-compilable files
  --include-dotfiles                          Include dotfiles when compiling and copying non-compilable files
  --verbose                                   Log everything
  --delete-dir-on-start                       Delete the out directory before compilation
  -V, --version                               output the version number
  -h, --help                                  output usage information
0x6upsns

0x6upsns1#

所以我发现,如果你做babel * -d ./build/,那么它会扁平化所有的第一级目录。
所以我只好

-- server/
 |  apis/
 |    mainApi.js
 |  store/
 |    models/
 |      User.js
 |  index.js

./server目录运行babel * -d ./build/,得到:

-- server/
 |  build/
 |    models/
 |      User.js
 |    index.js
 |    mainApi.js
 |  apis/
 |    mainApi.js
 |  store/
 |    models/
 |      User.js
 |  index.js

输出是扁平的,但是仍然有一个models文件夹,其中有User.js,这使我相信第一级dirs将被扁平化。
所以我把所有东西都放进一个server/src/文件夹,然后从./server/运行babel src/ -d ./build/,现在它保留了我的目录结构。

-- server/
 |  build/
 |    apis/
 |      mainApi.js
 |    store/
 |      models/
 |        User.js
 |    index.js
 |
 |  src/
 |    apis/
 |      mainApi.js
 |    store/
 |      models/
 |        User.js
 |    index.js
eufgjt7s

eufgjt7s2#

巴别塔有一面特殊的旗帜:

--relative  Compile into an output directory relative to input directory or file. Requires --out-dir [out]

如果你想模仿输入文件的路径,你可以尝试使用带有这个标志的命令。

相关问题