doric cli mapping
This commit is contained in:
51
doric-cli/scripts/command.js
Normal file
51
doric-cli/scripts/command.js
Normal file
@@ -0,0 +1,51 @@
|
||||
require('shelljs/global')
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const SourceMapMerger = require("source-map-merger");
|
||||
|
||||
function fromDir(startPath, filter) {
|
||||
if (!fs.existsSync(startPath)) {
|
||||
console.log("no dir ", startPath);
|
||||
return;
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(startPath);
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const filename = path.join(startPath, files[i]);
|
||||
const stat = fs.lstatSync(filename);
|
||||
if (stat.isDirectory()) {
|
||||
fromDir(filename, filter);
|
||||
}
|
||||
else if (filename.indexOf(filter) >= 0) {
|
||||
doMerge(startPath, files[i])
|
||||
};
|
||||
};
|
||||
};
|
||||
function doMerge(startPath, fileName) {
|
||||
// console.log('-- found: ', startPath, fileName);
|
||||
const filePath = fileName ? path.join(startPath, fileName) : startPath
|
||||
// console.log('-- merge: ', filePath.replace(/bundle\//, 'build/'), filePath)
|
||||
const mergedMap = SourceMapMerger.createMergedSourceMapFromFiles([
|
||||
filePath.replace(/bundle\//, 'build/'),
|
||||
filePath,
|
||||
], true);
|
||||
fs.writeFileSync(filePath, mergedMap)
|
||||
return mergedMap
|
||||
}
|
||||
function mergeMappings() {
|
||||
fromDir("bundle", ".map")
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
build: () => {
|
||||
exec('npm run build')
|
||||
console.log('Deal mapping')
|
||||
mergeMappings()
|
||||
},
|
||||
clean: () => {
|
||||
exec('npm run clean')
|
||||
},
|
||||
mergeMappings,
|
||||
doMerge,
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
const chokidar = require('chokidar')
|
||||
const ws = require('./server')
|
||||
const fs = require("fs")
|
||||
const doMerge = require("./command").doMerge
|
||||
|
||||
require('shelljs/global')
|
||||
|
||||
@@ -13,13 +14,17 @@ setTimeout(() => {
|
||||
ignored: /(^|[\/\\])\../,
|
||||
}).on('change', (path) => {
|
||||
fs.readFile(path, 'utf-8', (error, data) => {
|
||||
console.log('File change:', path)
|
||||
ws.connections.forEach(e => {
|
||||
e.sendText(JSON.stringify({
|
||||
script: data,
|
||||
source: path.match(/[^/\\]*$/)[0],
|
||||
}))
|
||||
})
|
||||
if (!path.endsWith('.map')) {
|
||||
console.log('File change:', path)
|
||||
const sourceMap = doMerge(path + ".map")
|
||||
ws.connections.forEach(e => {
|
||||
e.sendText(JSON.stringify({
|
||||
script: fs.readFileSync(path),
|
||||
source: path.match(/[^/\\]*$/)[0],
|
||||
sourceMap,
|
||||
}))
|
||||
})
|
||||
}
|
||||
})
|
||||
});
|
||||
}, 3000);
|
||||
|
Reference in New Issue
Block a user