當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript PolymerProject.sources方法代碼示例

本文整理匯總了TypeScript中polymer-build.PolymerProject.sources方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript PolymerProject.sources方法的具體用法?TypeScript PolymerProject.sources怎麽用?TypeScript PolymerProject.sources使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在polymer-build.PolymerProject的用法示例。


在下文中一共展示了PolymerProject.sources方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: build

export async function build(
    options: ProjectBuildOptions,
    polymerProject: PolymerProject): Promise<void> {
  const buildName = options.name || 'default';
  const optimizeOptions:
      OptimizeOptions = {css: options.css, js: options.js, html: options.html};

  // If no name is provided, write directly to the build/ directory.
  // If a build name is provided, write to that subdirectory.
  const buildDirectory = path.join(mainBuildDirectoryName, buildName);
  logger.debug(`"${buildDirectory}": Building with options:`, options);

  // Fork the two streams to guarentee we are working with clean copies of each
  // file and not sharing object references with other builds.
  const sourcesStream = forkStream(polymerProject.sources());
  const depsStream = forkStream(polymerProject.dependencies());
  const htmlSplitter = new HtmlSplitter();

  let buildStream: NodeJS.ReadableStream = pipeStreams([
    mergeStream(sourcesStream, depsStream),
    htmlSplitter.split(),
    getOptimizeStreams(optimizeOptions),
    htmlSplitter.rejoin()
  ]);

  const compiledToES5 = !!(optimizeOptions.js && optimizeOptions.js.compile);
  if (compiledToES5) {
    buildStream = buildStream.pipe(polymerProject.addBabelHelpersInEntrypoint())
                      .pipe(polymerProject.addCustomElementsEs5Adapter());
  }

  const bundled = !!(options.bundle);

  async function getPolymerVersion(): Promise<string> {
    return new Promise<string>(
        (resolve, _reject) =>
            bower.commands.list({}, {offline: true})
                .on('end',
                    (result: any) => {
                      if (result && result.dependencies &&
                          result.dependencies.polymer &&
                          result.dependencies.polymer.pkgMeta &&
                          result.dependencies.polymer.pkgMeta.version) {
                        resolve(result.dependencies.polymer.pkgMeta.version);
                      } else {
                        resolve('');
                      }
                    })
                .on('error', (oops: Error) => {
                  resolve('');
                  console.warn(oops.message);
                }));
  }

  if (bundled) {
    // Polymer 1.x and Polymer 2.x deal with relative urls in dom-module
    // templates differently.  Polymer CLI will attempt to provide a sensible
    // default value for the `rewriteUrlsInTemplates` option passed to
    // `polymer-bundler` based on the version of Polymer found in the project's
    // folders.  We will default to Polymer 1.x behavior unless 2.x is found.
    const polymerVersion = await getPolymerVersion();
    const bundlerOptions = {
      rewriteUrlsInTemplates: !polymerVersion.startsWith('2.')
    };
    if (typeof options.bundle === 'object') {
      Object.assign(bundlerOptions, options.bundle);
    }
    buildStream = buildStream.pipe(polymerProject.bundler(bundlerOptions));
  }

  if (options.insertPrefetchLinks) {
    buildStream = buildStream.pipe(polymerProject.addPrefetchLinks());
  }

  buildStream.once('data', () => {
    logger.info(`(${buildName}) Building...`);
  });

  if (options.basePath) {
    let basePath = options.basePath === true ? buildName : options.basePath;
    if (!basePath.startsWith('/')) {
      basePath = '/' + basePath;
    }
    if (!basePath.endsWith('/')) {
      basePath = basePath + '/';
    }
    buildStream = buildStream.pipe(polymerProject.updateBaseTag(basePath));
  }

  if (options.addPushManifest) {
    buildStream = buildStream.pipe(polymerProject.addPushManifest());
  }

  // Finish the build stream by piping it into the final build directory.
  buildStream = buildStream.pipe(dest(buildDirectory));

  // If a service worker was requested, parse the service worker config file
  // while the build is in progress. Loading the config file during the build
  // saves the user ~300ms vs. loading it afterwards.
  const swPrecacheConfigPath = path.resolve(
//.........這裏部分代碼省略.........
開發者ID:asdfg9822,項目名稱:polymer-cli,代碼行數:101,代碼來源:build.ts

示例2: PolymerProject

  return new Promise<any>((buildResolve, _) => {
    let polymerProject = new PolymerProject({
      root: config.root,
      shell: config.shell,
      entrypoint: config.entrypoint,
      fragments: config.fragments,
      sourceGlobs: options.sources,
      includeDependencies: options.includeDependencies,
    });

    if (options.insertDependencyLinks) {
      logger.debug(`Additional dependency links will be inserted into application`);
    }

    // mix in optimization options from build command
    // TODO: let this be set by the user
    let optimizeOptions = {
      html: Object.assign({removeComments: true}, options.html),
      css: Object.assign({stripWhitespace: true}, options.css),
      js: Object.assign({minify: true}, options.js),
    };

    logger.info(`Building application...`);

    logger.debug(`Reading source files...`);
    let sourcesStream = polymerProject.sources()
      .pipe(polymerProject.splitHtml())
      .pipe(gulpif(/\.js$/, uglify(optimizeOptions.js)))
      .pipe(gulpif(/\.css$/, cssSlam(optimizeOptions.css)))
      .pipe(gulpif(/\.html$/, htmlmin(optimizeOptions.html)))
      .pipe(polymerProject.rejoinHtml());

    logger.debug(`Reading dependencies...`);
    let depsStream = polymerProject.dependencies()
      .pipe(polymerProject.splitHtml())
      .pipe(polymerProject.rejoinHtml());

    let buildStream = mergeStream(sourcesStream, depsStream)
      .once('data', () => { logger.debug('Analyzing build dependencies...'); })
      .pipe(polymerProject.analyzer);

    let unbundledPhase = forkStream(buildStream)
      .once('data', () => { logger.info('Generating build/unbundled...'); })
      .pipe(
        gulpif(
          options.insertDependencyLinks,
          new PrefetchTransform(polymerProject.root, polymerProject.entrypoint,
            polymerProject.shell, polymerProject.fragments,
            polymerProject.analyzer)
        )
      )
      .pipe(gulp.dest('build/unbundled'));

    let bundledPhase = forkStream(buildStream)
      .once('data', () => { logger.info('Generating build/bundled...'); })
      .pipe(polymerProject.bundler)
      .pipe(gulp.dest('build/bundled'));

    let swPrecacheConfig = path.resolve(polymerProject.root, options.swPrecacheConfig || 'sw-precache-config.js');
    let loadSWConfig = parsePreCacheConfig(swPrecacheConfig);

    loadSWConfig.then((swConfig) => {
      if (swConfig) {
        logger.debug(`Service worker config found`, swConfig);
      } else {
        logger.debug(`No service worker configuration found at ${swPrecacheConfig}, continuing with defaults`);
      }
    });

    // Once the unbundled build stream is complete, create a service worker for the build
    let unbundledPostProcessing = Promise.all([loadSWConfig, waitFor(unbundledPhase)]).then((results) => {
      let swConfig: SWConfig = results[0];
      return addServiceWorker({
        buildRoot: 'build/unbundled',
        project: polymerProject,
        swConfig: swConfig,
      });
    });

    // Once the bundled build stream is complete, create a service worker for the build
    let bundledPostProcessing = Promise.all([loadSWConfig, waitFor(bundledPhase)]).then((results) => {
      let swConfig: SWConfig = results[0];
      return addServiceWorker({
        buildRoot: 'build/bundled',
        project: polymerProject,
        swConfig: swConfig,
        bundled: true,
      });
    });

    return Promise.all([unbundledPostProcessing, bundledPostProcessing]).then(() => {
      logger.info('Build complete!');
      buildResolve();
    });

  });
開發者ID:mbn18,項目名稱:polymer-cli,代碼行數:96,代碼來源:build.ts

示例3: PolymerProject

  return new Promise<any>((buildResolve, _) => {
    let polymerProject = new PolymerProject({
      root: config.root,
      shell: config.shell,
      entrypoint: config.entrypoint,
      fragments: config.fragments,
      sourceGlobs: options.sources,
      includeDependencies: options.includeDependencies,
    });

    if (options.insertDependencyLinks) {
      logger.debug(`Additional dependency links will be inserted into application`);
    }

    // mix in optimization options from build command
    // TODO: let this be set by the user
    let optimizeOptions = {
      html: Object.assign({removeComments: true}, options.html),
      css: Object.assign({stripWhitespace: true}, options.css),
      js: Object.assign({minify: true}, options.js),
    };

    logger.info(`Building application...`);

    logger.debug(`Reading source files...`);
    let sourcesStream = polymerProject.sources()
      .pipe(polymerProject.splitHtml())
      .pipe(gulpif(/\.js$/, uglify(optimizeOptions.js)))
      .pipe(gulpif(/\.css$/, cssSlam(optimizeOptions.css)))
      .pipe(gulpif(/\.html$/, htmlmin(optimizeOptions.html)))
      .pipe(polymerProject.rejoinHtml());

    logger.debug(`Reading dependencies...`);
    let depsStream = polymerProject.dependencies()
      .pipe(polymerProject.splitHtml())
      .pipe(polymerProject.rejoinHtml());

    let buildStream = mergeStream(sourcesStream, depsStream)
      .once('data', () => { logger.debug('Analyzing build dependencies...'); })
      .pipe(polymerProject.analyze);

    let serviceWorkerName = 'service-worker.js';

    let unbundledPhase = forkStream(buildStream)
      .once('data', () => { logger.info('Generating build/unbundled...'); })
      .pipe(
        gulpif(
          options.insertDependencyLinks,
          new PrefetchTransform(polymerProject.root, polymerProject.entrypoint,
            polymerProject.shell, polymerProject.fragments,
            polymerProject.analyze)
        )
      )
      .pipe(gulp.dest('build/unbundled'));

    let bundledPhase = forkStream(buildStream)
      .once('data', () => { logger.info('Generating build/bundled...'); })
      .pipe(polymerProject.bundle)
      .pipe(gulp.dest('build/bundled'));


    let genSW = (buildRoot: string, deps: string[], swConfig: SWConfig, scriptAndStyleDeps?: string[]) => {
      logger.debug(`Generating service worker for ${buildRoot}...`);
      logger.debug(`Script and style deps: ${scriptAndStyleDeps}`);
      return generateServiceWorker({
        root: polymerProject.root,
        entrypoint: polymerProject.entrypoint,
        deps,
        scriptAndStyleDeps,
        buildRoot,
        swConfig: clone(swConfig),
        serviceWorkerPath: path.join(polymerProject.root, buildRoot, serviceWorkerName),
      });
    };

    let swPrecacheConfig = path.resolve(polymerProject.root, options.swPrecacheConfig || 'sw-precache-config.js');
    return Promise.all([
      parsePreCacheConfig(swPrecacheConfig),
      polymerProject.analyze.analyzeDependencies,
      waitForAll([unbundledPhase, bundledPhase])
    ]).then((results) => {
      let swConfig: SWConfig = results[0];
      let depsIndex: DepsIndex = results[1];

      if (swConfig) {
        logger.debug(`Service worker config found`, swConfig);
      } else {
        logger.debug(`No service worker configuration found at ${swPrecacheConfig}, continuing with defaults`);
      }

      let unbundledDeps = polymerProject.analyze.allFragments
          .concat(Array.from(depsIndex.depsToFragments.keys()));
      let bundledDeps = polymerProject.analyze.allFragments
          .concat(polymerProject.bundle.sharedBundleUrl);
      let fullDeps = Array.from(depsIndex.fragmentToFullDeps.values());
      let scriptAndStyleDeps = new Set<string>();
      fullDeps.forEach(d => {
        d.scripts.forEach((s) => scriptAndStyleDeps.add(s));
        d.styles.forEach((s) => scriptAndStyleDeps.add(s));
      });
//.........這裏部分代碼省略.........
開發者ID:catiejojo,項目名稱:polymer-cli,代碼行數:101,代碼來源:build.ts


注:本文中的polymer-build.PolymerProject.sources方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。