mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 19:13:34 +08:00
Moving Angular-CLI into `packages/` and building the angular-cli. The published packages will now have JavaScript only (with their d.ts), instead of being a mix of TypeScript and JavaScript, and it won't have the hacks of having a custom require extension to load the TypeScript. Our development flow will be more streamlined, tools are going to be better (no more undefined symbols that are actually defined), and the npm packages will boot up way faster (since there's no more compilation during runtime). Locally nothing changes, use `npm link` and run your local `ng`. Using the `npm` releases though it should be all javascript from the release. The E2E tests also now uses the `dist` folder output to install in the new E2E app, making sure the process works for an app published to npm. Note: Even though we're following the lerna structure, we won't be using their tool anytime soon.
67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
/* eslint-disable no-console */
|
|
'use strict';
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const ts = require('typescript');
|
|
|
|
|
|
global.angularCliIsLocal = true;
|
|
|
|
const compilerOptions = JSON.parse(fs.readFileSync(path.join(__dirname, '../tsconfig.json')));
|
|
|
|
const oldRequireTs = require.extensions['.ts'];
|
|
require.extensions['.ts'] = function(m, filename) {
|
|
// If we're in node module, either call the old hook or simply compile the
|
|
// file without transpilation. We do not touch node_modules/**.
|
|
// We do touch `angular-cli` files anywhere though.
|
|
if (!filename.match(/angular-cli/) && filename.match(/node_modules/)) {
|
|
if (oldRequireTs) {
|
|
return oldRequireTs(m, filename);
|
|
}
|
|
return m._compile(fs.readFileSync(filename), filename);
|
|
}
|
|
|
|
// Node requires all require hooks to be sync.
|
|
const source = fs.readFileSync(filename).toString();
|
|
|
|
try {
|
|
const result = ts.transpile(source, compilerOptions['compilerOptions']);
|
|
|
|
// Send it to node to execute.
|
|
return m._compile(result, filename);
|
|
} catch (err) {
|
|
console.error('Error while running script "' + filename + '":');
|
|
console.error(err.stack);
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
//
|
|
// require('ts-node').register({
|
|
// project: path.dirname(__dirname),
|
|
// lazy: true
|
|
// });
|
|
|
|
// If we're running locally, meaning npm linked. This is basically "developer mode".
|
|
if (!__dirname.match(new RegExp(`\\${path.sep}node_modules\\${path.sep}`))) {
|
|
const packages = require('./packages');
|
|
|
|
// We mock the module loader so that we can fake our packages when running locally.
|
|
const Module = require('module');
|
|
const oldLoad = Module._load;
|
|
Module._load = function (request, parent) {
|
|
if (request in packages) {
|
|
return oldLoad.call(this, packages[request].main, parent);
|
|
} else if (request.startsWith('angular-cli/')) {
|
|
// We allow deep imports (for now).
|
|
// TODO: move tests to inside angular-cli package so they don't have to deep import.
|
|
const dir = path.dirname(parent.filename);
|
|
const newRequest = path.relative(dir, path.join(__dirname, '../packages', request));
|
|
return oldLoad.call(this, newRequest, parent);
|
|
} else {
|
|
return oldLoad.apply(this, arguments);
|
|
}
|
|
};
|
|
}
|