Angular2 AOT编译失败;找不到资源文件

时间:2017-03-15 09:09:01

标签: angular build webpack aot

要使用AOT构建我的angular2项目,我首先运行此命令。

perl

我收到一些错误,但我无法解决,

$ ./node_modules/.bin/ngc -p ./src/

我认为发生了因为Error: Compilation failed. Resource file not found: /Users/home_user/Devs/ds_dev_team/dmp-dashboard/src/public/main-web/app/styles/form at ModuleResolutionHostAdapter.readResource (/Users/home_user/Devs/ds_dev_team/dmp-dashboard/node_modules/@angular/compiler-cli/src/compiler_host.js:291:19) at CompilerHost.loadResource (/Users/home_user/Devs/ds_dev_team/dmp-dashboard/node_modules/@angular/compiler-cli/src/compiler_host.js:230:85) at Object.DirectiveNormalizer.get (/Users/home_user/Devs/ds_dev_team/dmp-dashboard/node_modules/@angular/compiler/bundles/compiler.umd.js:26374:111) at DirectiveNormalizer._fetch (/Users/home_user/Devs/ds_dev_team/dmp-dashboard/node_modules/@angular/compiler/bundles/compiler.umd.js:13753:47) at /Users/home_user/Devs/ds_dev_team/dmp-dashboard/node_modules/@angular/compiler/bundles/compiler.umd.js:13870:57 at Array.map (native) at DirectiveNormalizer._loadMissingExternalStylesheets (/Users/home_user/Devs/ds_dev_team/dmp-dashboard/node_modules/@angular/compiler/bundles/compiler.umd.js:13870:18) at /Users/1002719/Devs/ds_dev_team/dmp-dashboard/node_modules/@angular/compiler/bundles/compiler.umd.js:13873:30 at process._tickCallback (internal/process/next_tick.js:103:7) at Module.runMain (module.js:592:11) 缺失,所以我处理了它(删除或合并到其他文件等),但它可以解决。

下面是webpack配置。 这些都来自https://github.com/AngularClass/angular2-webpack-starter

webpack.common.js

form.scss

webpack.prod.js

const webpack = require('webpack');
const helpers = require('./helpers');
const path = require('path');

// problem with copy-webpack-plugin
const AssetsPlugin = require('assets-webpack-plugin');
const NormalModuleReplacementPlugin = require('webpack/lib/NormalModuleReplacementPlugin');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const HtmlElementsPlugin = require('./html-elements-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const ngcWebpack = require('ngc-webpack');

// const ENV = process.env.NODE_ENV;
// if (ENV !== 'local' && ENV !== 'local-int' && ENV !== 'alpha' && ENV !== 'prod') {
//   throw new Error('NODE_ENV must be specified (local or local-int or alpha or prod)');
// }
const HMR = helpers.hasProcessFlag('hot');
const AOT = helpers.hasNpmFlag('aot');
const METADATA = {
  title: 'Dashboard',
  baseUrl: '/',
  isDevServer: helpers.isWebpackDevServer()
};

module.exports = function(options) {
  isProd = options.env === 'prod';
  return {
    entry: {
      'polyfills': './src/public/main-web/polyfills.browser.ts',
      'main': AOT ? './src/public/main-web/main.browser.aot.ts' : './src/public/main-web/main.browser.ts'
    },
    resolve: {
      extensions: ['.ts', '.js', '.json', '.css', '.scss', '.html'],
      alias: {
        '@angular/material': '@angular/material/bundles/material.umd.js',
      },
      modules: [helpers.root('./src/public/main-web'), helpers.root('node_modules')]
    },
    module: {
      rules: [
        {
          test: /\.ts$/,
          use: [
            {
              loader: '@angularclass/hmr-loader',
              options: {
                pretty: !isProd,
                prod: isProd
              }
            },
            {
              loader: 'ng-router-loader',
              options: {
                loader: 'async-import',
                genDir: 'compiled',
                aot: AOT
              }
            },
            {
              loader: 'awesome-typescript-loader',
              options: {
                configFileName: 'tsconfig.webpack.json'
              }
            },
            {
              loader: 'angular2-template-loader'
            }
          ],
          exclude: [/\.(spec|e2e)\.ts$/]
        },
        {
          test: /\.json$/,
          use: 'json-loader'
        },
        {
          test: /\.css$/,
          use: ['to-string-loader', 'css-loader'],
          exclude: [helpers.root('src', 'styles')]
        },
        {
          test: /\.scss$/,
          use: ['to-string-loader', 'css-loader', 'sass-loader'],
          exclude: [helpers.root('src', 'styles')]
        },
        {
          test: /\.html$/,
          use: 'raw-loader',
          exclude: [helpers.root('src/public/views/index.html')]
        },
        {
          test: /\.(jpg|png|gif)$/,
          use: 'file-loader'
        },
        { 
          test: /\.(eot|woff2?|svg|ttf)([\?]?.*)$/,
          use: 'file-loader'
        }
      ]
    },

    plugins: [
      new AssetsPlugin({
        path: helpers.root('src/dist'),
        filename: 'webpack-assets.json',
        prettyPrint: true
      }),
      new CheckerPlugin(),
      new CommonsChunkPlugin({
        name: 'polyfills',
        chunks: ['polyfills']
      }),
      new CommonsChunkPlugin({
        name: 'vendor',
        chunks: ['main'],
        minChunks: module => /node_modules/.test(module.resource)
      }),
      new CommonsChunkPlugin({
        name: ['polyfills', 'vendor'].reverse()
      }),
      new ContextReplacementPlugin(
        /angular(\\|\/)core(\\|\/)src(\\|\/)linker/,
        helpers.root('./src/public/main-web'),
        {
          // your Angular Async Route paths relative to this root directory
        }
      ),
      new CopyWebpackPlugin([
        {from: 'src/public/imgs', to: 'imgs'},
        {from: 'src/public/external-css', to: 'external-css'},
        {from: 'src/public/main-web/app/fonts', to: 'fonts'},
        {from: 'src/public/main-web/app/shared/error-page/error-page.html', to: 'errors/error.html'}
      ]),
      new HtmlWebpackPlugin({
        template: 'src/public/views/index.html',
        title: METADATA.title,
        chunksSortMode: 'dependency',
        metadata: METADATA,
        inject: 'head'
      }),
      new ScriptExtHtmlWebpackPlugin({
        defaultAttribute: 'defer'
      }),
      new LoaderOptionsPlugin({}),
      new NormalModuleReplacementPlugin(
        /facade(\\|\/)async/,
        helpers.root('node_modules/@angular/core/src/facade/async.js')
      ),
      new NormalModuleReplacementPlugin(
        /facade(\\|\/)collection/,
        helpers.root('node_modules/@angular/core/src/facade/collection.js')
      ),
      new NormalModuleReplacementPlugin(
        /facade(\\|\/)errors/,
        helpers.root('node_modules/@angular/core/src/facade/errors.js')
      ),
      new NormalModuleReplacementPlugin(
        /facade(\\|\/)lang/,
        helpers.root('node_modules/@angular/core/src/facade/lang.js')
      ),
      new NormalModuleReplacementPlugin(
        /facade(\\|\/)math/,
        helpers.root('node_modules/@angular/core/src/facade/math.js')
      ),
      new ngcWebpack.NgcWebpackPlugin({
        disabled: !AOT,
        tsConfig: helpers.root('tsconfig.webpack.json'),
        resourceOverride: helpers.root('client-config/resource-override.js')
      })
    ],
    node: {
      global: true,
      crypto: 'empty',
      process: true,
      module: false,
      clearImmediate: false,
      setImmediate: false
    }
  }
};

0 个答案:

没有答案