to-string-loader
raw JSON → 1.2.0 verified Sat Apr 25 auth: no javascript maintenance
Webpack loader that converts CSS/SASS compiled output from an array to a plain string, useful for frameworks like Angular that require styles as strings. Current stable version: 1.2.0 (last released 2016-10-15). No recent updates; primarily used in legacy Angular 2 projects. Alternative: css-to-string-loader or raw-loader for similar functionality. Works with webpack 1-2; compatibility with webpack 3+ is unclear.
Common errors
error Module parse failed: Unexpected character '@' (1:0) ↓
cause Missing sass-loader before to-string-loader in loaders array.
fix
Add sass-loader to loaders: ['to-string', 'css', 'sass'].
error Cannot find module 'to-string-loader' ↓
cause Module not installed.
fix
Run: npm install --save-dev to-string-loader
Warnings
deprecated Package has not been updated since 2016; consider using css-to-string-loader or raw-loader. ↓
fix Replace with 'css-to-string-loader' or 'raw-loader' in modern webpack setups.
gotcha Loader must be placed before css and sass in the loaders array, or the order is wrong. ↓
fix Ensure loaders order is: ['to-string', 'css', 'sass'].
Install
npm install to-string-loader yarn add to-string-loader pnpm add to-string-loader Imports
- default wrong
import { toString } from 'to-string-loader'correctimport toString from 'to-string-loader' - require (runtime usage) wrong
let output = require('to-string!./my.scss');correctlet output = require('to-string!css!sass!./my.scss'); - webpack config loader wrong
loaders: ['to-string-loader', 'css', 'sass']correctloaders: ['to-string', 'css', 'sass']
Quickstart
// webpack.config.js
module.exports = {
module: {
loaders: [
{
test: /\.scss$/,
loaders: ['to-string', 'css', 'sass']
}
]
}
};
// In your code (e.g., Angular component)
@Component({
styles: [require('./my.scss')] // now returns a string
})
export class MyComponent {}
// Or inline:
let style = require('to-string!css!sass!./my.scss');