forked from Shopify/discount-app-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loom.config.ts
234 lines (214 loc) · 6.64 KB
/
loom.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import {
createPackage,
createWorkspacePlugin,
createProjectPlugin,
DiagnosticError,
} from '@shopify/loom';
import {
buildLibrary,
buildLibraryWorkspace,
rollupPlugins,
} from '@shopify/loom-plugin-build-library';
import {eslint} from '@shopify/loom-plugin-eslint';
import {stylelint} from '@shopify/loom-plugin-stylelint';
import {prettier} from '@shopify/loom-plugin-prettier';
import replace from '@rollup/plugin-replace';
import image from '@rollup/plugin-image';
import json from '@rollup/plugin-json';
import alias from '@rollup/plugin-alias';
import packageJSON from './package.json';
import {styles} from './config/rollup/plugin-styles';
import {generateScopedName} from './config/rollup/namespaced-classname';
import postcssPlugins from './config/postcss-plugins';
// Needed so TS realises what configuration hooks are provided by Jest
import type {} from '@shopify/loom-plugin-jest';
import _ from 'lodash';
import path from 'path';
const projectRootDir = path.resolve(__dirname);
// eslint-disable-next-line import/no-default-export
export default createPackage((pkg) => {
pkg.entry({root: './src/index.ts'});
pkg.use(
buildLibrary({
rootEntrypoints: false,
jestTestEnvironment: 'jsdom',
targets: 'extends @shopify/browserslist-config, node 12.20',
commonjs: true,
esmodules: true,
esnext: true,
}),
rollupAliasPlugin(),
buildLibraryWorkspace(),
eslint(),
stylelint({files: '**/*.scss'}),
prettier({files: '**/*.{md,json,yaml,yml}'}),
rollupAdjustPluginsPlugin(),
rollupAdjustOutputPlugin(),
jestAdjustmentsPlugin(),
preAndPostBuildPlugin(),
rollupDynamicLocaleImportPlugin(),
);
});
function jestAdjustmentsPlugin() {
return createProjectPlugin('DiscountAppComponents.Jest', ({tasks: {test}}) => {
process.env.TZ = 'UTC';
test.hook(({hooks}) => {
hooks.configure.hook((configuration) => {
// Aliases for root-level imports, which are used in test files
// `tests/*` as an alias for test-only utilities is acceptable, but
// avoid others as paths working in jest but not in rollup is confusing
configuration.jestModuleNameMapper?.hook((moduleNameMapper) => ({
...moduleNameMapper,
'^tests/(.*)': '<rootDir>/tests/$1',
'\\.s?css$': require.resolve('./config/jest-transform-style'),
"^@locales(.*)$": "<rootDir>/locales$1",
}));
configuration.jestConfig?.hook((config) => ({
...config,
testPathIgnorePatterns: ['/node_modules/'],
}));
configuration.jestConfig?.hook((config) => ({
...config,
globalSetup: './config/global-setup',
}));
// Novel file types - scss and images
configuration.jestTransform?.hook((transforms) => ({
...transforms,
'\\.svg$': require.resolve('./config/jest-transform-image'),
}));
});
});
});
}
function preAndPostBuildPlugin() {
return createWorkspacePlugin(
'DiscountAppComponents.PrePost',
({api, tasks: {build}}) => {
build.hook(({hooks}) => {
hooks.post.hook((steps) => [
...steps,
api.createStep(
{
id: 'DiscountAppComponentsBuild.Post',
label: 'discount-app-components post-build',
},
async (step) => {
try {
await step.exec(
'node_modules/.bin/downlevel-dts',
['build/ts/latest', 'build/ts/3.4'],
{all: true},
);
await step.exec(
'node_modules/.bin/copyfiles',
['./src/**/*.md', './build/docs', '--up=1'],
{all: true},
);
} catch (error) {
throw new DiagnosticError({
title: 'Error runing postbuild steps',
content: error.all,
});
}
},
),
]);
});
},
);
}
function rollupAliasPlugin() {
return rollupPlugins(() => [
alias({
entries: [
{ find: '@locales', replacement: './locales' },
],
})
]
);
}
function rollupDynamicLocaleImportPlugin() {
return rollupPlugins((target) => {
return [
replace({
include: ['src/**/*.tsx'],
values: {
'locales_dynamic': '../../../../../../locales',
},
}),
];
}
);
}
function rollupAdjustPluginsPlugin() {
return rollupPlugins((target) => {
const stylesConfig = target.options.rollupEsnext
? {
mode: 'esnext',
modules: {
generateScopedName: generateScopedName({includeHash: true}),
},
plugins: postcssPlugins,
}
: {
mode: 'standalone',
output: 'styles.css',
modules: {
generateScopedName: generateScopedName({includeHash: false}),
},
plugins: postcssPlugins,
};
return [
replace({
'{{DISCOUNT_APP_COMPONENTS_VERSION}}': packageJSON.version,
delimiters: ['', ''],
}),
image(),
json({
compact: true,
}),
styles(stylesConfig),
];
});
}
/**
* Output .js files for the esm build instead of .mjs files
*
* By default webpack 4 handles imports within js and mjs files differently.
* mjs files are subject to a stricter set of rules when importing from commonjs
* files such as react.
* Some apps (including sewing-kit based apps) work around this by adding
* `config.module.rules.push({test: /\.mjs$/, type: 'javascript/auto'});`
* to their webpack config, which tells webpack to treat .mjs files the same
* as .js files. However we should not rely on this behaviour being present.
*
* Thus publish our esm files with a .js file extension.
*/
function rollupAdjustOutputPlugin() {
return createProjectPlugin(
'DiscountAppComponents.RollupOutput',
({tasks: {build}}) => {
build.hook(({hooks}) => {
hooks.target.hook(({hooks, target}) => {
const isDefaultBuild = Object.keys(target.options).length === 0;
if (!isDefaultBuild) {
return;
}
hooks.configure.hook(async (configuration) => {
configuration.rollupOutputs?.hook((outputs) => {
for (const output of outputs) {
if (typeof output.entryFileNames === 'string') {
output.entryFileNames = output.entryFileNames.replace(
/\.mjs$/,
'.js',
);
}
}
return outputs;
});
});
});
});
},
);
}