我是 Grunt 及其所有插件的新手,但我想学习和设置一些很棒的前端工具。话虽如此,我一直在关注Grunt docs以及 Grunt 和 Babel 的一些类似问题,通过 Github ,但我似乎不断收到以下回溯:
Running "babel:dist" (babel) task
Warning: Unable to write "dist" file (Error code: EISDIR). Use --force to continue.
Aborted due to warnings.
任何对这个新手更清晰的解释将不胜感激。我是 JS 编程和设置 DevOps 的新手,因此鼓励提供一些有用的提示和最佳实践。
<小时/>设置:
js_practice/(根项目)
|----package.json
所有发行版和软件包
|---node_modules/
服务器端( Node )支持
|---es6/
| ------ test.js
|---距离/
客户端(浏览器)支持
|----公共(public)/
|---es6/(位于公共(public)文件夹内)
| ----- test2.js
|---dist/(位于公共(public)文件夹内)
这是我当前的代码:
Grunt file.js
module.exports = function(grunt) {
// All of your Grunt code must be specified inside this function!
// Setup configuration...
// Load tasks...
// Define tasks...
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
babel: {
options: {
sourceMap: true,
presets: ['babel-preset-es2015']
},
dist: {
files: [
// Node source
{
src: ['es6/**/*.js'],
dest: 'dist'},
// Browser source
{
src: ['public/es6/**/*.js'],
dest: 'public/dist'},
],
},
},
browserify: {
dist: {
options: {
transform: [["babelify", { "stage": 0 }]]
},
files: {
"build/bundle.js": "src/main.js"
}
}
},
jshint: {
scripts: {
src: ['scripts/**.js', 'lib/**.js']
},
tests: { // We can have more than one jshint task, this ones called `jshint:tests`
src: 'tests/**.js'
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
},
scripts: {
expand: true,
cwd: 'scripts/',
src: '**.js',
dest: 'build/',
ext: '.min.js'
}
},
watch: {
scripts: {
files: ['**/*.js'],
tasks: ['jshint'],
},
styles: {
files: 'styles/**.less',
task: 'less:styles'
}
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-babel');
grunt.registerTask('default', ['babel','browserify','jshint']);
grunt.registerTask('build', ['jshint', 'uglify']);
};
最佳答案
只是扩展我在评论中所说的内容
Any clearer explanation to this newbie would be HIGHLY appreciated.
Nodejs 正在尝试写入一个名为 dist
的文件,但会产生错误,因为存在具有该名称的目录。
这个问题的原因可以在babel任务中找到。
files: [{
src: ['es6/**/*.js'],
dest: 'dist'
},
// Browser source
{
src: ['public/es6/**/*.js'],
dest: 'public/dist'
}]
你必须告诉 Babel 获取 es6/
中的每个文件,对其进行转换并将新文件放置在 dist/
中文件夹。就目前而言,变压器尝试创建一个名为 dist.txt 的文件。将其重写为
files: [{
expand: true,
cwd: 'es6/'
src: ['**/*.js'],
dest: 'dist/'
},
// Browser source
{
expand: true,
cwd: 'public/es6/'
src: ['**/*.js'],
dest: 'public/dist/'
}]
应该会产生更好的结果,但可以尝试一下。正如我提到的,我已经有一段时间没有使用 Grunt 了。 Take a look at the documentation on building the files object
就像我在评论中所说的那样,使用 jshint 或其他此类工具(eslint 是所有炒作......)来保持您自己的代码整洁。不要浪费时间对您可能不想自行修复的 lib 文件运行 jshint 任务。并且始终在源文件上运行 jshint,而不是由 babel 或 browserify 转换的文件。它只是一个帮助您编写更好代码的工具,而不是一些生成器。
关于javascript - Grunt/Babel 错误 : Unable to write "dist" file (Error code: EISDIR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36071303/