2017-02-21 7 views
0

저는 Babel을 프로그래밍 방식으로 사용합니다. 플러그인이 거의 필요하지 않고 모든 것이 잘 작동합니다.프로그래밍 방식으로 바벨 변환/플러그인에 옵션을 전달하는 방법은 무엇입니까?

Tho, 바벨 변환에 옵션을 전달하고 싶습니다. 나는 내가 아직 그 일을하지 않았 음을 깨달았고, 나는 단지 노력하지 않는 것이 효과가없는 것처럼 보였다.

특히 글로벌 엄격 모드를 사용하지 않으려면 babel-transform-strict-mode을 포함하고 strict:false을 전달하고 싶습니다. 내가 가진 내 코드에서

// with options 
{ 
    "plugins": [ 
    ["transform-strict-mode", { 
     "strict": true 
    }] 
    ] 
} 

:

워드 프로세서는 .babelrc 파일을 가지고 때를 사용하는 방법에 대해 설명

const babelify = require("babelify") 
    , es2015 = require("babel-preset-es2015") 
    , reactify = require("babel-preset-react") 
    , strictMode = require("babel-plugin-transform-strict-mode") 
    ; 

... 

this.browserify.transform(babelify, { 
    global: true, 
    babelrc: false, 
    presets: [ 
     reactify 
     , es2015 
     , [ 
      strictMode 
      , { strict: false } 
     ] 
    ] 
}); 

es2015하면서 presets 배열에 큰 reactify 일, strictMode 추가 { strict: false }은 작동하지 않습니다.

오류 :

ReferenceError: [BABEL] /home/.../index.js: Unknown option: foreign.visitor. 
Check out http://babeljs.io/docs/usage/options/ for more 
information about options. 

A common cause of this error is the presence of a 
configuration options object without the corresponding 
preset name. Example: 

Invalid: 
    `{ presets: [{option: value}] }` 
Valid: 
    `{ presets: [['presetName', {option: value}]] }` 

For more detailed information on preset configuration, 
please see 
http://babeljs.io/docs/plugins/#pluginpresets-options. 

나는이 다른 모듈의 일부이기 때문에, 그것은 분명, 모듈을 찾을 수 없습니다 이름 (["transform-strict-mode", { strict: false }]를) 변환 strictMode 대신 사용하는 경우.

require 모듈 (이 경우 strictMode)에 프로그래밍 방식으로 (babelrc없이) 옵션을 전달할 수 있습니까?

답변

1

일반적으로 ES6 모듈은 자동 엄격하므로 ES6 모듈 지원을 사용하지 않는 것이 좋습니다. 예 : 문제가 node_modules 물건을 파괴하기 때문에 특정 경우

this.browserify.transform(babelify, { 
    sourceType: 'script', 
    presets: [ 
    reactify, 
    [es2015, {modules: false}], 
    ], 
}) 

은이 때문에 global: true의 사용량이다.

node_modules 컨테이너 ES6이 있기 때문에 특별히 global: true을 전달한다고 가정합니다. 그렇다면, 당신은 babelify 같은 대한 ignore 정규식을 지정하여 컴파일 것을 허용 목록을해야합니다

// ... 
global: true, 
ignore: /node_modules\/(?:some_es6_module|some_other_es6_module)/, 
// ... 

some_es6_module 및 라는 이름의 모듈을 제외 경로에 node_modules있는 모든 파일을 무시. 그런 식으로 underscore 같은 것은 영향을받지 않습니다.

+0

아, 재미 있습니다. 그러면 foo를 "foo"에서 더 이상 가져올 수 없다는 뜻입니까? 나는 [this plugin] (https://github.com/genify/babel-plugin-transform-remove-strict-mode)을 사용하여 끝났는데, 아마도 그것은 단지 "엄격한 사용"을 제거 할 뿐이다. 감사! –

+0

그럴만 한 옵션입니다. 코드가 더 이상 규격을 따르지 않기 때문에 Node에 도착한 후에 ES6 모듈을 사용하려면 CommonJS를 사용하지 않고도이 문제를 다시 해결할 수 없습니다. – loganfsmyth

+0

클라이언트 측에서'if (this._) {...} '를 검사했지만'this'가'undefined' 였기 때문에 일부 라이브러리 (예 :'밑줄')를 사용하는 데 문제가있었습니다. 따라서 실패했습니다 . –

0

babel-plugin-transform-strict-mode는 미리 설정된 구성 요소가 아닌 플러그인이므로 plugins 옵션에서 plugin을 설정해야합니다.

this.browserify.transform(babelify, { 
global: true, 
babelrc: false, 
presets: [ 
    reactify 
    , es2015 
], 
plugins:[ 
    [ 
     strictMode 
     , { strict: false } 
    ] 
] 

});