Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack require还可以这样用! #141

Open
mishe opened this issue Aug 24, 2016 · 1 comment
Open

webpack require还可以这样用! #141

mishe opened this issue Aug 24, 2016 · 1 comment

Comments

@mishe
Copy link
Owner

mishe commented Aug 24, 2016

webpack打包工具现在是超级流行,插件超多;成为目前最主流的打包工具。模块间的引入都是采用require的方式(ES6 用 import)来引入自身的依赖。

一般大家用require的时候都是采用以下几种方式;

载入当前目录下的a.js

方法1:

require('./a.js')

方法2

require('./a')

如果当前目录下有a,b,c,d....N个模块,当前模块有一个参数配置opt,这个配置是调用该模块的主模块传入的,决定引入那个子模块(a,b,c,d...),那么这个require应该怎么来写?

第一种写法

var a=require('a')
var b=require('b')
.....
var n=require('n')
eval(opt)():

有一个非常明显的缺点,需要显式的require所有模块;

第二中写法

var o={
a:'./a'
b:'./a'
...
n:'./a'
}
require(s[opt])

采用参数配置的方式,动态的根据opt的值来决定加载相应的模块。

@mishe
Copy link
Owner Author

mishe commented Feb 20, 2017

突然发现webpack 也支持 require('./view/'+id+'.html') 这种方式了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant