gpt4 book ai didi

typescript - 如何使用 Gulp 和 SystemJS 捆绑 Angular 2 Typescript 应用程序?

转载 作者:太空狗 更新时间:2023-10-29 16:50:18 25 4
gpt4 key购买 nike

在我的 Angular 2 项目中,我使用 Typescript 和 SystemJS 进行模块加载,并使用 Gulp 作为任务运行器。项目中 Angular 的当前版本是 RC2,但问题也存在于 RC1 中。我按照白兰度回答的步骤here .

捆绑我的应用程序并初始加载网站 SystemJS 后抛出错误:

Error: http://localhost:57462/app/app.bundle.js detected as register but didn't execute.

应用程序可以运行,但控制台中的错误绝对不是一件好事。

我在空项目上测试了我的配置,但问题再次出现,所以我认为问题出在配置上。

这里是:

gulp 文件

var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
var typescript = require('gulp-typescript');
var jspm = require('gulp-jspm-build');

gulp.task('compile:ts', function () {
return gulp.src(['./appTS/**/*.ts'])
.pipe(sourcemaps.init())
.pipe(typescript({
noEmitOnError: true,
target: 'ES5',
removeComments: false,
experimentalDecorators: true,
emitDecoratorMetadata: true,
module: 'system',
moduleResolution: 'node'
}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./app/'));
});

gulp.task('copy:vendor', function () {
return gulp.src([
'node_modules/systemjs/dist/system-polyfills.js',
'node_modules/reflect-metadata/Reflect.js',
'node_modules/core-js/client/shim.min.js',
'node_modules/zone.js/dist/zone.min.js',
'node_modules/systemjs/dist/system.js',
'node_modules/rxjs/bundles/Rx.js'
]).pipe(gulp.dest('./assets/vendor/'));
});

gulp.task('bundle:app', ['compile:ts'], function () {
return jspm({
bundleOptions: {
minify: true,
mangle: false
},
bundleSfx: true,
bundles: [
{ src: './app/main.js', dst: 'bundle.js' }
]
})
.pipe(gulp.dest('./app/'));
});

gulp.task('bundle', ['bundle:app', 'copy:vendor'], function () {
return gulp.src([
'./assets/vendor/Reflect.js',
'./assets/vendor/shim.min.js',
'./assets/vendor/zone.min.js',
'./app/bundle.js'])
.pipe(concat('app.bundle.js'))
.pipe(gulp.dest('./app/'))
});

gulp.task('default', ['bundle']);

var packages = {
app: {
format: 'register',
defaultExtension: 'js'
},
"symbol-observable": { main: 'index.js', defaultExtension: 'js' },
"reflect-metadata": { main: 'Reflect.js', defaultExtension: 'js' }
};

var ngPackageNames = ['common',
'compiler',
'core',
'http',
'platform-browser',
'platform-browser-dynamic',
'router',
'router-deprecated',
'upgrade'];

ngPackageNames.forEach(function (element, index, array) {
packages['@angular/' + element] = { main: 'bundles/' + element + '.umd.min.js', defaultExtension: 'js' };
});

System.config({
main: 'dispel.bundle.min',
defaultExtension: 'js',
format: 'register',
packages: packages,
baseURL: "/",
defaultJSExtensions: true,
transpiler: false,
paths: {
"node_modules*": "node_modules*",
"@angular*": "node_modules/@angular/*"
},
map: {
"@angular": "node_modules/@angular",
"symbol-observable": "node_modules/symbol-observable",
"ng2-translate": "node_modules/ng2-translate",
"es6-shim": "node_modules/es6-shim",
"reflect-metadata": "node_modules/reflect-metadata",
"rxjs": "node_modules/rxjs",
"zone.js": "node_modules/zone.js"
}
});

最佳答案

您是否尝试过使用 SystemJS Builder在您的 bundle:app gulp 任务中而不是 jspm?

以下是如何捆绑运行 2.0.0-rc.1(sourcelive example)的英雄之旅的简化版本。

gulpfile.js

var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
var typescript = require('gulp-typescript');
var systemjsBuilder = require('systemjs-builder');

// Compile TypeScript app to JS
gulp.task('compile:ts', function () {
return gulp
.src([
"appTS/**/*.ts",
"typings/*.d.ts"
])
.pipe(sourcemaps.init())
.pipe(typescript({
"module": "system",
"moduleResolution": "node",
"outDir": "app",
"target": "ES5"
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('app'));
});

// Generate systemjs-based bundle (app/app.js)
gulp.task('bundle:app', function() {
var builder = new systemjsBuilder('public', './system.config.js');
return builder.buildStatic('app', 'app/app.js');
});

// Copy and bundle dependencies into one file (vendor/vendors.js)
// system.config.js can also bundled for convenience
gulp.task('bundle:vendor', function () {
return gulp.src([
'node_modules/zone.js/dist/zone.js',
'node_modules/reflect-metadata/Reflect.js',
'node_modules/systemjs/dist/system-polyfills.js',
'node_modules/core-js/client/shim.min.js',
'node_modules/systemjs/dist/system.js',
'system.config.js',
])
.pipe(concat('vendors.js'))
.pipe(gulp.dest('vendor'));
});

// Copy dependencies loaded through SystemJS into dir from node_modules
gulp.task('copy:vendor', function () {
return gulp.src([
'node_modules/rxjs/bundles/Rx.js',
'node_modules/@angular/**/*'
])
.pipe(gulp.dest('vendor'));
});

gulp.task('vendor', ['bundle:vendor', 'copy:vendor']);
gulp.task('app', ['compile:ts', 'bundle:app']);

// Bundle dependencies and app into one file (app.bundle.js)
gulp.task('bundle', ['vendor', 'app'], function () {
return gulp.src([
'app/app.js',
'vendor/vendors.js'
])
.pipe(concat('app.bundle.js'))
.pipe(uglify())
.pipe(gulp.dest('./app'));
});

gulp.task('default', ['bundle']);

system.config.js

var map = {
'app': 'app',
'rxjs': 'vendor/rxjs',
'zonejs': 'vendor/zone.js',
'reflect-metadata': 'vendor/reflect-metadata',
'@angular': 'vendor/@angular'
};

var packages = {
'app': { main: 'main', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' },
'zonejs': { main: 'zone', defaultExtension: 'js' },
'reflect-metadata': { main: 'Reflect', defaultExtension: 'js' }
};

var packageNames = [
'@angular/common',
'@angular/compiler',
'@angular/core',
'@angular/http',
'@angular/platform-browser',
'@angular/platform-browser-dynamic',
'@angular/router',
'@angular/router-deprecated',
'@angular/testing',
'@angular/upgrade',
];

packageNames.forEach(function(pkgName) {
packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
});

System.config({
map: map,
packages: packages
});

关于typescript - 如何使用 Gulp 和 SystemJS 捆绑 Angular 2 Typescript 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37862457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com