2017-08-16 174 views
0

我试图创建一个可重用的NPM模块。我遵循的教程说我需要dist文件夹中的package.json文件。所以起初我只是将package.json文件从我的项目的根目录复制到dist文件夹中,一切都很好。npm包:package.json处理

问题是,当涉及到源代码控制时,您确实不希望在dist文件夹中检入任何内容,所以我所做的是在我的项目的根目录中创建一个package.dist.json文件,然后在构建步骤将其复制到dist文件夹并重新命名。

这不起作用。这里是我的构建脚本:

"scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1", 
    "transpile": "ngc", 
    "package": "rollup -c", 
    "minify": "uglifyjs dist/bundles/test-service.umd.js --screw-ie8 --compress --mangle --comments --output dist/bundles/test-service.umd.min.js", 
    "build": "copyfiles package.dist.json dist && rename dist/package.dist.json dist/package.json && npm run transpile && npm run package && npm run minify && npm pack dist/" 
}, 

这里是我的devDependencies:

"devDependencies": { 
    "@angular/compiler": "^4.0.0", 
    "@angular/compiler-cli": "^4.0.0", 
    "rollup": "^0.47.4", 
    "typescript": "^2.4.2", 
    "uglify-js": "^3.0.27", 
    "copyfiles": "^1.0.0", 
    "rename-cli": "^4.0.0" 
    }, 

这不是工作,我得到一个错误说:

该命令的语法不正确。

我甚至不确定这是否是最好的方法。有更好的方法吗?如果没有,为什么我的build任务不工作?

这里是rollup.config.js:

import angularInline from 'rollup-plugin-angular-inline'; 

export default { 
    entry: 'dist/hello-world.js', 
    dest: 'dist/bundles/hello-world.umd.js', 
    sourceMap: 'inline', 
    format: 'umd', 
    moduleName: 'hello-world-app', 
    globals: { 
     '@angular/core': 'ng.core', 
     '@angular/router': 'ng.router' 
    }, 
    plugins: [ 
     angularInline({ 
      include: './dist/src/**/*.component.js' 
     }) 
    ], 
    external: ['@angular/core', '@angular/router'], 
    treeshake: true 
} 
+0

您另外一个脚本必须畸形。你能否用你的全部包裹更新你的问题。json'scripts'(minify,transpile,等) –

+0

我已经用所有脚本更新了我的问题。 – Zeus82

+0

你也介意发布你的rollup.config文件。 –

回答

0

看来您使用的是Windows,作为quick google search表示该错误是一个Windows命令行错误。如果您在命令行问题上寻求建议(以备将来参考),此信息非常有用。

尝试以下操作:

  • 包文件名要复制/引号移动

    copyfiles "package.dist.json" dist 
    
  • 使用Windows路径分隔符\(不是Unix/Linux操作系统分离/)时复制/移动

    rename dist\package.dist.json dist\package.json 
    # also try with quotes 
    rename "dist\package.dist.json" "dist\package.json" 
    

然而,当试图建立一个分发模块,用户可能要到餐桌您的代码并运行自己建立在不同的平台上,我建议建立你的代码,这将跨平台工作更强大的技术。

  • 选项1:一饮而尽,咕噜,和/或朋友
  • 选项2:写在你的NodeJS构建代码。这可以只是一个简单的JS文件,它使用NodeJS和内置的fs模块完成所有的复制,重命名等操作。你的构建命令只是"build": "node my-build-script.js"