์ค๋ 1.9.2์์ 1.10.2๋ก ์
๋ฐ์ดํธํ์ผ๋ฉฐ require('underscore');
์ฌ์ฉํ ๋ ๋ฏน์ค์ธ์ด ๋ ์ด์ ์๋ํ์ง ์์ต๋๋ค.
TypeError: _.camelcase is not a function
์คํจํฉ๋๋ค.
const _ = require('underscore');
const underscoreString = require('underscore.string');
_.mixin(underscoreString.exports());
console.log(_.camelcase('foo-bar'));
๊ทธ๋ฌ๋ ์ด๊ฒ์ ์๋ํฉ๋๋ค.
import _ from 'underscore';
const underscoreString = require('underscore.string');
_.mixin(underscoreString.exports());
console.log(_.camelcase('foo-bar'));
@dmaicher ๋์ ๋ฌผ์ด๋ฏ์ด์ ์ฃ์กํฉ๋๋ค. ๊ทธ๋ฌ๋ Underscore 1.10.2 ๋ฐ underscore.string 3.3.5์์๋ ์ด๊ฒ์ ์ฌํํ ์ ์์ต๋๋ค. ์ค์ ์ ํน๋ณํ ๊ฒ์ด ์์ต๋๊น?
๋น ๋ฅธ ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค :blush:
[email protected]
๋ฅผ ์ฌ์ฉํ๊ณ ์๋๋ฐ ๋ธ๋ผ์ฐ์ ์ ๋ฒ๋ค๋ก ์ ๊ณต๋๋ ์ฝ๋์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. CLI์์ ๋
ธ๋๋ก ์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ฌธ์ ๊ฐ ์๋ ๊ฒ์ฒ๋ผ ๋ณด์
๋๋ค.
๋ค์์ ์ต์ํ์ ์ฌ์์ฐ์ ๋๋ค. https://github.com/dmaicher/underscore_issue_2852
๋ฌธ์ ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๊ทธ ์์
๊ฒฝ๋ก๋, underscore/modules/index-all.js
์์์ ์ป์ ์นํฉํฉ๋๋ค module
์ฐ๋ฆฌ์ ํ๋ package.json
:
๋ฐ๋ฉด main
ํ๋๋ CommonJS ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฌ์ฉํด์ผ ํ๋ ํ๋์
๋๋ค. module
๋ ์ด๋ฅผ ์ง์ํ๋ ๋๊ตฌ์์ ES ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ํ ๊ฒ์ด๊ณ main
๋ ๋ค๋ฅธ ๋ชจ๋ ๊ฒ์ ์ํ ๊ฒ์
๋๋ค. ์ด๊ฒ์ ์ฝ๋๊ฐ import
์์๋ ์๋ํ์ง๋ง require()
์์๋ ์๋ํ์ง ์๋ ์ด์ ๋ฅผ ์ค๋ช
ํฉ๋๋ค.
๋ถ๋ช
ํ WebPack์ ํ์ฌ ๊ธฐ๋ณธ์ ์ผ๋ก ES ๋ชจ๋์ ๊ฐ์ ํ์ง๋ง CommonJS ๊ฐ์ ธ์ค๊ธฐ์ ๋ํ ๋์ฒด ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ES ๋ชจ๋์ ๊ฐ์ ํ์ง ์๊ฑฐ๋ module
ํ๋๋ฅผ ๋ฌด์ํ๋๋ก ์ด๋ป๊ฒ๋ ๊ตฌ์ฑํด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. ๋๋ ES ๋ชจ๋๋ก ์ ํํ๊ณ ์ถ์ ์๋ ์์ต๋๋ค.
์ด๊ฒ์ ๋ฐ์ค ๋ฌธ์ ๊ฐ ์๋๋ผ WebPack ๋ฌธ์ ์ด๊ธฐ ๋๋ฌธ์ ์ง๊ธ ๋ซ์ต๋๋ค. ๊ทธ๋๋ ๊ณ์ํด์ ๋๊ธ์ ๋จ๊ฒจ์ฃผ์ธ์.
@jgonggrijp ํ์ธ
webpack ๊ตฌ์ฑ์์ ํด๊ฒฐํ๊ธฐ ์ํ ๋ณ์นญ์ ์ถ๊ฐํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์์ต๋๋ค.
'underscore': 'underscore/underscore.js',
๊ทธ๋ฌ๋ ์ด๊ฒ์ ๋์๊ฒ ์กฐ๊ธ ์ด์ํ๊ฒ ๋ณด์
๋๋ค. ๋ง์ ๋ค๋ฅธ ํจํค์ง๋ ๋ด๊ฐ ์ง์ ์ฌ์ฉ "main"
๋ฐ "module"
์์ ์ ๋ด๋ถ package.json
๊ทธ๋ค์ ์ํด ์๋ ์.
Webpack์ ['browser', 'module', 'main']
์์๋ก ์ฐพ์ ์ฒซ ๋ฒ์งธ ํ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
https://webpack.js.org/configuration/resolve/#resolvemainfields ์ฐธ์กฐ
๋ฐ์ค์๋ง ์คํจํ์ง๋ง ๋ค๋ฅธ ํจํค์ง์์๋ ์๋ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค.
๋ค๋ฅธ ํจํค์ง์ ์ด๋ฆ์ ์ง์ ํ๋ฉด ๊ธฐ๊บผ์ด ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์ด์จ๋ module
๋ฐ main
๋ ์ํธ ๊ตํ์ด ๊ฐ๋ฅํ์ง ์์ผ๋ฏ๋ก WebPack์ด ์ด๋ฅผ ๊ทธ๋๋ก ์ทจ๊ธํ๋ ๊ฒ์ ๋ถ๋นํฉ๋๋ค.
https://github.com/rollup/rollup/wiki/pkg.module
๋งํฌ๋ ๋ฌธ์๋ resolve.mainFields
๋ฅผ ['main']
๋ก ์ค์ ํ๋ ์ฝ๊ฐ ๋ ์ถํ ์๋ฃจ์
์ ๋ํ ํํธ๋ฅผ ์ ๊ณตํฉ๋๋ค. ๋ค์ ๋งํ์ง๋ง WebPack์ CommonJS ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ฌ์ฉํ๊ณ ์๊ธฐ ๋๋ฌธ์ ์ฒ์๋ถํฐ module
๋ฅผ ๊ณ ๋ คํด์๋ ์ ๋๋ค๊ณ ์๊ฐํฉ๋๋ค.
module
๋ฐ main
๊ฐ ๋ชจ๋ ์๋ ํจํค์ง๊ฐ ์ฌ๊ธฐ์ ์๋ ๊ฒฝ์ฐ๋ฅผ ๋๋นํ์ฌ:
https://www.npmjs.com/package/vuejs-datepicker
https://www.npmjs.com/package/bootstrap-vue
https://www.npmjs.com/package/acorn
https://www.npmjs.com/package/perfect-scrollbar
๊ด๋ จ ์นํฉ ๋ฌธ์ : https://github.com/webpack/webpack/issues/5756
์, ๊ทธ๊ฒ์ ๋น์ ์ ๋ฌธ์ ์
๋๋ค. module
ํด์ํ ๋ WebPack์ด CommonJS๋ฅผ ์๋ฎฌ๋ ์ดํธํ๋ ๋ฐฉ์์๋ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ ์์
์ ์ํํ๋ ๊ฒ์ผ๋ก ๋ณด์
๋๋ค.
// converting modules/index-all.js to ES3
var underscoreESModule = {
'default': _,
map: _.map,
filter: _.filter,
// ...
};
// in your code that imports underscore
var _ = underscoreESModule;
ํ์์ ํ์ ๋ค์๊ณผ ๊ฐ์ด ํ๋ ๊ฒฝ์ฐ ์๋ฏธ๊ฐ ์์ต๋๋ค.
import * as _ from 'underscore';
ํ์ง๋ง ์ค์ ๋ก ํ๋ ์ผ์ ์ด๊ฒ๊ณผ ๋์ผํฉ๋๋ค.
import _ from 'underscore';
WebPack์์ ES3๋ก ๋ณํํ๋ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
var _ = underscoreESModule['default'];
์ด๊ฒ์ ๋ค๋ฅธ ์ฌ๋๋ค์ด webpack/webpack#5756์์ ๊ฒช๊ณ ์๋ ๋ฌธ์ ์ด๊ธฐ๋ ํฉ๋๋ค.
Browserify ๋ฐ Rollup๊ณผ ๊ฐ์ ๋ค๋ฅธ ๋น๋ ๋๊ตฌ๋ ์ด๋ฅผ ๋ณด๋ค ์ง๋ฅ์ ์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค. ๋ง๋ง...