@quilted/rollup 0.1.9 → 0.1.11

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @quilted/rollup
2
2
 
3
+ ## 0.1.11
4
+
5
+ ### Patch Changes
6
+
7
+ - [`a1ecacac`](https://github.com/lemonmade/quilt/commit/a1ecacaca6229233cdecb7d27cb6165974aefcc6) Thanks [@lemonmade](https://github.com/lemonmade)! - Add package rollup config
8
+
9
+ - [`06b9abe3`](https://github.com/lemonmade/quilt/commit/06b9abe3a399e04b9fb9e8abe82b5f4d87e005dd) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix some output file names
10
+
11
+ ## 0.1.10
12
+
13
+ ### Patch Changes
14
+
15
+ - [`488b2c89`](https://github.com/lemonmade/quilt/commit/488b2c89b467b97feaf1e6f189bbf8c1aeea80f3) Thanks [@lemonmade](https://github.com/lemonmade)! - Delete build files on build
16
+
17
+ - [`0dafd7d0`](https://github.com/lemonmade/quilt/commit/0dafd7d02d9a361e06b2968f3dda48a30552a1f1) Thanks [@lemonmade](https://github.com/lemonmade)! - Add support for browserlist configuration
18
+
3
19
  ## 0.1.9
4
20
 
5
21
  ### Patch Changes
package/build/cjs/app.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  var path = require('node:path');
4
4
  var fs = require('node:fs/promises');
5
5
  var glob = require('glob');
6
+ var node_url = require('node:url');
6
7
  var constants = require('./constants.cjs');
7
8
  var strings = require('./shared/strings.cjs');
8
9
  var rollup = require('./shared/rollup.cjs');
@@ -29,6 +30,7 @@ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
29
30
  var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
30
31
 
31
32
  async function quiltAppBrowser({
33
+ root: rootPath = process.cwd(),
32
34
  app,
33
35
  entry,
34
36
  env,
@@ -36,10 +38,25 @@ async function quiltAppBrowser({
36
38
  module,
37
39
  graphql = true
38
40
  } = {}) {
39
- const root = process.cwd();
41
+ const root = node_url.fileURLToPath(rootPath);
40
42
  const mode = (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
41
43
  const minify = assets?.minify ?? mode === 'production';
42
44
  const baseURL = assets?.baseURL ?? '/assets/';
45
+ const assetTargets = assets?.targets ?? {};
46
+ const targets = Array.isArray(assetTargets) ? {
47
+ browsers: assetTargets
48
+ } : assetTargets;
49
+ const targetBrowsers = targets.browsers ?? (await (async () => {
50
+ const {
51
+ default: browserslist
52
+ } = await import('browserslist');
53
+ const config = browserslist.findConfig(root);
54
+ if (config == null) return ['defaults'];
55
+ const targetName = targets.name ?? 'defaults';
56
+ return config[targetName] ?? ['defaults'];
57
+ })());
58
+ const normalizedTargetName = targets.name === 'defaults' ? 'default' : targets.name;
59
+ const targetFilenamePart = normalizedTargetName ? `.${normalizedTargetName}` : '';
43
60
  const [{
44
61
  visualizer
45
62
  }, {
@@ -59,13 +76,16 @@ async function quiltAppBrowser({
59
76
  const plugins = [...nodePlugins, systemJS({
60
77
  minify
61
78
  }), sourceCode({
62
- mode
79
+ mode,
80
+ targets: targetBrowsers
63
81
  }), css({
64
82
  minify,
65
83
  emit: true
66
84
  }), rawAssets(), staticAssets({
67
85
  baseURL,
68
86
  emit: true
87
+ }), rollup.removeBuildFiles(['build/assets', 'build/manifests', 'build/reports'], {
88
+ root
69
89
  })];
70
90
  const tsconfigAliases = await createTSConfigAliasPlugin();
71
91
  if (tsconfigAliases) {
@@ -108,7 +128,7 @@ async function quiltAppBrowser({
108
128
  graphql
109
129
  } = await Promise.resolve().then(function () { return require('./features/graphql.cjs'); });
110
130
  plugins.push(graphql({
111
- manifest: path__namespace.resolve(`manifests/graphql.json`)
131
+ manifest: path__namespace.resolve(`manifests/graphql${targetFilenamePart}.json`)
112
132
  }));
113
133
  }
114
134
  if (minify) {
@@ -117,18 +137,25 @@ async function quiltAppBrowser({
117
137
  } = await import('rollup-plugin-esbuild');
118
138
  plugins.push(minify());
119
139
  }
140
+ const cacheKey = targets.name ? {
141
+ browserTarget: targets.name
142
+ } : undefined;
143
+ const id = targets.name ? targets.name : undefined;
120
144
  plugins.push(
121
145
  // @ts-expect-error The plugin still depends on Rollup 3
122
146
  assetManifest({
147
+ id,
148
+ cacheKey,
123
149
  baseUrl: baseURL,
124
- path: path__namespace.resolve(`build/manifests/assets.json`)
150
+ path: path__namespace.resolve(`build/manifests/assets${targetFilenamePart}.json`),
151
+ priority: assets?.priority
125
152
  }), visualizer({
126
153
  template: 'treemap',
127
154
  open: false,
128
155
  brotliSize: true,
129
- filename: path__namespace.resolve(`build/reports/bundle-visualizer.html`)
156
+ filename: path__namespace.resolve(`build/reports/bundle-visualizer${targetFilenamePart}.html`)
130
157
  }));
131
- const finalEntry = entry ?? (await glob.glob('browser.{ts,tsx,mjs,js,jsx}', {
158
+ const finalEntry = entry ?? (await glob.glob('{browser,client}.{ts,tsx,mjs,js,jsx}', {
132
159
  cwd: root,
133
160
  nodir: true,
134
161
  absolute: true
@@ -148,9 +175,9 @@ async function quiltAppBrowser({
148
175
  // format: isESM ? 'esm' : 'systemjs',
149
176
  format: 'esm',
150
177
  dir: path__namespace.resolve(`build/assets`),
151
- entryFileNames: `app.[hash].js`,
152
- assetFileNames: `[name].[hash].[ext]`,
153
- chunkFileNames: `[name].[hash].js`,
178
+ entryFileNames: `app${targetFilenamePart}.[hash].js`,
179
+ assetFileNames: `[name]${targetFilenamePart}.[hash].[ext]`,
180
+ chunkFileNames: `[name]${targetFilenamePart}.[hash].js`,
154
181
  manualChunks: createManualChunksSorter()
155
182
  }
156
183
  };
@@ -179,12 +206,15 @@ async function quiltAppServer({
179
206
  magicModuleRequestRouterEntry
180
207
  }, nodePlugins] = await Promise.all([import('rollup-plugin-visualizer'), Promise.resolve().then(function () { return require('./features/source-code.cjs'); }), Promise.resolve().then(function () { return require('./features/typescript.cjs'); }), Promise.resolve().then(function () { return require('./features/css.cjs'); }), Promise.resolve().then(function () { return require('./features/assets.cjs'); }), Promise.resolve().then(function () { return require('./features/request-router.cjs'); }), rollup.getNodePlugins()]);
181
208
  const plugins = [...nodePlugins, sourceCode({
182
- mode
209
+ mode,
210
+ targets: ['current node']
183
211
  }), css({
184
212
  emit: false,
185
213
  minify
186
214
  }), rawAssets(), staticAssets({
187
215
  emit: false
216
+ }), rollup.removeBuildFiles(['build/server'], {
217
+ root
188
218
  })];
189
219
  const tsconfigAliases = await createTSConfigAliasPlugin();
190
220
  if (tsconfigAliases) {
@@ -243,10 +273,10 @@ async function quiltAppServer({
243
273
  plugins.push(visualizer({
244
274
  template: 'treemap',
245
275
  open: false,
246
- brotliSize: true,
247
- filename: path__namespace.resolve(`build/reports/bundle-visualizer.html`)
276
+ brotliSize: false,
277
+ filename: path__namespace.resolve(`build/reports/bundle-visualizer.server.html`)
248
278
  }));
249
- const finalEntry = entry ?? (await glob.glob('server.{ts,tsx,mjs,js,jsx}', {
279
+ const finalEntry = entry ?? (await glob.glob('{server,service,backend}.{ts,tsx,mjs,js,jsx}', {
250
280
  cwd: root,
251
281
  nodir: true,
252
282
  absolute: true
@@ -1,8 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var app = require('./app.cjs');
4
+ var _package = require('./package.cjs');
4
5
 
5
6
 
6
7
 
7
8
  exports.quiltAppBrowser = app.quiltAppBrowser;
8
9
  exports.quiltAppServer = app.quiltAppServer;
10
+ exports.quiltPackageESModules = _package.quiltPackageESModules;
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
3
+ var path = require('node:path');
4
+ var glob = require('glob');
5
+ var node_url = require('node:url');
6
+ var rollup = require('./shared/rollup.cjs');
7
+ var packageJson = require('./shared/package-json.cjs');
8
+
9
+ function _interopNamespaceDefault(e) {
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n["default"] = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
27
+
28
+ async function quiltPackageESModules({
29
+ root: rootPath = process.cwd()
30
+ } = {}) {
31
+ const root = node_url.fileURLToPath(rootPath);
32
+ const outputDirectory = path__namespace.join(root, 'build/esm');
33
+ const [{
34
+ sourceCode
35
+ }, nodePlugins, packageJSON] = await Promise.all([Promise.resolve().then(function () { return require('./features/source-code.cjs'); }), rollup.getNodePlugins(), packageJson.loadPackageJSON(root)]);
36
+ const [entries] = await Promise.all([sourceEntriesForPackage(root, packageJSON)]);
37
+ let sourceRoot = root;
38
+ for (const entry of Object.values(entries)) {
39
+ if (!entry.startsWith(root)) continue;
40
+ sourceRoot = path__namespace.resolve(root, path__namespace.relative(root, entry).split(path__namespace.sep)[0] ?? '.');
41
+ break;
42
+ }
43
+ const plugins = [...nodePlugins, sourceCode({
44
+ mode: 'production'
45
+ }), rollup.removeBuildFiles(['build/esm'], {
46
+ root
47
+ })];
48
+ return {
49
+ input: entries,
50
+ plugins,
51
+ onwarn(warning, defaultWarn) {
52
+ // Removes annoying warnings for React-focused libraries that
53
+ // include 'use client' directives.
54
+ if (warning.code === 'MODULE_LEVEL_DIRECTIVE' && /['"]use client['"]/.test(warning.message)) {
55
+ return;
56
+ }
57
+ defaultWarn(warning);
58
+ },
59
+ output: {
60
+ preserveModules: true,
61
+ preserveModulesRoot: sourceRoot,
62
+ format: 'esm',
63
+ dir: outputDirectory,
64
+ entryFileNames: `[name].mjs`,
65
+ assetFileNames: `[name].[ext]`
66
+ // chunkFileNames: createChunkNamer({
67
+ // extension: ESM_EXTENSION,
68
+ // sourceRoot: sourceRootDirectory,
69
+ // }),
70
+ }
71
+ };
72
+ }
73
+
74
+ async function sourceEntriesForPackage(root, packageJSON) {
75
+ const {
76
+ main,
77
+ exports
78
+ } = packageJSON;
79
+ const entries = {};
80
+ if (typeof main === 'string') {
81
+ entries['.'] = await resolveTargetFileAsSource(main, root);
82
+ }
83
+ if (typeof exports === 'string') {
84
+ entries['.'] = await resolveTargetFileAsSource(exports, root);
85
+ return entries;
86
+ } else if (exports == null || typeof exports !== 'object') {
87
+ return entries;
88
+ }
89
+ for (const [exportPath, exportCondition] of Object.entries(exports)) {
90
+ let targetFile = null;
91
+ if (exportCondition == null) continue;
92
+ if (typeof exportCondition === 'string') {
93
+ targetFile = exportCondition;
94
+ } else {
95
+ targetFile ?? (targetFile = exportCondition['source'] ?? exportCondition['quilt:source'] ?? exportCondition['quilt:esnext'] ?? Object.values(exportCondition).find(condition => typeof condition === 'string' && condition.startsWith('./build/')));
96
+ }
97
+ if (targetFile == null) continue;
98
+ const sourceFile = await resolveTargetFileAsSource(targetFile, root);
99
+ entries[exportPath] = sourceFile;
100
+ }
101
+ return entries;
102
+ }
103
+ async function resolveTargetFileAsSource(file, root) {
104
+ const sourceFile = file.includes('/build/') ? (await glob.glob(file.replace(/[/]build[/][^/]+[/]/, '/*/').replace(/(\.d\.ts|\.[\w]+)$/, '.*'), {
105
+ cwd: root,
106
+ absolute: true,
107
+ ignore: [path__namespace.resolve(root, file)]
108
+ }))[0] : path__namespace.resolve(root, file);
109
+ return sourceFile;
110
+ }
111
+
112
+ exports.quiltPackageESModules = quiltPackageESModules;
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ var path = require('node:path');
4
+ var node_url = require('node:url');
5
+ var fs = require('node:fs/promises');
6
+
7
+ function _interopNamespaceDefault(e) {
8
+ var n = Object.create(null);
9
+ if (e) {
10
+ Object.keys(e).forEach(function (k) {
11
+ if (k !== 'default') {
12
+ var d = Object.getOwnPropertyDescriptor(e, k);
13
+ Object.defineProperty(n, k, d.get ? d : {
14
+ enumerable: true,
15
+ get: function () { return e[k]; }
16
+ });
17
+ }
18
+ });
19
+ }
20
+ n["default"] = e;
21
+ return Object.freeze(n);
22
+ }
23
+
24
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
25
+
26
+ async function loadPackageJSON(root) {
27
+ const file = await fs.readFile(path__namespace.join(node_url.fileURLToPath(root), 'package.json'), 'utf8');
28
+ return JSON.parse(file);
29
+ }
30
+
31
+ exports.loadPackageJSON = loadPackageJSON;
@@ -1,7 +1,28 @@
1
1
  'use strict';
2
2
 
3
+ var fs = require('node:fs/promises');
4
+ var glob = require('glob');
3
5
  var replace = require('@rollup/plugin-replace');
4
6
 
7
+ function _interopNamespaceDefault(e) {
8
+ var n = Object.create(null);
9
+ if (e) {
10
+ Object.keys(e).forEach(function (k) {
11
+ if (k !== 'default') {
12
+ var d = Object.getOwnPropertyDescriptor(e, k);
13
+ Object.defineProperty(n, k, d.get ? d : {
14
+ enumerable: true,
15
+ get: function () { return e[k]; }
16
+ });
17
+ }
18
+ });
19
+ }
20
+ n["default"] = e;
21
+ return Object.freeze(n);
22
+ }
23
+
24
+ var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
25
+
5
26
  function smartReplace(values, options) {
6
27
  return replace({
7
28
  // @see https://github.com/vitejs/vite/blob/2b1ffe86328f9d06ef9528ee117b61889893ddcc/packages/vite/src/node/plugins/define.ts#L108-L119
@@ -11,6 +32,23 @@ function smartReplace(values, options) {
11
32
  values
12
33
  });
13
34
  }
35
+ function removeBuildFiles(patterns, {
36
+ root = process.cwd()
37
+ } = {}) {
38
+ return {
39
+ name: '@quilt/remove-build-files',
40
+ async buildStart() {
41
+ const matches = await glob.glob(patterns, {
42
+ cwd: root,
43
+ absolute: true
44
+ });
45
+ await Promise.all(matches.map(file => fs__namespace.rm(file, {
46
+ recursive: true,
47
+ force: true
48
+ })));
49
+ }
50
+ };
51
+ }
14
52
  async function getNodePlugins() {
15
53
  const [{
16
54
  default: commonjs
@@ -30,4 +68,5 @@ async function getNodePlugins() {
30
68
  }
31
69
 
32
70
  exports.getNodePlugins = getNodePlugins;
71
+ exports.removeBuildFiles = removeBuildFiles;
33
72
  exports.smartReplace = smartReplace;
package/build/esm/app.mjs CHANGED
@@ -1,12 +1,14 @@
1
1
  import * as path from 'node:path';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import { glob } from 'glob';
4
+ import { fileURLToPath } from 'node:url';
4
5
  import { MAGIC_MODULE_ENTRY, MAGIC_MODULE_APP_COMPONENT, MAGIC_MODULE_REQUEST_ROUTER, MAGIC_MODULE_BROWSER_ASSETS } from './constants.mjs';
5
6
  import { multiline } from './shared/strings.mjs';
6
- import { getNodePlugins } from './shared/rollup.mjs';
7
+ import { getNodePlugins, removeBuildFiles } from './shared/rollup.mjs';
7
8
  import { createMagicModulePlugin } from './shared/magic-module.mjs';
8
9
 
9
10
  async function quiltAppBrowser({
11
+ root: rootPath = process.cwd(),
10
12
  app,
11
13
  entry,
12
14
  env,
@@ -14,10 +16,25 @@ async function quiltAppBrowser({
14
16
  module,
15
17
  graphql = true
16
18
  } = {}) {
17
- const root = process.cwd();
19
+ const root = fileURLToPath(rootPath);
18
20
  const mode = (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
19
21
  const minify = assets?.minify ?? mode === 'production';
20
22
  const baseURL = assets?.baseURL ?? '/assets/';
23
+ const assetTargets = assets?.targets ?? {};
24
+ const targets = Array.isArray(assetTargets) ? {
25
+ browsers: assetTargets
26
+ } : assetTargets;
27
+ const targetBrowsers = targets.browsers ?? (await (async () => {
28
+ const {
29
+ default: browserslist
30
+ } = await import('browserslist');
31
+ const config = browserslist.findConfig(root);
32
+ if (config == null) return ['defaults'];
33
+ const targetName = targets.name ?? 'defaults';
34
+ return config[targetName] ?? ['defaults'];
35
+ })());
36
+ const normalizedTargetName = targets.name === 'defaults' ? 'default' : targets.name;
37
+ const targetFilenamePart = normalizedTargetName ? `.${normalizedTargetName}` : '';
21
38
  const [{
22
39
  visualizer
23
40
  }, {
@@ -37,13 +54,16 @@ async function quiltAppBrowser({
37
54
  const plugins = [...nodePlugins, systemJS({
38
55
  minify
39
56
  }), sourceCode({
40
- mode
57
+ mode,
58
+ targets: targetBrowsers
41
59
  }), css({
42
60
  minify,
43
61
  emit: true
44
62
  }), rawAssets(), staticAssets({
45
63
  baseURL,
46
64
  emit: true
65
+ }), removeBuildFiles(['build/assets', 'build/manifests', 'build/reports'], {
66
+ root
47
67
  })];
48
68
  const tsconfigAliases = await createTSConfigAliasPlugin();
49
69
  if (tsconfigAliases) {
@@ -86,7 +106,7 @@ async function quiltAppBrowser({
86
106
  graphql
87
107
  } = await import('./features/graphql.mjs');
88
108
  plugins.push(graphql({
89
- manifest: path.resolve(`manifests/graphql.json`)
109
+ manifest: path.resolve(`manifests/graphql${targetFilenamePart}.json`)
90
110
  }));
91
111
  }
92
112
  if (minify) {
@@ -95,18 +115,25 @@ async function quiltAppBrowser({
95
115
  } = await import('rollup-plugin-esbuild');
96
116
  plugins.push(minify());
97
117
  }
118
+ const cacheKey = targets.name ? {
119
+ browserTarget: targets.name
120
+ } : undefined;
121
+ const id = targets.name ? targets.name : undefined;
98
122
  plugins.push(
99
123
  // @ts-expect-error The plugin still depends on Rollup 3
100
124
  assetManifest({
125
+ id,
126
+ cacheKey,
101
127
  baseUrl: baseURL,
102
- path: path.resolve(`build/manifests/assets.json`)
128
+ path: path.resolve(`build/manifests/assets${targetFilenamePart}.json`),
129
+ priority: assets?.priority
103
130
  }), visualizer({
104
131
  template: 'treemap',
105
132
  open: false,
106
133
  brotliSize: true,
107
- filename: path.resolve(`build/reports/bundle-visualizer.html`)
134
+ filename: path.resolve(`build/reports/bundle-visualizer${targetFilenamePart}.html`)
108
135
  }));
109
- const finalEntry = entry ?? (await glob('browser.{ts,tsx,mjs,js,jsx}', {
136
+ const finalEntry = entry ?? (await glob('{browser,client}.{ts,tsx,mjs,js,jsx}', {
110
137
  cwd: root,
111
138
  nodir: true,
112
139
  absolute: true
@@ -126,9 +153,9 @@ async function quiltAppBrowser({
126
153
  // format: isESM ? 'esm' : 'systemjs',
127
154
  format: 'esm',
128
155
  dir: path.resolve(`build/assets`),
129
- entryFileNames: `app.[hash].js`,
130
- assetFileNames: `[name].[hash].[ext]`,
131
- chunkFileNames: `[name].[hash].js`,
156
+ entryFileNames: `app${targetFilenamePart}.[hash].js`,
157
+ assetFileNames: `[name]${targetFilenamePart}.[hash].[ext]`,
158
+ chunkFileNames: `[name]${targetFilenamePart}.[hash].js`,
132
159
  manualChunks: createManualChunksSorter()
133
160
  }
134
161
  };
@@ -157,12 +184,15 @@ async function quiltAppServer({
157
184
  magicModuleRequestRouterEntry
158
185
  }, nodePlugins] = await Promise.all([import('rollup-plugin-visualizer'), import('./features/source-code.mjs'), import('./features/typescript.mjs'), import('./features/css.mjs'), import('./features/assets.mjs'), import('./features/request-router.mjs'), getNodePlugins()]);
159
186
  const plugins = [...nodePlugins, sourceCode({
160
- mode
187
+ mode,
188
+ targets: ['current node']
161
189
  }), css({
162
190
  emit: false,
163
191
  minify
164
192
  }), rawAssets(), staticAssets({
165
193
  emit: false
194
+ }), removeBuildFiles(['build/server'], {
195
+ root
166
196
  })];
167
197
  const tsconfigAliases = await createTSConfigAliasPlugin();
168
198
  if (tsconfigAliases) {
@@ -221,10 +251,10 @@ async function quiltAppServer({
221
251
  plugins.push(visualizer({
222
252
  template: 'treemap',
223
253
  open: false,
224
- brotliSize: true,
225
- filename: path.resolve(`build/reports/bundle-visualizer.html`)
254
+ brotliSize: false,
255
+ filename: path.resolve(`build/reports/bundle-visualizer.server.html`)
226
256
  }));
227
- const finalEntry = entry ?? (await glob('server.{ts,tsx,mjs,js,jsx}', {
257
+ const finalEntry = entry ?? (await glob('{server,service,backend}.{ts,tsx,mjs,js,jsx}', {
228
258
  cwd: root,
229
259
  nodir: true,
230
260
  absolute: true
@@ -1 +1,2 @@
1
1
  export { quiltAppBrowser, quiltAppServer } from './app.mjs';
2
+ export { quiltPackageESModules } from './package.mjs';
@@ -0,0 +1,91 @@
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.mjs';
5
+ import { loadPackageJSON } from './shared/package-json.mjs';
6
+
7
+ async function quiltPackageESModules({
8
+ root: rootPath = process.cwd()
9
+ } = {}) {
10
+ const root = fileURLToPath(rootPath);
11
+ const outputDirectory = path.join(root, 'build/esm');
12
+ const [{
13
+ sourceCode
14
+ }, nodePlugins, packageJSON] = await Promise.all([import('./features/source-code.mjs'), getNodePlugins(), loadPackageJSON(root)]);
15
+ const [entries] = await Promise.all([sourceEntriesForPackage(root, packageJSON)]);
16
+ let sourceRoot = root;
17
+ for (const entry of Object.values(entries)) {
18
+ if (!entry.startsWith(root)) continue;
19
+ sourceRoot = path.resolve(root, path.relative(root, entry).split(path.sep)[0] ?? '.');
20
+ break;
21
+ }
22
+ const plugins = [...nodePlugins, sourceCode({
23
+ mode: 'production'
24
+ }), removeBuildFiles(['build/esm'], {
25
+ root
26
+ })];
27
+ return {
28
+ input: entries,
29
+ plugins,
30
+ onwarn(warning, defaultWarn) {
31
+ // Removes annoying warnings for React-focused libraries that
32
+ // include 'use client' directives.
33
+ if (warning.code === 'MODULE_LEVEL_DIRECTIVE' && /['"]use client['"]/.test(warning.message)) {
34
+ return;
35
+ }
36
+ defaultWarn(warning);
37
+ },
38
+ output: {
39
+ preserveModules: true,
40
+ preserveModulesRoot: sourceRoot,
41
+ format: 'esm',
42
+ dir: outputDirectory,
43
+ entryFileNames: `[name].mjs`,
44
+ assetFileNames: `[name].[ext]`
45
+ // chunkFileNames: createChunkNamer({
46
+ // extension: ESM_EXTENSION,
47
+ // sourceRoot: sourceRootDirectory,
48
+ // }),
49
+ }
50
+ };
51
+ }
52
+
53
+ async function sourceEntriesForPackage(root, packageJSON) {
54
+ const {
55
+ main,
56
+ exports
57
+ } = packageJSON;
58
+ const entries = {};
59
+ if (typeof main === 'string') {
60
+ entries['.'] = await resolveTargetFileAsSource(main, root);
61
+ }
62
+ if (typeof exports === 'string') {
63
+ entries['.'] = await resolveTargetFileAsSource(exports, root);
64
+ return entries;
65
+ } else if (exports == null || typeof exports !== 'object') {
66
+ return entries;
67
+ }
68
+ for (const [exportPath, exportCondition] of Object.entries(exports)) {
69
+ let targetFile = null;
70
+ if (exportCondition == null) continue;
71
+ if (typeof exportCondition === 'string') {
72
+ targetFile = exportCondition;
73
+ } else {
74
+ targetFile ?? (targetFile = exportCondition['source'] ?? exportCondition['quilt:source'] ?? exportCondition['quilt:esnext'] ?? Object.values(exportCondition).find(condition => typeof condition === 'string' && condition.startsWith('./build/')));
75
+ }
76
+ if (targetFile == null) continue;
77
+ const sourceFile = await resolveTargetFileAsSource(targetFile, root);
78
+ entries[exportPath] = sourceFile;
79
+ }
80
+ return entries;
81
+ }
82
+ async function resolveTargetFileAsSource(file, root) {
83
+ const sourceFile = file.includes('/build/') ? (await glob(file.replace(/[/]build[/][^/]+[/]/, '/*/').replace(/(\.d\.ts|\.[\w]+)$/, '.*'), {
84
+ cwd: root,
85
+ absolute: true,
86
+ ignore: [path.resolve(root, file)]
87
+ }))[0] : path.resolve(root, file);
88
+ return sourceFile;
89
+ }
90
+
91
+ export { quiltPackageESModules };
@@ -0,0 +1,10 @@
1
+ import * as path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { readFile } from 'node:fs/promises';
4
+
5
+ async function loadPackageJSON(root) {
6
+ const file = await readFile(path.join(fileURLToPath(root), 'package.json'), 'utf8');
7
+ return JSON.parse(file);
8
+ }
9
+
10
+ export { loadPackageJSON };
@@ -1,3 +1,5 @@
1
+ import * as fs from 'node:fs/promises';
2
+ import { glob } from 'glob';
1
3
  import replace from '@rollup/plugin-replace';
2
4
 
3
5
  function smartReplace(values, options) {
@@ -9,6 +11,23 @@ function smartReplace(values, options) {
9
11
  values
10
12
  });
11
13
  }
14
+ function removeBuildFiles(patterns, {
15
+ root = process.cwd()
16
+ } = {}) {
17
+ return {
18
+ name: '@quilt/remove-build-files',
19
+ async buildStart() {
20
+ const matches = await glob(patterns, {
21
+ cwd: root,
22
+ absolute: true
23
+ });
24
+ await Promise.all(matches.map(file => fs.rm(file, {
25
+ recursive: true,
26
+ force: true
27
+ })));
28
+ }
29
+ };
30
+ }
12
31
  async function getNodePlugins() {
13
32
  const [{
14
33
  default: commonjs
@@ -27,4 +46,4 @@ async function getNodePlugins() {
27
46
  }), commonjs(), json()];
28
47
  }
29
48
 
30
- export { getNodePlugins, smartReplace };
49
+ export { getNodePlugins, removeBuildFiles, smartReplace };