1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | var path_1 = require("path");
|
11 | var webpack = require("webpack");
|
12 | var AssetsPlugin = require("assets-webpack-plugin");
|
13 | var WebpackMd5Hash = require("webpack-md5-hash");
|
14 | var utilities_1 = require("./utilities");
|
15 | function deepMergeObject(objectOne, objectTwo) {
|
16 | if (objectOne === void 0) { objectOne = {}; }
|
17 | if (objectTwo === void 0) { objectTwo = {}; }
|
18 | var newObject = objectOne;
|
19 | Object.keys(objectTwo).forEach(function (key) {
|
20 | if (typeof objectTwo[key] === 'function') {
|
21 | var mergeFunction = objectTwo[key];
|
22 | newObject[key] = mergeFunction(objectOne[key]);
|
23 | }
|
24 | else if (typeof objectTwo[key] !== 'object') {
|
25 | newObject[key] = objectOne[key] || objectTwo[key];
|
26 | }
|
27 | else if (Array.isArray(objectTwo[key])) {
|
28 | newObject[key] = (objectOne[key] || []).concat(objectTwo[key]);
|
29 | }
|
30 | else {
|
31 | newObject[key] = deepMergeObject(objectOne[key], objectTwo[key]);
|
32 | }
|
33 | });
|
34 | return newObject;
|
35 | }
|
36 | function smartMergeUserConfig(object, merger) {
|
37 | var finalConfig = __assign({}, object);
|
38 | return deepMergeObject(object, merger);
|
39 | }
|
40 | function removeEmptyPlugins(plugins) {
|
41 | var filteredPlugins = [];
|
42 | for (var _i = 0, plugins_1 = plugins; _i < plugins_1.length; _i++) {
|
43 | var plugin = plugins_1[_i];
|
44 | if (plugin) {
|
45 | filteredPlugins.push(plugin);
|
46 | }
|
47 | }
|
48 | return filteredPlugins;
|
49 | }
|
50 | module.exports = function createShopifyWebpackConfigFactory(options) {
|
51 | if (options === void 0) { options = {}; }
|
52 | var env = new utilities_1.Env(options);
|
53 | var paths = new utilities_1.Paths();
|
54 | var project = new utilities_1.Project(paths);
|
55 | var cdn = options.cdn, appRoot = options.appRoot, assetDevServerPort = options.assetDevServerPort, assetDevServerHost = options.assetDevServerHost, assetDevServerPath = options.assetDevServerPath, outputDirectory = options.outputDirectory;
|
56 | var uiRoot = appRoot || path_1.resolve(paths.app.root, project.ifRails('./app/ui', './app'));
|
57 | return function createShopifyWebpackConfig(additionalConfig) {
|
58 | if (additionalConfig === void 0) { additionalConfig = {}; }
|
59 | var config = additionalConfig;
|
60 | return smartMergeUserConfig(config, {
|
61 | target: env.ifServer('node', 'web'),
|
62 |
|
63 |
|
64 |
|
65 | node: {
|
66 | __dirname: true,
|
67 | __filename: true,
|
68 | },
|
69 | devtool: env.ifServer('source-map', env.ifDevelopment('source-map', 'hidden-source-map')),
|
70 | entry: function (currentEntry) {
|
71 | return currentEntry || uiRoot;
|
72 | },
|
73 | output: {
|
74 | filename: env.ifProduction('[name]-[chunkhash].js', '[name].js'),
|
75 | path: outputDirectory || project.ifRails(path_1.join(paths.app.root, 'public/bundles'), path_1.join(paths.app.root, 'build')),
|
76 | publicPath: env.ifProduction(cdn, project.ifRails('/webpack/assets/', "http://" + (assetDevServerHost || 'localhost') + ":" + assetDevServerPort + "/" + (assetDevServerPath || ''))) || '/assets/',
|
77 | chunkFilename: '[name]-[chunkhash].js',
|
78 | libraryTarget: env.ifServer('commonjs2', 'var'),
|
79 | },
|
80 | resolve: {
|
81 | modules: function (currentModules) {
|
82 | var modules = new Set([uiRoot].concat((currentModules || ['node_modules'])));
|
83 | return Array.from(modules);
|
84 | },
|
85 | extensions: function (currentExtensions) {
|
86 | if (currentExtensions === void 0) { currentExtensions = []; }
|
87 | var extensions = new Set(currentExtensions);
|
88 | extensions.add('.js');
|
89 | extensions.add('.json');
|
90 | if (project.isTypeScript) {
|
91 | extensions.add('.ts');
|
92 | extensions.add('.tsx');
|
93 | }
|
94 | return Array.from(extensions);
|
95 | },
|
96 | },
|
97 | resolveLoader: {
|
98 | modules: function (currentModules) {
|
99 | var modules = new Set([paths.own.nodeModules].concat((currentModules || ['node_modules'])));
|
100 | return Array.from(modules);
|
101 | },
|
102 | },
|
103 | module: {
|
104 | rules: function (currentRules) {
|
105 | var rules = currentRules || [];
|
106 | var jsRuleMatcher = utilities_1.createPartialObjectMatcher({ test: /\.js$/ });
|
107 | if (!rules.some(jsRuleMatcher)) {
|
108 | rules.push({
|
109 | test: /\.js$/,
|
110 | exclude: /node_modules/,
|
111 | loader: 'babel-loader',
|
112 | options: {
|
113 | babelrc: false,
|
114 | presets: [
|
115 | [require.resolve('babel-preset-shopify/web'), { modules: false }],
|
116 | require.resolve('babel-preset-shopify/react'),
|
117 | ],
|
118 | },
|
119 | });
|
120 | }
|
121 | if (project.isTypeScript) {
|
122 | var tsRuleMatcher = utilities_1.createPartialObjectMatcher({ test: /\.tsx?$/ });
|
123 | if (!rules.some(tsRuleMatcher)) {
|
124 | rules.push({
|
125 | test: /\.tsx?$/,
|
126 | loader: 'awesome-typescript-loader',
|
127 | exclude: /node_modules/,
|
128 | options: {
|
129 | silent: true,
|
130 | useBabel: true,
|
131 | useCache: true,
|
132 | compiler: path_1.resolve(paths.app.nodeModules, 'typescript'),
|
133 | cacheDirectory: path_1.resolve(project.ifRails(path_1.join(paths.app.root, 'tmp'), path_1.join(paths.app.root, 'build')), 'cache/.typescript'),
|
134 | babelOptions: {
|
135 | babelrc: false,
|
136 | plugins: project.hasDependency('react-hot-loader') ? ['react-hot-loader/babel'] : [],
|
137 | presets: [
|
138 | [require.resolve('babel-preset-shopify/web'), { modules: false }],
|
139 | require.resolve('babel-preset-shopify/react'),
|
140 | ],
|
141 | },
|
142 | },
|
143 | });
|
144 | }
|
145 | }
|
146 | if (project.hasDependency('@shopify/quilt')) {
|
147 | rules.push({
|
148 | test: /\.scss$/,
|
149 | loader: 'scss-loader',
|
150 | include: [path_1.resolve(paths.app.nodeModules, '@shopify/quilt')],
|
151 | });
|
152 | }
|
153 | return rules;
|
154 | },
|
155 | },
|
156 | plugins: removeEmptyPlugins([
|
157 | project.ifRails(new AssetsPlugin({
|
158 | filename: 'webpack-manifest.json',
|
159 | path: path_1.resolve(paths.app.root, 'tmp'),
|
160 | })),
|
161 | env.ifDevelopmentClient(new webpack.NamedModulesPlugin()),
|
162 | env.ifProductionClient(new WebpackMd5Hash()),
|
163 |
|
164 |
|
165 |
|
166 | env.ifProductionClient(new webpack.optimize.UglifyJsPlugin({
|
167 | sourceMap: true,
|
168 | comments: false,
|
169 | compress: {
|
170 | screw_ie8: true,
|
171 | warnings: false,
|
172 | },
|
173 | mangle: {
|
174 | screw_ie8: true,
|
175 | },
|
176 | })),
|
177 | ]),
|
178 | });
|
179 | };
|
180 | };
|