8000 Fix for latest ionic-app-scripts · senseoverflow/ionic-img-viewer@de73f48 · GitHub
[go: up one dir, main page]

Skip to content

Commit de73f48

Browse files
committed
Fix for latest ionic-app-scripts
Build is more resilient => Inlining styles
1 parent 3e92016 commit de73f48

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Thumbs.db
1515
*.css
1616
*.tgz
1717

18+
# Exceptions
19+
!ngc-build.js
20+
1821
# Ignored folders
1922
dist
2023
aot
24+
.tmp

ngc-build.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const sass = require('node-sass');
2+
const fs = require('fs-extra');
3+
const path = require('path');
4+
const { exec } = require('child_process');
5+
6+
const tempFolder = '.tmp';
7+
const componentPath = `./${tempFolder}/image-viewer.component.ts`;
8+
const scssPath = `./${tempFolder}/image-viewer.scss`;
9+
10+
process();
11+
12+
function process() {
13+
console.log('Running build...');
14+
deleteFolderRecursiveSync(tempFolder);
15+
copyRecursiveSync('src', tempFolder);
16+
17+
const component = getFileContent(componentPath);
18+
const css = getCss(getFileContent(scssPath));
19+
20+
console.log('Inlining styles...');
21+
const newComponent = component.replace('styles: []', `styles: ['${css}']`);
22+
23+
writeFile(componentPath, newComponent, () => {
24+
console.log('About to run ngc async...');
25+
26+
runNgc('tsconfig.json');
27+
runNgc('tsconfig.umd.json');
28+
});
29+
}
30+
31+
function runNgc(tsConfigPath) {
32+
console.log('Started for', tsConfigPath);
33+
34+
const ngc = path.resolve('node_modules', '.bin', 'ngc');
35+
exec(`${ngc} -p ${tsConfigPath}`, (err, stdout, stdeer) => {
36+
if (err) {
37+
console.log('Error !', err);
38+
return;
39+
}
40+
41+
console.log('Done for', tsConfigPath);
42+
});
43+
}
44+
45+
function getCss(scss_content) {
46+
const style = sass.renderSync({
47+
data: scss_content
48+
});
49+
50+
return style.css
51+
.toString()
52+
.replace(/([\n\r]\s*)+/gm, ' ')
53+
.replace(/"/g, '\\"');
54+
}
55+
56+
function getFileContent(path) {
57+
return fs.readFileSync(path, 'utf8');
58+
}
59+
60+
function writeFile(path, data, callback) {
61+
return fs.writeFile(path, data, err => {
62+
if (err) {
63+
console.log('Error while writing file !', err);
64+
}
65+
66+
callback();
67+
});
68+
}
69+
70+
function copyRecursiveSync(src, dest) {
71+
const exists = fs.existsSync(src);
72+
const stats = exists && fs.statSync(src);
73+
const isDirectory = exists && stats.isDirectory();
74+
if (exists && isDirectory) {
75+
fs.mkdirSync(dest);
76+
fs.readdirSync(src).forEach(function(childItemName) {
77+
copyRecursiveSync(
78+
path.join(src, childItemName),
79+
path.join(dest, childItemName)
80+
);
81+
});
82+
} else {
83+
fs.copySync(src, dest);
84+
}
85+
}
86+
87+
function deleteFolderRecursiveSync(path) {
88+
if (fs.existsSync(path)) {
89+
fs.readdirSync(path).forEach(function(file, index) {
90+
var curPath = path + '/' + file;
91+
if (fs.lstatSync(curPath).isDirectory()) {
92+
// recurse
93+
deleteFolderRecursive(curPath);
94+
} else {
95+
// delete file
96+
fs.removeSync(curPath);
97+
}
98+
});
99+
fs.rmdirSync(path);
100+
}
101+
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"typings": "./dist/es2015/ionic-img-viewer.d.ts",
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
9-
"prepublish": "node_modules/.bin/ngc && copyfiles ./src/*.scss ./dist/es2015/ && ngc -p tsconfig.umd.json && copyfiles ./src/*.scss ./dist/umd/"
9+
"prepublish": "node ngc-build.js"
1010
},
1111
"repository": {
1212
"type": "git",
@@ -30,10 +30,11 @@
3030
"@angular/platform-browser": "4.4.4",
3131
"@angular/platform-browser-dynamic": "4.4.4",
3232
"@angular/platform-server": "4.4.4",
33+
"fs-extra": "^4.0.2",
34+
"node-sass": "^4.5.3",
3335
"rxjs": "5.4.3",
34-
"zone.js": "0.8.18",
3536
"typescript": "~2.3.4",
36-
"copyfiles": "1.0.0"
37+
"zone.js": "0.8.18"
3738
},
3839
"peerDependencies": {
3940
"ionic-angular": "3.7.1"

src/image-viewer.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { ImageViewerEnter, ImageViewerLeave } from './image-viewer-transitions';
3636
</div>
3737
</div>
3838
`,
39-
styleUrls: ['./image-viewer.scss']
39+
styles: []
4040
})
4141
export class ImageViewerComponent extends Ion implements OnInit, OnDestroy, AfterViewInit {
4242
public imageUrl: SafeUrl;

0 commit comments

Comments
 (0)
0