简体   繁体   English

使用 WebPack 和 Babel 组合、缩小并转换为 ES5

[英]Combine, Minify, and convert to ES5 with WebPack and Babel

I have a project that works perfect and was written in TS, I had to convert it to plain JS and it works for the most part.我有一个完美的项目,它是用 TS 编写的,我不得不将它转换为普通的 JS 并且它在大多数情况下都有效。 The issue where I am struggling is removing the default WebPack loader after the files have been combined and minified, WebPack includes a loader to the final output even thought I do not need a loader since all the files are combined into one large file.我正在努力解决的问题是在文件合并和缩小后删除默认的 WebPack 加载器,WebPack 在最终输出中包含一个加载器,即使我认为我不需要加载器,因为所有文件都合并到一个大文件中。

+ filea.js
+ fileb.js
+ filec.js
+ filed.js
-> output bundle.js

I have read a few articles/posts that recommend manually creating a config file providing the name of each of the files that will combined and minified, this may work OK but the problem is that the project I am working on is broken into small chunks (modules) so that tools such WebPack can be smart enough and know when a file should be added as a dependency in the final output.我读过一些文章/帖子,建议手动创建一个配置文件,提供将组合和缩小的每个文件的名称,这可能工作正常,但问题是我正在处理的项目被分成小块(模块),以便像 WebPack 这样的工具可以足够智能,并且知道何时应该将文件作为依赖项添加到最终输出中。

I know we can combine and minify multiple individual JS files but when it comes to exporting a single file it seems like the task is trivial With TS but in the vanilla JS world there is little or no information about the subject.我知道我们可以合并和缩小多个单独的 JS 文件,但是在导出单个文件时,使用 TS 似乎任务很简单,但在普通 JS 世界中,关于该主题的信息很少或没有。

I don't understand something, do you want to have one big file or small individual modules (chunks)?我有点不明白,你想要一个大文件还是小的单个模块(块)?

An example of small modules:小模块的一个例子:

module.exports = {  
  entry: {    
    app: './src/app.js',
    admin: './src/admin.js',
    contact: './src/contact.js'  
  }
};

Another method is one main module and it contains all smaller modules.另一种方法是一个主模块,它包含所有较小的模块。

module.exports = {  
  entry: {    
    app: './src/app.js'  
  }
};

You can also use something like lazy loading.您还可以使用延迟加载之类的东西。 Then the modules (chunks) will be dynamically loaded only when needed.然后模块(块)将仅在需要时动态加载。 lazy-loading延迟加载

Here is an example of using several entries webpack-boilerplate .这是使用多个条目webpack-boilerplate的示例。

Sounds like you have a project with several JS files and you want to use webpack to bundle all of them and minify the result.听起来您有一个包含多个 JS 文件的项目,并且您想使用 webpack 捆绑所有这些文件并缩小结果。

Webpack was built for this. Webpack 就是为此而构建的。

You'll need to add a build step in your package.json like this:您需要在 package.json 中添加一个构建步骤,如下所示:

"scripts": {
  "build": "webpack --config prod.config.js"
}

Then you'll need to create a webpack.config.js with a module.exports block that has an entry point and rules to include in your project.然后,您需要使用 module.exports 块创建一个 webpack.config.js,该块具有要包含在您的项目中的入口点和规则。 The following should be a minimal setup that can get your started:以下应该是可以让您入门的最小设置:

const path = require('path');

module.exports = {
  entry: "./your/path/to/src",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  module: {},
  plugins: [
    new MinifyPlugin(minifyOpts, pluginOpts)
  ]
}

You can add modules that perform additional code transpilation for files that matcha a certain regex. You can also use a plugin to perform minification such as babel-minify-webpack-plugin as documented here https://webpack.js.org/plugins/babel-minify-webpack-plugin/. (Note you will need to add this dependency.)

The full webpack configuration can be found here: https://webpack.js.org/configuration/

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

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