Skip to content

Commit

Permalink
fix: support ES module syntax (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Dec 17, 2019
1 parent c7eda97 commit 2f72e1a
Show file tree
Hide file tree
Showing 29 changed files with 1,115 additions and 770 deletions.
39 changes: 24 additions & 15 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
strategy:
maxParallel: 5
matrix:
node-13:
node_version: ^13.0.0
webpack_version: latest
node-12:
node_version: ^12.0.0
webpack_version: latest
Expand All @@ -47,11 +50,11 @@ jobs:
node-8:
node_version: ^8.9.0
webpack_version: latest
node-6:
node_version: ^6.9.0
webpack_version: latest
node-8-canary:
node_version: ^8.9.0
# node-6:
# node_version: ^6.9.0
# webpack_version: latest
node-10-canary:
node_version: ^10.13.0
webpack_version: next
continue_on_error: true
steps:
Expand Down Expand Up @@ -93,6 +96,9 @@ jobs:
strategy:
maxParallel: 5
matrix:
node-13:
node_version: ^13.0.0
webpack_version: latest
node-12:
node_version: ^12.0.0
webpack_version: latest
Expand All @@ -102,11 +108,11 @@ jobs:
node-8:
node_version: ^8.9.0
webpack_version: latest
node-6:
node_version: ^6.9.0
webpack_version: latest
node-8-canary:
node_version: ^8.9.0
# node-6:
# node_version: ^6.9.0
# webpack_version: latest
node-10-canary:
node_version: ^10.13.0
webpack_version: next
continue_on_error: true
steps:
Expand Down Expand Up @@ -148,6 +154,9 @@ jobs:
strategy:
maxParallel: 5
matrix:
node-13:
node_version: ^13.0.0
webpack_version: latest
node-12:
node_version: ^12.0.0
webpack_version: latest
Expand All @@ -157,11 +166,11 @@ jobs:
node-8:
node_version: ^8.9.0
webpack_version: latest
node-6:
node_version: ^6.9.0
webpack_version: latest
node-8-canary:
node_version: ^8.9.0
# node-6:
# node_version: ^6.9.0
# webpack_version: latest
node-10-canary:
node_version: ^10.13.0
webpack_version: next
continue_on_error: true
steps:
Expand Down
1,512 changes: 771 additions & 741 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test:only": "cross-env NODE_ENV=test jest",
"test:watch": "cross-env NODE_ENV=test jest --watch",
"test:coverage": "cross-env NODE_ENV=test jest --collectCoverageFrom=\"src/**/*.js\" --coverage",
"test:manual": "webpack-dev-server test/manual/src/index.js --open --config test/manual/webpack.config.js",
"test:manual": "npm run build && webpack-dev-server test/manual/src/index.js --open --config test/manual/webpack.config.js",
"pretest": "npm run lint",
"test": "cross-env NODE_ENV=test npm run test:coverage",
"defaults": "webpack-defaults"
Expand All @@ -45,9 +45,9 @@
"webpack-sources": "^1.1.0"
},
"devDependencies": {
"@babel/cli": "^7.5.0",
"@babel/core": "^7.5.4",
"@babel/preset-env": "^7.5.4",
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/preset-env": "^7.7.6",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@webpack-contrib/defaults": "^5.0.2",
Expand All @@ -56,23 +56,23 @@
"babel-jest": "^24.8.0",
"commitlint-azure-pipelines-cli": "^1.0.2",
"cross-env": "^5.2.0",
"css-loader": "^3.0.0",
"css-loader": "^3.3.2",
"del": "^4.1.1",
"del-cli": "^1.1.0",
"es-check": "^5.0.0",
"eslint": "^6.0.1",
"eslint": "^6.7.2",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-import": "^2.19.1",
"file-loader": "^4.0.0",
"husky": "^3.0.0",
"jest": "^24.8.0",
"jest-junit": "^6.4.0",
"lint-staged": "^9.2.0",
"jest-junit": "^10.0.0",
"lint-staged": "^9.5.0",
"memory-fs": "^0.4.1",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"standard-version": "^6.0.1",
"webpack": "^4.35.3",
"prettier": "^1.19.1",
"standard-version": "^7.0.1",
"webpack": "^4.41.3",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.7.2"
},
Expand Down
4 changes: 3 additions & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export function pitch(request) {

try {
let dependencies;
const exports = evalModuleCode(this, source, request);
let exports = evalModuleCode(this, source, request);
// eslint-disable-next-line no-underscore-dangle
exports = exports.__esModule ? exports.default : exports;
locals = exports && exports.locals;
if (!Array.isArray(exports)) {
dependencies = [[null, exports]];
Expand Down
4 changes: 4 additions & 0 deletions test/cases/simple-commonjs-syntax/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: red;
}

1 change: 1 addition & 0 deletions test/cases/simple-commonjs-syntax/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
3 changes: 3 additions & 0 deletions test/cases/simple-commonjs-syntax/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
25 changes: 25 additions & 0 deletions test/cases/simple-commonjs-syntax/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
Self.loader,
{
loader: 'css-loader',
// TODO Uncomment after `css-loader` release the `esModule` option
// options: { esModule: false },
},
],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
12 changes: 12 additions & 0 deletions test/cases/simple-css-modules-mode-global/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.a {
background: red;
}

.foo__style__b {
color: green;
}

.c {
color: blue;
}

1 change: 1 addition & 0 deletions test/cases/simple-css-modules-mode-global/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
11 changes: 11 additions & 0 deletions test/cases/simple-css-modules-mode-global/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.a {
background: red;
}

:local(.b) {
color: green;
}

:global(.c) {
color: blue;
}
29 changes: 29 additions & 0 deletions test/cases/simple-css-modules-mode-global/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
Self.loader,
{
loader: 'css-loader',
options: {
modules: {
mode: 'global',
localIdentName: 'foo__[name]__[local]',
},
},
},
],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
12 changes: 12 additions & 0 deletions test/cases/simple-css-modules-mode-local/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.foo__style__a {
background: red;
}

.foo__style__b {
color: green;
}

.c {
color: blue;
}

1 change: 1 addition & 0 deletions test/cases/simple-css-modules-mode-local/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
11 changes: 11 additions & 0 deletions test/cases/simple-css-modules-mode-local/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.a {
background: red;
}

:local(.b) {
color: green;
}

:global(.c) {
color: blue;
}
29 changes: 29 additions & 0 deletions test/cases/simple-css-modules-mode-local/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
Self.loader,
{
loader: 'css-loader',
options: {
modules: {
mode: 'local',
localIdentName: 'foo__[name]__[local]',
},
},
},
],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
4 changes: 4 additions & 0 deletions test/cases/simple-css-modules-mode-pure/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.foo__style__a {
background: red;
}

1 change: 1 addition & 0 deletions test/cases/simple-css-modules-mode-pure/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
3 changes: 3 additions & 0 deletions test/cases/simple-css-modules-mode-pure/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a {
background: red;
}
29 changes: 29 additions & 0 deletions test/cases/simple-css-modules-mode-pure/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
Self.loader,
{
loader: 'css-loader',
options: {
modules: {
mode: 'pure',
localIdentName: 'foo__[name]__[local]',
},
},
},
],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
4 changes: 4 additions & 0 deletions test/cases/simple-es-module-syntax/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: red;
}

1 change: 1 addition & 0 deletions test/cases/simple-es-module-syntax/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
3 changes: 3 additions & 0 deletions test/cases/simple-es-module-syntax/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
25 changes: 25 additions & 0 deletions test/cases/simple-es-module-syntax/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
Self.loader,
{
loader: 'css-loader',
// TODO Uncomment after `css-loader` release the `esModule` option
// options: { esModule: true },
},
],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
12 changes: 12 additions & 0 deletions test/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
<div class="test initial-css">
Initial CSS: Must be green
</div>
<div class="hot-reload">
<p>Hot Module Replacement</p>
<div class="r">RED</div>
<div class="g">GREEN</div>
<div class="b">BLUE</div>
</div>
<div class="hot-reload">
<p>Hot Module Replacement + CSS modules</p>
<div class="rr">RED</div>
<div class="gg">GREEN</div>
<div class="bb">BLUE</div>
</div>
<div class="test lazy-css">
<p>Lazy CSS: Must be red, but turn green when <button class="lazy-button">pressing this button</button>.</p>
<p>But turn orange, when <button class="lazy-button2">pressing this button</button>. Additional clicks have no effect.</p>
Expand Down
Loading

0 comments on commit 2f72e1a

Please sign in to comment.