3 Star 0 Fork 0

mirrors_niksy/node-stdlib-browser

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
4年前
4个月前
4个月前
Loading...
README
MIT

node-stdlib-browser

Build Status

Node standard library for browser.

Features:

  • Based on node-libs-browser for Webpack
  • Maintained with newer versions and modern implementations
  • Works with Webpack, Rollup, Vite, esbuild and Browserify, but should also work with other bundlers
  • Exports implementation with node: protocol which allows for builtin modules to be referenced by valid absolute URL strings

Check example to see how modules work in browser environment.

Install

npm install node-stdlib-browser --save-dev

Usage

Webpack

Show me

As of Webpack 5, aliases and globals provider need to be explicitly configured. If you want to handle node: protocol imports, you need to provide helper plugin.

// webpack.config.js
const stdLibBrowser = require('node-stdlib-browser');
const {
	NodeProtocolUrlPlugin
} = require('node-stdlib-browser/helpers/webpack/plugin');
const webpack = require('webpack');

module.exports = {
	// ...
	resolve: {
		alias: stdLibBrowser
	},
	plugins: [
		new NodeProtocolUrlPlugin(),
		new webpack.ProvidePlugin({
			process: stdLibBrowser.process,
			Buffer: [stdLibBrowser.buffer, 'Buffer']
		})
	]
};

If you’re using ESM config, additional configuration is needed to handle unspecified extensions:

// webpack.config.js
module.exports = {
	// ...
	module: {
		rules: [
			{
				test: /\.m?js$/,
				resolve: {
					fullySpecified: false
				}
			}
		]
	}
};

Rollup

Show me

Since many packages expose only CommonJS implementation, you need to apply plugins to handle CommonJS exports. Those packages could have dependencies installed with npm so they need to be properly resolved (taking into account browser-specific implementations).

Some dependencies can have circular dependencies and Rollup will warn you about that. You can ignore these warnings with helper function (reference).

// rollup.config.js
const stdLibBrowser = require('node-stdlib-browser');
const {
	handleCircularDependancyWarning
} = require('node-stdlib-browser/helpers/rollup/plugin');
const { default: resolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const alias = require('@rollup/plugin-alias');
const inject = require('@rollup/plugin-inject');

module.exports = {
	// ...
	plugins: [
		alias({
			entries: stdLibBrowser
		}),
		resolve({
			browser: true
		}),
		commonjs(),
		json(),
		inject({
			process: stdLibBrowser.process,
			Buffer: [stdLibBrowser.buffer, 'Buffer']
		})
	],
	onwarn: (warning, rollupWarn) => {
		handleCircularDependancyWarning(warning, rollupWarn);
	}
};

Vite

Show me

Vite config uses combination of Rollup and esbuild plugins. It’s important to use dynamic import when using CommonJS configuration so ESM version of modules is picked up. This allows Vite bundling to use our mocking implementation and implement heuristics such as proper tree-shaking and dead code removal marking.

const inject = require('@rollup/plugin-inject');

const esbuildShim = require.resolve('node-stdlib-browser/helpers/esbuild/shim');

module.exports = async () => {
	const { default: stdLibBrowser } = await import('node-stdlib-browser');
	return {
		resolve: {
			alias: stdLibBrowser
		},
		optimizeDeps: {
			include: ['buffer', 'process']
		},
		plugins: [
			{
				...inject({
					global: [esbuildShim, 'global'],
					process: [esbuildShim, 'process'],
					Buffer: [esbuildShim, 'Buffer']
				}),
				enforce: 'post'
			}
		]
	};
};

Vite plugins

If you wish to use simpler configuration, you can use one of the available Vite plugins which use this package under the hood:

esbuild

Show me

Using esbuild requires you to use helper utilities and plugins.

const path = require('path');
const esbuild = require('esbuild');
const plugin = require('node-stdlib-browser/helpers/esbuild/plugin');
const stdLibBrowser = require('node-stdlib-browser');

(async () => {
	await esbuild.build({
		// ...
		inject: [require.resolve('node-stdlib-browser/helpers/esbuild/shim')],
		define: {
			global: 'global',
			process: 'process',
			Buffer: 'Buffer'
		},
		plugins: [plugin(stdLibBrowser)]
	});
})();

Browserify

Show me

Bundling ES modules is currently not supported natively in Browserify, but you can try using esmify or babelify for transforming to CommonJS first.

const fs = require('fs');
const path = require('path');
const browserify = require('browserify');
const aliasify = require('aliasify');
const stdLibBrowser = require('node-stdlib-browser');

const b = browserify(
	[
		/* ... */
	],
	{
		// ...
		transform: [[aliasify, { aliases: stdLibBrowser }]],
		insertGlobalVars: {
			process: () => {
				return `require('${stdLibBrowser.process}')`;
			},
			Buffer: () => {
				return `require('${stdLibBrowser.buffer}').Buffer`;
			}
		}
	}
);

Package contents

Module Browser implementation Mock implementation Notes
assert assert
buffer buffer buffer buffer@5 for IE 11 support
child_process
cluster
console console-browserify console
constants constants-browserify
crypto crypto-browserify
dgram
dns dns
domain domain-browser
events events
fs Mocking fs
http stream-http
https https-browserify
module
net net
os os-browserify
path path-browserify
process process process Contains additional exports from newer Node
punycode punycode punycode@1 for browser support
querystring querystring-es3 Contains additional exports from newer Node versions
readline
repl
stream stream-browserify
string_decoder string_decoder
sys util
timers timers-browserify
timers/promises isomorphic-timers-promises
tls tls
tty tty-browserify tty
url node-url Contains additional exports from newer Node versions (URL and URLSearchParams are not polyfilled)
util util
vm vm-browserify
zlib browserify-zlib
_stream_duplex readable-stream
_stream_passthrough readable-stream
_stream_readable readable-stream
_stream_transform readable-stream
_stream_writable readable-stream

API

packages

Returns: object

Exports absolute paths to each module directory (where package.json is located), keyed by module names. Modules without browser replacements return module with default export null.

Some modules have mocks in the mock directory. These are replacements with minimal functionality.

Tips

Mocking fs

fs package doesn’t return anything since there are many different ways you can implement file system functionality in browser.

Examples of implementations:

Node support

Minimum supported version should be Node 10.

If you’re using ESM in Node < 12.20, note that subpath patterns are not supported so mocks can’t be handled. In that case, it’s recommended to use CommonJS implementation.

Browser support

Minimum supported version should be Internet Explorer 11, but most modules support even Internet Explorer 9.

Types

You can use default @types/node types.

License

MIT © Ivan Nikolić

Copyright (c) Ivan Nikolić <http://ivannikolic.com> Copyright (c) Tobias Koppers Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者 (3)

全部

近期动态

3年多前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_niksy/node-stdlib-browser.git
git@gitee.com:mirrors_niksy/node-stdlib-browser.git
mirrors_niksy
node-stdlib-browser
node-stdlib-browser
master

搜索帮助