Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

108 exclude list #110

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ Object {
},
Object {
"exclude": Array [
/\\[\\\\/\\\\\\\\\\\\\\\\\\]node_modules\\[\\\\/\\\\\\\\\\\\\\\\\\]/,
/\\[/\\\\\\\\\\\\\\\\\\]node_modules\\[/\\\\\\\\\\\\\\\\\\]/,
],
"loader": "<PROJECT_ROOT>/node_modules/babel-loader/lib/index.js",
"options": Object {
"babelrc": false,
"cacheCompression": false,
"cacheDirectory": true,
"cacheIdentifier": "{\\"babel-loader\\":\\"8.0.2\\",\\"@babel/core\\":\\"7.1.2\\",\\"babelConfig\\":\\"\\",\\"env\\":\\"test\\",\\"browserslist\\":[\\"and_chr 67\\",\\"and_uc 11.8\\",\\"android 4.4.3-4.4.4\\",\\"android 4.4\\",\\"chrome 68\\",\\"chrome 67\\",\\"chrome 66\\",\\"chrome 65\\",\\"chrome 63\\",\\"chrome 55\\",\\"chrome 49\\",\\"chrome 29\\",\\"edge 17\\",\\"edge 16\\",\\"firefox 61\\",\\"firefox 60\\",\\"firefox 52\\",\\"ie 11\\",\\"ios_saf 11.3-11.4\\",\\"ios_saf 11.0-11.2\\",\\"ios_saf 10.3\\",\\"ios_saf 10.0-10.2\\",\\"ios_saf 9.3\\",\\"opera 53\\",\\"safari 11.1\\",\\"safari 11\\",\\"safari 10.1\\",\\"samsung 6.2\\",\\"samsung 4\\"]}",
"cacheIdentifier": "{\\"babel-loader\\":\\"8.2.2\\",\\"@babel/core\\":\\"7.12.10\\",\\"babelConfig\\":\\"\\",\\"env\\":\\"test\\",\\"browserslist\\":[\\"and_chr 87\\",\\"and_ff 83\\",\\"and_uc 12.12\\",\\"chrome 87\\",\\"chrome 86\\",\\"chrome 85\\",\\"chrome 84\\",\\"chrome 49\\",\\"edge 87\\",\\"firefox 84\\",\\"firefox 83\\",\\"ie 11\\",\\"ios_saf 14.0-14.3\\",\\"ios_saf 13.4-13.7\\",\\"ios_saf 13.3\\",\\"ios_saf 12.2-12.4\\",\\"opera 72\\",\\"safari 14\\",\\"safari 13.1\\",\\"samsung 13.0\\",\\"samsung 12.0\\"]}",
"compact": false,
"configFile": false,
"presets": Array [
Expand All @@ -52,7 +52,7 @@ Object {
"babelrc": false,
"cacheCompression": false,
"cacheDirectory": true,
"cacheIdentifier": "{\\"babel-loader\\":\\"8.0.2\\",\\"@babel/core\\":\\"7.1.2\\",\\"babelConfig\\":\\"\\",\\"env\\":\\"test\\",\\"browserslist\\":[\\"and_chr 67\\",\\"and_uc 11.8\\",\\"android 4.4.3-4.4.4\\",\\"android 4.4\\",\\"chrome 68\\",\\"chrome 67\\",\\"chrome 66\\",\\"chrome 65\\",\\"chrome 63\\",\\"chrome 55\\",\\"chrome 49\\",\\"chrome 29\\",\\"edge 17\\",\\"edge 16\\",\\"firefox 61\\",\\"firefox 60\\",\\"firefox 52\\",\\"ie 11\\",\\"ios_saf 11.3-11.4\\",\\"ios_saf 11.0-11.2\\",\\"ios_saf 10.3\\",\\"ios_saf 10.0-10.2\\",\\"ios_saf 9.3\\",\\"opera 53\\",\\"safari 11.1\\",\\"safari 11\\",\\"safari 10.1\\",\\"samsung 6.2\\",\\"samsung 4\\"]}",
"cacheIdentifier": "{\\"babel-loader\\":\\"8.2.2\\",\\"@babel/core\\":\\"7.12.10\\",\\"babelConfig\\":\\"\\",\\"env\\":\\"test\\",\\"browserslist\\":[\\"and_chr 87\\",\\"and_ff 83\\",\\"and_uc 12.12\\",\\"chrome 87\\",\\"chrome 86\\",\\"chrome 85\\",\\"chrome 84\\",\\"chrome 49\\",\\"edge 87\\",\\"firefox 84\\",\\"firefox 83\\",\\"ie 11\\",\\"ios_saf 14.0-14.3\\",\\"ios_saf 13.4-13.7\\",\\"ios_saf 13.3\\",\\"ios_saf 12.2-12.4\\",\\"opera 72\\",\\"safari 14\\",\\"safari 13.1\\",\\"samsung 13.0\\",\\"samsung 12.0\\"]}",
"compact": false,
"configFile": false,
"presets": Array [
Expand Down Expand Up @@ -129,6 +129,7 @@ Object {
"options": Object {
"cache": true,
"cacheKeys": [Function],
"chunkFilter": [Function],
"exclude": undefined,
"extractComments": false,
"include": undefined,
Expand All @@ -154,7 +155,7 @@ Object {
"ecma": 8,
},
},
"test": /\\\\\\.js\\(\\\\\\?\\.\\*\\)\\?\\$/i,
"test": /\\\\\\.m\\?js\\(\\\\\\?\\.\\*\\)\\?\\$/i,
"warningsFilter": [Function],
},
},
Expand All @@ -181,13 +182,21 @@ Object {
"plugins": Array [
AssetsWebpackPlugin {
"options": Object {
"fileTypes": Array [
"js",
"css",
],
"filename": "assets.json",
"fullPath": true,
"includeAllFileTypes": true,
"integrity": false,
"keepInMemory": false,
"manifestFirst": true,
"path": "<PROJECT_ROOT>/_site",
"prettyPrint": false,
"processOutput": [Function],
"update": false,
"useCompilerPath": false,
},
"writer": [Function],
},
Expand Down
39 changes: 37 additions & 2 deletions lib/webpack-config/babel-loader-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const browserslist = require('browserslist');
const chalk = require('chalk');
const semver = require('semver');

const logger = require('../logger');

Expand Down Expand Up @@ -77,7 +78,6 @@ function getConfigForNodeModules(urc) {

const loaderConfig = {
test: /\.js$/,
// We want to avoid sending babel/runtime back to babel.
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
Expand Down Expand Up @@ -108,14 +108,49 @@ function getConfigForNodeModules(urc) {
};
// By default we compile all of the `node_modules`, but the user can also pass a
// selective list of node_modules to compile.
if (Array.isArray(urc.compileNodeModules)) {
const selectiveCompile = Array.isArray(urc.compileNodeModules);
if (selectiveCompile) {
loaderConfig.include = new RegExp(
urc.compileNodeModules.map(m => `${m}(?!/node_modules).*`).join('|')
);
}

// If the app uses mapbox-gl 2.0+, do not transpile. mapbox-gl 2.0+ is
// incompatible with babel without additional configuration beacuse it uses
// an inlined webworker.
let excludeList = urc.excludeCompileNodeModules || [];
if (usesMapboxGl2({ urc }) && !selectiveCompile) {
excludeList = excludeList.concat('mapbox-gl');
}

// If excludeList is provided, compile all node modules except the provided
// list of modules.
if (excludeList.length) {
if (selectiveCompile) {
throw new Error(
"Cannot use 'excludeCompileNodeModules' if 'compileNodeModules' is an array. If you want to exclude a list of modules and include the rest, set 'compileNodeModules: true'."
);
}
loaderConfig.include = new RegExp(
`/node_modules/(${excludeList.map(m => `(?!${m}/)`)})`
);
}
return loaderConfig;
}

function usesMapboxGl2({ urc }) {
let usesMapboxGl2 = false;
try {
const pkg = JSON.parse(
fs.readFileSync(path.join(urc.rootDirectory, 'package.json'), 'utf8')
);
const glJsVer = pkg.dependencies['mapbox-gl'];
usesMapboxGl2 = semver.gte(semver.coerce(glJsVer), '2.0.0');
} catch (e) {} // eslint-disable-line

return usesMapboxGl2;
}

// `babel-preset-mapbox` depends on the `browserslist` and
// any change in its value would fail to change the default `cacheIdentifier` of `babel-loader`
// hence leading to stale babel output. To mitigate this we need to create a more accurate `cacheIdentifier`
Expand Down
64 changes: 64 additions & 0 deletions lib/webpack-config/create-webpack-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,70 @@ test('Basic Test', () => {
).resolves.toMatchSnapshot();
});

test('Adds mapbox-gl to exclude if user is on 2.0.0 or above', () => {
getUserConfig.mockResolvedValueOnce({});
const tempDir = tempy.directory();
fs.writeFileSync(
path.join(tempDir, 'package.json'),
`{
"name": "fake",
"version": "0.0.0",
"dependencies": {
"mapbox-gl": "^2.0.0"
}
}`
);

fs.writeFileSync(
path.join(tempDir, 'babel.config.js'),
`module.exports = {}`
);

const urcPromise = config(
getCliOpts({
configPath: path.join(tempDir, 'underreact.config.js')
})
);

return urcPromise.then(urc => {
const config = createWebpackConfig(urc);
expect(config.module.rules[0].oneOf[1].include).toEqual(
/\/node_modules\/((?!mapbox-gl\/))/
);
});
});

test('Does not add mapbox-gl to exclude if user is on a version below 2.0.0', () => {
getUserConfig.mockResolvedValueOnce({});
const tempDir = tempy.directory();
fs.writeFileSync(
path.join(tempDir, 'package.json'),
`{
"name": "fake",
"version": "0.0.0",
"dependencies": {
"mapbox-gl": "~1.9.0"
}
}`
);

fs.writeFileSync(
path.join(tempDir, 'babel.config.js'),
`module.exports = {}`
);

const urcPromise = config(
getCliOpts({
configPath: path.join(tempDir, 'underreact.config.js')
})
);

return urcPromise.then(urc => {
const config = createWebpackConfig(urc);
expect(config.module.rules[0].oneOf[1].include).toBeUndefined();
});
});
Copy link
Author

Choose a reason for hiding this comment

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

Could use a couple more tests:

  • Error case if user provides both include and exclude lists
  • Case where use provides their own set of exclude modules


test('Uses babel.config.js if it exists at project root', () => {
getUserConfig.mockResolvedValueOnce({});
const tempDir = tempy.directory();
Expand Down
Loading