@quilted/rollup 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,134 @@
1
+ import * as path from 'node:path';
2
+ import { glob } from 'glob';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { getNodePlugins, removeBuildFiles } from './shared/rollup.esnext';
5
+ import { loadPackageJSON } from './shared/package-json.esnext';
6
+ import { getBrowserTargetDetails } from './shared/browserslist.esnext';
7
+
8
+ async function quiltModule({
9
+ root: rootPath = process.cwd(),
10
+ env,
11
+ assets,
12
+ graphql = true
13
+ } = {}) {
14
+ const root = typeof rootPath === 'string' ? rootPath : fileURLToPath(rootPath);
15
+ const mode = (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
16
+ const outputDirectory = path.join(root, 'build/assets');
17
+ const minify = assets?.minify ?? true;
18
+ const hash = assets?.hash ?? true;
19
+ const hashFilenamePart = hash ? '.[hash]' : '';
20
+ const browserTarget = await getBrowserTargetDetails(assets?.targets, {
21
+ root
22
+ });
23
+ const targetFilenamePart = browserTarget.name ? `.${browserTarget.name}` : '';
24
+ const [{
25
+ visualizer
26
+ }, {
27
+ magicModuleEnv,
28
+ replaceProcessEnv
29
+ }, {
30
+ sourceCode
31
+ }, nodePlugins, packageJSON] = await Promise.all([import('rollup-plugin-visualizer'), import('./features/env.esnext'), import('./features/source-code.esnext'), getNodePlugins(), loadPackageJSON(root)]);
32
+ const source = await sourceForPackage(root, packageJSON);
33
+ const plugins = [...nodePlugins, replaceProcessEnv({
34
+ mode
35
+ }), magicModuleEnv({
36
+ ...env,
37
+ mode
38
+ }), sourceCode({
39
+ mode: 'production'
40
+ }), removeBuildFiles(['build/assets', 'build/reports'], {
41
+ root
42
+ })];
43
+ if (graphql) {
44
+ const {
45
+ graphql
46
+ } = await import('./features/graphql.esnext');
47
+ plugins.push(graphql({
48
+ manifest: false
49
+ }));
50
+ }
51
+ if (minify) {
52
+ const {
53
+ minify
54
+ } = await import('rollup-plugin-esbuild');
55
+ plugins.push(minify());
56
+ }
57
+ plugins.push(visualizer({
58
+ template: 'treemap',
59
+ open: false,
60
+ brotliSize: true,
61
+ filename: path.resolve(root, `build/reports/bundle-visualizer${targetFilenamePart}.html`)
62
+ }));
63
+ return {
64
+ input: source.files,
65
+ plugins,
66
+ onwarn(warning, defaultWarn) {
67
+ // Removes annoying warnings for React-focused libraries that
68
+ // include 'use client' directives.
69
+ if (warning.code === 'MODULE_LEVEL_DIRECTIVE' && /['"]use client['"]/.test(warning.message)) {
70
+ return;
71
+ }
72
+ defaultWarn(warning);
73
+ },
74
+ output: {
75
+ format: 'esm',
76
+ dir: outputDirectory,
77
+ entryFileNames: `[name]${targetFilenamePart}${hashFilenamePart}.js`,
78
+ assetFileNames: `[name]${targetFilenamePart}${hashFilenamePart}.[ext]`
79
+ }
80
+ };
81
+ }
82
+ async function sourceForPackage(root, packageJSON) {
83
+ const [entries] = await Promise.all([sourceEntriesForPackage(root, packageJSON)]);
84
+ let sourceRoot = root;
85
+ const sourceEntryFiles = Object.values(entries);
86
+ for (const entry of sourceEntryFiles) {
87
+ if (!entry.startsWith(root)) continue;
88
+ sourceRoot = path.resolve(root, path.relative(root, entry).split(path.sep)[0] ?? '.');
89
+ break;
90
+ }
91
+ return {
92
+ root: sourceRoot,
93
+ files: sourceEntryFiles
94
+ };
95
+ }
96
+ async function sourceEntriesForPackage(root, packageJSON) {
97
+ const {
98
+ main,
99
+ exports
100
+ } = packageJSON;
101
+ const entries = {};
102
+ if (typeof main === 'string') {
103
+ entries['.'] = await resolveTargetFileAsSource(main, root);
104
+ }
105
+ if (typeof exports === 'string') {
106
+ entries['.'] = await resolveTargetFileAsSource(exports, root);
107
+ return entries;
108
+ } else if (exports == null || typeof exports !== 'object') {
109
+ return entries;
110
+ }
111
+ for (const [exportPath, exportCondition] of Object.entries(exports)) {
112
+ let targetFile = null;
113
+ if (exportCondition == null) continue;
114
+ if (typeof exportCondition === 'string') {
115
+ targetFile = exportCondition;
116
+ } else {
117
+ targetFile ??= exportCondition['source'] ?? exportCondition['quilt:source'] ?? exportCondition['quilt:esnext'] ?? Object.values(exportCondition).find(condition => typeof condition === 'string' && condition.startsWith('./build/'));
118
+ }
119
+ if (targetFile == null) continue;
120
+ const sourceFile = await resolveTargetFileAsSource(targetFile, root);
121
+ entries[exportPath] = sourceFile;
122
+ }
123
+ return entries;
124
+ }
125
+ async function resolveTargetFileAsSource(file, root) {
126
+ const sourceFile = file.includes('/build/') ? (await glob(file.replace(/[/]build[/][^/]+[/]/, '/*/').replace(/(\.d\.ts|\.[\w]+)$/, '.*'), {
127
+ cwd: root,
128
+ absolute: true,
129
+ ignore: [path.resolve(root, file)]
130
+ }))[0] : path.resolve(root, file);
131
+ return sourceFile;
132
+ }
133
+
134
+ export { quiltModule };
@@ -0,0 +1,23 @@
1
+ async function getBrowserTargetDetails(targetSelection = {}, {
2
+ root
3
+ } = {}) {
4
+ const targets = Array.isArray(targetSelection) ? {
5
+ browsers: targetSelection
6
+ } : targetSelection;
7
+ const targetBrowsers = targets.browsers ?? (await (async () => {
8
+ const {
9
+ default: browserslist
10
+ } = await import('browserslist');
11
+ const config = browserslist.findConfig(root);
12
+ if (config == null) return ['defaults'];
13
+ const targetName = targets.name ?? 'defaults';
14
+ return config[targetName] ?? ['defaults'];
15
+ })());
16
+ const name = targets.name === 'defaults' ? 'default' : targets.name;
17
+ return {
18
+ name,
19
+ browsers: targetBrowsers
20
+ };
21
+ }
22
+
23
+ export { getBrowserTargetDetails };