Skip to content

Instantly share code, notes, and snippets.

@eavichay
Created September 4, 2018 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eavichay/432af50496c02961c9dd70417411cfdd to your computer and use it in GitHub Desktop.
Save eavichay/432af50496c02961c9dd70417411cfdd to your computer and use it in GitHub Desktop.
Typescript compiler output to browser es-modules + watch
const fs = require('fs');
const watch = require('node-watch');
const { execSync } = require('child_process');
const StaticServer = require('static-server');
const walker = function(dir, fn) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
if (fs.statSync(dir + '/' + file).isDirectory()) {
walker(dir + '/' + file + '/', fn);
} else {
if (file.endsWith('.js')) {
console.log('testing file: ' + dir + file);
fn(dir + '/' + file);
}
}
});
};
const run = () => {
execSync('npm run compile', { stdio: [0, 1, 2] });
walker('./dist', (filePath) => {
let content = fs.readFileSync(filePath).toString('utf-8');
let wasModified = false;
const regex = /^[import|export]{1}(?:["'\s]*(?:[\w*{}\n, ]+)[from]?\s*)?[\"|\'\s]*(.*)[\"|\']/gm;
let match;
while ((match = regex.exec(content)) !== null) {
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
const importPath = match[1];
const libName = match[1];
let replacement;
if (importPath.endsWith('.ts')) {
replacement = importPath.slice(0, libName.lastIndexOf('.ts')) + '.js'
} else if (!importPath.endsWith('.js')) {
replacement = importPath + '.js';
}
console.log('\tFound statement:', libName, '>', replacement);
content = content.replace(match[1], replacement);
wasModified = true;
}
if (wasModified) {
console.log(content);
fs.writeFileSync(filePath, content);
} else {
console.log('(Unmodified)')
}
});
};
watch('./src', { recursive: true }, () => {
run();
});
run();
const server = new StaticServer({
rootPath: './',
port: '8080',
cors: '*',
followSymlink: true
});
server.start(() => {
console.log('server started');
});
{
"name": "energx",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"dev": "node build.js",
"compile": "tsc"
},
"devDependencies": {
"typescript": "^3.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment