Skip to content

chore: use babel for client code #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/node_modules
/src/runtime
/coverage
/dist
/node_modules
15 changes: 15 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading