Skip to content

Commit

Permalink
Merge pull request #623 from davismj/build/typescript
Browse files Browse the repository at this point in the history
refactor: Convert to TypeScript
  • Loading branch information
davismj authored Oct 18, 2018
2 parents 93e273b + b40a9b4 commit 531eff1
Show file tree
Hide file tree
Showing 37 changed files with 7,660 additions and 8,099 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.json

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ jspm_packages
bower_components
.idea
.DS_STORE
.rollupcache
build/reports
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "aurelia-router/node_modules/typescript/lib"
}
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,26 @@ To build the code, follow these steps.
```shell
npm install
```
3. Ensure that [Gulp](http://gulpjs.com/) is installed. If you need to install it, use the following command:
3. To build the code, you can now run:

```shell
npm install -g gulp
npm run build
```
4. To build the code, you can now run:
4. You will find the compiled code in the `dist` folder, available in three module formats: AMD, CommonJS and ES6.

## Development

1. To run the project in development mode, you can run:

```shell
gulp build
npm start
```
5. You will find the compiled code in the `dist` folder, available in three module formats: AMD, CommonJS and ES6.

6. See `gulpfile.js` for other tasks related to generating the docs and linting.
2. If you want to copy over the newly built bundle, you can specified `--target`:

```
npm start -- --target ..\my-test-project
```

## Running The Tests

Expand Down
94 changes: 94 additions & 0 deletions build/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const rollup = require('rollup');
const typescript = require('rollup-plugin-typescript2');
const rimraf = require('rimraf');

const LIB_NAME = 'aurelia-router';
const cacheRoot = '.rollupcache';
const externalLibs = [
'aurelia-dependency-injection',
'aurelia-event-aggregator',
'aurelia-logging',
'aurelia-history',
'aurelia-route-recognizer'
];

clean().then(build).then(generateDts);

/**
* @type {() => Promise<Error | null>}
*/
function clean() {
console.log('\n==============\nCleaning dist folder...\n==============');
return new Promise(resolve => {
rimraf('dist', (error) => {
if (error) {
throw error;
}
resolve();
});
});
}

function generateDts() {
console.log('\n==============\nGenerating dts bundle...\n==============');
return new Promise(resolve => {
const ChildProcess = require('child_process');
ChildProcess.exec('npm run bundle-dts', (err, stdout, stderr) => {
if (err || stderr) {
console.log('Generating dts error:');
console.log(stderr);
} else {
console.log('Generated dts bundle successfully');
console.log(stdout);
}
resolve();
});
});
};

function build() {
console.log('\n==============\nBuidling...\n==============');
return Promise.all([
{
input: 'src/index.ts',
output: [
{ file: 'dist/es2015/index.js', format: 'es' }
],
external: externalLibs,
plugins: [
typescript({
tsconfigOverride: {
compilerOptions: {
target: 'es2015'
}
},
cacheRoot: cacheRoot
}),
]
},
{
input: 'src/index.ts',
output: [
{ file: 'dist/commonjs/index.js', format: 'cjs' },
{ file: 'dist/amd/index.js', format: 'amd', amd: { id: LIB_NAME } },
{ file: 'dist/native-modules/index.js', format: 'es' }
],
external: externalLibs,
plugins: [
typescript({
useTsconfigDeclarationDir: true,
tsconfigOverride: {
compilerOptions: {
target: 'es5'
}
},
cacheRoot: cacheRoot
}),
]
}
].map(cfg => {
return rollup
.rollup(cfg)
.then(bundle => Promise.all(cfg.output.map(o => bundle.write(o))));
}));
};
16 changes: 16 additions & 0 deletions build/scripts/changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fs = require('fs');
const paths = require('../paths');
const path = require('path');
const conventionalChangelog = require('conventional-changelog');
const dest = path.resolve(process.cwd(), paths.doc, 'CHANGELOG.md');

let changelogChunk = '';
const changelogStream = conventionalChangelog({ preset: 'angular' })
.on('data', chunk => changelogChunk += chunk.toString('utf8'))
.on('end', () => {
changelogStream.removeAllListeners();
const data = fs.readFileSync(dest, 'utf-8');
const fd = fs.openSync(dest, 'w+');
fs.writeSync(fd, Buffer.from(changelogChunk, 'utf8'), 0, changelogChunk.length, 0);
fs.writeSync(fd, Buffer.from(data, 'utf8'), 0, data.length, changelogChunk.length);
});
142 changes: 142 additions & 0 deletions build/scripts/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const args = require('../tasks/args');
const rollup = require('rollup');
const typescript = require('rollup-plugin-typescript2');
const ChildProcess = require('child_process');

const targetFormats = args.format || ['commonjs']; // by default only run devs for commonjs
const targetDir = args.target;

const buildConfigs = {
es2015: {
output: {
file: 'dist/es2015/index.js',
format: 'es'
},
tsConfig: {
tsconfigOverride: {
compilerOptions: {
target: 'es2015'
}
}
}
},
amd: {
output: {
file: 'dist/amd/index.js',
format: 'amd',
amd: { id: 'aurelia-router' }
},
tsConfig: {
tsconfigOverride: {
compilerOptions: {
target: 'es5'
}
}
}
},
commonjs: {
output: {
file: 'dist/commonjs/index.js',
format: 'cjs'
},
tsConfig: {
tsconfigOverride: {
compilerOptions: {
target: 'es5'
}
}
}
},
'native-modules': {
output: {
file: 'dist/commonjs/index.js',
format: 'es'
},
tsConfig: {
tsconfigOverride: {
compilerOptions: {
target: 'es5'
}
}
}
}
};

console.log('Running dev with targets:', targetFormats);

/**
* @param {string} format
*/
async function roll(format) {
const inputOptions = {
input: 'src/index.ts',
external: [
'aurelia-dependency-injection',
'aurelia-event-aggregator',
'aurelia-logging',
'aurelia-history',
'aurelia-route-recognizer'
],
plugins: [
typescript(Object.assign(
{ cacheRoot: '.rollupcache' },
buildConfigs[format].tsConfig
))
]
};
console.log('Starting watcher');
const watcher = rollup
.watch({
...inputOptions,
output: buildConfigs[format].output
});

watcher.on('event', (e) => {
if (e.code === 'BUNDLE_END') {
console.log('Finished compilation. Running post task bundling dts.');
generateDtsBundle();
}
});
}

function generateDtsBundle() {
return new Promise(resolve => {
ChildProcess.exec('npm run bundle-dts', (err, stdout, stderr) => {
if (err || stderr) {
console.log('Bundling dts error');
console.log(err);
console.log('========');
console.log('stderr');
console.log(stderr);
} else {
console.log('Generated dts bundle successfully');
}
resolve(err ? [null, err] : [null, null]);
});
});
}

targetFormats.forEach(roll);

console.log('Target directory for copy: "' + targetDir + '"');
if (targetDir) {
console.log('Watching dist folder');
const gulpWatch = require('gulp-watch');
const path = require('path');
const cwd = process.cwd();
const destPath = path.join(cwd, targetDir, 'node_modules', 'aurelia-router');
const fs = require('fs');
gulpWatch('dist/**/*.*', { ignoreInitial: true }, (vinyl) => {
if (vinyl.event !== 'unlink') {
console.log(`change occurred at "${vinyl.path}". Copying over to specified project`);
const subPath = vinyl.path.replace(cwd, '');
try {
fs.createReadStream(vinyl.path)
.pipe(fs.createWriteStream(path.join(destPath, subPath)));
} catch (ex) {
console.log(`Error trying to copy file from "${vinyl.path}" to "${destPath}"`);
console.log(ex);
}
}
});
}
11 changes: 11 additions & 0 deletions build/tasks/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = require('yargs')
.options('target', {
alias: 't',
description: 'target module dir to copy build results into (eg. "--target ../other-module" to copy build results into "../other-module/node_modules/this-module/dist/…" whenever they change)'
})
.options('format', {
alias: 'f',
array: true,
description: 'format to compile to (eg. "es2015", "commonjs", …). Can be set muliple times to compile to multiple formats. Default is all formats.'
})
.argv;
Loading

0 comments on commit 531eff1

Please sign in to comment.