Skip to content

Commit 7a2752f

Browse files
author
Zhicheng Wang
committed
Initial Commit
1 parent b36a2f0 commit 7a2752f

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

.gitignore

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Node template
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# node-waf configuration
27+
.lock-wscript
28+
29+
# Compiled binary addons (http://nodejs.org/api/addons.html)
30+
build/Release
31+
32+
# Dependency directories
33+
node_modules
34+
jspm_packages
35+
36+
# Optional npm cache directory
37+
.npm
38+
39+
# Optional eslint cache
40+
.eslintcache
41+
42+
# Optional REPL history
43+
.node_repl_history
44+
45+
# Output of 'npm pack'
46+
*.tgz
47+
48+
# Yarn Integrity file
49+
.yarn-integrity
50+
51+
### JetBrains template
52+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
53+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
54+
55+
# User-specific stuff:
56+
.idea/workspace.xml
57+
.idea/tasks.xml
58+
59+
# Sensitive or high-churn files:
60+
.idea/dataSources/
61+
.idea/dataSources.ids
62+
.idea/dataSources.xml
63+
.idea/dataSources.local.xml
64+
.idea/sqlDataSources.xml
65+
.idea/dynamic.xml
66+
.idea/uiDesigner.xml
67+
68+
# Gradle:
69+
.idea/gradle.xml
70+
.idea/libraries
71+
72+
# Mongo Explorer plugin:
73+
.idea/mongoSettings.xml
74+
75+
## File-based project format:
76+
*.iws
77+
78+
## Plugin-specific files:
79+
80+
# IntelliJ
81+
/out/
82+
83+
# mpeltonen/sbt-idea plugin
84+
.idea_modules/
85+
86+
# JIRA plugin
87+
atlassian-ide-plugin.xml
88+
89+
# Crashlytics plugin (for Android Studio and IntelliJ)
90+
com_crashlytics_export_strings.xml
91+
crashlytics.properties
92+
crashlytics-build.properties
93+
fabric.properties

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# css-with-mapping loader for webpack
2+
3+
## Usage
4+
5+
```js
6+
var result = require('css-with-mapping!css!sass!./test.scss');
7+
// => returns sass rendered to CSS with /*# sourceURL=... */ and /*# sourceMappingURL=... */.
8+
```
9+
10+
## Use Case
11+
12+
In some cases (e.g. Angular2 [Component Styles](https://angular.io/docs/ts/latest/guide/component-styles.html)) you need to have style as a string.
13+
14+
You can cast the require output to a string, e.g.
15+
16+
```typescript
17+
@Component({
18+
selector: 'my-css-test',
19+
template: '<div>some content</div>',
20+
styles: [
21+
require('css-with-mapping!css!sass!./test.scss') // Of course, you will certainly configure the loader in the webpack configuration
22+
]
23+
})
24+
```
25+
26+
** Note that the current version of Angular (2.4.5) will lose the sourceURL and sourceMappingURL comment, so unless you patch the code for Angular, otherwise the loader will not work properly. Follow Pull Request https://github.com/angular/angular/pull/14175 for more information **

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "css-with-mapping-loader",
3+
"description": "css to string with source mapping loader for webpack",
4+
"version": "0.0.1",
5+
"main": "./src/css-with-mapping.js",
6+
"author": {
7+
"name": "Zhicheng Wang",
8+
"email": "asnowwolf@gmail.com",
9+
"url": "https://github.com/asnowwolf/css-with-mapping-loader"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git@github.com:asnowwolf/css-with-mapping-loader.git"
14+
},
15+
"keywords": [
16+
"webpack",
17+
"css-with-mapping",
18+
"css-to-string",
19+
"to-string"
20+
],
21+
"license": "MIT",
22+
"dependencies": {
23+
"convert-source-map": "^1.3.0"
24+
}
25+
}

src/css-with-mapping.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @see https://webpack.github.io/docs/loaders.html
3+
*/
4+
module.exports = function (content) {
5+
if (!content || content.length !== 1) {
6+
throw new Error('The css-with-mapping-loader must be chained after the css-loader');
7+
}
8+
var cssObject = content[0];
9+
var cssContent = cssObject[1] || '';
10+
var cssMapping = cssObject[3];
11+
if (!cssMapping) {
12+
return cssContent;
13+
}
14+
var convertSourceMap = require('convert-source-map');
15+
var sourceMapping = convertSourceMap.fromObject(cssMapping).toComment({multiline: true});
16+
var sourceURLs = cssMapping.sources.map(function (source) {
17+
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
18+
});
19+
return [cssContent].concat(sourceURLs).concat([sourceMapping]).join('\n');
20+
};

0 commit comments

Comments
 (0)