Underscore: WebPack์€ require('underscore')๋ฅผ ์ž˜๋ชป ์—๋ฎฌ๋ ˆ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2020๋…„ 05์›” 25์ผ  ยท  9์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: jashkenas/underscore

์˜ค๋Š˜ 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'));

๋ชจ๋“  9 ๋Œ“๊ธ€

@dmaicher ๋‹˜์„ ๋ฌผ์–ด๋œฏ์–ด์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Underscore 1.10.2 ๋ฐ underscore.string 3.3.5์—์„œ๋Š” ์ด๊ฒƒ์„ ์žฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์„ค์ •์— ํŠน๋ณ„ํ•œ ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๊นŒ?

๋น ๋ฅธ ๋‹ต๋ณ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :blush:

[email protected] ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๋ธŒ๋ผ์šฐ์ €์— ๋ฒˆ๋“ค๋กœ ์ œ๊ณต๋˜๋Š” ์ฝ”๋“œ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. CLI์—์„œ ๋…ธ๋“œ๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ตœ์†Œํ•œ์˜ ์žฌ์ƒ์‚ฐ์ž…๋‹ˆ๋‹ค. https://github.com/dmaicher/underscore_issue_2852

๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

https://github.com/dmaicher/underscore_issue_2852/blob/348eef226ec7392b2ced2b92ddf2fcba2517ab5c/public/build/app.js#L13

๊ทธ ์ˆ˜์ž… ๊ฒฝ๋กœ๋Š”, underscore/modules/index-all.js ์˜์—์„œ ์–ป์€ ์›นํŒฉํ•ฉ๋‹ˆ๋‹ค module ์šฐ๋ฆฌ์— ํ•„๋“œ package.json :

https://github.com/jashkenas/underscore/blob/5d8ab5e37c9724f6f1181c5f95d0020815e4cb77/package.json#L17 -L18

๋ฐ˜๋ฉด 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๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ๋นŒ๋“œ ๋„๊ตฌ๋Š” ์ด๋ฅผ ๋ณด๋‹ค ์ง€๋Šฅ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ง๋งŒ...

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰