我正在使用實現Browserify-Rails來實現Babelify的Rails 4.0項目。我缺少什麼Babelify預設/插件來解析此代碼?
在我的本地和遠程Beta服務器上,似乎一切都會好起來。但是當我部署到生產環境時,如果某些文件中包含某個文件,則會跳過某個文件require("")
。
我原來的設置是這樣的:
config.browserify_rails.commandline_options = ["-t [ babelify --presets [ es2015 react stage-0 ] --plugins [ syntax-async-functions transform-regenerator ] ]"]
這使得它跳過那些有這條線中有一個文件:
var { configureStore } = require('./configureStore');
需要在這裏導致:
let { createStore, combineReducers, applyMiddleware, compose } = require('redux');
let thunk = require('redux-thunk').default
let session = require('./reducers/session').default;
let search = require('./reducers/search').default;
const configureStore =() => {
const reducers = combineReducers({
session,
search,
});
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
return createStore(
reducers,
composeEnhancers(applyMiddleware(thunk))
);
}
export { configureStore };
我猜測也許有一些我在這裏使用的JS風格超過ES2015
因此,我繼續前進並使用配置,添加Incremental,並更新到ES2017。哪個讓我通過了那個破碎的文件。我的新的配置是這樣的:
config.browserify_rails.use_browserifyinc = true
config.browserify_rails.commandline_options = ["-t [ babelify --presets [ es2017 react stage-0 ] --plugins [ syntax-async-functions transform-regenerator ] ]", "-t coffeeify --extension=\".js.coffee\""]
但以此爲據讓我爲這條線另一文件:
let { search } = require('../../actions/search');
它加載這一點(和你要知道,後續文件被解析並翻譯就好):
export const SEARCH_RESULTS_RECEIVED = 'SEARCH_RESULTS_RECEIVED';
export const search = (term, token) => {
return (dispatch) => {
if (term && term.trim() !== '') {
return _search(term, token).then((results) => dispatch(resultsReceived(results)));
} else {
return dispatch(resultsReceived([]));
}
}
};
const _search = (term, token) => {
let promise = new Promise((resolve, reject) => {
fetch(encodeURI(`/api/search?search=${term}`), {
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${token}`
}
})
.then(response => response.json())
.then(function(response) {
return resolve(response.results);
})
.catch((error) => {
return reject(error);
});
});
return promise;
};
export const resultsReceived = (searchResults) => ({
type: SEARCH_RESULTS_RECEIVED,
searchResults
});
我很好奇,如果有任何Babelify插件/預置,我可能會爲了使這項工作會丟失。
呦!非常感謝喬!我真的無法在任何地方找到答案。謝謝! – Trip
@Trip:很高興我能幫上忙,我想很多人都會因此而感到困惑!爲了將來的參考,所有正式的預設都記錄在這裏[http://babeljs.io/docs/plugins/]。 –