我正在使用实现Browserify-Rails的 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 };
我猜也许我在这里使用了几个 ES2015 以上的 JS 样式
所以我继续玩配置,添加增量,并更新到 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 插件/预设来完成这项工作。