Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
webpack,如何将外部文件从node_模块复制到另一个文件夹_Webpack_Angular Cli - Fatal编程技术网

webpack,如何将外部文件从node_模块复制到另一个文件夹

webpack,如何将外部文件从node_模块复制到另一个文件夹,webpack,angular-cli,Webpack,Angular Cli,我使用的是Angular cli,它使用引擎盖下的网页包 我想要的是将所有外部(angular/core、rx/*、module.exports中定义的所有内容)复制到target/classes/static(我的输出文件夹) 关于上下文的更多信息:我希望这些依赖项在我的js包之外,这样我就可以在angular应用程序范围之外访问它 看起来很容易,但我做不到 这是我的webpack-config.js const fs = require('fs'); const path = require

我使用的是Angular cli,它使用引擎盖下的网页包 我想要的是将所有外部(angular/core、rx/*、module.exports中定义的所有内容)复制到target/classes/static(我的输出文件夹)

关于上下文的更多信息:我希望这些依赖项在我的js包之外,这样我就可以在angular应用程序范围之外访问它

看起来很容易,但我做不到

这是我的webpack-config.js

const fs = require('fs');
const path = require('path');
const ConcatPlugin = require('webpack-concat-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
const postcssUrl = require('postcss-url');
const cssnano = require('cssnano');

const { NoEmitOnErrorsPlugin, SourceMapDevToolPlugin, NamedModulesPlugin } = require('webpack');
const { InsertConcatAssetsWebpackPlugin, NamedLazyChunksWebpackPlugin, BaseHrefWebpackPlugin } = require('@angular/cli/plugins/webpack');
const { CommonsChunkPlugin } = require('webpack').optimize;
const { AotPlugin } = require('@ngtools/webpack');

const nodeModules = path.join(process.cwd(), 'node_modules');
const realNodeModules = fs.realpathSync(nodeModules);
const genDirNodeModules = path.join(process.cwd(), 'src', '$$_gendir', 'node_modules');
const entryPoints = ["inline","polyfills","sw-register","styles","vendor","main"];
const minimizeCss = false;
const baseHref = "";
const deployUrl = "";
const postcssPlugins = function () {
        // safe settings based on: https://github.com/ben-eb/cssnano/issues/358#issuecomment-283696193
        const importantCommentRe = /@preserve|@license|[@#]\s*source(?:Mapping)?URL|^!/i;
        const minimizeOptions = {
            autoprefixer: false,
            safe: true,
            mergeLonghand: false,
            discardComments: { remove: (comment) => !importantCommentRe.test(comment) }
        };
        return [
            postcssUrl({
                url: (URL) => {
                    // Only convert root relative URLs, which CSS-Loader won't process into require().
                    if (!URL.startsWith('/') || URL.startsWith('//')) {
                        return URL;
                    }
                    if (deployUrl.match(/:\/\//)) {
                        // If deployUrl contains a scheme, ignore baseHref use deployUrl as is.
                        return `${deployUrl.replace(/\/$/, '')}${URL}`;
                    }
                    else if (baseHref.match(/:\/\//)) {
                        // If baseHref contains a scheme, include it as is.
                        return baseHref.replace(/\/$/, '') +
                            `/${deployUrl}/${URL}`.replace(/\/\/+/g, '/');
                    }
                    else {
                        // Join together base-href, deploy-url and the original URL.
                        // Also dedupe multiple slashes into single ones.
                        return `/${baseHref}/${deployUrl}/${URL}`.replace(/\/\/+/g, '/');
                    }
                }
            }),
            autoprefixer(),
        ].concat(minimizeCss ? [cssnano(minimizeOptions)] : []);
    };




module.exports = {
  //add externals for all external libs
  "externals": {
    '@angular/core': 'ng.core',
    '@angular/common': 'ng.common',
    '@angular/compiler': 'ng.compiler',
    '@angular/platform-browser': 'ng.platformBrowser',
    '@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
    '@angular/http': 'ng.http',
    '@angular/forms': 'ng.forms',

    'rxjs/Observable': 'Rx',
    'rxjs/Subject': 'Rx',
    'rxjs/Observer': 'Rx',
    'rxjs/operator/last': 'Rx.Observable.prototype',
    'rxjs/operator/filter': 'Rx.Observable.prototype',
    'rxjs/operator/concatMap': 'Rx.Observable.prototype',
    'rxjs/operator/toPromise': 'Rx.Observable.prototype',
    },
  "devtool": "source-map",
  "resolve": {
    "extensions": [
      ".ts",
      ".js"
    ],
    "modules": [
      "./node_modules",
      "./node_modules"
    ],
    "symlinks": true
  },
  "resolveLoader": {
    "modules": [
      "./node_modules",
      "./node_modules"
    ]
  },
  "entry": {
    "main": [
      "./src/main.ts"
    ],
    "polyfills": [
      "./src/polyfills.ts"
    ],
    "styles": [
      "./src/styles.css",
      "./node_modules/prismjs/themes/prism-okaidia.css"
    ]
  },
  "output": {
    "path": path.join(process.cwd(), "../../../../target/classes/static"),
    "filename": "[name].bundle.js",
    "chunkFilename": "[id].chunk.js"
  },
  "module": {
    "rules": [
      {
        "enforce": "pre",
        "test": /\.js$/,
        "loader": "source-map-loader",
        "exclude": [
          /(\\|\/)node_modules(\\|\/)/
        ]
      },
      {
        "test": /\.html$/,
        "loader": "raw-loader"
      },
      {
        "test": /\.(eot|svg|cur)$/,
        "loader": "file-loader?name=[name].[hash:20].[ext]"
      },
      {
        "test": /\.(jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,
        "loader": "url-loader?name=[name].[hash:20].[ext]&limit=10000"
      },
      {
        "exclude": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.css$/,
        "use": [
          "exports-loader?module.exports.toString()",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          }
        ]
      },
      {
        "exclude": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.scss$|\.sass$/,
        "use": [
          "exports-loader?module.exports.toString()",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          },
          {
            "loader": "sass-loader",
            "options": {
              "sourceMap": false,
              "precision": 8,
              "includePaths": []
            }
          }
        ]
      },
      {
        "exclude": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.less$/,
        "use": [
          "exports-loader?module.exports.toString()",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          },
          {
            "loader": "less-loader",
            "options": {
              "sourceMap": false,
              "paths": []
            }
          }
        ]
      },
      {
        "exclude": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.styl$/,
        "use": [
          "exports-loader?module.exports.toString()",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          },
          {
            "loader": "stylus-loader",
            "options": {
              "sourceMap": false,
              "paths": []
            }
          }
        ]
      },
      {
        "include": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.css$/,
        "use": [
          "style-loader",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          }
        ]
      },
      {
        "include": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.scss$|\.sass$/,
        "use": [
          "style-loader",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          },
          {
            "loader": "sass-loader",
            "options": {
              "sourceMap": false,
              "precision": 8,
              "includePaths": []
            }
          }
        ]
      },
      {
        "include": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.less$/,
        "use": [
          "style-loader",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          },
          {
            "loader": "less-loader",
            "options": {
              "sourceMap": false,
              "paths": []
            }
          }
        ]
      },
      {
        "include": [
          path.join(process.cwd(), "src/styles.css"),
          path.join(process.cwd(), "node_modules/prismjs/themes/prism-okaidia.css")
        ],
        "test": /\.styl$/,
        "use": [
          "style-loader",
          {
            "loader": "css-loader",
            "options": {
              "sourceMap": false,
              "importLoaders": 1
            }
          },
          {
            "loader": "postcss-loader",
            "options": {
              "ident": "postcss",
              "plugins": postcssPlugins
            }
          },
          {
            "loader": "stylus-loader",
            "options": {
              "sourceMap": false,
              "paths": []
            }
          }
        ]
      },
      {
        "test": /\.ts$/,
        "loader": "@ngtools/webpack"
      }
    ]
  },
  "plugins": [
    new NoEmitOnErrorsPlugin(),
    new ConcatPlugin({
      "uglify": false,
      "sourceMap": true,
      "name": "scripts",
      "fileName": "[name].bundle.js",
      "filesToConcat": [
        "/Users/seb/leroy/prod/kobi-designer/src/main/resources/angular/node_modules/systemjs/dist/system.src.js"
      ]
    }),
    new InsertConcatAssetsWebpackPlugin([
      "scripts"
    ]),
    new CopyWebpackPlugin([
      {
        "context": "/Users/seb/leroy/prod/kobi-designer/src/main/resources/angular/src/",
        "to": "",
        "from": {
          "glob": "assets",
          "dot": true
        }
      },
      {
        "context": "/Users/seb/leroy/prod/kobi-designer/src/main/resources/angular/src/",
        "to": "",
        "from": {
          "glob": "favicon.ico",
          "dot": true
        }
      }
    ], {
      "ignore": [
        ".gitkeep"
      ],
      "debug": "warning"
    }),
    new ProgressPlugin(),
    new CircularDependencyPlugin({
      "exclude": /(\\|\/)node_modules(\\|\/)/,
      "failOnError": false
    }),
    new NamedLazyChunksWebpackPlugin(),
    new HtmlWebpackPlugin({
      "template": "./src/not.used",
      "filename": "./not.used",
      "hash": false,
      "inject": true,
      "compile": true,
      "favicon": false,
      "minify": false,
      "cache": true,
      "showErrors": true,
      "chunks": "all",
      "excludeChunks": [],
      "title": "Webpack App",
      "xhtml": true,
      "chunksSortMode": function sort(left, right) {
        let leftIndex = entryPoints.indexOf(left.names[0]);
        let rightindex = entryPoints.indexOf(right.names[0]);
        if (leftIndex > rightindex) {
            return 1;
        }
        else if (leftIndex < rightindex) {
            return -1;
        }
        else {
            return 0;
        }
    }
    }),
    new BaseHrefWebpackPlugin({}),
    new CommonsChunkPlugin({
      "name": [
        "inline"
      ],
      "minChunks": null
    }),
    new CommonsChunkPlugin({
      "name": [
        "vendor"
      ],
      "minChunks": (module) => {
                return module.resource
                    && (module.resource.startsWith(nodeModules)
                        || module.resource.startsWith(genDirNodeModules)
                        || module.resource.startsWith(realNodeModules));
            },
      "chunks": [
        "main"
      ]
    }),
    new SourceMapDevToolPlugin({
      "filename": "[file].map[query]",
      "moduleFilenameTemplate": "[resource-path]",
      "fallbackModuleFilenameTemplate": "[resource-path]?[hash]",
      "sourceRoot": "webpack:///"
    }),
    new CommonsChunkPlugin({
      "name": [
        "main"
      ],
      "minChunks": 2,
      "async": "common"
    }),
    new NamedModulesPlugin({}),
    new AotPlugin({
      "mainPath": "main.ts",
      "replaceExport": false,
      "hostReplacementPaths": {
        "environments/environment.ts": "environments/environment.ts"
      },
      "exclude": [],
      "tsConfigPath": "src/tsconfig.app.json",
      "skipCodeGeneration": true
    })
  ],
  "node": {
    "fs": "empty",
    "global": true,
    "crypto": "empty",
    "tls": "empty",
    "net": "empty",
    "process": true,
    "module": false,
    "clearImmediate": false,
    "setImmediate": false
  },
  "devServer": {
    "historyApiFallback": true
  }
};
const fs=require('fs');
const path=require('path');
const ConcatPlugin=require('webpack-concat-plugin');
const CopyWebpackPlugin=require('copy-webpack-plugin');
const ProgressPlugin=require('webpack/lib/ProgressPlugin');
const CircularDependencyPlugin=require('circular-dependency-plugin');
const HtmlWebpackPlugin=require('html-webpack-plugin');
const autoprefixer=require('autoprefixer');
const postssurl=require('postsss-url');
const cssnano=require('cssnano');
const{NoEmitOnErrorsPlugin,SourceMapDevToolPlugin,NamedModulesPlugin}=require('webpack');
const{insertconcatassetswebackplugin,namedlazychunkswappackplugin,BaseHrefWebpackPlugin}=require('@angular/cli/plugins/webpack');
const{commonChunkPlugin}=require('webpack')。优化;
const{AotPlugin}=require('@ngtools/webpack');
const nodeModules=path.join(process.cwd(),'node_modules');
const realNodeModules=fs.realpathSync(nodeModules);
const genDirNodeModules=path.join(process.cwd(),'src','$$\u gendir','node\u modules');
常量入口点=[“内联”、“多边形填充”、“软件寄存器”、“样式”、“供应商”、“主”];
const minimizeCss=false;
const baseHref=“”;
const deployUrl=“”;
const postsssplugins=函数(){
//安全设置基于:https://github.com/ben-eb/cssnano/issues/358#issuecomment-283696193
const importantCommentRe=/@preserve |@license |[@#]\s*源(?:映射)?URL |^!/i;
常数最小化选项={
自动引用器:false,
安全:是的,
直接:错,
discardComments:{remove:(comment)=>!importantCommentRe.test(comment)}
};
返回[
后压缩({
url:(url)=>{
//仅将CSS加载程序不会处理的根相对URL转换为require()。
如果(!URL.startsWith('/')| | URL.startsWith('/')){
返回URL;
}
if(deployUrl.match(/:\/\/)){
//如果部署URL包含一个方案,请忽略baseHref按原样使用部署URL。
返回`${deployUrl.replace(/\/$/,'')}${URL}`;
}
else if(baseHref.match(/:\/\/)){
//如果baseHref包含一个方案,请按原样包含它。
返回baseHref.replace(/\/$/,'')+
`/${deployUrl}/${URL}`。替换(/\/\/+/+/g,'/');
}
否则{
//将基本href、部署url和原始url连接在一起。
//还可以将多个斜杠重复删除为单个斜杠。
返回`/${baseHref}/${deployUrl}/${URL}`。replace(/\/\/+/g,'/');
}
}
}),
autoprefixer(),
].concat(minimizeCss?[cssnano(minimizeOptions)]:[];
};
module.exports={
//为所有外部库添加外部库
“外部”:{
“@angular/core”:“ng.core”,
“@angular/common”:“ng.common”,
“@angular/compiler”:“ng.compiler”,
“@angular/platformBrowser”:“ng.platformBrowser”,
“@angular/platform browser dynamic”:“ng.PlatformBrowser dynamic”,
“@angular/http”:“ng.http”,
“@angular/forms”:“ng.forms”,
“rxjs/可观察”:“Rx”,
“rxjs/Subject”:“Rx”,
“rxjs/观察者”:“Rx”,
'rxjs/operator/last':'Rx.Observable.prototype',
“rxjs/operator/filter”:“Rx.Observable.prototype”,
“rxjs/operator/concatMap”:“Rx.Observable.prototype”,
“rxjs/operator/toPromise”:“Rx.Observable.prototype”,
},
“devtool”:“源映射”,
“决心”:{
“扩展”:[
“.ts”,
“.js”
],
“模块”:[
“/节点单元”,
“/节点\单元”
],
“符号链接”:正确
},
“resolveLoader”:{
“模块”:[
“/节点单元”,
“/节点\单元”
]
},
“条目”:{
“主要”:[
“/src/main.ts”
],
“polyfills”:[
“/src/polyfills.ts”
],
“风格”:[
“/src/styles.css”,
“/node_modules/prismjs/themes/prism-okaidia.css”
]
},
“产出”:{
“path”:path.join(process.cwd(),“../../../../../target/classes/static”),
“文件名”:“[name].bundle.js”,
“chunkFilename”:“[id].chunk.js”
},
“模块”:{
“规则”:[
{
“强制执行”:“预执行”,
“测试”:/\.js$/,
“加载器”:“源地图加载器”,
“排除”:[
/(\\\\\/)节点单元模块(\\\\\\/)/
]
},
{
“测试”:/\.html$/,
“加载器”:“原始加载器”
},
{
“测试”:/\(eot | svg | cur)$/,
“加载器”:“文件加载器?名称=[name]。[hash:20]。[ext]”
},
{
“测试”:/\(jpg | png | webp | gif | otf | ttf | woff | woff2 | ani)$/,
“加载器”:“url加载器?名称=[name]。[hash:20]。[ext]&限制=10000”
},
{
“排除”:[
join(process.cwd(),“src/styles.css”),
join(process.cwd(),“node_modules/prismjs/themes/prism-okaidia.css”)
],
“测试”:/\.css$/,
“使用”:[
“exports loader?module.exports.toString()”,
{
“加载器”:“css加载器”,
“选择”:{
“sourceMap”:false,
“导入加载程序”:1
}
},
{
“加载器”:“邮政编码加载器”,
“选择”:{
“标识”:“邮政编码”,
“插件”:postsssplugins
}
}
]
},
{
“排除”:[
拍打