@adobe/aio-lib-web 7.2.0 → 7.3.0

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/e2e/e2e.js CHANGED
@@ -25,7 +25,9 @@ const undeployWeb = require('../src/undeploy-web')
25
25
  const ENTRY_FILE = path.join(__dirname, 'sample-app/index.html')
26
26
  const DEST_FOLDER = path.join(__dirname, 'dist/')
27
27
  const CACHE_FILE = path.join(__dirname, '.cache')
28
- const SRC_REGEX = /<script.*src="(?<src>.*?)"/
28
+
29
+ // Regex to capture the `src` attribute of a `<script>` tag.
30
+ const SRC_REGEX = /<script.*src=["']?(?<src>[^"'\s>]+)["']?/
29
31
 
30
32
  const config = {
31
33
  app: {
@@ -97,15 +99,21 @@ describe('e2e', () => {
97
99
  })
98
100
 
99
101
  test('undeploy', async () => {
100
- let error, response
102
+ let error, response, contents
101
103
  try {
102
104
  await undeployWeb(config)
103
- response = await fetch(url)
105
+ // undeployWeb empties the S3 origin, but CloudFront may still serve the previously cached page on the canonical URL for its default TTL
106
+ // Adding a unique query string bypasses the edge cache so we read the current origin state.
107
+ response = await fetch(`${url}?nocache=${Date.now()}`)
108
+ contents = await response.text()
104
109
  } catch (e) {
105
110
  error = e
106
111
  console.error(e)
107
112
  }
108
113
  expect(error).toBeUndefined()
109
- expect(response.status).toEqual(404)
114
+ // assert that CDN serves its not-found page.
115
+ expect(response.status).toEqual(200)
116
+ expect(contents).toContain('404 Not Found')
117
+ expect(contents).toContain('The page you requested cannot be found.')
110
118
  })
111
119
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/aio-lib-web",
3
- "version": "7.2.0",
3
+ "version": "7.3.0",
4
4
  "description": "Utility tooling library to build and deploy Adobe I/O Project Firefly app static sites to CDN",
5
5
  "main": "index.js",
6
6
  "directories": {
package/src/build-web.js CHANGED
@@ -14,6 +14,8 @@ const fs = require('fs-extra')
14
14
  const path = require('path')
15
15
  const Bundler = require('@parcel/core').default
16
16
 
17
+ const { DEVELOPMENT_ENV, PRODUCTION_ENV } = require('./constants')
18
+
17
19
  /**
18
20
  * @deprecated since 4.1.0 ( January, 2021 ), use `bundle` instead
19
21
  */
@@ -25,6 +27,9 @@ const buildWeb = async (config, log) => {
25
27
  const dist = config.web.distProd
26
28
  const src = config.web.src
27
29
 
30
+ const nodeEnv = process.env.NODE_ENV || DEVELOPMENT_ENV
31
+ const mode = nodeEnv === PRODUCTION_ENV ? PRODUCTION_ENV : DEVELOPMENT_ENV
32
+
28
33
  // clean/create needed dirs
29
34
  await fs.emptyDir(dist)
30
35
 
@@ -32,10 +37,15 @@ const buildWeb = async (config, log) => {
32
37
  const bundler = new Bundler({
33
38
  entries: path.join(src, 'index.html'),
34
39
  defaultConfig: require.resolve('@parcel/config-default'),
40
+ mode,
41
+ env: {
42
+ NODE_ENV: nodeEnv
43
+ },
35
44
  shouldDisableCache: true,
36
45
  defaultTargetOptions: {
37
46
  distDir: dist,
38
- publicUrl: './'
47
+ publicUrl: './',
48
+ shouldOptimize: mode === 'production'
39
49
  },
40
50
  logLevel: 'none',
41
51
  shouldContentHash: true
package/src/bundle.js CHANGED
@@ -13,6 +13,8 @@ governing permissions and limitations under the License.
13
13
  const Bundler = require('@parcel/core').default
14
14
  const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web:bundle', { provider: 'debug' })
15
15
 
16
+ const { DEVELOPMENT_ENV, PRODUCTION_ENV } = require('./constants')
17
+
16
18
  /**
17
19
  * @typedef {object} BundleWebObject
18
20
  * @property {object} the Parcel bundler object
@@ -26,6 +28,10 @@ const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web:bun
26
28
  * @property {boolean} watch
27
29
  * @property {boolean} minify
28
30
  * @property {number} logLevel
31
+ * @property {'development'|'production'} mode
32
+ * @property {object} env
33
+ * @property {object} defaultTargetOptions
34
+ * @property {boolean} shouldOptimize
29
35
  */
30
36
 
31
37
  /**
@@ -37,7 +43,7 @@ const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web:bun
37
43
  * @param {Function} [log] the app logger
38
44
  * @returns {BundleWebObject} the BundleWebObject
39
45
  */
40
- module.exports = async (entries, dest, options = { shouldOptimize: false }, log = () => {}) => {
46
+ module.exports = async (entries, dest, options = {}, log = () => {}) => {
41
47
  aioLogger.debug(`bundle options: ${JSON.stringify(options, null, 2)}`)
42
48
 
43
49
  if (!entries) {
@@ -47,6 +53,16 @@ module.exports = async (entries, dest, options = { shouldOptimize: false }, log
47
53
  throw new Error('cannot build web, missing destination')
48
54
  }
49
55
 
56
+ const {
57
+ defaultTargetOptions = {},
58
+ env = {},
59
+ shouldOptimize,
60
+ ...parcelOptions
61
+ } = options
62
+
63
+ const nodeEnv = env.NODE_ENV || process.env.NODE_ENV || DEVELOPMENT_ENV
64
+ const mode = parcelOptions.mode || (nodeEnv === PRODUCTION_ENV ? PRODUCTION_ENV : DEVELOPMENT_ENV)
65
+
50
66
  // set defaults, but allow override by passed in values
51
67
  const parcelBundleOptions = {
52
68
  entries,
@@ -58,14 +74,20 @@ module.exports = async (entries, dest, options = { shouldOptimize: false }, log
58
74
  distDir: dest
59
75
  }
60
76
  },
61
- defaultTargetOptions: {
62
- distDir: dest,
63
- shouldOptimize: options.shouldOptimize
64
- },
65
77
  shouldPatchConsole: false,
66
78
  shouldContentHash: true,
67
79
  logLevel: 'error',
68
- ...options
80
+ ...parcelOptions,
81
+ mode,
82
+ env: {
83
+ ...env,
84
+ NODE_ENV: nodeEnv
85
+ },
86
+ defaultTargetOptions: {
87
+ distDir: dest,
88
+ ...defaultTargetOptions,
89
+ shouldOptimize: defaultTargetOptions.shouldOptimize ?? shouldOptimize ?? mode === 'production'
90
+ }
69
91
  }
70
92
 
71
93
  aioLogger.debug(`bundle bundleOptions: ${JSON.stringify(parcelBundleOptions, null, 2)}`)
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright 2026 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ const DEVELOPMENT_ENV = 'development'
14
+ const PRODUCTION_ENV = 'production'
15
+
16
+ module.exports = {
17
+ DEVELOPMENT_ENV,
18
+ PRODUCTION_ENV
19
+ }
@@ -15,6 +15,7 @@ const { vol } = global.mockFs()
15
15
  const buildWeb = require('../../src/build-web')
16
16
  const fs = require('fs-extra')
17
17
  jest.mock('fs-extra')
18
+ const originalNodeEnv = process.env.NODE_ENV
18
19
 
19
20
  describe('build-web', () => {
20
21
  beforeEach(() => {
@@ -24,6 +25,14 @@ describe('build-web', () => {
24
25
  global.cleanFs(vol)
25
26
  })
26
27
 
28
+ afterEach(() => {
29
+ if (originalNodeEnv === undefined) {
30
+ delete process.env.NODE_ENV
31
+ } else {
32
+ process.env.NODE_ENV = originalNodeEnv
33
+ }
34
+ })
35
+
27
36
  test('throws if config does not have an app, or frontEnd', async () => {
28
37
  await expect(buildWeb()).rejects.toThrow('cannot build web')
29
38
  await expect(buildWeb({ app: 'nothing-here' })).rejects.toThrow('cannot build web')
@@ -51,6 +60,7 @@ describe('build-web', () => {
51
60
  })
52
61
 
53
62
  test('check build options', async () => {
63
+ delete process.env.NODE_ENV
54
64
  const config = {
55
65
  app: {
56
66
  hasFrontend: true
@@ -68,13 +78,42 @@ describe('build-web', () => {
68
78
  defaultConfig: expect.stringContaining(path.join('parcel', 'config-default', 'index.json')),
69
79
  defaultTargetOptions: expect.objectContaining({
70
80
  distDir: 'dist',
71
- publicUrl: './'
81
+ publicUrl: './',
82
+ shouldOptimize: false
72
83
  }),
84
+ env: expect.objectContaining({ NODE_ENV: 'development' }),
73
85
  entries: path.join('fakeDir', 'index.html'),
74
86
  logLevel: 'none',
87
+ mode: 'development',
75
88
  shouldContentHash: true,
76
89
  shouldDisableCache: true
77
90
  })
78
91
  ])
79
92
  })
93
+
94
+ test('uses production build options from NODE_ENV', async () => {
95
+ process.env.NODE_ENV = 'production'
96
+ const config = {
97
+ app: {
98
+ hasFrontend: true
99
+ },
100
+ web: {
101
+ distProd: 'dist',
102
+ src: 'fakeDir'
103
+ }
104
+ }
105
+ global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
106
+ fs.readdir.mockReturnValue(['output.html'])
107
+
108
+ await expect(buildWeb(config)).resolves.toEqual(['output.html'])
109
+ expect(global._bundler__arguments).toEqual([
110
+ expect.objectContaining({
111
+ defaultTargetOptions: expect.objectContaining({
112
+ shouldOptimize: true
113
+ }),
114
+ env: expect.objectContaining({ NODE_ENV: 'production' }),
115
+ mode: 'production'
116
+ })
117
+ ])
118
+ })
80
119
  })
@@ -15,6 +15,7 @@ const bundle = require('../../src/bundle')
15
15
  const fs = require('fs-extra')
16
16
  jest.mock('fs-extra')
17
17
  const path = require('path')
18
+ const originalNodeEnv = process.env.NODE_ENV
18
19
 
19
20
  describe('bundle', () => {
20
21
  beforeEach(() => {
@@ -24,6 +25,14 @@ describe('bundle', () => {
24
25
  global.cleanFs(vol)
25
26
  })
26
27
 
28
+ afterEach(() => {
29
+ if (originalNodeEnv === undefined) {
30
+ delete process.env.NODE_ENV
31
+ } else {
32
+ process.env.NODE_ENV = originalNodeEnv
33
+ }
34
+ })
35
+
27
36
  test('throws if config does not have an app, or frontEnd', async () => {
28
37
  // much of this is actually now the callers responsibility
29
38
  await expect(bundle()).rejects.toThrow('cannot build web')
@@ -49,6 +58,7 @@ describe('bundle', () => {
49
58
  })
50
59
 
51
60
  test('check build options', async () => {
61
+ delete process.env.NODE_ENV
52
62
  global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
53
63
  await expect(bundle('fakeDir/index.html', 'distProd')).resolves.toEqual(expect.any(Object))
54
64
  expect(global._bundler__arguments).toEqual([
@@ -58,8 +68,10 @@ describe('bundle', () => {
58
68
  distDir: 'distProd',
59
69
  shouldOptimize: false
60
70
  }),
71
+ env: expect.objectContaining({ NODE_ENV: 'development' }),
61
72
  entries: 'fakeDir/index.html',
62
73
  logLevel: 'error',
74
+ mode: 'development',
63
75
  shouldContentHash: true,
64
76
  shouldDisableCache: false
65
77
  })])
@@ -99,6 +111,46 @@ describe('bundle', () => {
99
111
  })])
100
112
  })
101
113
 
114
+ test('uses production build options from NODE_ENV', async () => {
115
+ process.env.NODE_ENV = 'production'
116
+ global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
117
+
118
+ await expect(bundle('fakeDir/index.html', 'distProd')).resolves.toEqual(expect.any(Object))
119
+ expect(global._bundler__arguments).toEqual([
120
+ expect.objectContaining({
121
+ defaultTargetOptions: expect.objectContaining({
122
+ distDir: 'distProd',
123
+ shouldOptimize: true
124
+ }),
125
+ env: expect.objectContaining({ NODE_ENV: 'production' }),
126
+ mode: 'production'
127
+ })])
128
+ })
129
+
130
+ test('allows explicit Parcel mode, environment and target option overrides', async () => {
131
+ process.env.NODE_ENV = 'development'
132
+ global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
133
+
134
+ await expect(bundle('fakeDir/index.html', 'distProd', {
135
+ mode: 'production',
136
+ env: { NODE_ENV: 'production', CUSTOM_ENV: 'custom' },
137
+ defaultTargetOptions: { publicUrl: './', shouldOptimize: false }
138
+ })).resolves.toEqual(expect.any(Object))
139
+ expect(global._bundler__arguments).toEqual([
140
+ expect.objectContaining({
141
+ defaultTargetOptions: expect.objectContaining({
142
+ distDir: 'distProd',
143
+ publicUrl: './',
144
+ shouldOptimize: false
145
+ }),
146
+ env: {
147
+ NODE_ENV: 'production',
148
+ CUSTOM_ENV: 'custom'
149
+ },
150
+ mode: 'production'
151
+ })])
152
+ })
153
+
102
154
  test('returns {bundle, cleanup}', async () => {
103
155
  global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
104
156