| 1 | /**
|
|---|
| 2 | * Copyright 2018 Google Inc. All Rights Reserved.
|
|---|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 4 | * you may not use this file except in compliance with the License.
|
|---|
| 5 | * You may obtain a copy of the License at
|
|---|
| 6 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 7 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 8 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 10 | * See the License for the specific language governing permissions and
|
|---|
| 11 | * limitations under the License.
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | const rollup = require("rollup");
|
|---|
| 15 | const path = require("path");
|
|---|
| 16 | const omt = require(".");
|
|---|
| 17 | const fs = require("fs");
|
|---|
| 18 | const chalk = require("chalk");
|
|---|
| 19 |
|
|---|
| 20 | const karma = require("karma");
|
|---|
| 21 | const myKarmaConfig = require("./karma.conf.js");
|
|---|
| 22 |
|
|---|
| 23 | async function fileExists(file) {
|
|---|
| 24 | try {
|
|---|
| 25 | const stat = await fs.promises.stat(file);
|
|---|
| 26 | return stat.isFile();
|
|---|
| 27 | } catch (e) {
|
|---|
| 28 | return false;
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | async function init() {
|
|---|
| 33 | await Promise.all(
|
|---|
| 34 | [
|
|---|
| 35 | "./tests/fixtures/simple-bundle/entry.js",
|
|---|
| 36 | "./tests/fixtures/import-meta/entry.js",
|
|---|
| 37 | "./tests/fixtures/dynamic-import/entry.js",
|
|---|
| 38 | "./tests/fixtures/public-path/entry.js",
|
|---|
| 39 | "./tests/fixtures/worker/entry.js",
|
|---|
| 40 | "./tests/fixtures/module-worker/entry.js",
|
|---|
| 41 | "./tests/fixtures/more-workers/entry.js",
|
|---|
| 42 | "./tests/fixtures/amd-function-name/entry.js",
|
|---|
| 43 | "./tests/fixtures/single-default/entry.js",
|
|---|
| 44 | "./tests/fixtures/import-worker-url/entry.js",
|
|---|
| 45 | "./tests/fixtures/import-meta-worker/entry.js",
|
|---|
| 46 | "./tests/fixtures/import-worker-url-custom-scheme/entry.js",
|
|---|
| 47 | "./tests/fixtures/assets-in-worker/entry.js",
|
|---|
| 48 | "./tests/fixtures/url-import-meta-worker/entry.js"
|
|---|
| 49 | ].map(async input => {
|
|---|
| 50 | const pathName = path.dirname(input);
|
|---|
| 51 | const outputOptions = {
|
|---|
| 52 | dir: path.join(pathName, "build"),
|
|---|
| 53 | format: "amd"
|
|---|
| 54 | };
|
|---|
| 55 | let rollupConfig = {
|
|---|
| 56 | input,
|
|---|
| 57 | strictDeprecations: true,
|
|---|
| 58 | // Copied / adapted from default `onwarn` in Rollup CLI.
|
|---|
| 59 | onwarn: warning => {
|
|---|
| 60 | console.warn(`⚠️ ${chalk.bold(warning.message)}`);
|
|---|
| 61 |
|
|---|
| 62 | if (warning.url) {
|
|---|
| 63 | console.warn(chalk.cyan(warning.url));
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (warning.loc) {
|
|---|
| 67 | console.warn(
|
|---|
| 68 | `${warning.loc.file} (${warning.loc.line}:${warning.loc.column})`
|
|---|
| 69 | );
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if (warning.frame) {
|
|---|
| 73 | console.warn(chalk.dim(warning.frame));
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | console.warn("");
|
|---|
| 77 | }
|
|---|
| 78 | };
|
|---|
| 79 | const rollupConfigPath = "./" + path.join(pathName, "rollup.config.js");
|
|---|
| 80 | const configPath = "./" + path.join(pathName, "config.json");
|
|---|
| 81 | if (await fileExists(rollupConfigPath)) {
|
|---|
| 82 | require(rollupConfigPath)(rollupConfig, outputOptions, omt);
|
|---|
| 83 | } else if (await fileExists(configPath)) {
|
|---|
| 84 | rollupConfig.plugins = [omt(require(configPath))];
|
|---|
| 85 | } else {
|
|---|
| 86 | rollupConfig.plugins = [omt()];
|
|---|
| 87 | }
|
|---|
| 88 | const bundle = await rollup.rollup(rollupConfig);
|
|---|
| 89 | await bundle.write(outputOptions);
|
|---|
| 90 | })
|
|---|
| 91 | );
|
|---|
| 92 |
|
|---|
| 93 | const karmaConfig = { port: 9876 };
|
|---|
| 94 | myKarmaConfig({
|
|---|
| 95 | set(config) {
|
|---|
| 96 | Object.assign(karmaConfig, config);
|
|---|
| 97 | }
|
|---|
| 98 | });
|
|---|
| 99 | const server = new karma.Server(karmaConfig, code => {
|
|---|
| 100 | console.log(`Karma exited with code ${code}`);
|
|---|
| 101 | process.exit(code);
|
|---|
| 102 | });
|
|---|
| 103 | server.start();
|
|---|
| 104 | }
|
|---|
| 105 | init();
|
|---|