为什么可以在vue-cli构建的vue里使用node模块?

为什么可以在vue-cli构建的vue里使用node模块?

<template>

...

</template>

<script>

import path from 'path'

import test from 'http'

console.log(path)

console.log(test)

// 浏览器中path和test是可以打印的,为什么这里可以使用path和http这种node里的模块?

</script>

<style>

...

<style>

补充:
找到了个相关的内容:webpack5删掉了自动提供的node polyfills,所以webpack <=4自动提供node polyfills功能相关内容可以在哪里看到呢

2020-10-10-webpack-5-release

Automatic Node.js Polyfills Removed
In the early days, webpack's aim was to allow running most Node.js modules in the browser, but the module landscape changed and many module uses are now written mainly for frontend purposes. Webpack <= 4 ships with polyfills for many of the Node.js core modules, which are automatically applied once a module uses any of the core modules (i.e. the crypto module).

While this makes using modules written for Node.js easier, it adds these huge polyfills to the bundle. In many cases these polyfills are unnecessary.

Webpack 5 stops automatically polyfilling these core modules and focus on frontend-compatible modules. Our goal is to improve compatibility with the web platform, where Node.js core modules are not available.


回答:

webpack@4 以前,默认包含了 node.js 的模块 polyfill,所以是可以用的。所以理论上 vue-cli@3,@4 都可以这么用。到了 @vue/cli@5,用的 webpack@5,就不行了。


回答:

补充:

文档
https://v4.webpack.js.org/con...
https://v4.webpack.js.org/gui...

官方维护的lib
https://github.com/webpack/no...

webpack源码的具体实现
https://github.com/webpack/we...

以上是 为什么可以在vue-cli构建的vue里使用node模块? 的全部内容, 来源链接: utcz.com/p/937218.html

回到顶部