javascript - react 服務端渲染怎么處理less文件
問題描述
背景:用react.js + node 實現(xiàn)react組件服務端渲染。
由于當前 node 還不支持 import,我用nodemon --harmony server.js --exec babel-node命令執(zhí)行,其中server.js是后端入口文件,這樣是可以支持 import 了,但是當我使用 antd 的 Button 組件時卻報不支持 @import, 因為 antd 的 button 組件導入了 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...
相關文章:
1. python bottle跑起來以后,定時執(zhí)行的任務為什么每次都重復(多)執(zhí)行一次?2. node.js - vue中 post數(shù)據(jù)遇到問題3. javascript - ios返回不執(zhí)行js怎么解決?4. javascript - angular使從elastichearch中取出的文本高亮顯示,如圖所示5. javascript - 求幫助 , ATOM不顯示界面!!!!6. 前端 - 誰來解釋下這兩個 CSS selector 區(qū)別7. html5 - HTML代碼中的文字亂碼是怎么回事?8. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處9. javascript - vue2如何獲取v-model變量名10. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題
