/* eslint-disable max-statements */
function generateConfig(options) {
  const composer = new WebpackConfigComposer();
  composer.addProfiles(options.profiles);
  composer.addPartials(partialConfigs.partials);

  let customConfig;
  const customDirs = [process.cwd(), Path.resolve("archetype/config/webpack")];

  const foundDir = customDirs.find(d => {
    customConfig = optionalRequire(Path.join(d, options.configFilename));
    return !!customConfig;
  });
  if (foundDir) {
    const dir = xsh.pathCwd.replace(foundDir);
    logger.info(`Custom webpack config ${options.configFilename} loaded from ${dir}`);
  } else {
    const dirs = customDirs.map(d => xsh.pathCwd.replace(d)).join("; ");
    logger.info(`No custom webpack config ${options.configFilename} found in dirs ${dirs}`);
  }

  const keepCustomProps = options.keepCustomProps;
  const compose = () => composer.compose({ keepCustomProps }, options.profileNames);

  let config;

  if (customConfig) {
    if (_.isFunction(customConfig)) {
      config = customConfig(composer, options, compose);
    } else {
      config = _.merge(compose(), customConfig);
    }
  } else {
    config = compose();
  }

  logger.verbose("Final Webpack config", JSON.stringify(config, null, 2));

  return config;
}
module.exports = function(settings) {
  const browser = archetype.karma.browser.toLowerCase();
  if (browser === "chrome") {
    settings.browsers = ["ChromeHeadless"];

    logger.info("Using Chrome Headless to run Karma test");
  } else if (browser === "phantomjs") {
    settings.frameworks.push("phantomjs-shim");
    settings.browsers = ["PhantomJS"];

    logger.warn(
      "Using PhantomJS to run Karma test. It's been deprecated and may be removed in the future."
    );
  } else {
    logger.error(`Unknown browser ${browser} set for Karma test. Failed.`);
    return process.exit(1);
  }

  return settings;
};
Beispiel #3
0
module.exports = function(options) {
  /* eslint max-statements: 0 */
  const swConfig = optionalRequire(swConfigPath, true) || {};
  const severConfig = optionalRequire(serverConfigPath, true) || {};

  if (!swConfig.manifest) {
    return {};
  }

  logger.info(`PWA enabled with config from ${swConfigPath}`);

  mkdirp.sync(Path.resolve("dist"));

  const manifestConfig = assign(
    {
      background: "#FFFFFF",
      logo: "images/electrode.png",
      title: "Electrode",
      short_name: "Electrode",
      statsFilename: "../server/iconstats.json"
    },
    swConfig.manifest
  );

  const cacheConfig = assign(
    {
      staticFileGlobs: ["dist/js/*.{js,css}"],
      stripPrefix: "dist/js/",
      cacheId: "electrode",
      filepath: "dist/sw.js",
      maximumFileSizeToCacheInBytes: 4194304,
      skipWaiting: false,
      noWarning: true
    },
    swConfig.cache
  );

  if (cacheConfig.runtimeCaching) {
    cacheConfig.runtimeCaching = cacheConfig.runtimeCaching.map(runtimeCache => {
      return {
        handler: runtimeCache.handler,
        urlPattern: new RegExp(runtimeCache.urlPattern)
      };
    });
  }

  /**
   * If importScripts exists in the cache config we need to overwrite
   * the entry config and output config so we get an entry point for each
   * script with unique names.
    */
  let entry = options.currentConfig.entry;
  let output = {};
  if (cacheConfig.importScripts) {
    const importScripts = cacheConfig.importScripts;

    cacheConfig.importScripts = process.env.WEBPACK_DEV === "true"
      ? importScripts.map(getDevelopmentPath)
      : importScripts.map(getHashedPath);

    entry = createEntryConfigFromScripts(importScripts, options.currentConfig.entry);

    output = {
      filename: "[name].[hash].js"
    };
  }

  const logoPath = Path.resolve(AppMode.src.client, manifestConfig.logo);
  const plugins = [
    new FaviconsWebpackPlugin({
      logo: logoPath,
      emitStats: true,
      inject: false,
      background: manifestConfig.background,
      title: manifestConfig.title,
      statsFilename: manifestConfig.statsFilename,
      icons: {
        android: true,
        appleIcon: true,
        appleStartup: true,
        favicons: true
      }
    }),
    new AddManifestFieldsPlugin({
      gcm_sender_id: manifestConfig.gcm_sender_id,
      short_name: manifestConfig.short_name,
      theme_color: manifestConfig.theme_color
    }),
    new SWPrecacheWebpackPlugin(cacheConfig)
  ];

  /**
   * In dev we need to write the stats file to disk
   * so we can properly read which chunk(s) need to be
   * served. We write the stats file to our build artifacts
   * folder, which is .etmp by default.
   */
  if (process.env.WEBPACK_DEV === "true") {
    plugins.push(
      new DiskPlugin({
        output: {
          path: Path.resolve(severConfig.buildArtifactsPath || ".etmp")
        },
        files: [
          {
            asset: /\/stats.json$/,
            output: {
              filename: "stats.json"
            }
          }
        ]
      })
    );
  }

  return {
    entry,
    output,
    module: {
      rules: [
        {
          _name: "manifest",
          test: /manifest.json$/,
          use: [
            {
              loader: fileLoader,
              options: {
                name: "manifest.json"
              }
            },
            webAppManifestLoader
          ]
        }
      ]
    },
    plugins
  };
};