Skip to content

Commit 5f0fce1

Browse files
fix: support ES module syntax
1 parent c7eda97 commit 5f0fce1

File tree

28 files changed

+1091
-755
lines changed

28 files changed

+1091
-755
lines changed

package-lock.json

+771-741
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"test:only": "cross-env NODE_ENV=test jest",
2828
"test:watch": "cross-env NODE_ENV=test jest --watch",
2929
"test:coverage": "cross-env NODE_ENV=test jest --collectCoverageFrom=\"src/**/*.js\" --coverage",
30-
"test:manual": "webpack-dev-server test/manual/src/index.js --open --config test/manual/webpack.config.js",
30+
"test:manual": "npm run build && webpack-dev-server test/manual/src/index.js --open --config test/manual/webpack.config.js",
3131
"pretest": "npm run lint",
3232
"test": "cross-env NODE_ENV=test npm run test:coverage",
3333
"defaults": "webpack-defaults"
@@ -45,9 +45,9 @@
4545
"webpack-sources": "^1.1.0"
4646
},
4747
"devDependencies": {
48-
"@babel/cli": "^7.5.0",
49-
"@babel/core": "^7.5.4",
50-
"@babel/preset-env": "^7.5.4",
48+
"@babel/cli": "^7.7.5",
49+
"@babel/core": "^7.7.5",
50+
"@babel/preset-env": "^7.7.6",
5151
"@commitlint/cli": "^8.1.0",
5252
"@commitlint/config-conventional": "^8.1.0",
5353
"@webpack-contrib/defaults": "^5.0.2",
@@ -56,23 +56,23 @@
5656
"babel-jest": "^24.8.0",
5757
"commitlint-azure-pipelines-cli": "^1.0.2",
5858
"cross-env": "^5.2.0",
59-
"css-loader": "^3.0.0",
59+
"css-loader": "^3.3.2",
6060
"del": "^4.1.1",
6161
"del-cli": "^1.1.0",
6262
"es-check": "^5.0.0",
63-
"eslint": "^6.0.1",
63+
"eslint": "^6.7.2",
6464
"eslint-config-prettier": "^6.0.0",
65-
"eslint-plugin-import": "^2.18.0",
65+
"eslint-plugin-import": "^2.19.1",
6666
"file-loader": "^4.0.0",
6767
"husky": "^3.0.0",
6868
"jest": "^24.8.0",
69-
"jest-junit": "^6.4.0",
70-
"lint-staged": "^9.2.0",
69+
"jest-junit": "^10.0.0",
70+
"lint-staged": "^9.5.0",
7171
"memory-fs": "^0.4.1",
7272
"npm-run-all": "^4.1.5",
73-
"prettier": "^1.18.2",
74-
"standard-version": "^6.0.1",
75-
"webpack": "^4.35.3",
73+
"prettier": "^1.19.1",
74+
"standard-version": "^7.0.1",
75+
"webpack": "^4.41.3",
7676
"webpack-cli": "^3.3.6",
7777
"webpack-dev-server": "^3.7.2"
7878
},

src/loader.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ export function pitch(request) {
179179

180180
try {
181181
let dependencies;
182-
const exports = evalModuleCode(this, source, request);
182+
let exports = evalModuleCode(this, source, request);
183+
// eslint-disable-next-line no-underscore-dangle
184+
exports = exports.__esModule ? exports.default : exports;
183185
locals = exports && exports.locals;
184186
if (!Array.isArray(exports)) {
185187
dependencies = [[null, exports]];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
{
12+
loader: 'css-loader',
13+
// TODO Uncomment after `css-loader` release the `esModule` option
14+
// options: { esModule: false },
15+
},
16+
],
17+
},
18+
],
19+
},
20+
plugins: [
21+
new Self({
22+
filename: '[name].css',
23+
}),
24+
],
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.a {
2+
background: red;
3+
}
4+
5+
.foo__style__b {
6+
color: green;
7+
}
8+
9+
.c {
10+
color: blue;
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.a {
2+
background: red;
3+
}
4+
5+
:local(.b) {
6+
color: green;
7+
}
8+
9+
:global(.c) {
10+
color: blue;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
{
12+
loader: 'css-loader',
13+
options: {
14+
modules: {
15+
mode: 'global',
16+
localIdentName: 'foo__[name]__[local]',
17+
},
18+
},
19+
},
20+
],
21+
},
22+
],
23+
},
24+
plugins: [
25+
new Self({
26+
filename: '[name].css',
27+
}),
28+
],
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.foo__style__a {
2+
background: red;
3+
}
4+
5+
.foo__style__b {
6+
color: green;
7+
}
8+
9+
.c {
10+
color: blue;
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.a {
2+
background: red;
3+
}
4+
5+
:local(.b) {
6+
color: green;
7+
}
8+
9+
:global(.c) {
10+
color: blue;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
{
12+
loader: 'css-loader',
13+
options: {
14+
modules: {
15+
mode: 'local',
16+
localIdentName: 'foo__[name]__[local]',
17+
},
18+
},
19+
},
20+
],
21+
},
22+
],
23+
},
24+
plugins: [
25+
new Self({
26+
filename: '[name].css',
27+
}),
28+
],
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.foo__style__a {
2+
background: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
background: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
{
12+
loader: 'css-loader',
13+
options: {
14+
modules: {
15+
mode: 'pure',
16+
localIdentName: 'foo__[name]__[local]',
17+
},
18+
},
19+
},
20+
],
21+
},
22+
],
23+
},
24+
plugins: [
25+
new Self({
26+
filename: '[name].css',
27+
}),
28+
],
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
{
12+
loader: 'css-loader',
13+
// TODO Uncomment after `css-loader` release the `esModule` option
14+
// options: { esModule: true },
15+
},
16+
],
17+
},
18+
],
19+
},
20+
plugins: [
21+
new Self({
22+
filename: '[name].css',
23+
}),
24+
],
25+
};

test/manual/index.html

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
<div class="test initial-css">
3030
Initial CSS: Must be green
3131
</div>
32+
<div class="hot-reload">
33+
<p>Hot Module Replacement</p>
34+
<div class="r">RED</div>
35+
<div class="g">GREEN</div>
36+
<div class="b">BLUE</div>
37+
</div>
38+
<div class="hot-reload">
39+
<p>Hot Module Replacement + CSS modules</p>
40+
<div class="rr">RED</div>
41+
<div class="gg">GREEN</div>
42+
<div class="bb">BLUE</div>
43+
</div>
3244
<div class="test lazy-css">
3345
<p>Lazy CSS: Must be red, but turn green when <button class="lazy-button">pressing this button</button>.</p>
3446
<p>But turn orange, when <button class="lazy-button2">pressing this button</button>. Additional clicks have no effect.</p>

test/manual/src/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
/* eslint-disable no-console, camelcase, no-global-assign */
44

55
import './initial.css';
6+
import './simple.css';
7+
import classes from './simple.module.css';
8+
9+
console.log('___CLASSES__');
10+
console.log(classes);
11+
12+
function replaceClass(originalClass, newClass) {
13+
const nodes = document.querySelectorAll(`.${originalClass}`);
14+
15+
nodes.forEach((node) => {
16+
const { classList } = node;
17+
classList.remove(originalClass);
18+
classList.add(newClass);
19+
});
20+
}
21+
22+
Object.keys(classes).forEach((localClass) => {
23+
replaceClass(localClass, classes[localClass]);
24+
});
625

726
const handleError = (err) => {
827
document.querySelector('.errors').textContent += `\n${err.toString()}`;

test/manual/src/simple.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.r {
2+
color: red;
3+
}
4+
5+
.g {
6+
color: green;
7+
}
8+
9+
.b {
10+
color: blue;
11+
}

test/manual/src/simple.module.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.rr {
2+
color: red;
3+
}
4+
5+
.gg {
6+
color: green;
7+
}
8+
9+
.bb {
10+
color: blue;
11+
}

0 commit comments

Comments
 (0)