如何使用 webpack 从 npm 依赖项编译 .less 文件?
How to compile .less files from an npm dependency using webpack?
我正在使用 npm 模块 elemental
, which contains a /less
folder with all the relevant styling for its react ui. I'm currently trying to do this with less-loader
:
/tasks/config/webpack.js
:
'use strict';
let path = require('path');
let ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = function(grunt) {
grunt.config.set('webpack', {
client: {
entry: {
app: `${process.cwd()}/src/app.jsx`,
},
output: {
filename: '[name].js',
chunkFilename: '[id].js',
path: `${process.cwd()}/dist`,
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components|dist)/,
loader: 'babel',
query: {
presets: ['react', 'es2015'],
},
},
{
test: /\.less$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!less-loader'),
},
],
},
plugins: [
new ExtractTextPlugin('[name].css'),
],
},
});
grunt.loadNpmTasks('grunt-webpack');
};
/src/app.jsx
:
'use strict';
let path = require('path');
require(path.resolve(process.cwd(), 'node_modules/elemental/less/elemental.less'));
但这似乎不起作用,因为它会生成警告:
WARNING in ./src/app.jsx
Critical dependencies:
5:0-82 the request of a dependency is an expression
@ ./src/app.jsx 5:0-82
WARNING in ./src ^\.\/.*$
Module not found: Error: a dependency to an entry point is not allowed
@ ./src ^\.\/.*$
我将其解释为您不能使用包含 node_modules
.
中的文件的 require()
s
编辑
请注意,任何地方都没有 .css
文件输出,输出 /dist/app.js
也无法正常工作。
为什么不尝试只写 require('elemental/less/elemental.less');
呢?
在您的错误消息中:“5:0-82 依赖项的请求 是一个表达式”。
我正在使用 npm 模块 elemental
, which contains a /less
folder with all the relevant styling for its react ui. I'm currently trying to do this with less-loader
:
/tasks/config/webpack.js
:
'use strict';
let path = require('path');
let ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = function(grunt) {
grunt.config.set('webpack', {
client: {
entry: {
app: `${process.cwd()}/src/app.jsx`,
},
output: {
filename: '[name].js',
chunkFilename: '[id].js',
path: `${process.cwd()}/dist`,
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components|dist)/,
loader: 'babel',
query: {
presets: ['react', 'es2015'],
},
},
{
test: /\.less$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!less-loader'),
},
],
},
plugins: [
new ExtractTextPlugin('[name].css'),
],
},
});
grunt.loadNpmTasks('grunt-webpack');
};
/src/app.jsx
:
'use strict';
let path = require('path');
require(path.resolve(process.cwd(), 'node_modules/elemental/less/elemental.less'));
但这似乎不起作用,因为它会生成警告:
WARNING in ./src/app.jsx
Critical dependencies:
5:0-82 the request of a dependency is an expression
@ ./src/app.jsx 5:0-82
WARNING in ./src ^\.\/.*$
Module not found: Error: a dependency to an entry point is not allowed
@ ./src ^\.\/.*$
我将其解释为您不能使用包含 node_modules
.
require()
s
编辑
请注意,任何地方都没有 .css
文件输出,输出 /dist/app.js
也无法正常工作。
为什么不尝试只写 require('elemental/less/elemental.less');
呢?
在您的错误消息中:“5:0-82 依赖项的请求 是一个表达式”。