如何将加载程序输出转换为字符串 [英] How to cast loader output to a string
问题描述
我的 webpack 配置如下:
My webpack configuration looks like this:
var webpack = require('webpack');
module.exports = {
// ..
module: {
loaders: [
// ..
{
test: /\.scss$/,
loaders: ['css', 'sass']
}
]
}
};
我想要 require('./my-style.scss')
返回一个字符串.但是,现在它返回一个 Array
对象:
I want require('./my-style.scss')
to return a string. However, now it is returning an Array
object:
0: Array[3]
0: 223
1: "html,↵body,↵ol,↵ul,↵li,↵p { margin: 0; padding: 0; }↵"
2: ""
length: 3
i: (modules, mediaQuery) { .. }
length: 1
toString: toString()
我可以将 require 语句强制转换为字符串 (require('./my-style.scss').toString()
),尽管我希望 webpack 为我做这件事.
I can cast the require statement to a string (require('./my-style.scss').toString()
), though I'd webpack to do that for me.
如何修改加载器定义以生成字符串作为最终输出?
How do I modify the loader definition to produce a string as the final output?
推荐答案
我编写了一个将对象转换为字符串的小型加载程序,to-string.
I have written a small loader that casts object to a string, to-string.
它是一个简单的加载器,它像模块一样执行内容并将输出转换为字符串:
It is a simple loader that executes content like a module and casts output to a string:
/**
* @see https://github.com/webpack/webpack/wiki/Loader-Specification
*/
module.exports = function (content) {
return 'module.exports = ' + JSON.stringify(this.exec(content, this.resource).toString());
};
这篇关于如何将加载程序输出转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!