2017-08-16 264 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"