Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 361b424

Browse files
committed
Added goog resolver webpack loader, kemp only compressed pxt files, moved arduino files to client
1 parent cc7f491 commit 361b424

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+31833
-8561
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
[submodule "pxt-blockly"]
2-
path = pxt-blockly
3-
url = https://github.com/microsoft/pxt-blockly.git

client/config/goog-loader/index.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
const path = require("path");
2+
const paths = require("../paths");
3+
const ProvideCollector = require("./provideCollector");
4+
5+
const sources = [
6+
path.join(paths.appSrc, "blockly")
7+
];
8+
9+
let provideMap = [];
10+
(function collectProvides(){
11+
let collector = new ProvideCollector(sources);
12+
provideMap = collector.collect();
13+
})();
14+
15+
const googRegex = new RegExp("\\s*goog\\.(provide|require)\\(\\s*[\"'](.*)[\"']\\s*\\)");
16+
const googLibRegex = new RegExp(".*goog\\..*", "g");
17+
const dotRegex = new RegExp("\\.", "g");
18+
const strictRegex = new RegExp("\\s*[\"']use strict[\"'].*", "g");
19+
20+
let output = "";
21+
function appendLine(line){
22+
output += line + "\n";
23+
}
24+
function append(code){
25+
if(Array.isArray(code)){
26+
code.forEach(appendLine);
27+
}else{
28+
appendLine(code);
29+
}
30+
}
31+
32+
const googs = { provide: gProvide, require: gRequire };
33+
34+
let libIncluded = false;
35+
36+
function processLine(line){
37+
if(line.match(strictRegex) !== null) return;
38+
39+
let result = line.match(googRegex);
40+
41+
if(line.startsWith("//") || result === null){
42+
append(line);
43+
return;
44+
}
45+
46+
let goog = result[1];
47+
let module = result[2];
48+
49+
if(googs.hasOwnProperty(goog)){
50+
googs[goog](module); // Yay for overcomplicating things ;)
51+
}else{
52+
append(line);
53+
}
54+
}
55+
56+
let provides = [];
57+
function gProvide(module){
58+
provides.push(module);
59+
buildup(module).forEach(object => append(optionalObject(object)));
60+
append(optionalObject(module));
61+
}
62+
63+
function gRequire(module){
64+
let basename = module.split(".")[0];
65+
if(basename === "goog") return;
66+
67+
let file = (typeof provideMap[module] === "undefined") ? module : provideMap[module];
68+
let imp = "require('" + file + "')";
69+
append("var " + basename + " = typeof " + basename + " === 'undefined' ? " + imp + " : Object.assign(" + basename + ", " + imp + ");");
70+
}
71+
72+
function printProvides(){
73+
if(provides.length === 0) return;
74+
75+
let mapped = [];
76+
77+
provides.forEach(provide => {
78+
let renamed = rename(provide);
79+
mapped.push(renamed + ": " + provide);
80+
});
81+
82+
//append("module.exports = { " + mapped.join(", ") + " }");
83+
append("module.exports = " + provides[0].split(".")[0] + ";");
84+
}
85+
86+
function rename(module){
87+
return module.replace(dotRegex, "___");
88+
}
89+
90+
function optionalObject(object){
91+
let addVar = object.split(".").length === 1;
92+
93+
return (addVar ? "var " : "") + object + " = typeof " + object + " === 'undefined' ? {} : " + object + ";";
94+
}
95+
96+
function buildup(namespace){
97+
let objects = [];
98+
99+
let parts = namespace.split(".");
100+
101+
for(let i = 0; i < parts.length-1; i++){
102+
let upTo = parts.slice(0, i+1);
103+
objects.push(upTo.join("."));
104+
}
105+
106+
return objects;
107+
}
108+
109+
function init(){
110+
output = "";
111+
provides = [];
112+
libIncluded = false;
113+
}
114+
115+
module.exports = function(content, map, meta) {
116+
117+
return function(source){
118+
init();
119+
120+
source.split("\n").forEach(processLine);
121+
printProvides();
122+
123+
return output;
124+
}(content);
125+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const path = require("path");
2+
const walker = require("walk");
3+
const fs = require('fs');
4+
5+
class ProvideCollector {
6+
7+
constructor(roots){
8+
if(Array.isArray(roots)){
9+
this.roots = roots;
10+
}else{
11+
this.roots = [ roots ];
12+
}
13+
14+
this.regex = new RegExp("goog\\.provide\\(\\s*[\"'](.*)[\"']\\s*\\)", "g");
15+
16+
let context = this;
17+
18+
this.walkOptions = {
19+
followLinks: false,
20+
mask: "*.js",
21+
22+
listeners: {
23+
file: (root, fileStats, next) => {
24+
let extension = path.extname(fileStats.name).toLowerCase();
25+
if(extension !== ".js") next();
26+
27+
context.checkFile(path.join(root, fileStats.name), () => next());
28+
}
29+
}
30+
};
31+
}
32+
33+
checkFile(filepath, callback){
34+
let regex = this.regex;
35+
let map = this.provides;
36+
37+
let data = fs.readFileSync(filepath, { encoding: "UTF-8" });
38+
data.split("\n").forEach(line => {
39+
let match = regex.exec(line);
40+
while(match !== null){
41+
let provide = match[1];
42+
map[provide] = filepath;
43+
match = regex.exec(line);
44+
}
45+
});
46+
47+
callback();
48+
}
49+
50+
collect(){
51+
this.provides = {};
52+
53+
this.roots.forEach(root => walker.walkSync(root, this.walkOptions));
54+
55+
return this.provides;
56+
}
57+
}
58+
59+
module.exports = ProvideCollector;

client/config/webpack.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ module.exports = function(webpackEnv) {
334334
],
335335
include: paths.appSrc,
336336
},
337+
// Goog
338+
{
339+
test: /\.(js|mjs|jsx|ts|tsx)$/,
340+
include: path.join(paths.appSrc, "blockly"),
341+
loader: require.resolve('./goog-loader'),
342+
enforce: 'pre'
343+
},
337344
{
338345
// "oneOf" will traverse all following loaders until one will
339346
// match the requirements. When no loader matches it will fall

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"ts-pnp": "1.1.2",
6666
"typescript": "3.5.3",
6767
"url-loader": "2.1.0",
68+
"walk": "^2.3.14",
6869
"webpack": "4.39.1",
6970
"webpack-dev-server": "3.2.1",
7071
"webpack-manifest-plugin": "2.0.4",

0 commit comments

Comments
 (0)