Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parcel v1 to v2 migration #150

Merged
merged 11 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"klaw": "^3.0.0",
"lodash.clonedeep": "^4.5.0",
"mime-types": "^2.1.24",
"parcel-bundler": "1.12.3"
"parcel": "^2.0.0-beta.2"
moritzraho marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@types/hapi__joi": "^16.0.2",
Expand Down
21 changes: 12 additions & 9 deletions src/build-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ governing permissions and limitations under the License.

const fs = require('fs-extra')
const path = require('path')
const Bundler = require('parcel-bundler')
const Bundler = require('@parcel/core').default

/**
* @deprecated since 4.1.0 ( January, 2021 ), use `bundle` instead
Expand All @@ -29,16 +29,19 @@ const buildWeb = async (config, log) => {
await fs.emptyDir(dist)

// 2. build files
const bundler = new Bundler(path.join(src, 'index.html'), {
cache: false,
outDir: dist,
publicUrl: './',
watch: false,
logLevel: 0,
contentHash: true
const bundler = new Bundler({
entries: path.join(src, 'index.html'),
defaultConfig: require.resolve('@parcel/config-default'),
shouldDisableCache: true,
defaultTargetOptions: {
distDir: dist,
publicUrl: './'
},
logLevel: 'none',
shouldContentHash: true
})

await bundler.bundle()
await bundler.run()

// 3. show built files ( if we are passed a log function )
const files = await fs.readdir(dist)
Expand Down
36 changes: 19 additions & 17 deletions src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const Bundler = require('parcel-bundler')
const Bundler = require('@parcel/core').default
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web:bundle', { provider: 'debug' })
const fs = require('fs-extra')
/**
Expand Down Expand Up @@ -49,26 +49,28 @@ module.exports = async (entryFile, dest, options = {}, log = () => {}) => {

// set defaults, but allow override by passed in values
const parcelBundleOptions = {
cache: true,
outDir: dest,
contentHash: true, // false if dev, true if prod ??
watch: false, // currently false if dev true if prod,
minify: false,
logLevel: 1,
entries: entryFile,
defaultConfig: require.resolve('@parcel/config-default'),
shouldDisableCache: false,
targets: {
action: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why action is it some parcel jargon or you choose the target key ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok should we use web-assets key instead ?

includeNodeModules: true,
distDir: dest
}
},
defaultTargetOptions: {
distDir: dest,
shouldOptimize: false
},
shouldPatchConsole: false,
shouldContentHash: true,
logLevel: 'error',
...options
moritzraho marked this conversation as resolved.
Show resolved Hide resolved
}

aioLogger.debug(`bundle bundleOptions: ${JSON.stringify(parcelBundleOptions, null, 2)}`)
log(`bundling ${entryFile}`)
const bundler = new Bundler(entryFile, parcelBundleOptions)
const bundler = new Bundler(parcelBundleOptions)

const cleanup = async () => {
aioLogger.debug('cleanup bundler...')
await bundler.stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this needs to be replaced with onBuildEnd event from the bundler for the cleanup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the onBuildEnd of Parcel V1, we have reporter plugin in V2. But i think it's different from the stop API. This event handler is to see what assets were built but not for cleanup.

}

return {
bundler,
cleanup
}
return bundler
}
44 changes: 22 additions & 22 deletions test/__mocks__/parcel-bundler.js → test/__mocks__/@parcel/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ governing permissions and limitations under the License.
*/

const mockBundle = jest.fn()
const mockMiddleware = jest.fn()
const mockConstructor = jest.fn()
const mockServe = jest.fn()
const mockStop = jest.fn()

// hack to expose constructor, somehow returning a jest.fn doesn't work as expected for commonjs (only es6)
const Bundler = function (...args) {
mockConstructor(...args)
global._bundler__arguments = args
return {
bundle: mockBundle,
middleware: mockMiddleware,
serve: mockServe,
stop: mockStop
class core {
constructor (...args) {
mockConstructor(...args)
global._bundler__arguments = args
}

run () {
mockBundle()
}

watch () {
mockServe()
}
}
Bundler.mockBundle = mockBundle
Bundler.mockConstructor = mockConstructor
Bundler.mockMiddleware = mockMiddleware
Bundler.mockServe = mockServe
Bundler.mockStop = mockStop
core.mockBundle = mockBundle
core.mockConstructor = mockConstructor
core.mockServe = mockServe

// alias
Bundler.mockReset = () => {
Bundler.mockConstructor.mockReset()
Bundler.mockBundle.mockReset()
Bundler.mockMiddleware.mockReset()
Bundler.mockServe.mockReset()
Bundler.mockStop.mockReset()
core.mockReset = () => {
core.mockConstructor.mockReset()
core.mockBundle.mockReset()
core.mockServe.mockReset()
}

module.exports = Bundler
module.exports = {
default: core
}
14 changes: 13 additions & 1 deletion test/src/build-web.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ describe('build-web', () => {
global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
fs.readdir.mockReturnValue(['output.html'])
await expect(buildWeb(config)).resolves.toEqual(['output.html'])
expect(global._bundler__arguments).toEqual([path.join('fakeDir', 'index.html'), { cache: false, contentHash: true, logLevel: 0, outDir: 'dist', publicUrl: './', watch: false }])
expect(global._bundler__arguments).toEqual([
expect.objectContaining({
defaultConfig: expect.stringContaining(path.join('parcel', 'config-default', 'index.json')),
defaultTargetOptions: expect.objectContaining({
distDir: 'dist',
publicUrl: './'
}),
entries: path.join('fakeDir', 'index.html'),
logLevel: 'none',
shouldContentHash: true,
shouldDisableCache: true
})
])
})
})
42 changes: 21 additions & 21 deletions test/src/bundle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { vol } = global.mockFs()
const bundle = require('../../src/bundle')
const fs = require('fs-extra')
jest.mock('fs-extra')
const path = require('path')

describe('bundle', () => {
beforeEach(() => {
Expand Down Expand Up @@ -49,43 +50,42 @@ describe('bundle', () => {

test('check build options', async () => {
global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
await expect(bundle('fakeDir/index.html', 'distProd')).resolves.toEqual(
expect.objectContaining({ bundler: expect.any(Object) }))
await expect(bundle('fakeDir/index.html', 'distProd')).resolves.toEqual(expect.any(Object))
expect(global._bundler__arguments).toEqual([
expect.stringContaining('fakeDir'),
expect.objectContaining({
cache: true,
contentHash: true,
logLevel: 1,
outDir: 'distProd',
watch: false
defaultConfig: expect.stringContaining(path.join('parcel', 'config-default', 'index.json')),
defaultTargetOptions: expect.objectContaining({
distDir: 'distProd',
shouldOptimize: false
}),
entries: 'fakeDir/index.html',
logLevel: 'error',
shouldContentHash: true,
shouldDisableCache: false
})])
})

test('uses build options', async () => {
global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })
await expect(bundle('fakeDir/index.html', 'distProd', { contentHash: false, logLevel: 5 }))
.resolves.toEqual(expect.objectContaining({ bundler: expect.any(Object) }))
.resolves.toEqual(expect.any(Object))
expect(global._bundler__arguments).toEqual([
expect.stringContaining('fakeDir'),
expect.objectContaining({
cache: true,
contentHash: false,
logLevel: 5,
outDir: 'distProd',
watch: false
defaultConfig: expect.stringContaining(path.join('parcel', 'config-default', 'index.json')),
defaultTargetOptions: expect.objectContaining({
distDir: 'distProd',
shouldOptimize: false
}),
entries: 'fakeDir/index.html',
shouldContentHash: true,
shouldDisableCache: false
})])
})

test('returns {bundle, cleanup}', async () => {
global.addFakeFiles(vol, 'fakeDir', { 'index.html': '' })

const { bundler, cleanup } = await bundle('fakeDir/index.html', 'distProd', { contentHash: false, logLevel: 5 })
const bundler = await bundle('fakeDir/index.html', 'distProd', { contentHash: false, logLevel: 5 })
expect(bundler).toBeDefined()
expect(cleanup).toBeDefined()
expect(typeof cleanup).toBe('function')
bundler.stop = jest.fn()
cleanup()
expect(bundler.stop).toHaveBeenCalled()
})
})