This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
2,004 additions
and
2,479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,90 @@ | ||
var handler = function (compileStep) { | ||
var source = compileStep.read().toString('utf8'); | ||
var outputFile = compileStep.inputPath + '.js'; | ||
class UniverseBabelCompiler extends BabelCompiler { | ||
|
||
var path = compileStep.inputPath.split('.import.'); | ||
var moduleId = path[0]; | ||
|
||
if(process.platform === 'win32') { | ||
// windows support, replace backslashes with forward slashes | ||
moduleId = moduleId.replace(/\\/g, '/'); | ||
processFilesForTarget (inputFiles) { | ||
inputFiles.forEach(this.processFile); | ||
} | ||
|
||
if (compileStep.packageName) { | ||
// inside package, prefix module | ||
moduleId = '{' + compileStep.packageName + '}/' + moduleId; | ||
} | ||
processFile (inputFile) { | ||
|
||
var extraWhitelist = [ | ||
'es6.modules', | ||
// @todo make this configurable: | ||
'es7.decorators' | ||
]; | ||
if (path[1] === 'jsx') { | ||
// add support for React in *.import.jsx files | ||
extraWhitelist.push('react'); | ||
} | ||
// Full contents of the file as a string | ||
const source = inputFile.getContentsAsString(); | ||
|
||
// Relative path of file to the package or app root directory (always uses forward slashes) | ||
const filePath = inputFile.getPathInPackage(); | ||
|
||
// Options from api.addFile | ||
const fileOptions = inputFile.getFileOptions(); | ||
|
||
// Name of the package or null if the file is not in a package. | ||
const packageName = inputFile.getPackageName(); | ||
|
||
// moduleId - Module name (full patch without extension) | ||
// ext - File extension (either js or jsx) | ||
let [moduleId, ext] = filePath.split('.import.'); | ||
|
||
// prefix module name accordingly | ||
if (packageName) { | ||
// inside package | ||
moduleId = '/_modules_/packages/' + packageName.replace(':', '/') + '/' + moduleId; | ||
} else { | ||
// inside main app | ||
moduleId = '/_modules_/app/' + moduleId; | ||
} | ||
|
||
try { | ||
var result = Babel.transformMeteor(source, { | ||
const extraWhitelist = [ | ||
'es6.modules', | ||
// @todo make this configurable: | ||
'es7.decorators', | ||
'regenerator' | ||
]; | ||
|
||
if (ext === 'jsx') { | ||
// add support for React in *.import.jsx files | ||
extraWhitelist.push('react'); | ||
} | ||
|
||
const babelDefaultOptions = Babel.getDefaultOptions(this.extraFeatures); | ||
|
||
const babelOptions = _({}).extend(babelDefaultOptions, { | ||
sourceMap: true, | ||
filename: compileStep.pathForSourceMap, | ||
sourceMapName: compileStep.pathForSourceMap, | ||
extraWhitelist: extraWhitelist, | ||
filename: filePath, | ||
sourceFileName: '/' + filePath, | ||
sourceMapName: '/' + filePath + '.map', | ||
modules: 'system', | ||
moduleIds: true, | ||
moduleId: moduleId | ||
moduleId, | ||
whitelist: _.union(babelDefaultOptions.whitelist, extraWhitelist) | ||
}); | ||
} catch (e) { | ||
if (e.loc) { | ||
// Babel error | ||
compileStep.error({ | ||
message: e.message, | ||
sourcePath: compileStep.inputPath, | ||
line: e.loc.line, | ||
column: e.loc.column | ||
}); | ||
return; | ||
} else { | ||
|
||
try { | ||
var result = Babel.compile(source, babelOptions); | ||
} catch (e) { | ||
if (e.loc) { | ||
inputFile.error({ | ||
message: e.message, | ||
sourcePath: filePath, | ||
line: e.loc.line, | ||
column: e.loc.column | ||
}); | ||
return; | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
compileStep.addJavaScript({ | ||
path: outputFile, | ||
sourcePath: compileStep.inputPath, | ||
data: result.code, | ||
sourceMap: JSON.stringify(result.map) | ||
}); | ||
}; | ||
inputFile.addJavaScript({ | ||
sourcePath: filePath, | ||
path: filePath, | ||
data: result.code, | ||
hash: result.hash, | ||
sourceMap: result.map, | ||
bare: !!fileOptions.bare | ||
}); | ||
} | ||
} | ||
|
||
Plugin.registerSourceHandler('import.js', handler); | ||
Plugin.registerSourceHandler('import.jsx', handler); | ||
Plugin.registerCompiler({ | ||
extensions: ['import.js', 'import.jsx'], | ||
filenames: [] | ||
}, function () { | ||
return new UniverseBabelCompiler(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Following script allows to import variables exported from Meteor packages | ||
* @example `import {UniCollection, UniUsers} from '{universe:collection}!exports'` | ||
* @example `import {DDP} from '{ddp}!exports'` | ||
*/ | ||
|
||
System.set('exports', System.newModule({ | ||
locate ({name, metadata}) { | ||
return new Promise((resolve, reject) => { | ||
let [, dir,, author, packageName] = name.split('/'); | ||
|
||
// check if we're in valid namespace | ||
if (dir !== '_modules_') { | ||
reject(new Error('[Universe Modules]: trying to get exported values from invalid package: ' + name)); | ||
return; | ||
} | ||
|
||
// construct package name in Meteor's format | ||
let meteorPackageName = (author ? author + ':' : '') + packageName; | ||
|
||
if (!Package[meteorPackageName]) { | ||
// ups, there is no such package | ||
reject(new Error(`[Universe Modules]: Cannot find Meteor package exports for {${meteorPackageName}}`)); | ||
return; | ||
} | ||
|
||
// everything is ok, proceed | ||
metadata.meteorPackageName = meteorPackageName; | ||
resolve(name); | ||
}); | ||
}, | ||
fetch () { | ||
// we don't need to fetch anything for this to work | ||
return ''; | ||
}, | ||
instantiate ({metadata}) { | ||
return Package[metadata.meteorPackageName]; | ||
} | ||
})); |
Oops, something went wrong.