简体   繁体   English

如何让Webpack中的Babel忽略.less文件

[英]How to have Babel in Webpack ignore .less files

I am trying to load less files directly from node_modules, specifically the 'react-widgets' library. 我试图直接从node_modules加载较少的文件,特别是'react-widgets'库。 I set up a very simple example following the directions from their site , into a basic template . 我根据他们网站的指示设置了一个非常简单的示例,进入基本模板 To my dismay I get the following error message: 令我沮丧的是,我收到以下错误消息:

 D:\GitHub\react-webpack-template\node_modules\react-widgets\lib\less\react-widgets.less:1
 (function (exports, require, module, __filename, __dirname) { @import "./variables.less";
                                                               ^
 SyntaxError: Unexpected token ILLEGAL
 at Object.exports.runInThisContext (vm.js:53:16)
 at Module._compile (module.js:513:28)
 at Object.Module._extensions..js (module.js:550:10)
 at Module.load (module.js:458:32)
 at tryModuleLoad (module.js:417:12)
 at Function.Module._load (module.js:409:3)
 at Module.require (module.js:468:17)
 at require (internal/module.js:20:19)
 at Object.<anonymous> (D:\GitHub\react-webpack-template\webpack.config.js:4:1)
 at Module._compile (module.js:541:32)

It's complaining about the @import at the start of the file. 它抱怨文件开头的@import。 This is valid Less so I'm pretty sure it's Babel interfering, but I'm unsure how to get it to ignore my .less files. 这是有效的,所以我很确定它是Babel干扰,但我不确定如何让它忽略我的.less文件。

This is my webpack config 这是我的webpack配置

 require('react-widgets/lib/less/react-widgets.less') module.exports = { entry: './index', output: { filename: 'browser-bundle.js' }, devtool: 'source-map', module: { loaders: [ { test: /\\.js$/, loader: 'babel-loader', query: { presets: ['es2015', 'react'] } }, { test: /\\.css$/, loader: 'style-loader!css-loader' }, { test: /\\.less$/, loader: 'style-loader!css-loader!less-loader' }, { test: /\\.gif$/, loader: "url-loader?mimetype=image/png" }, { test: /\\.woff(2)?(\\?v=[0-9].[0-9].[0-9])?$/, loader: "url-loader?mimetype=application/font-woff" }, { test: /\\.(ttf|eot|svg)(\\?v=[0-9].[0-9].[0-9])?$/, loader: "file-loader?name=[name].[ext]" }, ] } }; 

Similar SO responses that I've attempted but still left me scratching my head. 类似的SO回复,我试过,但仍然让我挠头。

What else should I try? 我还应该尝试什么?

The problem is that if that is your webpack config, you can't require('react-widgets/lib/less/react-widgets.less') inside of it. 问题是,如果这是你的webpack配置,你不能require('react-widgets/lib/less/react-widgets.less') -widgets require('react-widgets/lib/less/react-widgets.less') That goes inside a component somewhere. 这是在某个组件内部。

This actually has nothing to do with babel, and everything to do with webpack itself. 这实际上与babel无关,而且与webpack本身有关。 The problem you're running into is you're trying to require/import a .less file inside your webpack configuration file . 您遇到的问题是您正在尝试在webpack配置文件中要求/导入.less 文件 That just plain isn't going to work, as that's not how webpack is designed. 这简直不行,因为这不是webpack的设计方式。 Nothing inside your webpack.config.js file makes it down to the client, it's meant to configure how your application code is built. webpack.config.js文件中没有任何内容可以归结为客户端,它旨在配置应用程序代码的构建方式。

Since you already have less-loader configured, you need to move the line: 由于您已经配置了less-loader ,因此您需要移动该行:

require('react-widgets/lib/less/react-widgets.less')

out of webpack.config.js and into your entry point ( ./index.js in this case). webpack.config.js进入你的入口点(在这种情况下为./index.js )。

(I've kept my original answer below). (我在下面保留了原来的答案)。


(Original answer) (原始答案)

You don't want to ignore them, you want to process them with less-loader . 你不想忽略它们,你想用less-loader处理它们。 That will compile them to valid css, and then you can use the style loader to load them into the document: 这会将它们编译为有效的css,然后你可以使用样式加载器将它们加载到文档中:

$ npm install --save-dev less less-loader css-loader style-loader

and then in webpack config: 然后在webpack配置中:

loaders: [
    { test: /\.less$/, loader: 'style!css!less' }
]

From the less-loader github page : less-loader github页面

webpack provides an advanced mechanism to resolve files. webpack提供了一种解决文件的高级机制。 The less-loader stubs less' fileLoader and passes all queries to the webpack resolving engine. 较少的加载器存根较少'fileLoader并将所有查询传递给webpack解析引擎。 Thus you can import your less-modules from node_modules. 因此,您可以从node_modules导入较少的模块。 Just prepend them with a ~ which tells webpack to look-up the modulesDirectories 只需在它们前面添加一个〜,它告诉webpack查找modulesDirectories

 @import "~bootstrap/less/bootstrap"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM