Skip to content

Commit b071848

Browse files
committed
WIP
0 parents  commit b071848

27 files changed

+8247
-0
lines changed

.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
lib/
2+
node_modules/
3+
test/fixtures/
4+
test/integration/
5+
__file_snapshots__/

.eslintrc.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"plugins": ["prettier"],
3+
"extends": ["4catalyzer", "4catalyzer-jest", "prettier"],
4+
"env": {
5+
"node": true
6+
},
7+
"rules": {
8+
"prettier/prettier": "error",
9+
"global-require": "off",
10+
"import/no-dynamic-require": "off"
11+
}
12+
}

.gitignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
# Logs
3+
logs
4+
*.log
5+
npm-debug.log*
6+
yarn-debug.log*
7+
yarn-error.log*
8+
9+
# Runtime data
10+
pids
11+
*.pid
12+
*.seed
13+
*.pid.lock
14+
15+
# Directory for instrumented libs generated by jscoverage/JSCover
16+
lib-cov
17+
18+
# Coverage directory used by tools like istanbul
19+
coverage
20+
21+
# nyc test coverage
22+
.nyc_output
23+
24+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
25+
.grunt
26+
27+
# Bower dependency directory (https://bower.io/)
28+
bower_components
29+
30+
# node-waf configuration
31+
.lock-wscript
32+
33+
# Compiled binary addons (http://nodejs.org/api/addons.html)
34+
build/Release
35+
36+
# Dependency directories
37+
node_modules/
38+
jspm_packages/
39+
40+
# Typescript v1 declaration files
41+
typings/
42+
43+
# Optional npm cache directory
44+
.npm
45+
46+
# Optional eslint cache
47+
.eslintcache
48+
49+
# Optional REPL history
50+
.node_repl_history
51+
52+
# Output of 'npm pack'
53+
*.tgz
54+
55+
# Yarn Integrity file
56+
.yarn-integrity
57+
58+
# dotenv environment variables file
59+
.env
60+

.prettierignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**/coverage/**
2+
**/lib/**
3+
**/fixtures/**
4+
**/flow-typed/**
5+
**/node_modules/**
6+
**/CHANGELOG.md
7+
**/package.json

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
node_js:
3+
- '10'
4+
- '8'
5+
6+
cache: yarn
7+
8+
branches:
9+
only:
10+
- master

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 4Catalyzer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

getLocalName.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/** Taken from css-loader */
2+
3+
const path = require('path');
4+
const cssesc = require('cssesc');
5+
const loaderUtils = require('loader-utils');
6+
7+
// eslint-disable-next-line no-control-regex
8+
const filenameReservedRegex = /[<>:"/\\|?*\x00-\x1F]/g;
9+
// eslint-disable-next-line no-control-regex
10+
const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g;
11+
const reRelativePath = /^\.+/;
12+
13+
module.exports = function getLocalName(
14+
localName,
15+
loaderContext,
16+
loaderOptions,
17+
) {
18+
const hashPrefix = loaderOptions.hashPrefix || '';
19+
const context = loaderOptions.context || loaderContext.rootContext;
20+
const request = path.relative(context || '', loaderContext.resourcePath);
21+
22+
// eslint-disable-next-line no-param-reassign
23+
const content = `${hashPrefix + request}+${localName}`;
24+
25+
// Using `[path]` placeholder outputs `/` we need escape their
26+
// Also directories can contains invalid characters for css we need escape their too
27+
return cssesc(
28+
loaderUtils
29+
.interpolateName(
30+
loaderContext,
31+
loaderOptions.localIdentName || '[name]--[local]--[hash:base64:5]',
32+
{
33+
context,
34+
content,
35+
hashPrefix,
36+
regexp: loaderOptions.localIdentRegExp || null,
37+
},
38+
)
39+
// For `[hash]` placeholder
40+
.replace(/^((-?[0-9])|--)/, '_$1')
41+
.replace(filenameReservedRegex, '-')
42+
.replace(reControlChars, '-')
43+
.replace(reRelativePath, '-')
44+
.replace(/\./g, '-'),
45+
{ isIdentifier: true },
46+
).replace(/\\\[local\\\]/gi, localName);
47+
};

index.js

Whitespace-only changes.

loader.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const Processor = require('@modular-css/processor');
2+
const path = require('path');
3+
const { promisify } = require('util');
4+
const { getOptions } = require('loader-utils');
5+
6+
const getLocalName = require('./getLocalName');
7+
8+
const PROCESSOR = Symbol('@modular-css processor');
9+
10+
function getLoadFilePrefix(loaderContext) {
11+
// loads a file with all loaders configured after this one
12+
const loadersRequest = loaderContext.loaders
13+
.slice(loaderContext.loaderIndex + 1)
14+
.map(x => x.request)
15+
.join('!');
16+
17+
return `-!${require.resolve('./stringifyLoader')}!${loadersRequest}!`;
18+
}
19+
20+
function loader(src) {
21+
const { resourcePath, _compilation: compilation } = this;
22+
const cb = this.async();
23+
24+
const options = getOptions(this) || {};
25+
const dir = path.dirname(resourcePath);
26+
27+
const prefix = getLoadFilePrefix(this);
28+
29+
const loadModule = promisify((request, done) =>
30+
this.loadModule(request, (err, moduleSource) => {
31+
done(err, JSON.parse(moduleSource.toString()));
32+
}),
33+
);
34+
35+
if (!compilation[PROCESSOR]) {
36+
compilation[PROCESSOR] = new Processor({
37+
namer: (_, localName) => getLocalName(localName, this, options),
38+
async loadFile(file) {
39+
// console.log('LOAD');
40+
const txt = await loadModule(`${prefix}${file}`);
41+
// console.log(txt);
42+
return txt;
43+
},
44+
});
45+
}
46+
47+
const processor = compilation[PROCESSOR];
48+
49+
const process = async () => {
50+
const { details, exports } = await processor.string(resourcePath, src);
51+
52+
const deps = processor.dependencies(resourcePath);
53+
54+
const icssImport = deps.reduce(
55+
(acc, dep) => `${acc}@import "${path.relative(dir, dep)}";\n`,
56+
'',
57+
);
58+
59+
let icssExport = ':export {\n';
60+
for (const [key, value] of Object.entries(exports)) {
61+
icssExport += ` ${key}: ${value.join(' ')}\n`;
62+
}
63+
icssExport += '}';
64+
65+
return `${details.result.css}\n\n${icssImport}\n\n${icssExport}`;
66+
};
67+
68+
return process().then(
69+
r => cb(null, r),
70+
err => {
71+
cb(err);
72+
},
73+
);
74+
}
75+
76+
module.exports = loader;

package.json

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"name": "css-module-loader",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/4Catalyzer/css-module-loader.git"
8+
},
9+
"author": "4Catalyzer",
10+
"license": "MIT",
11+
"scripts": {
12+
"format": "4c format src './*'",
13+
"lint": "4c lint src './*'",
14+
"release": "4c release",
15+
"tdd": "jest --watch",
16+
"test": "yarn lint && jest",
17+
"testonly": "jest"
18+
},
19+
"publishConfig": {
20+
"access": "public"
21+
},
22+
"prettier": "@4c/prettier-config",
23+
"husky": {
24+
"hooks": {
25+
"pre-commit": "lint-staged"
26+
}
27+
},
28+
"lint-staged": {
29+
"*": [
30+
"yarn 4c lint --fix",
31+
"git add"
32+
]
33+
},
34+
"jest": {
35+
"preset": "@4c/jest-preset",
36+
"testEnvironment": "node",
37+
"setupFilesAfterEnv": [
38+
"./test/setup.js"
39+
],
40+
"watchPathIgnorePatterns": [
41+
"build",
42+
"__file_snapshots__"
43+
]
44+
},
45+
"release": {},
46+
"devDependencies": {
47+
"@4c/cli": "^0.7.12",
48+
"@4c/import-sort": "^4.3.3",
49+
"@4c/jest-preset": "^1.3.0",
50+
"@4c/prettier-config": "^1.1.0",
51+
"css-loader": "^3.2.0",
52+
"eslint": "^5.12.0",
53+
"eslint-config-4catalyzer": "^0.7.0",
54+
"eslint-config-4catalyzer-jest": "^1.1.0",
55+
"eslint-config-prettier": "^4.0.0",
56+
"eslint-plugin-import": "^2.8.0",
57+
"eslint-plugin-jest": "^22.1.2",
58+
"eslint-plugin-prettier": "^3.0.1",
59+
"husky": "^1.1.2",
60+
"jest": "^24.0.0",
61+
"jest-file-snapshot": "^0.3.7",
62+
"lint-staged": "^8.1.0",
63+
"mini-css-extract-plugin": "^0.8.0",
64+
"prettier": "^1.15.3",
65+
"sass": "^1.22.12",
66+
"strip-ansi": "^5.2.0"
67+
},
68+
"dependencies": {
69+
"@modular-css/processor": "^24.2.2",
70+
"cssesc": "^3.0.0",
71+
"fs-extra": "^8.1.0",
72+
"loader-utils": "^1.2.3",
73+
"memory-fs": "^0.4.1",
74+
"sass-loader": "^8.0.0",
75+
"webpack": "^4.40.2"
76+
}
77+
}

stringifyLoader.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Borrowed from less-loader, prevents webpack from complaining that the file type is unknown by ensuring any output is
2+
// turned into a valid JSON string
3+
function stringifyLoader(content) {
4+
return JSON.stringify(content);
5+
}
6+
7+
module.exports = stringifyLoader;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["main"],{
2+
3+
/***/ "./test/integration/common/externals.scss":
4+
/*!************************************************!*\
5+
!*** ./test/integration/common/externals.scss ***!
6+
\************************************************/
7+
/*! no static exports found */
8+
/***/ (function(module, exports, __webpack_require__) {
9+
10+
// extracted by mini-css-extract-plugin
11+
module.exports = {"toolbar":"externals--toolbar--3Adsi"};
12+
13+
/***/ }),
14+
15+
/***/ "./test/integration/externals.js":
16+
/*!***************************************!*\
17+
!*** ./test/integration/externals.js ***!
18+
\***************************************/
19+
/*! no static exports found */
20+
/***/ (function(module, exports, __webpack_require__) {
21+
22+
const styles = __webpack_require__(/*! ./common/externals.scss */ "./test/integration/common/externals.scss");
23+
24+
25+
/***/ })
26+
27+
},[["./test/integration/externals.js","runtime~main"]]]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.externals--base--2HU_u {
2+
color: blue;
3+
}
4+
.externals--button--3mvEh {
5+
color: red;
6+
}
7+
.externals--toolbar--3Adsi > .externals--base--2HU_u.externals--button--3mvEh {
8+
margin-left: 2rem;
9+
}

0 commit comments

Comments
 (0)