javascript - react 服務(wù)端渲染怎么處理less文件
問題描述
背景:用react.js + node 實現(xiàn)react組件服務(wù)端渲染。
由于當(dāng)前 node 還不支持 import,我用nodemon --harmony server.js --exec babel-node命令執(zhí)行,其中server.js是后端入口文件,這樣是可以支持 import 了,但是當(dāng)我使用 antd 的 Button 組件時卻報不支持 @import, 因為 antd 的 button 組件導(dǎo)入了 default.less,怎樣才能解決這個問題呢?
server.js
import React, { Component } from ’react’;import { renderToString } from ’react-dom/server’import { Button } from ’antd’;var Koa = require(’koa’);var app = new Koa();const render = require(’koa-ejs’);const path = require(’path’);render(app, { root: path.join(__dirname, ’server/view’), layout: ’template’, viewExt: ’html’, cache: false, debug: true});app.use(async function (ctx, next){ const html = renderToString( <Button>hello</Button> ); await ctx.render(’demo’, {’html’: html});});app.keys = [’i love yuewen’];app.listen(3000);
報錯如下:
/Users/joy.hu/Sites/yue/node_modules/antd/lib/style/index.less:1(function (exports, require, module, __filename, __dirname) { @import './themes/default'; ^SyntaxError: Invalid or unexpected token at createScript (vm.js:53:10) at Object.runInThisContext (vm.js:95:10) at Module._compile (module.js:543:28) at Module._extensions..js (module.js:580:10) at Object.require.extensions.(anonymous function) [as .js] (/Users/joy.hu/Sites/yue/node_modules/babel-register/lib/node.js:152:7) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at require (internal/module.js:20:19) at Object.<anonymous> (/Users/joy.hu/Sites/yue/node_modules/antd/lib/button/style/index.js:3:1)[nodemon] app crashed - waiting for file changes before starting...
問題解答
回答1:https://babeljs.io/docs/usage...
https://github.com/babel/exam...
相關(guān)文章:
1. django - 后臺返回的json數(shù)據(jù)經(jīng)過Base64加密,獲取時用python如何解密~!2. docker 17.03 怎么配置 registry mirror ?3. 老哥們求助啊4. tp6表單令牌5. 我的html頁面一提交,網(wǎng)頁便顯示出了我的php代碼,求問是什么原因?6. node.js - node 客戶端socket一直報錯Error: read ECONNRESET,用php的socket沒問題哈。。7. html5 - angularjs中外部模版加載無法使用8. css3 - 請問一下在移動端CSS布局布局中通常需要用到哪些元素,屬性?9. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?10. angular.js - 如何通俗易懂的解釋“依賴注入”?
