te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Accessing global variable after webpack build - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing global variable after webpack build - Stack Overflow

programmeradmin4浏览0评论

Let I have a file index.js with content

var a = 1;

I have index.html file which include index.js using <script> tag. When I open index.html page in browser, then the a variable can be accessed directly in browser console. Because a have global scope and it is global variable.

Now I am using npm and webpack. My package.json file content is

"scripts": {
  "build": "webpack"
},

and webpack.config.js file content is

module.exports = {
entry: './index.js',
output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
}

Now I run npm build then new file is created in dist folder named bundle.js.

Now I replace index.js from script tag of index.html file with dist/bundle.js .

Now I run index.html in browser, but variable a is no longer access in browser console directly. It is not a global variable now.

My question is : Is there a way by which we can access variable a globally just like we access initially?

I have not found exact answer on internet. I found a loader expose-loader but it also can't solve my problem.

Let I have a file index.js with content

var a = 1;

I have index.html file which include index.js using <script> tag. When I open index.html page in browser, then the a variable can be accessed directly in browser console. Because a have global scope and it is global variable.

Now I am using npm and webpack. My package.json file content is

"scripts": {
  "build": "webpack"
},

and webpack.config.js file content is

module.exports = {
entry: './index.js',
output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
}

Now I run npm build then new file is created in dist folder named bundle.js.

Now I replace index.js from script tag of index.html file with dist/bundle.js .

Now I run index.html in browser, but variable a is no longer access in browser console directly. It is not a global variable now.

My question is : Is there a way by which we can access variable a globally just like we access initially?

I have not found exact answer on internet. I found a loader expose-loader but it also can't solve my problem.

Share Improve this question edited Jul 16, 2018 at 0:20 intentionally-left-nil 8,3048 gold badges39 silver badges62 bronze badges asked Jul 15, 2018 at 18:44 Meraj AhmedMeraj Ahmed 2781 gold badge6 silver badges14 bronze badges 8
  • 1 what do you need the global var for ? I mean what is the use case? – Luillyfe Commented Jul 15, 2018 at 19:44
  • 1 Possible duplicate of Define global variable with webpack – intentionally-left-nil Commented Jul 16, 2018 at 0:21
  • @Luillyfe I have old writtten many js file for a website which are included in html by script tag. Many function and variable are globally define, so that they can access by other included javascript file. – Meraj Ahmed Commented Jul 16, 2018 at 6:51
  • Buy after the build what you need for ? Before the build you can still access the var, Right? – Luillyfe Commented Jul 16, 2018 at 7:07
  • @Luillyfe I want to minify js file by using UglifyJsPlugin – Meraj Ahmed Commented Jul 16, 2018 at 7:47
 |  Show 3 more ments

1 Answer 1

Reset to default 12

When webpack bundles your javascript it wraps all of your individual files/modules in functions so they are no longer run in the global scope, therefore if you want to make a variable global you have to explicitly set it on the window object, i.e.

window.a = 1;

This will make a accessible from the browser console in index.html.

发布评论

评论列表(0)

  1. 暂无评论