webpack的一些基础知识
Posted 赏花赏景赏时光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webpack的一些基础知识相关的知识,希望对你有一定的参考价值。
本文是对webpack实践做的一些简单笔记,用于个人查缺补漏用的
1、webpack中的 process.env.NODE_ENV是什么
node环境下有个全局变量process(进程),该变量包括的属性如下图所示,发现并没有NODE_ENV属性,而有env属性,env表示该进程下的环境变量。NODE_ENV变量值常用于区分开发、测试、生产环境,于是在项目配置和脚本运行命令行中,通常会在env对象下设置NODE_ENV属性。
$ node process.js
process
title: 'node',
version: 'v4.4.4',
moduleLoadList:
[....],
versions:
http_parser: '2.5.2',
node: '4.4.4',
v8: '4.5.103.35',
uv: '1.8.0',
zlib: '1.2.8',
ares: '1.10.1-DEV',
icu: '56.1',
modules: '46',
openssl: '1.0.2h' ,
arch: 'x64',
platform: 'darwin',
release:
name: 'node',
lts: 'Argon',
sourceUrl: 'https://nodejs.org/download/release/v4.4.4/node-v4.4.4.tar.gz',
headersUrl: 'https://nodejs.org/download/release/v4.4.4/node-v4.4.4-headers.tar.gz' ,
argv:
[ '/Users/tugenhua/.nvm/versions/node/v4.4.4/bin/node',
'/Users/tugenhua/个人demo/process.js' ],
execArgv: [],
env:
TERM_PROGRAM: 'Apple_Terminal',
SHELL: '/bin/zsh',
TERM: 'xterm-256color',
TMPDIR: '/var/folders/l7/zndlx1qs05v29pjhvkgpmhjm0000gn/T/',
Apple_PubSub_Socket_Render: '/private/tmp/com.apple.launchd.7Ax4C1EWMx/Render',
TERM_PROGRAM_VERSION: '404',
TERM_SESSION_ID: '82E05668-442D-4180-ADA3-8CF64D85E5A9',
USER: 'tugenhua',
SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.MYOMheYcL3/Listeners',
PATH: '/Users/tugenhua/.nvm/versions/node/v4.4.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin',
PWD: '/Users/tugenhua/个人demo',
LANG: 'zh_CN.UTF-8',
XPC_FLAGS: '0x0',
XPC_SERVICE_NAME: '0',
SHLVL: '1',
HOME: '/Users/tugenhua',
LOGNAME: 'tugenhua',
SECURITYSESSIONID: '186a8',
OLDPWD: '/Users/tugenhua/工作文档/sns_pc',
ZSH: '/Users/tugenhua/.oh-my-zsh',
PAGER: 'less',
LESS: '-R',
LC_CTYPE: 'zh_CN.UTF-8',
LSCOLORS: 'Gxfxcxdxbxegedabagacad',
NVM_DIR: '/Users/tugenhua/.nvm',
NVM_NODEJS_ORG_MIRROR: 'https://nodejs.org/dist',
NVM_IOJS_ORG_MIRROR: 'https://iojs.org/dist',
NVM_RC_VERSION: '',
MANPATH: '/Users/tugenhua/.nvm/versions/node/v4.4.4/share/man:/usr/local/share/man:/usr/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man',
NVM_PATH: '/Users/tugenhua/.nvm/versions/node/v4.4.4/lib/node',
NVM_BIN: '/Users/tugenhua/.nvm/versions/node/v4.4.4/bin',
_: '/Users/tugenhua/.nvm/versions/node/v4.4.4/bin/node',
__CF_USER_TEXT_ENCODING: '0x1F5:0x19:0x34' ,
pid: 14034,
features:
debug: false,
uv: true,
ipv6: true,
tls_npn: true,
tls_sni: true,
tls_ocsp: true,
tls: true ,
_needImmediateCallback: false,
config: ,
nextTick: [Function: nextTick],
_tickCallback: [Function: _tickCallback],
_tickDomainCallback: [Function: _tickDomainCallback],
stdout: [Getter],
stderr: [Getter],
stdin: [Getter],
openStdin: [Function],
exit: [Function],
kill: [Function],
mainModule:
Module
id: '.',
exports: ,
parent: null,
filename: '/Users/tugenhua/个人demo/process.js',
loaded: false,
children: [],
paths:
[ '/Users/tugenhua/个人demo/node_modules',
'/Users/tugenhua/node_modules',
'/Users/node_modules',
'/node_modules' ]
那么设置NODE_ENV属性的值需要插件cross-env和webpack.DefinePluginc插件结合。
首先,我们大致了解下插件cross-env和webpack.DefinePlugin插件的作用:
1)cross-env是运行跨平台设置和使用环境变量的脚本,通常我们设置环境变量如下所示
"scripts":
"dev": "cross-env NODE_ENV=development webpack-dev-server --inline --progress --config build/webpack.dev.conf.js"
像上面我们直接设置变量NODE_ENV=development,在windos下会抛错,因为window下设置环境变量的方式不一样,因此cross-env就是用来解决跨平台设置环境变量的问题,cross-env能够提供一个设置环境变量的scripts。
2)webpack.DefinePlugin允许创建一个在编译时可以配置的全局常量
举个例子:
webpack.DefinePlugin配置如下:
plugins: [
new webpack.DefinePlugin(
'process.env':
NODE_ENV: '"production"' // 或者'"development"' '"test"'
)
]
与启动命令结合:
"scripts":
"dev": "cross-env NODE_ENV=development webpack-dev-server --inline --progress --config build/webpack.dev.conf.js"
以上是关于webpack的一些基础知识的主要内容,如果未能解决你的问题,请参考以下文章