forked from Shopify/polaris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
loom.config.ts
204 lines (190 loc) · 6.23 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
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 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';
// 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,
}),
buildLibraryWorkspace(),
eslint(),
stylelint({files: '**/*.scss'}),
prettier({files: '**/*.{md,json,yaml,yml}'}),
rollupAdjustPluginsPlugin(),
rollupAdjustOutputPlugin(),
jestAdjustmentsPlugin(),
preAndPostBuildPlugin(),
);
});
function jestAdjustmentsPlugin() {
return createProjectPlugin('Polaris.Jest', ({tasks: {test}}) => {
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',
}));
// Ignore tests in the examples folder
configuration.jestConfig?.hook((config) => ({
...config,
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/examples/'],
}));
// Novel file types - scss and images
configuration.jestTransform?.hook((transforms) => ({
...transforms,
'\\.s?css$': require.resolve('./config/jest-transform-style'),
'\\.svg$': require.resolve('./config/jest-transform-image'),
}));
});
});
});
}
function preAndPostBuildPlugin() {
return createWorkspacePlugin('Polaris.PrePost', ({api, tasks: {build}}) => {
build.hook(({hooks}) => {
hooks.pre.hook((steps) => [
...steps,
api.createStep(
{id: 'PolarisBuild.Pre', label: 'polaris pre-build'},
async (step) => {
try {
await step.exec('yarn', ['run', 'copy-polaris-tokens'], {
all: true,
});
} catch (error) {
throw new DiagnosticError({
title: 'Error runing prebuild steps',
content: error.all,
});
}
},
),
]);
hooks.post.hook((steps) => [
...steps,
api.createStep(
{id: 'PolarisBuild.Post', label: 'polaris 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},
);
await step.exec(
'node_modules/.bin/copyfiles',
['./src/styles/**/*.scss', './build/styles', '--up=2'],
{all: true},
);
} catch (error) {
throw new DiagnosticError({
title: 'Error runing postbuild steps',
content: error.all,
});
}
},
),
]);
});
});
}
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({
'{{POLARIS_VERSION}}': packageJSON.version,
delimiters: ['', ''],
}),
image(),
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('Polaris.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;
});
});
});
});
});
}