Skip to content

Commit c598c79

Browse files
refactor: restructure runtime code (#798)
1 parent dd1eab4 commit c598c79

File tree

7 files changed

+34
-17
lines changed

7 files changed

+34
-17
lines changed

.eslintignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
/node_modules
2-
/lib/url/escape.js
3-
/lib/css-base.js
2+
/lib/runtime

lib/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ module.exports = function loader(content, map) {
113113
if (query.url !== false && result.urlItems.length > 0) {
114114
urlEscapeHelper = `var escape = require(${loaderUtils.stringifyRequest(
115115
this,
116-
require.resolve('./url/escape.js')
116+
require.resolve('./runtime/escape.js')
117117
)});\n`;
118118

119119
cssAsString = cssAsString.replace(result.urlItemRegExpG, (item) => {
@@ -184,7 +184,7 @@ module.exports = function loader(content, map) {
184184
null,
185185
`${urlEscapeHelper}exports = module.exports = require(${loaderUtils.stringifyRequest(
186186
this,
187-
require.resolve('./css-base.js')
187+
require.resolve('./runtime/api.js')
188188
)})(${sourceMap});\n` +
189189
`// imports\n${importJs}\n\n` +
190190
`// module\n${moduleJs}\n\n` +
File renamed without changes.
File renamed without changes.

test/helpers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ function getEvaluated(output, modules) {
1515
);
1616
m = { exports: {}, id: 1 };
1717
fn(m, m.exports, (module) => {
18-
if (module.indexOf('css-base') >= 0) {
18+
if (module.indexOf('runtime/api') >= 0) {
1919
// eslint-disable-next-line global-require
20-
return require('../lib/css-base');
20+
return require('../lib/runtime/api');
2121
}
22-
if (module.indexOf('url/escape') >= 0) {
22+
if (module.indexOf('runtime/escape') >= 0) {
2323
// eslint-disable-next-line global-require
24-
return require('../lib/url/escape');
24+
return require('../lib/runtime/escape');
2525
}
2626
if (module.indexOf('-!/path/css-loader!') === 0) {
2727
// eslint-disable-next-line no-param-reassign

test/cssBaseTest.js renamed to test/runtime-api.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/* eslint-env mocha*/
1+
/* eslint-env mocha */
22

3-
const base = require('../lib/css-base');
3+
const api = require('../lib/runtime/api');
44

55
describe('css-base', () => {
66
before(() => {
@@ -22,23 +22,23 @@ describe('css-base', () => {
2222
});
2323

2424
it('should toString a single module', () => {
25-
const m = base();
25+
const m = api();
2626
m.push([1, 'body { a: 1; }', '']);
2727
m.toString().should.be.eql('body { a: 1; }');
2828
});
2929
it('should toString multiple modules', () => {
30-
const m = base();
30+
const m = api();
3131
m.push([2, 'body { b: 2; }', '']);
3232
m.push([1, 'body { a: 1; }', '']);
3333
m.toString().should.be.eql('body { b: 2; }body { a: 1; }');
3434
});
3535
it('should toString with media query', () => {
36-
const m = base();
36+
const m = api();
3737
m.push([1, 'body { a: 1; }', 'screen']);
3838
m.toString().should.be.eql('@media screen{body { a: 1; }}');
3939
});
4040
it('should import modules', () => {
41-
const m = base();
41+
const m = api();
4242
const m1 = [1, 'body { a: 1; }', 'screen'];
4343
const m2 = [2, 'body { b: 2; }', ''];
4444
const m3 = [3, 'body { c: 3; }', ''];
@@ -55,7 +55,7 @@ describe('css-base', () => {
5555
);
5656
});
5757
it('should import named modules', () => {
58-
const m = base();
58+
const m = api();
5959
const m1 = ['./module1', 'body { a: 1; }', 'screen'];
6060
const m2 = ['./module2', 'body { b: 2; }', ''];
6161
const m3 = ['./module3', 'body { c: 3; }', ''];
@@ -72,7 +72,7 @@ describe('css-base', () => {
7272
);
7373
});
7474
it('should toString with source mapping', () => {
75-
const m = base(true);
75+
const m = api(true);
7676
m.push([
7777
1,
7878
'body { a: 1; }',
@@ -90,7 +90,7 @@ describe('css-base', () => {
9090
});
9191
it('should toString without source mapping if btoa not avalibale', () => {
9292
global.btoa = null;
93-
const m = base(true);
93+
const m = api(true);
9494
m.push([
9595
1,
9696
'body { a: 1; }',

test/runtime-escape.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-env mocha */
2+
3+
require('should');
4+
const escape = require('../lib/runtime/escape');
5+
6+
describe('runtime escape', () => {
7+
it('should escape url', () => {
8+
escape(true).should.be.eql(true);
9+
escape('image.png').should.be.eql('image.png');
10+
escape('"image.png"').should.be.eql('image.png');
11+
escape("'image.png'").should.be.eql('image.png');
12+
escape('image other.png').should.be.eql('"image other.png"');
13+
escape('"image other.png"').should.be.eql('"image other.png"');
14+
escape("'image other.png'").should.be.eql('"image other.png"');
15+
escape('image"other.png').should.be.eql('"image\\"other.png"');
16+
escape('image\nother.png').should.be.eql('"image\\nother.png"');
17+
});
18+
});

0 commit comments

Comments
 (0)