-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configuratin with
.ng-packagr.json
file
- Loading branch information
Showing
18 changed files
with
2,404 additions
and
2,350 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"src": "sample", | ||
"dest": "sample/dist", | ||
"workingDirectory": ".ng_build", | ||
"ngc": { | ||
"entry": "src/public_api.ts", | ||
"tsconfig": "tsconfig.lib.json" | ||
}, | ||
"rollup": { | ||
"config": "rollup-config.js" | ||
} | ||
} |
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 @@ | ||
sample/ |
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,112 @@ | ||
// BUILD STEP IMPLEMENTATIONS | ||
import { processAssets } from './steps/assets'; | ||
import { copyFiles } from './steps/copy'; | ||
import { ngc } from './steps/ngc'; | ||
import { createPackage, readJson, preparePackage } from './steps/package'; | ||
import { rimraf } from './steps/rimraf'; | ||
import { rollup } from './steps/rollup'; | ||
import { remapSourcemap } from './steps/sorcery'; | ||
import { downlevelWithTsc } from './steps/tsc'; | ||
|
||
|
||
// Logging | ||
import { error, warn, info, success, debug } from './util/log'; | ||
|
||
// There are no type definitions available for these imports. | ||
const fs = require('mz/fs'); | ||
const uglify = require('uglify-js'); | ||
|
||
|
||
/** Options passed to the main entry of the packaging script */ | ||
export interface NgPackagrOptions { | ||
/** Path to the '.ng-packagr.json' file */ | ||
project: string | ||
} | ||
|
||
/** Config object from '.ng-packagr.json' */ | ||
export interface NgPackagrConfig { | ||
src: string, | ||
dest: string, | ||
workingDirectory: string, | ||
ngc: { | ||
tsconfig: string | ||
}, | ||
rollup: { | ||
config: string | ||
} | ||
} | ||
|
||
|
||
export const packageAngular = (opts: NgPackagrOptions): Promise<any> => { | ||
info(`Building Angular library from ${opts.project}`); | ||
|
||
/** Project configuration */ | ||
let project: NgPackagrConfig; | ||
let tsConfig: any; | ||
let flatModuleFile: string; | ||
let sourcePkg: any; | ||
|
||
return readJson(opts.project) | ||
.then((p) => { | ||
project = p; | ||
|
||
// 0. CLEAN | ||
return Promise.all([ | ||
rimraf(p.dest), | ||
rimraf(p.workingDirectory) | ||
]); | ||
}) | ||
// 1. READ PACKGE | ||
.then(() => preparePackage(project.src) | ||
.then((pkg) => { | ||
sourcePkg = pkg; | ||
|
||
return Promise.resolve(pkg); | ||
}) | ||
) | ||
.then(() => readJson(`${project.src}/${project.ngc.tsconfig}`) | ||
.then((cfg) => { | ||
tsConfig = cfg; | ||
|
||
flatModuleFile = `${project.workingDirectory}/${tsConfig.compilerOptions.outDir}/${tsConfig.angularCompilerOptions.flatModuleOutFile}.js`; | ||
}) | ||
) | ||
// 2. ASSETS | ||
.then(() => processAssets(project.src, `${project.workingDirectory}`)) | ||
// 3. NGC | ||
.then(() => ngc(`${project.src}/${project.ngc.tsconfig}`, `${project.workingDirectory}`)) | ||
.then(() => remapSourcemap(flatModuleFile)) | ||
// 4. FESM15: ROLLUP | ||
.then(() => rollup({ | ||
moduleName: `${sourcePkg.meta.name}`, | ||
entry: flatModuleFile, | ||
format: 'es', | ||
dest: `${project.workingDirectory}/${sourcePkg.dest.es2015}` | ||
})) | ||
.then(() => remapSourcemap(`${project.workingDirectory}/${sourcePkg.dest.es2015}`)) | ||
// 5. FESM5: TSC | ||
.then(() => downlevelWithTsc( | ||
`${project.workingDirectory}/${sourcePkg.dest.es2015}`, | ||
`${project.workingDirectory}/${sourcePkg.dest.module}`)) | ||
.then(() => remapSourcemap(`${project.workingDirectory}/${sourcePkg.dest.module}`)) | ||
// 6. UMD: ROLLUP | ||
.then(() => rollup({ | ||
moduleName: `${sourcePkg.meta.name}`, | ||
entry: `${project.workingDirectory}/${sourcePkg.dest.module}`, | ||
format: 'umd', | ||
dest: `${project.workingDirectory}/${sourcePkg.dest.main}` | ||
})) | ||
.then(() => remapSourcemap(`${project.workingDirectory}/${sourcePkg.dest.main}`)) | ||
// 7. COPY FILES | ||
.then(() => copyFiles(`${project.workingDirectory}/${sourcePkg.meta.prefix}/**/*.{js,js.map}`, `${project.dest}/${sourcePkg.meta.prefix}`)) | ||
.then(() => copyFiles(`${project.workingDirectory}/bundles/**/*.{js,js.map}`, `${project.dest}/bundles`)) | ||
.then(() => copyFiles(`${project.workingDirectory}/**/*.{d.ts,metadata.json}`, `${project.dest}`)) | ||
.then(() => copyFiles(`${project.src}/README.md`, project.dest)) | ||
.then(() => copyFiles(`${project.src}/LICENSE`, project.dest)) | ||
// 8. PACKAGE | ||
.then(() => createPackage(`${project.src}`, `${project.dest}`, sourcePkg.dest)) | ||
.then(() => { | ||
success(`Built Angular library in ${project.src}, written to ${project.dest}`); | ||
}); | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,100 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const GROUP_NAME_SEPARATOR = '/'; | ||
|
||
/** | ||
* Reads a JSON file. | ||
* | ||
* @param file Source file | ||
*/ | ||
export const readJson = (file: string): Promise<any> => { | ||
|
||
return new Promise((resolve, reject) => { | ||
fs.readFile(`${file}`, (err, data) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
|
||
try { | ||
resolve(JSON.parse(data)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
|
||
}); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Prepares a package... | ||
* | ||
* @param src Source directory containg `package.json` file | ||
*/ | ||
export const preparePackage = (src: string): Promise<any> => { | ||
|
||
return readJson(`${src}/package.json`) | ||
.then((pkg) => { | ||
let sourcePackage: any = {}; | ||
sourcePackage.pkg = pkg; | ||
|
||
// read metadata from package | ||
sourcePackage.meta = {}; | ||
sourcePackage.meta.name = `${sourcePackage.pkg.name}`; | ||
|
||
// split into name and prefix (@<group>/name) | ||
if (sourcePackage.pkg.name.includes(GROUP_NAME_SEPARATOR)) { | ||
const idx = `${sourcePackage.pkg.name}`.indexOf(GROUP_NAME_SEPARATOR); | ||
sourcePackage.meta.prefix = `${sourcePackage.pkg.name}`.substring(0, idx); | ||
sourcePackage.meta.name = `${sourcePackage.pkg.name}`.substring(idx + 1); | ||
} else { | ||
sourcePackage.meta.prefix = `${sourcePackage.pkg.name}`; | ||
} | ||
|
||
// set destination paths for package | ||
sourcePackage.dest = {}; | ||
sourcePackage.dest.main = `bundles/${sourcePackage.meta.name}.umd.js`; | ||
sourcePackage.dest.module = `${sourcePackage.meta.prefix}/${sourcePackage.meta.name}.es5.js`; | ||
sourcePackage.dest.es2015 = `${sourcePackage.meta.prefix}/${sourcePackage.meta.name}.js`; | ||
sourcePackage.dest.typings = `src/index.d.ts`; | ||
|
||
return Promise.resolve(sourcePackage); | ||
}); | ||
|
||
} | ||
|
||
|
||
/** | ||
* Creates a `package.json` file from 'packages/*' to 'dist/@prefix/*' | ||
* | ||
* @param src Source folder | ||
*/ | ||
export const createPackage = (src: string, dest: string, additionalProperties?: {}): Promise<any> => { | ||
|
||
return readJson(`${src}/package.json`) | ||
.then((packageJson) => { | ||
|
||
// set additional properties | ||
if (additionalProperties) { | ||
Object.keys(additionalProperties).forEach((key) => { | ||
packageJson[key] = additionalProperties[key]; | ||
}); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
|
||
const content = JSON.stringify(packageJson, undefined, 4); | ||
fs.writeFile(`${dest}/package.json`, content, (err) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
|
||
resolve(); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
const path = require('path'); | ||
|
||
// Read CLI arguments | ||
const ARGS = require('minimist')(process.argv.slice(2)); | ||
|
||
const project = ARGS.p || ARGS.project || path.resolve(process.cwd(), '.ng-packagr.json'); | ||
|
||
|
||
// @see https://github.com/TypeStrong/ts-node#programmatic-usage | ||
require('ts-node').register({ | ||
project: path.join(__dirname, '..', 'tsconfig.packagr.json') | ||
}); | ||
|
||
const ngPackagr = require('../lib/ng-packagr'); | ||
|
||
ngPackagr.packageAngular({ | ||
project | ||
}); |
Oops, something went wrong.