Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Need example transpiling es6 modules in node_modules folder #19

Open
bahmutov opened this issue Oct 30, 2018 · 2 comments
Open

Need example transpiling es6 modules in node_modules folder #19

bahmutov opened this issue Oct 30, 2018 · 2 comments

Comments

@bahmutov
Copy link
Contributor

If the client wants to use import { merge } from 'lodash-es'; then we need to show how to modify browserify options to transpile node_modules folder

Cypress 3.1.0

For cypress-io/cypress#2679

@bahmutov
Copy link
Contributor Author

bahmutov commented Oct 30, 2018

Solution

const browserify = require('@cypress/browserify-preprocessor')

module.exports = (on) => {
  const options = browserify.defaultOptions
  // print options to find babelify, it is inside transforms at index 1
  // and it is [filename, options]
  const babelOptions = options.browserifyOptions.transform[1][1]
  babelOptions.global = true
  // ignore all modules except files in lodash-es
  babelOptions.ignore = [/\/node_modules\/(?!lodash-es\/)/]
  // if you want to see the final options
  // console.log('%o', babelOptions)

  on('file:preprocessor', browserify(options))
}

Example cypress/integration/spec.js file

// use es6 module from node_modules
import { merge } from 'lodash-es';
// use our es6 module (works right out of the box)
import { foo } from './foo';

describe('Preprocessor Reproducer', () => {
  it('test', () => {
    expect(foo).to.equal('foo')
    expect(merge({ foo: 'bar'}, { baz: 'bar'})).to.deep.equal({
      foo: 'bar',
      baz: 'bar'
    })
  })
});

there is noticeable transpile time for the test (this is why people recommend NOT to transpile es6), but the test passes

screen shot 2018-10-30 at 9 36 43 pm

note: https://github.com/babel/babelify#why-arent-files-in-node_modules-being-transformed

@ben8p
Copy link

ben8p commented Jan 8, 2021

For people seeking the same with typescript: until #61 is fixed, you can go with this workaround:

Object.defineProperty(options.browserifyOptions.transform, 'filter', {
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		value: function<TType extends string>(this: TType[], filterCallback: (value: TType, index: number, array: TType[]) => boolean, thisArg?: any) {
			const output = [];
			const callback = thisArg ? filterCallback.bind(thisArg) : filterCallback;
			for (let index = 0; index < this.length; index++) {
				if (Array.isArray(this[index]) && typeof this[index][0] === 'string' && this[index][0].includes('babelify')) {
					output.push(this[index]);
					continue;
				}
				if (Array.isArray(this[index]) && typeof this[index][0] === 'string' && this[index][0].includes('coffeeify')) {
					continue;
				}
				if (callback(this[index], index, this)) {
					output.push(this[index]);
				}
			}
			return output;
		},
		enumerable: false,
	});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants