美文网首页
webpack target属性

webpack target属性

作者: 炎武森禄 | 来源:发表于2017-02-26 10:37 被阅读0次

Because JavaScript can be written for both server and browser, webpack offers multiple deployment targets that you can set in your webpack configuration.

W> The webpack target property is not to be confused with the output.libraryTarget property. For more information see our guide on the output property.

Usage

To set the target property, you simply set the target value in your webpack config:

webpack.config.js

module.exports = {
  target: 'node'
};

In the example above, using node webpack will compile for usage in a Node.js-like environment (uses Node.js require to load chunks and not touch any built in modules like fs or path).

Each target has a variety of deployment/environment specific additions, support to fit its needs. See what targets are available.

?>Further expansion for other popular target values

Multiple Targets

Although webpack does not support multiple strings being passed into the target property, you can create an isomorphic library by bundling two separate configurations:

webpack.config.js

var path = require('path');
var serverConfig = {
  target: 'node',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'lib.node.js'
  }
  //…
};

var clientConfig = {
  target: 'web', // <=== can be omitted as default is 'web'
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'lib.js'
  }
  //…
};

module.exports = [ serverConfig, clientConfig ];

The example above will create a lib.js and lib.node.js file in your dist folder.

Resources

As seen from the options above there are multiple different deployment targets that you can choose from. Below is a list of examples, and resources that you can refer to.

Bundle Output Comparison

compare-webpack-target-bundles: A great resource for testing and viewing different webpack targets. Also great for bug reporting.

?> Need to find up to date examples of these webpack targets being used in live code or boilerplates.

相关文章

  • webpack target属性

    Because JavaScript can be written for both server and bro...

  • 2018-07-21 学习总结

    1.target属性 标签的 target 属性规定在何处打开链接文档。target的四个属性值:_blank ...

  • target事件属性

    定义和用法 target 事件属性可返回事件的目标节点(触发该事件的节点),如生成事件的元素、文档或窗口。 语法 ...

  • 2021-03-26

    html 简述超链接target属性的取值和作用a 标签的target属性规定在何处打开链接文档语法: _blan...

  • vue入门笔记 day1

    webpack属性配置 javascript(参考webpack.config.js) webpack-ES6的处...

  • webpack配置详情

    webpack安装 yarn add webpack webpack-cli -S mode属性,指明打包环境 开...

  • 标签的基本使用详讲

    herf 属性 11 target属性...

  • DOM事件中target和currentTarget的区别

    target和currentTarget的概念: 1、target和currentTarget都是事件对象中的属性...

  • webpack属性

    为引入包起别名 配置server 添加源码映射 方便调试 source-map: 单独生成一个source-map...

  • WebPack4总结

    一、webpack4新特性 1.mode属性 二、webpack介绍 安装:npm install webpack...

网友评论

      本文标题:webpack target属性

      本文链接:https://www.haomeiwen.com/subject/trrowttx.html