我可以为您提供一个 Grunt 脚本来了解任务的顺序。实际上我使用的顺序是这个:
- 创建构建目录。我通常使用/build/web。我通常在 /web 目录中创建这些文件(index.html、main.dart、/css 等)。我将其余组件创建到 /lib 目录中。
- 将包含 main() 函数的 .dart 文件(对于更简单的项目,在我的情况下为“main.dart”)文件编译为 Javascript 并将其放入 /build/web 目录
- 将其他需要的文件和文件夹复制到 /build/web 目录。此外,在此过程中,您将复制项目所需的包。您将在下面提供的示例中看到。
- 从项目中删除所有空文件夹
- 构建过程结束后,您可以创建 Grunt 任务以在浏览器中打开 /index.html 文件(我不会提供此示例)
飞镖测试项目的结构:
testApp
- gruntfile.js
- package.js
/lib
/packages
/angular
/web
- index.html
- main.dart
/css
/img
因此,涵盖 1 到 4 步骤的 Grunt 示例脚本如下所示(将其复制到 gruntfile.js):
module.exports = function (grunt) {
grunt.initConfig({
// 1.
// create build web directory
mkdir: {
build: {
options: {
create: ['build/web']
}
}
},
// 2.
// compile dart files
dart2js: {
options: {
// use this to fix a problem into dart2js node module. The module calls dart2js not dart2js.bat.
// this is needed for Windows. So use the path to your dart2js.bat file
"dart2js_bin": "C:/dart/dart-sdk/bin/dart2js.bat"
},
compile: {
files: {'build/web/main.dart.js': 'web/main.dart'}
}
},
// 3.
// copy all needed files, including all needed packages
// except the .dart files.
copy: {
build: {
files: [
{
expand: true,
src: [
'web/!(*.dart)',
'web/css/*.css',
'web/res/*.svg',
'web/packages/angular/**/!(*.dart)',
'web/packages/browser/**/!(*.dart)'
],
dest: 'build'
}
]
}
},
// 4.
// remove empty directories copied using the previous task
cleanempty: {
build: {
options: {
files: false
},
src: ['build/web/packages/**/*']
}
},
});
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.registerTask('default', [
'mkdir:build',
'dart2js',
'copy:build',
'cleanempty:build'
]);
};
这是 Grunt 脚本示例。
在项目的根目录中创建一个 /gruntfile.js 文件并将脚本复制/粘贴到其中。
在项目的根目录中创建一个 /package.json 文件并复制/粘贴以下脚本:
{
"name": "testApp",
"version": "0.0.1",
"description": "SomeDescriptionForTheTestApp",
"main": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "YourName",
"peerDependencies": {
"grunt-cli": "^0.1.13"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-cleanempty": "^1.0.3",
"grunt-contrib-copy": "^0.7.0",
"grunt-dart2js": "0.0.5",
"grunt-mkdir": "^0.1.2",
"matchdep": "^0.3.0"
}
}
在 Windows 中打开命令提示符,在 Linux 中打开终端,导航到项目的根目录并使用以下命令:
npm install
等到所有需要的 Grunt 模块都下载到你的本地项目中。完成后,在命令提示符或终端中发出以下命令:
node -e "require('grunt').cli()"
您可以使用它来启动 Grunt 默认任务,而无需在系统上全局安装 Grunt。
现在,要了解项目的确切构建结构(包括项目所需的包),请使用 Pub Build 进行构建。然后您将能够指示 Grunt 创建相同的 dir 结构。
如果需要,您可以添加其他任务(如缩小)。
希望这将帮助大家理解该过程并让您首先开始使用测试应用程序。添加您的评论以使其变得更好并更加简化。