1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | var webpack_1 = __importDefault(require("webpack"));
|
7 | var util_1 = __importDefault(require("util"));
|
8 |
|
9 |
|
10 | var webpackPreprocessor = require('@cypress/webpack-preprocessor');
|
11 | var debug = require('debug')('cypress-vue-unit-test');
|
12 | var VueLoaderPlugin = require('vue-loader/lib/plugin');
|
13 |
|
14 | var fw = require('find-webpack');
|
15 |
|
16 | function preventChunking(options) {
|
17 | if (options === void 0) { options = {}; }
|
18 | if (options && options.optimization && options.optimization.splitChunks) {
|
19 | delete options.optimization.splitChunks;
|
20 | }
|
21 | options.plugins = options.plugins || [];
|
22 | options.plugins.push(new webpack_1.default.optimize.LimitChunkCountPlugin({
|
23 | maxChunks: 1,
|
24 | }));
|
25 | return options;
|
26 | }
|
27 |
|
28 | function inlineUrlLoadedAssets(options) {
|
29 | if (options === void 0) { options = {}; }
|
30 | var isUrlLoader = function (use) {
|
31 | return use && use.loader && use.loader.indexOf('url-loader') > -1;
|
32 | };
|
33 | var mergeUrlLoaderOptions = function (use) {
|
34 | if (isUrlLoader(use)) {
|
35 | use.options = use.options || {};
|
36 | use.options.limit = Number.MAX_SAFE_INTEGER;
|
37 | }
|
38 | return use;
|
39 | };
|
40 | if (options.module && options.module.rules) {
|
41 | options.module.rules = options.module.rules.map(function (rule) {
|
42 | if (Array.isArray(rule.use)) {
|
43 | rule.use = rule.use.map(mergeUrlLoaderOptions);
|
44 | }
|
45 | return rule;
|
46 | });
|
47 | }
|
48 | return options;
|
49 | }
|
50 | function compileTemplate(options) {
|
51 | if (options === void 0) { options = {}; }
|
52 | options.resolve = options.resolve || {};
|
53 | options.resolve.alias = options.resolve.alias || {};
|
54 | options.resolve.alias['vue$'] = 'vue/dist/vue.esm.js';
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function removeForkTsCheckerWebpackPlugin(options) {
|
61 | if (!Array.isArray(options.plugins)) {
|
62 | return;
|
63 | }
|
64 | options.plugins = options.plugins.filter(function (plugin) {
|
65 | return plugin.typescript === undefined;
|
66 | });
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function insertBabelLoader(config, options) {
|
74 | var skipCodeCoverage = config && config.env && config.env.coverage === false;
|
75 | if (!options.devtool) {
|
76 | options.devtool = '#eval-source-map';
|
77 | }
|
78 | var babelRule = {
|
79 | test: /\.js$/,
|
80 | loader: 'babel-loader',
|
81 | exclude: /node_modules/,
|
82 | options: {
|
83 | plugins: [
|
84 |
|
85 | [
|
86 | '@babel/plugin-transform-modules-commonjs',
|
87 | {
|
88 | loose: true,
|
89 | },
|
90 | ],
|
91 | ],
|
92 | },
|
93 | };
|
94 | if (skipCodeCoverage) {
|
95 | debug('not adding code instrument plugin');
|
96 | }
|
97 | else {
|
98 | debug('adding code coverage plugin');
|
99 |
|
100 |
|
101 | var instrumentPlugin = [
|
102 | 'babel-plugin-istanbul',
|
103 | {
|
104 |
|
105 |
|
106 | extension: ['.js', '.vue'],
|
107 | },
|
108 | ];
|
109 | babelRule.options.plugins.push(instrumentPlugin);
|
110 | }
|
111 | options.module.rules.push(babelRule);
|
112 | options.plugins = options.plugins || [];
|
113 | var pluginFound = options.plugins.find(function (plugin) {
|
114 | return (plugin.constructor && plugin.constructor.name === VueLoaderPlugin.name);
|
115 | });
|
116 | if (!pluginFound) {
|
117 | debug('inserting VueLoaderPlugin');
|
118 | options.plugins.push(new VueLoaderPlugin());
|
119 | }
|
120 | else {
|
121 | debug('found plugin VueLoaderPlugin already');
|
122 | }
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | var onFileDefaultPreprocessor = function (config) {
|
128 | var webpackOptions = fw.getWebpackOptions();
|
129 | if (!webpackOptions) {
|
130 | debug('Could not find webpack options, starting with default');
|
131 | webpackOptions = {};
|
132 | }
|
133 | webpackOptions.mode = 'development';
|
134 | inlineUrlLoadedAssets(webpackOptions);
|
135 | preventChunking(webpackOptions);
|
136 | compileTemplate(webpackOptions);
|
137 | insertBabelLoader(config, webpackOptions);
|
138 |
|
139 |
|
140 |
|
141 | if (debug.enabled) {
|
142 | console.error('final webpack');
|
143 | console.error(util_1.default.inspect(webpackOptions, false, 2, true));
|
144 | }
|
145 | return webpackPreprocessor({
|
146 | webpackOptions: webpackOptions,
|
147 | });
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | var onFilePreprocessor = function (webpackOptions) {
|
161 | if (typeof webpackOptions === 'string') {
|
162 |
|
163 | webpackOptions = require(webpackOptions);
|
164 | }
|
165 | return webpackPreprocessor({
|
166 | webpackOptions: webpackOptions,
|
167 | });
|
168 | };
|
169 | module.exports = { onFilePreprocessor: onFilePreprocessor, onFileDefaultPreprocessor: onFileDefaultPreprocessor };
|