Skip to content

Commit

Permalink
chore: use babel for client code (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed May 17, 2019
1 parent 0c8a23b commit 9dabcec
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 117 deletions.
4 changes: 2 additions & 2 deletions .eslintignore
@@ -1,3 +1,3 @@
/node_modules
/src/runtime
/coverage
/dist
/node_modules
15 changes: 15 additions & 0 deletions babel.config.js
Expand Up @@ -15,5 +15,20 @@ module.exports = (api) => {
},
],
],
overrides: [
{
test: './src/runtime',
presets: [
[
'@babel/preset-env',
{
targets: {
node: '0.12',
},
},
],
],
},
],
};
};
57 changes: 35 additions & 22 deletions src/runtime/api.js
Expand Up @@ -3,35 +3,45 @@
Author Tobias Koppers @sokra
*/
// css base code, injected by the css-loader
// eslint-disable-next-line func-names
module.exports = function(useSourceMap) {
var list = [];
const list = [];

// return the list of modules as css string
list.toString = function toString() {
return this.map(function(item) {
var content = cssWithMappingToString(item, useSourceMap);
return this.map((item) => {
const content = cssWithMappingToString(item, useSourceMap);

if (item[2]) {
return '@media ' + item[2] + '{' + content + '}';
} else {
return content;
return `@media ${item[2]}{${content}}`;
}

return content;
}).join('');
};

// import a list of modules into the list
// eslint-disable-next-line func-names
list.i = function(modules, mediaQuery) {
if (typeof modules === 'string') {
// eslint-disable-next-line no-param-reassign
modules = [[null, modules, '']];
}
var alreadyImportedModules = {};
for (var i = 0; i < this.length; i++) {
var id = this[i][0];

const alreadyImportedModules = {};

for (let i = 0; i < this.length; i++) {
// eslint-disable-next-line prefer-destructuring
const id = this[i][0];

if (id != null) {
alreadyImportedModules[id] = true;
}
}
for (i = 0; i < modules.length; i++) {
var item = modules[i];

for (let i = 0; i < modules.length; i++) {
const item = modules[i];

// skip already imported module
// this implementation is not 100% perfect for weird media query combinations
// when a module is imported multiple times with different media queries.
Expand All @@ -40,27 +50,31 @@ module.exports = function(useSourceMap) {
if (mediaQuery && !item[2]) {
item[2] = mediaQuery;
} else if (mediaQuery) {
item[2] = '(' + item[2] + ') and (' + mediaQuery + ')';
item[2] = `(${item[2]}) and (${mediaQuery})`;
}

list.push(item);
}
}
};

return list;
};

function cssWithMappingToString(item, useSourceMap) {
var content = item[1] || '';
var cssMapping = item[3];
const content = item[1] || '';
// eslint-disable-next-line prefer-destructuring
const cssMapping = item[3];

if (!cssMapping) {
return content;
}

if (useSourceMap && typeof btoa === 'function') {
var sourceMapping = toComment(cssMapping);
var sourceURLs = cssMapping.sources.map(function(source) {
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */';
});
const sourceMapping = toComment(cssMapping);
const sourceURLs = cssMapping.sources.map(
(source) => `/*# sourceURL=${cssMapping.sourceRoot}${source} */`
);

return [content]
.concat(sourceURLs)
Expand All @@ -74,9 +88,8 @@ function cssWithMappingToString(item, useSourceMap) {
// Adapted from convert-source-map (MIT)
function toComment(sourceMap) {
// eslint-disable-next-line no-undef
var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
var data =
'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
const base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
const data = `sourceMappingURL=data:application/json;charset=utf-8;base64,${base64}`;

return '/*# ' + data + ' */';
return `/*# ${data} */`;
}
3 changes: 2 additions & 1 deletion src/runtime/url-escape.js
Expand Up @@ -5,13 +5,14 @@ module.exports = function escape(url, needQuotes) {

// If url is already wrapped in quotes, remove them
if (/^['"].*['"]$/.test(url)) {
// eslint-disable-next-line no-param-reassign
url = url.slice(1, -1);
}

// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/["'() \t\n]/.test(url) || needQuotes) {
return '"' + url.replace(/"/g, '\\"').replace(/\n/g, '\\n') + '"';
return `"${url.replace(/"/g, '\\"').replace(/\n/g, '\\n')}"`;
}

return url;
Expand Down

0 comments on commit 9dabcec

Please sign in to comment.