mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
feat(packaging): move packaging to a single "@angular/material" (#1286)
This commit is contained in:
@@ -29,7 +29,7 @@ const blocked_statements = [
|
||||
];
|
||||
|
||||
const sourceFolders = ['./src', './e2e'];
|
||||
const scopePackages = glob('src/lib/*');
|
||||
const libRoot = 'src/lib';
|
||||
const blockedRegex = new RegExp(blocked_statements.join('|'), 'g');
|
||||
const importRegex = /from\s+'(.*)';/g;
|
||||
|
||||
@@ -58,7 +58,9 @@ findTestFiles()
|
||||
lineCount++;
|
||||
|
||||
let matches = line.match(blockedRegex);
|
||||
let scopeImport = isRelativeScopeImport(fileName, line);
|
||||
let scopeImport = path.relative(libRoot, fileName).startsWith('..')
|
||||
? isRelativeScopeImport(fileName, line)
|
||||
: false;
|
||||
|
||||
if (matches || scopeImport) {
|
||||
|
||||
@@ -155,6 +157,10 @@ function findChangedFiles() {
|
||||
* @param line Line to be checked.
|
||||
*/
|
||||
function isRelativeScopeImport(fileName, line) {
|
||||
if (fileName.startsWith(libRoot)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let importMatch = importRegex.exec(line);
|
||||
|
||||
// We have to reset the last index of the import regex, otherwise we
|
||||
@@ -184,8 +190,7 @@ function isRelativeScopeImport(fileName, line) {
|
||||
if (fileScope.path !== importScope.path) {
|
||||
|
||||
// Creates a valid import statement, which uses the correct scope package.
|
||||
let importFilePath = path.relative(importScope.path, importPath);
|
||||
let validImportPath = `@angular2-material/${importScope.name}/${importFilePath}`;
|
||||
let validImportPath = `@angular/material`;
|
||||
|
||||
return {
|
||||
fileScope: fileScope.name,
|
||||
@@ -200,9 +205,7 @@ function isRelativeScopeImport(fileName, line) {
|
||||
filePath = path.normalize(filePath);
|
||||
|
||||
// Iterate through all scope paths and try to find them inside of the file path.
|
||||
var scopePath = scopePackages
|
||||
.filter(scope => filePath.indexOf(path.normalize(scope)) !== -1)
|
||||
.pop();
|
||||
var scopePath = filePath.indexOf(path.normalize(libRoot)) == -1 ? libRoot : filePath;
|
||||
|
||||
// Return an object containing the name of the scope and the associated path.
|
||||
return {
|
||||
|
||||
@@ -30,10 +30,14 @@ const writeFile = promiseify(fs.writeFile);
|
||||
|
||||
|
||||
function inlineResources(globs) {
|
||||
if (typeof globs == 'string') {
|
||||
globs = [globs];
|
||||
}
|
||||
|
||||
/**
|
||||
* For every argument, inline the templates and styles under it and write the new file.
|
||||
*/
|
||||
for (let pattern of globs) {
|
||||
return Promise.all(globs.map(pattern => {
|
||||
if (pattern.indexOf('*') < 0) {
|
||||
// Argument is a directory target, add glob patterns to include every files.
|
||||
pattern = path.join(pattern, '**', '*');
|
||||
@@ -43,17 +47,32 @@ function inlineResources(globs) {
|
||||
.filter(name => /\.js$/.test(name)); // Matches only JavaScript files.
|
||||
|
||||
// Generate all files content with inlined templates.
|
||||
files.forEach(filePath => {
|
||||
readFile(filePath, 'utf-8')
|
||||
.then(content => inlineTemplate(filePath, content))
|
||||
.then(content => inlineStyle(filePath, content))
|
||||
.then(content => removeModuleId(filePath, content))
|
||||
return Promise.all(files.map(filePath => {
|
||||
return readFile(filePath, 'utf-8')
|
||||
.then(content => inlineResourcesFromString(content, url => {
|
||||
return path.join(path.dirname(filePath), url);
|
||||
}))
|
||||
.then(content => writeFile(filePath, content))
|
||||
.catch(err => {
|
||||
console.error('An error occured: ', err);
|
||||
});
|
||||
});
|
||||
}
|
||||
}));
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Inline resources from a string content.
|
||||
* @param content {string} The source file's content.
|
||||
* @param urlResolver {Function} A resolver that takes a URL and return a path.
|
||||
* @returns {string} The content with resources inlined.
|
||||
*/
|
||||
function inlineResourcesFromString(content, urlResolver) {
|
||||
// Curry through the inlining functions.
|
||||
return [
|
||||
inlineTemplate,
|
||||
inlineStyle,
|
||||
removeModuleId
|
||||
].reduce((content, fn) => fn(content, urlResolver), content);
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
@@ -64,13 +83,13 @@ if (require.main === module) {
|
||||
/**
|
||||
* Inline the templates for a source file. Simply search for instances of `templateUrl: ...` and
|
||||
* replace with `template: ...` (with the content of the file included).
|
||||
* @param filePath {string} The path of the source file.
|
||||
* @param content {string} The source file's content.
|
||||
* @param urlResolver {Function} A resolver that takes a URL and return a path.
|
||||
* @return {string} The content with all templates inlined.
|
||||
*/
|
||||
function inlineTemplate(filePath, content) {
|
||||
function inlineTemplate(content, urlResolver) {
|
||||
return content.replace(/templateUrl:\s*'([^']+?\.html)'/g, function(m, templateUrl) {
|
||||
const templateFile = path.join(path.dirname(filePath), templateUrl);
|
||||
const templateFile = urlResolver(templateUrl);
|
||||
const templateContent = fs.readFileSync(templateFile, 'utf-8');
|
||||
const shortenedTemplate = templateContent
|
||||
.replace(/([\n\r]\s*)+/gm, ' ')
|
||||
@@ -83,16 +102,16 @@ function inlineTemplate(filePath, content) {
|
||||
/**
|
||||
* Inline the styles for a source file. Simply search for instances of `styleUrls: [...]` and
|
||||
* replace with `styles: [...]` (with the content of the file included).
|
||||
* @param filePath {string} The path of the source file.
|
||||
* @param urlResolver {Function} A resolver that takes a URL and return a path.
|
||||
* @param content {string} The source file's content.
|
||||
* @return {string} The content with all styles inlined.
|
||||
*/
|
||||
function inlineStyle(filePath, content) {
|
||||
function inlineStyle(content, urlResolver) {
|
||||
return content.replace(/styleUrls:\s*(\[[\s\S]*?\])/gm, function(m, styleUrls) {
|
||||
const urls = eval(styleUrls);
|
||||
return 'styles: ['
|
||||
+ urls.map(styleUrl => {
|
||||
const styleFile = path.join(path.dirname(filePath), styleUrl);
|
||||
const styleFile = urlResolver(styleUrl);
|
||||
const styleContent = fs.readFileSync(styleFile, 'utf-8');
|
||||
const shortenedStyle = styleContent
|
||||
.replace(/([\n\r]\s*)+/gm, ' ')
|
||||
@@ -107,13 +126,13 @@ function inlineStyle(filePath, content) {
|
||||
|
||||
/**
|
||||
* Remove every mention of `moduleId: module.id`.
|
||||
* @param _ {string} The file path of the source file, currently ignored.
|
||||
* @param content {string} The source file's content.
|
||||
* @returns {string} The content with all moduleId: mentions removed.
|
||||
*/
|
||||
function removeModuleId(_, content) {
|
||||
function removeModuleId(content) {
|
||||
return content.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, '');
|
||||
}
|
||||
|
||||
|
||||
module.exports = inlineResources;
|
||||
module.exports.inlineResourcesFromString = inlineResourcesFromString;
|
||||
|
||||
Reference in New Issue
Block a user