简体   繁体   English

使用 Webpack 将 ES6 转译为单独的文件

[英]Using Webpack To Transpile ES6 as separate files

Is it possible to configure webpack to do the equivalent of:是否可以将 webpack 配置为执行以下操作:

babel src --watch --out-dir lib

So that a directory structure that looks like this:这样一个目录结构看起来像这样:

- src
  - alpha
    - beta.js
    - charlie
       - delta.js
    - echo.js
    - foxtrot
      - golf
        - hotel.js

Would compile all the files to ES5 and output them in an identical structure under a lib directory:将所有文件编译为 ES5 并在lib目录下以相同的结构输出它们:

- lib
  - alpha
    - beta.js
    - charlie
       - delta.js
    - echo.js
    - foxtrot
      - golf
        - hotel.js

I took a pass at globbing all the filepaths and passing them in as separate entries, but it seems that webpack 'forgets' the locations of the files when it comes to defining the output files.我尝试遍历所有文件路径并将它们作为单独的条目传递,但在定义输出文件时,webpack 似乎“忘记”了文件的位置。 Output.path only offers the [hash] token, while Output.file has more options, but only offers [name] , [hash] and [chunk] , so it appears at least, that this kind of compilation isn't supported. Output.path只提供[hash]标记,而Output.file有更多选项,但只提供[name][hash][chunk] ,所以至少看起来不支持这种编译。

To give my question some context, I am creating an npm module consisting of React components and their related styles.为了给我的问题提供一些背景信息,我正在创建一个由 React 组件及其相关样式组成的 npm 模块。 I am using CSS modules, so I need a way to compile both JavaScript and CSS into the module's lib dir.我正在使用 CSS 模块,所以我需要一种方法将 JavaScript 和 CSS 编译到模块的 lib 目录中。

If you want to output to multiple directories, you can use the path as the entry name. 如果要输出到多个目录,可以使用路径作为条目名称。

entry: {
  'foo/f.js': __dirname + '/src/foo/f.js',
  'bar/b.js': __dirname + '/src/bar/b.js',
},
output: {
  path: path.resolve(__dirname, 'lib'),
  filename: '[name]',
},

Therefore you can use a function to generate a list of entries for you that satisfy the above: 因此,您可以使用函数为您生成满足上述条目的条目列表:

const glob = require('glob');

function getEntries(pattern) {
  const entries = {};

  glob.sync(pattern).forEach((file) => {
    entries[file.replace('src/', '')] = path.join(__dirname, file);
  });

  return entries;
}

module.exports = {
  entry: getEntries('src/**/*.js'),
  output: {
    path: path.resolve(__dirname, 'lib'),
    filename: '[name]',
  },
  // ...
}

Looks this plugin transpile-webpack-plugin resolves the problem.看起来这个插件transpile-webpack-plugin解决了这个问题。 You may setup it as below:您可以按如下方式设置它:

const TranspilePlugin = require('transpile-webpack-plugin');

module.exports = {
  entry: './src/alpha/beta.js',
  output: {
    path: __dirname + '/lib',
  },
  plugins: [new TranspilePlugin({ longestCommonDir: './src' })]
};

Then, the plugin collects all the files directly or indirectly imported by ./src/alpha/beta.js and generates output files seperately in lib .然后,插件会收集./src/alpha/beta.js直接或间接导入的所有文件,并在lib中单独生成输出文件。 It is file-to-file transpiling which preserves both file names and directory structure.它是文件到文件的转换,它保留了文件名和目录结构。

Btw, if specifying multiple named entries and output filename [name] in webpack config, the output will be multiple bundles every of which contains all the direct and indirect imports and can't interact with each other.顺便说一句,如果在 webpack 配置中指定多个命名条目和输出文件名[name] ,输出将是多个包,每个包都包含所有直接和间接导入并且不能相互交互。 It's not the same as babel command actually...它实际上与babel命令不同......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM